Expanding responses use cases

Learn how to leverage expand to perform common tasks

See the Stripe fee for a given payment

In some cases, you may want to see the processing fees associated with a payment. You can only see the Stripe fees after a payment has been processed. After a payment succeeds, the fees are available on the associated Charge’s balance transaction. Rather than retrieving a balance transaction separately, you can retrieve it in a single call using expand, for example:

curl https://api.stripe.com/v1/payment_intents/pi_1Gpl8kLHughnNhxyIb1RvRTu \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d "expand[]"="charges.data.balance_transaction" \ -G
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' payment_intent = Stripe::PaymentIntent.retrieve({ id: 'pi_1Gpl8kLHughnNhxyIb1RvRTu', expand: ['charges.data.balance_transaction'], }) fee_details = payment_intent.charges.data[0].balance_transaction.fee_details
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' payment_intent = stripe.PaymentIntent.retrieve( 'pi_1Gpl8kLHughnNhxyIb1RvRTu', expand=['charges.data.balance_transaction'], ) fee_details = payment_intent.charges.data[0].balance_transaction.fee_details
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $paymentIntent = \Stripe\PaymentIntent::retrieve([ 'id' => 'pi_1Gpl8kLHughnNhxyIb1RvRTu', 'expand' => ['charges.data.balance_transaction'], ]); $feeDetails = $paymentIntent->charges->data[0]->balance_transaction->fee_details;
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; PaymentIntentRetrieveParams params = PaymentIntentRetrieveParams.builder() .addExpand("charges.data.balance_transaction") .build(); PaymentIntent paymentIntent = PaymentIntent.retrieve("pi_1Gpl8kLHughnNhxyIb1RvRTu", params, null); List<BalanceTransaction.Fee> feeDetails = paymentIntent.getCharges().getData().get(0).getBalanceTransactionObject().getFeeDetails();
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const paymentIntent = await stripe.paymentIntents.retrieve( 'pi_1Gpl8kLHughnNhxyIb1RvRTu', { expand: ['charges.data.balance_transaction'], } ); const feeDetails = paymentIntent.charges.data[0].balance_transaction.fee_details;
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" params := &stripe.PaymentIntentParams{} params.AddExpand("charges.data.balance_transaction") pi, _ := paymentintent.Get("pi_1Gpl8kLHughnNhxyIb1RvRTu", params) feeDetails := pi.Charges.Data[0].BalanceTransaction.FeeDetails
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; var options = new PaymentIntentGetOptions(); options.AddExpand("charges.data.balance_transaction"); var service = new PaymentIntentService(); PaymentIntent paymentIntent = service.Get("pi_1Gpl8kLHughnNhxyIb1RvRTu", options); List<BalanceTransactionFeeDetail> feeDetails = paymentIntent.Charges.Data[0].BalanceTransaction.FeeDetails;

See the charges included in a payout

Every automatic payout is tied to historical changes to the balance of your Stripe account. The API records these historical changes as balance transactions, which you can retrieve using the List Balance Transactions API. From a list of balance transactions you can expand the source property to gather information on what triggered the change to the account balance (Charge, Refund, Transfer, etc.). For example:

curl https://api.stripe.com/v1/balance_transactions \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d payout=po_1Gl3ZLLHughnNhxyDrOia0vI \ -d type=charge \ -d "expand[]"="data.source" \ -G
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' balance_transactions = Stripe::BalanceTransaction.list({ payout: 'po_1Gl3ZLLHughnNhxyDrOia0vI', type: 'charge', expand: ['data.source'], }) balance_transactions.data.each do |txn| charge = txn.source puts "#{charge.id} #{charge.amount} #{charge.currency}" end
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' balance_transactions = stripe.BalanceTransaction.list( payout='po_1Gl3ZLLHughnNhxyDrOia0vI', type='charge', expand=['data.source'], ) for txn in balance_transactions.data: charge = txn.source print(charge.id, charge.amount, charge.currency)
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $balanceTransactions = \Stripe\BalanceTransaction::all([ 'payout' => 'po_1Gl3ZLLHughnNhxyDrOia0vI', 'type' => 'charge', 'expand' => ['data.source'], ]); foreach ($balanceTransactions->data as $txn) { $charge = $txn->source; echo "{$charge->id} {$charge->amount} {$charge->currency}\n"; }
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; BalanceTransactionListParams params = BalanceTransactionListParams.builder() .setPayout("po_1Gl3ZLLHughnNhxyDrOia0vI") .setType("charge") .addExpand("data.source") .build(); BalanceTransactionCollection balanceTransactions = BalanceTransaction.list(params); for (BalanceTransaction txn : balanceTransactions.getData()) { Charge charge = (Charge) txn.getSourceObject(); System.out.printf("%s %s %s\n", charge.getId(), charge.getAmount(), charge.getCurrency()); }
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const balanceTransactions = await stripe.balanceTransactions.list({ payout: 'po_1Gl3ZLLHughnNhxyDrOia0vI', type: 'charge', expand: ['data.source'], }); const charges = balanceTransactions.data.map((txn) => txn.source);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" params := &stripe.BalanceTransactionListParams{ Payout: stripe.String("po_1Gl3ZLLHughnNhxyDrOia0vI"), Type: stripe.String(string(stripe.BalanceTransactionTypeCharge)), } params.AddExpand("data.source") i := balancetransaction.List(params) for i.Next() { bt := i.BalanceTransaction() ch := bt.Source.Charge fmt.Println(ch.ID, ch.Amount, ch.Currency) }
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; var options = new BalanceTransactionListOptions { Payout = "po_1Gl3ZLLHughnNhxyDrOia0vI", Type = "charge", }; options.AddExpand("data.source"); var service = new BalanceTransactionService(); StripeList<BalanceTransaction> balanceTransactions = service.List(options); foreach (BalanceTransaction txn in balanceTransactions.Data) { Charge charge = (Charge)txn.Source; Console.WriteLine($"{charge.Id} {charge.Amount} {charge.Currency}"); }

