Search code examples
javascriptnode.jsstripe-paymentswebhooksstripes

Splitting Payments Between Users Using Stripe Connect and PaymentIntents - Insufficient Balance Error


I am developing an affiliate marketing feature for an e-commerce platform, where we allow our sellers to create affiliate programs. I am currently using Stripe Connect with the PaymentIntents API.

When a customer purchases a product through an affiliate link, we want to split the payment between the seller (product owner) and the affiliate. I am attempting to achieve this by creating a PaymentIntent with transfer_data where the destination is the seller's Stripe account ID and then in the payment_intent.succeeded webhook, make a transfer to the affiliate's account.

The problem is that, even when the payment intent is successful, the funds aren't immediately available in the platform account. This causes an "Insufficient Balance" error when I try to transfer part of the payment to the affiliate's Stripe account.

Here's a snippet of the code for creating the PaymentIntent:

const paymentIntent = await stripe.paymentIntents.create({
  amount: adjustedPrice * 100,
  currency: "usd",
  transfer_data: {
    destination: sellerStripeAccountId,
  },
  application_fee_amount: affiliateCut * 100,
  metadata: {
    affiliate: affiliate || "",
    affiliateCut,
    affiliateAccountId,
  },
});

And here's the code for transferring the affiliate's cut (in the webhook):

if(paymentIntent.metadata.affiliate) {
  const affiliateTransfer = await stripe.transfers.create({
    amount: paymentIntent.metadata.affiliateCut * 100,
    currency: "usd",
    destination: paymentIntent.metadata.affiliateAccountId,
  });
}

I've tried checking the available balance before creating the transfer, but as mentioned earlier, the funds from the PaymentIntent don't seem to be available immediately.

Any insights or alternate approaches to this problem would be greatly appreciated. How can I ensure that I have enough balance in my Stripe platform account to transfer to the affiliate immediately after a successful PaymentIntent?

Thank you in advance!


Solution

  • Instead of using Destination Charges via the transfer_data parameter (which is meant for a one-to-one seller/buyer relationship) you want to use Separate Charges & Transfers (meant for a many-to-one seller/buyer relationship) to create multiple transfers -- one to the seller and one to the affiliate. When you do so, you want to use the source_transaction parameter (docs) when creating your transfer to tie the Transfers to the original Charge and allow you to create these transfers instantly. The funds still won't be available until the normal delay, but this will solve your "insufficient funds" issue.