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?
I've had an extensive exchange with Stripe's support team and there are several puzzle pieces necessary to get there:
Payouts are scoped by accounts
If you query stripe for a list of payouts, you will only receive the payout objects that you, the platform owner, get from stripe. To get the payout objects of a specific account you can use the normal authentication for the platform, but send the stripe account id as a header. So the code snippet to get the last payout looks like this (I'll use ruby snippets as examples for the rest of the answer):
Having the payout id, we can query the list of balance transactions, scoped to a payout:
Objects viewed as an account are stripped of most information, compared to those viewed as a platform owner
Even though you now have the payout id, the object is still scoped to the account and you cannot retrieve it as platform owner. But viewed as an account, the payout only shows pseudo charge and refund objects like these (notice the second transaction has a
py_7000001234567890aBcDeFgH
object as a source instead of a regularch_
charge object):You can let stripe automatically expand objects in the response
As an additional parameter, you can give stripe paths of objects which you want stripe to expand in their response. Thus we can walk from the pseudo objects back to the original charge objects via the transfers:
And if you want to process the whole list you need disambiguate between the
source.object
attribute:Refunds have no connecting object path back to the original ids
Unfortunately, there is currently no way to get the original
re_
objects from the pseudopyr_
that are returned by the BalanceTransaction list call for refund transactions. The best alternative I've found is to go via thedata.source.charge.source_transfer.source_transaction
path to get the charge id of the charge on which the refund was issued and use that in combination with thecreated
attribute of thepyr_
to match our database refund object. I'm not sure, though, how stable that method really is. The code to extract that data: