FinChain Solutions

Idempotency in payment APIs: the bug that charges customers twice

Every payment team meets this bug eventually. A customer taps Pay, the network hiccups, the app retries, and the card is charged twice. Here is how to design it out.

FinChain Solutions engineering team7 min read

Double charges rarely come from bad code in the obvious sense. They come from a perfectly reasonable retry meeting a system that cannot tell a new request from a repeated one. The client sends a payment, the server processes it, and the response never makes it back. From the client's side the payment failed, so it tries again.

Without protection the server sees two valid requests and charges twice. The customer sees two lines on their statement and contacts support, or their bank.

The idempotency key

The fix is old and simple: the client generates a unique key for each intended payment and sends it with every attempt. The server stores the key together with the result. If the same key arrives again, the server returns the stored result instead of doing the work again.

POST /v1/payments
Idempotency-Key: 6f1c2e0a-8d1b-4b6e-9a77-1c9e2f4d5a10

{ "amount": 125000, "currency": "EUR", "order_id": "A-1042" }

Three details decide whether this actually works in production:

  • The key belongs to the intent, not the attempt. Generate it when the customer presses Pay, keep it across retries, and discard it only when the payment is final.
  • Store the key and the request fingerprint. If the same key arrives with a different amount, reject it loudly. That is a client bug, not a retry.
  • Lock while processing. Two identical requests can arrive at the same millisecond. The second should wait or receive a 'still processing' response, never start its own charge.

The state everyone forgets: unknown

Idempotency protects you from your own clients. It does not protect you from your provider. When you call an acquirer and the call times out, you genuinely do not know whether the card was charged. Many systems treat this as failed. That is how double charges happen one level deeper: the system 'fails' the payment, the customer tries again, and both charges settle.

Model unknown as a real status. A payment in unknown cannot be retried. A background job asks the provider for the status using your own reference, and only then moves the payment to succeeded or failed.

Some providers make this explicit. With Adyen, captures and refunds are asynchronous: the API confirms your request, and the outcome arrives later in a webhook. Pay by bank works the same way, since a customer returning from their banking app proves nothing until the open banking provider reports a final status. The same goes for 3D Secure: a shopper who closes the challenge window leaves a payment you have to resolve, not guess.

SituationWrong reactionRight reaction
Client retries after timeoutCreate a new chargeReturn stored result for the same key
Provider call times outMark as failedMark as unknown, check status before any retry
Same key, different amountProcess itReject with a clear error
Webhook arrives twiceApply it twiceProcess each event ID once

Webhooks need the same discipline

Providers deliver webhooks at least once, which means sometimes more than once, and not always in order. Store every event ID you have processed. Before applying an event, check the payment's current state: a late 'authorised' event must not overwrite a 'refunded' status. On marketplaces built with Stripe Connect, also check which connected account an event belongs to before applying it.

Make the ledger idempotent too

The final safety net is the ledger. Each posting should carry a unique reference derived from the business event, for example the provider's transaction ID plus the event type. For bank transfers, the end-to-end ID and UETR carried in ISO 20022 messages do the same job. The database enforces uniqueness on that reference. If anything upstream slips, the ledger refuses to record the same money movement twice, and your reconciliation report shows the attempt instead of a wrong balance.

A short checklist

  1. Every payment-creating endpoint accepts an idempotency key
  2. Keys are stored with a request fingerprint and kept at least 24 hours
  3. Concurrent requests with the same key are locked
  4. Timeouts lead to an unknown state, never to failed
  5. Webhook event IDs are de-duplicated and ordering is checked
  6. Ledger postings have unique business references

None of this is exotic. It is the difference between a payment system that is correct most of the time and one that is correct when it matters: during outages, peak traffic and the 2 a.m. incidents nobody plans for.

Related services

Back to all articles

Tell us what you are building

Write a few lines. A person from the team will read them and reply by email.

  • London, United Kingdom
    68 Hanbury Street, London E1 5JL
    Aldgate East / Shoreditch High StreetOpen map
    local time
  • Kyiv, Ukraine
    2 Marii Kapnist St, Kyiv 03057
    Open map
    local time
Area

We reply within 1 business day.

Get an estimate