Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent duplicate charge using Stripe?

I'm using Stripe to allow my customer to save a credit card and pay invoices (that have a unique id). On payment, I use a form to send a POST request with the card id (provided by stripe) to my server, and the controller charges it. Then I marked the invoiced as payed on my side.

Turns out that if I double click my form's submit button quick enough, I send two POST request and the cards ends up being charged twice, because when the second POST request arrives on my server, the POST request from my server to Stripe API (from the first click) isn't finished, thus the invoice has not been marked as payed yet.

I'm considering disabling the form submit button after first click, but I feel there may be a cleaner way.

Is there on Stripe's side to prevent duplicating charges (maybe using my invoice id)? If not what would the best back-end method to do this on my side?

like image 558
Iwazaru Avatar asked Dec 04 '17 13:12

Iwazaru


People also ask

What system prevents duplicate payments?

Use accounts payable approval control One of the most efficient ways of preventing duplicate payments is to use an accounts payable approval control software. This can be an external software or an integrated mechanism in the ERP system, however, the first solution can be harder to implement.

Does Stripe charge for authorizations?

Stripe supports two-step card payments so you can first authorize a charge, then wait to settle (capture) it later. When a charge is authorized, the card issuer guarantees the funds and the amount held on the customer's card for up to 7 days, or 2 days for in-person payments using Terminal.

Why is Stripe charging?

Have Stripe automatically charge a customer's stored payment method. Stripe can automatically attempt to pay an invoice if the customer has a payment method on file. You can choose to automatically charge a customer when you're creating an invoice or through the API.


2 Answers

Disabling the form submission after the first click is definitely the easiest way to implement this.

Another approach is to use idempotent requests as documented here. The idea is that you pass a unique identifier when making a request to Stripe's API that ensures you can only run this transaction once. If you re-run the query with the exact same idempotency key you will get the original response from the first call which ensures you never have 2 charges created for the same "transaction" on your website.

like image 105
koopajah Avatar answered Oct 21 '22 02:10

koopajah


yes idempotent request is correct way to implement this.

you can refer this here

https://stripe.com/docs/api#idempotent_requests

another simple solution which you can implement is use of state machine by keeping track of stripe api call.

like image 35
Rahul Sharma Avatar answered Oct 21 '22 02:10

Rahul Sharma