thequokka
thequokka

Reputation: 13

How to retrieve the Stripe fee for a payment from a connected account (Node.js)

I've been reading the documentation for how to retrieve the Stripe fee from a given payment here:

// 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_xyz');

const paymentIntent = await stripe.paymentIntents.retrieve(
  'pi_1Gpl8kLHughnNhxyIb1RvRTu',
  {
    expand: ['charges.data.balance_transaction'],
  }
);

const feeDetails = paymentIntent.charges.data[0].balance_transaction.fee_details;

However I want to retrieve the Stripe fee for a payment made to a connected account. If I try the code above with a payment intent from a linked account I get the error:

Error: No such payment_intent: 'pi_1Gpl8kLHughnNhxyIb1RvRTu'

However, I can actually see the payment intent listed when I receive the posted data from the webhook:

{ id: 'evt_1HFJfyLNyLwMDlAN7ItaNezN',
   object: 'event',
   account: 'acct_1FxPu7LTTTTMDlAN',
   api_version: '2019-02-11',
   created: 1597237650,
   data:
    { object:
       { id: 'pi_1Gpl8kLHughnNhxyIb1RvRTu',
         object: 'payment_intent',

Any tips?

Upvotes: 1

Views: 816

Answers (1)

ttmarek
ttmarek

Reputation: 3260

I want to retrieve the Stripe fee for a payment made to a connected account. If I try the code above with a payment intent from a linked account I get the error:

In order to retrieve the Stripe fee for a payment made on behalf of a connected account (using a direct Charge) you need to make the retrieve request as the connected account by specifying the special Stripe-Account header in the request. When using stripe-node we'll add that header for you automatically if you pass in the account ID as part of the request options. For example:

  const paymentIntent = await stripe.paymentIntents.retrieve(
    "pi_1HCSheKNuiVAYpc7siO5HkJC",
    {
      expand: ["charges.data.balance_transaction"],
    },
    {
      stripeAccount: "acct_1GDvMqKNuiVAYpc7",
    }
  );

You can read more about making requests on behalf of connected accounts in stripe-node and our other libraries here: https://stripe.com/docs/api/connected_accounts

Upvotes: 1

Related Questions