Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active February 16, 2023 03:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/e7958e5f55136928f082 to your computer and use it in GitHub Desktop.
Save iloveitaly/e7958e5f55136928f082 to your computer and use it in GitHub Desktop.
Find Stripe transfer associated with charge
# Michael Bianco <mike@suitesync.io>
# Description: Find Stripe transfer (payout) associated with charge (payment)
require 'stripe'
Stripe.api_key = 'sk_test'
# NOTE `auto_paging_each` requires a recent stripe ruby gem version
def stripe_transfer_containing_charge(stripe_charge, limit: 100)
txn = stripe_charge.balance_transaction
Stripe::Transfer.list({ limit: 100, created: { gt: stripe_charge.created }}).auto_paging_each do |transfer|
Stripe::BalanceTransaction.list({ limit: 100, transfer: transfer.id }).auto_paging_each do |bt|
if bt.id == stripe_charge.balance_transaction
return transfer
end
end
limit -= 1
if limit < 0
return nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment