Search code examples
stripe-payments

Is there any way I can see on a charge or on balance transaction that it can from a subscription item, and get the subscription item id?


I'm trying to get subscription items when getting all balance transactions from a payout, but there are no fields on a balance transaction, or on a charge where I can see if the charge is for a subscription item.

Is there any way I can see on a charge or on balance transaction that it came from a subscription item, and get the subscription item id?


Solution

  • Yep, you can navigate through the API to get to the information you need.

    Balance Transactions have a source property that references the transaction the Balance Transaction is for. It sounds like in your case that will lead to a Charge.

    Subscriptions generate Invoices for each period, and Invoices generate Payment Intents to collect payment, and those Payment Intents generate Charges, so you can walk back up that chain to get what you want. There's a short video in the Stripe docs that explains the relationships between these objects in a bit more detail you might be interested in.

    The Charge referenced by the Balance Transaction will have a payment_intent property. The Payment Intent will have an invoice property. The Invoice will have a subscription property. On that Subscription you'll find the Subscription Items under items (although there are also lines on the Invoice you may be interested in).

    Also note that all of the properties mentioned above are expandable, which means you can tell the Stripe API to expand these objects in a single request instead of making individual API requests for each object. If you were starting from a Balance Transaction which referenced a Charge as the source, for example, you could expand source.payment_intent.invoice.subscription to get the Balance Transaction, Charge, Invoice, and Subscription objects in a single API request.