How can I get the original charge and refund ids of an automatic payout

Viewed 468

Stripe connect accounts are configurable to coalesce payouts in a regular payout schedule, e.g. for monthly payouts in our case. For these monthly payouts we need to explain the account owners which of the transactions on our platform (bookings and refunds in our case) produced the overall amount they receive. We store the stripe charge id (resp. refund id) in the booking (resp refund) objects in our database. Thus the question boils down to:

Given a stripe account id, how can you get the list of stripe charge and refund ids that contributed to the last payout?

2 Answers

It is now possible to get the refund ids via a "transfer reversal" object:

Stripe::BalanceTransaction.list({
  payout: 'po_1000001234567890aBcDeFgH',
  expand: [
    'data.source.source_transfer', # For charges
    'data.source.transfer_reversal', # For refunds
  ]
}, {
  stripe_account: 'acct_0000001234567890aBcDeFgH'
}).auto_paging_each do |balance_transaction|
  case balance_transaction.type
    when 'payment'
      charge_id = balance_transaction.source.source_transfer.source_transaction
    when 'payment_refund'
      refund_id = balance_transaction.source.charge.source_transfer.source_transaction
    end
  end
end
Related