If you’re using Connect with destination charges, you can retrieve the same information on behalf of your connected accounts. One difference is that destination charges involve both a transfer and a linked payment (in the form of a Charge object) to move funds to a connected account. So when listing the balance transactions bundled in your connected account’s payouts, each balance transaction’s source is linked to the transfer’s payment rather than the originating Charge. To retrieve the originating Charge, you need to expand a payment’s linked transfer through the source_transfer property; and from there, expand the transfer’s source_transaction property:

curl https://api.stripe.com/v1/balance_transactions \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d payout=po_1G7bnaD2wdkPsFGzdVOqU44u \ -d type=payment \ -d "expand[]"="data.source.source_transfer.source_transaction" \ -H "Stripe-Account: acct_1G7PaoD2wdkPsFGz" \ -G
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' balance_transactions = Stripe::BalanceTransaction.list({ payout: 'po_1G7bnaD2wdkPsFGzdVOqU44u', type: 'payment', expand: ['data.source.source_transfer.source_transaction'], }, { stripe_account: 'acct_1G7PaoD2wdkPsFGz', }) balance_transactions.data.each do |txn| charge = txn.source.source_transfer.source_transaction puts "#{charge.id} #{charge.amount} #{charge.currency}" end
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' balance_transactions = stripe.BalanceTransaction.list( payout='po_1G7bnaD2wdkPsFGzdVOqU44u', type='payment', expand=['data.source.source_transfer.source_transaction'], stripe_account='acct_1G7PaoD2wdkPsFGz', ) for txn in balance_transactions.data: charge = txn.source.source_transfer.source_transaction print(charge.id, charge.amount, charge.currency)
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $balanceTransactions = \Stripe\BalanceTransaction::all([ 'payout' => 'po_1G7bnaD2wdkPsFGzdVOqU44u', 'type' => 'payment', 'expand' => ['data.source.source_transfer.source_transaction'], ], [ 'stripe_account' => 'acct_1G7PaoD2wdkPsFGz', ]); foreach ($balanceTransactions->data as $txn) { $charge = $txn->source->source_transfer->source_transaction; echo "{$charge->id} {$charge->amount} {$charge->currency}\n"; }
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; BalanceTransactionListParams params = BalanceTransactionListParams.builder() .setPayout("po_1G7bnaD2wdkPsFGzdVOqU44u") .setType("payment") .addExpand("data.source.source_transfer.source_transaction") .build(); RequestOptions requestOptions = RequestOptions.builder() .setStripeAccount("acct_1G7PaoD2wdkPsFGz") .build(); BalanceTransactionCollection balanceTransactions = BalanceTransaction.list(params, requestOptions); for (BalanceTransaction txn : balanceTransactions.getData()) { Charge payment = (Charge) txn.getSourceObject(); Transfer sourceTransfer = payment.getSourceTransferObject(); Charge charge = (Charge) sourceTransfer.getSourceTransactionObject(); System.out.printf("%s %s %s\n", charge.getId(), charge.getAmount(), charge.getCurrency()); }
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const balanceTransactions = await stripe.balanceTransactions.list( { payout: 'po_1G7bnaD2wdkPsFGzdVOqU44u', type: 'payment', expand: ['data.source.source_transfer.source_transaction'], }, { stripeAccount: 'acct_1G7PaoD2wdkPsFGz', } ); const charges = balanceTransactions.data.map( (txn) => txn.source.source_transfer.source_transaction );
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" params := &stripe.BalanceTransactionListParams{ Payout: stripe.String("po_1G7bnaD2wdkPsFGzdVOqU44u"), Type: stripe.String(string(stripe.BalanceTransactionTypePayment)), } params.AddExpand("data.source.source_transfer.source_transaction") params.SetStripeAccount("acct_1G7PaoD2wdkPsFGz") i := balancetransaction.List(params) for i.Next() { bt := i.BalanceTransaction() ch := bt.Source.Charge.SourceTransfer.SourceTransaction.Charge fmt.Println(ch.ID, ch.Amount, ch.Currency) }
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; var options = new BalanceTransactionListOptions { Payout = "po_1G7bnaD2wdkPsFGzdVOqU44u", Type = "payment", }; options.AddExpand("data.source.source_transfer.source_transaction"); var requestOptions = new RequestOptions { StripeAccount = "acct_1G7PaoD2wdkPsFGz", }; var service = new BalanceTransactionService(); StripeList<BalanceTransaction> balanceTransactions = service.List( options, requestOptions ); foreach (BalanceTransaction txn in balanceTransactions.Data) { Charge payment = (Charge)txn.Source; Transfer sourceTransfer = (Transfer)payment.SourceTransfer; Charge charge = (Charge)sourceTransfer.SourceTransaction; Console.WriteLine($"{charge.Id} {charge.Amount} {charge.Currency}"); }
Was this page helpful?
Questions? Contact us.
Developer tutorials on YouTube.
You can unsubscribe at any time. Read our privacy policy.