gshow8 shac
gshow8 shac

Reputation: 421

Stripe create a direct charge node.js

I am trying to create a direct charge to a standard Stripe connected account from a node.js backend. The codes I wrote are:

async function create_direct_charge(language_learner_id, native_speaker_id, call_duration) {
  try {
    const customer_id = "xxxxxx";
    const connected_standard_account_id = "xxxxxx;

    const paymentMethods = await stripe.paymentMethods.list({
      customer: customer_id,
      type: 'card',
      limit: 1,
    });
    const paymentMethod = paymentMethods.data[0].id;

    const paymentIntent = await stripe.paymentIntents.create({
      payment_method_types: ['card'],
      payment_method: paymentMethod,
      amount: 1000,
      customer: customer_id,
      currency: "cad",
      off_session: true,
      confirm: true,
      application_fee_amount: 100,
    }, {
      stripeAccount: connected_standard_account_id,
    });
  }
  catch (error) {
    console.log(error);
  }
}

The above codes generate an error of: No such PaymentMethod: 'pm_xxxxxx'; OAuth key or Stripe-Account header was used but API request was provided with a platform-owned payment method ID. Please ensure that the provided payment method matches the specified account.

The thing is that the customer with the customer_id already has a valid payment method attached: enter image description here

The payment method is valid, since I was able to create a destination charge with that payment method. The payment is not set as the default source though. I am wondering if that is the problem. What did I do wrong here?

Upvotes: 0

Views: 687

Answers (1)

hmunoz
hmunoz

Reputation: 3361

The payment method is valid, since I was able to create a destination charge with that payment method.

That tells me that the PaymentMethod (e.g. pm_123) was created on the Platform Stripe account and hence lives there.

So when tell Stripe to "Charge pm_123 on Connect account acct_1 as a Direct Charge", that won't work as the PaymentMethod object pm_123 doesn't exist on the Connect account, it exists on the Platform account.

In order to get this to work, you have to first "clone" the PaymentMethod pm_123 to the Connect account, then create a Direct Charge with this newly cloned PaymentMethod token, as explained here: https://stripe.com/docs/payments/payment-methods/connect#cloning-payment-methods

Upvotes: 1

Related Questions