IdempotencyWebhooksAPI

Preventing Duplicate Payments with Idempotency and Webhooks

By PayEurasia Team · 2 October 2026 · 4 min read

Last updated 11 October 2026

Preventing Duplicate Payments with Idempotency and Webhooks

How idempotency keys, unique constraints and idempotent webhook handling prevent duplicate charges and duplicate credits in payment integrations.

Header image: conceptual illustration.

Networks fail at the worst moment: a request is sent, the connection drops, and your system does not know whether the payment was created. Retrying blindly risks charging twice; not retrying risks losing the payment. Webhooks have the mirror-image problem: providers may deliver the same event more than once. Idempotency solves both.

What idempotency means

An operation is idempotent if performing it several times has the same effect as performing it once. In payment APIs this is usually achieved with an idempotency key: a unique value the client sends with a request. The server stores the result for that key and returns the same result if the request is repeated.

Stripe's documentation describes this pattern: the server saves the status code and body of the first request for a given key and returns the same result for retries. The IETF is standardising an `Idempotency-Key` header in an HTTP API draft. Not every provider supports keys; check yours.

Outgoing requests: your side as the client

  1. Create your internal payment record first, with a unique ID.
  2. Derive the idempotency key from that record (for example the payment ID), not from a random value generated per attempt.
  3. On timeout, retry with the same key.
  4. If the provider does not support keys, query the payment status by your merchant reference before retrying.

Incoming webhooks: your side as the server

Stripe's webhook guide warns that endpoints may receive the same event more than once. Your handler should:

  1. Verify the signature, then confirm the event reports a successful final status and that its merchant/order reference, amount and currency match your payment record.
  2. Record the event ID in a table with a unique constraint; if it already exists, return success and stop.
  3. Apply state changes only if they move the payment forward (e.g. `pending → succeeded`), never backward.
  4. Credit balances inside the same database transaction as the status update, guarded by a unique constraint on the provider transaction ID.

Your own API

If you offer an API to merchants, accept idempotency keys and scope them per merchant. Store key, request fingerprint and response; reject reuse of a key with a different request body.

Hypothetical failure

*Invented example.* A checkout server sends "create payment", the provider creates it, but the response is lost. The server retries with a new random key; the provider creates a second payment and the customer authorises both. With a stable key derived from the order ID, the retry would have returned the first payment.

Testing checklist

  • Same request twice with the same key → one payment.
  • Same key, different body → rejected.
  • Same webhook delivered twice → one state change, one credit.
  • Webhooks out of order → final state correct.
  • Timeout then retry → one payment.

Generating good keys

Use a random UUID or a key derived from your own business identifier, such as an order or withdrawal ID. Generate the key once, before the first attempt, and reuse it on every retry of that same operation.

Never generate a new key per retry. That defeats the purpose entirely.

Server-side storage and scope

Store keys per merchant with the request fingerprint and the response. If the same key arrives with a different payload, reject it; that signals a client bug. Keep keys at least as long as any retry could plausibly occur, commonly 24 hours or more.

Handle concurrent requests with the same key by locking on the key, so only one is processed and the other waits for the result.

Idempotency across providers

When a payment is routed to several providers in turn, each attempt needs its own provider-level key under one merchant-level payment identity. That is what prevents cascading from producing double charges, as explained in payment orchestration and smart routing.

Record which provider attempt succeeded so later retries do not start a new cascade.

Idempotent webhook processing

The same principle applies to inbound events. Deduplicate webhooks on their event ID and make balance updates conditional on current state, so replaying an event never credits twice.

See the payment webhooks guide for the full consumer design.

Testing idempotency

Write tests that send the same request twice, send it concurrently, and send it with a modified body. Simulate timeouts after the server commits but before the client receives the response. These are exactly the conditions production will create.

Frequently asked questions

Is a database unique constraint enough?

It is an essential last line of defence for credits, but you still need idempotency on outgoing requests to avoid duplicate charges at the provider.

How long should keys be stored?

Long enough to cover your retry window; providers document their own retention periods.

Related: failed payments without double charging, how payment APIs work, API docs.

What is an idempotency key?

A unique value sent with a request so the server can recognise retries and return the original result.

How long are keys stored?

Commonly 24 hours or more, long enough to cover any realistic retry window.

Can I reuse a key for a different payment?

No. A key identifies one operation; reusing it with different data is rejected.

Do GET requests need keys?

No. Reads are naturally idempotent.

Does idempotency prevent duplicate payouts?

Yes, when every payout request carries a stable key tied to the withdrawal it represents.

Sources

  • Stripe Docs, "Idempotent requests": https://docs.stripe.com/api/idempotent_requests
  • Stripe Docs, webhooks: https://docs.stripe.com/webhooks
  • IETF draft, "The Idempotency-Key HTTP Header Field": https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/

Talk to PayEurasia

Working in a high-risk vertical across South Asia? We can probably help.

Request integration →

Explore country coverage

Related articles

View all articles →