stripe-node icon indicating copy to clipboard operation
stripe-node copied to clipboard

Feature request: idempotency for Payment Intents

Open tautvilas opened this issue 6 years ago • 5 comments

I can not find in API that Payment intents do support idempotency keys (like charges). This causes potential double-charging situation for me when using off session payments. I am using this code:

const paymentIntent = await stripe.paymentIntents.create({
    amount: 1099,
    currency: 'usd',
    customer: '{{CUSTOMER_ID}}',
    payment_method: '{{PAYMENT_METHOD_ID}}',
    off_session: true,
    confirm: true,
  });

In case there is a bug in my code and this code gets executed twice - the client would be charged twice. However if I could add some idempotency value that I could pass to intent creation (lets say it could be order id). That would mean that it would be impossible to create two off_session aut-confirmable payment intents for the same order.

tautvilas avatar Apr 24 '20 13:04 tautvilas

Hey @tautvilas :wave:

The PaymentIntents API can use idempotency keys [1] too. The idempotency key is passed in the request options object and looks like this:

  const paymentIntent = await stripe.paymentIntents.create({
    amount: 1099,
    currency: 'usd',
    customer: '{{CUSTOMER_ID}}',
    payment_method: '{{PAYMENT_METHOD_ID}}',
    off_session: true,
    confirm: true,
  }, {
    idempotencyKey: "random-key",
  });

[1] https://stripe.com/docs/api/idempotent_requests?lang=node

cjavilla-stripe avatar Apr 24 '20 15:04 cjavilla-stripe

Thank you for the quick response! I got confused reading all these different API docs :) Maybe there could be example with payment intents + idempotency in the api too

tautvilas avatar Apr 24 '20 15:04 tautvilas

:) Happy to help.

cjavilla-stripe avatar Apr 24 '20 16:04 cjavilla-stripe

Hi @cjavilla-stripe I am passing the options in the same in the code snippet you've added here. But I'm getting the following error. Stripe: Unknown arguments ([object Object]). Did you mean to pass an options object? See https://github.com/stripe/stripe-node/wiki/Passing-Options. (on API request to POST /payment_intents)

Could you please let me know why?

nandunbandara avatar Jul 21 '20 15:07 nandunbandara

Figured it out. I've been using an older version of stripe. So there the parameter should be idempotency_key instead of idempotencyKey.

nandunbandara avatar Jul 21 '20 16:07 nandunbandara