The customer clicks Pay and the spinner stops with an error. Your server sent the payment request, but the connection closed before the response arrived. There are two possibilities: the provider never saw the request, or it processed the charge and the reply was lost. A timeout cannot tell you which. Sending an ordinary second request can create a second payment.

Give the operation an identity

Create an internal checkout attempt before calling the provider. Give it a stable, unpredictable operation ID and store the cart total, currency, customer, and state alongside it. Use the provider’s idempotency mechanism with that ID when you create the payment. Every retry of the same intended operation must use the same key and identical parameters. A new purchase, even by the same customer for the same amount, needs a new key. Stripe’s idempotency documentation describes this contract and its retention limits; other providers have their own rules.

After the timeout, reconcile

Mark the attempt as uncertain, not failed. Retry only within the provider’s documented key window, and check the provider’s payment object or event feed using the stored operation ID. While reconciliation runs, show the customer a clear pending state and prevent the Pay button from starting another attempt for that order. An impatient double click should return the existing attempt, not generate a new provider request. If the provider cannot be reached, keep the order pending and alert a human rather than guessing from the local HTTP error.

Webhooks are another delivery channel

A webhook can arrive after the browser has closed, arrive more than once, or arrive before your synchronous response is recorded. Treat it as a signal to verify and update the same attempt. Store the provider event ID with a uniqueness constraint so duplicate delivery cannot apply the business transition twice. Commit the payment state before shipping a product or sending a receipt. Stripe’s webhook guide recommends handling duplicate events and describes delivery behavior.

A failure exercise worth running

In a test environment, force the connection to close after the provider accepts a request but before your service reads the reply. Retry the checkout and send the same webhook twice. The expected result is one provider charge, one paid order, and one receipt. Repeat with a changed amount under the same key: the application should reject it. Finally, test an attempt that stays pending past the provider’s key retention period; at that point, query the provider or escalate rather than blindly retrying.

Where idempotency ends

An idempotency key protects one API operation according to a provider’s contract. It does not make an entire checkout workflow atomic. Inventory reservation, tax calculation, payment capture, and email each need their own state transitions and recovery rules. Store enough evidence to answer a support request: order ID, operation ID, provider payment ID, and timestamps, while keeping sensitive payment data out of logs.

A state machine the support team can read

Use explicit states such as created, submitted, uncertain, paid, and rejected. Record why a transition happened: synchronous provider response, verified webhook, reconciliation query, or a manual decision. Do not treat every HTTP 500 as rejected; the provider may have completed the work before failing to return a normal response. A support agent should be able to find one order and see the sequence without reconstructing it from three disconnected log systems. If the provider says paid but your application says uncertain, update the order through a controlled reconciliation path that is safe to repeat.

Retry policy belongs at one layer

Check whether your HTTP client, queue worker, and provider SDK already retry. If all three independently try three times, a single click can become many requests under load. Set a total deadline for the operation and a bounded retry count at one chosen layer. Back off with jitter when the provider is overloaded, and honor its retry advice where documented. Measure how many requests end in uncertain state and how long reconciliation takes. The metric that matters to the customer is not “API call succeeded”; it is “order reached a trustworthy final state.”

Sources

Stripe: idempotent requests; Stripe: webhooks; Amazon Builders’ Library: timeouts and retries. Provider-specific semantics should be checked against the version of the API you use.

More on Software Engineering →