DEV Community

Cover image for Did the customer pay after a payment API timeout?
Nilu Mukherjee
Nilu Mukherjee

Posted on Edited on

Did the customer pay after a payment API timeout?

A customer clicks Pay. Five seconds later, your server gives up waiting for the payment provider. The charge may have gone through. The customer is still looking at your checkout screen.

I'd show "We're checking your payment" and give them an order status page they can refresh. Keep the payment attempt pending there until you can confirm what happened.

Reuse the same payment attempt

For order ORD-42, save payment attempt PAY-7 and its provider idempotency key before you send the charge request. If the customer double-clicks or the request times out, the handler or worker can load that attempt again. If your provider supports idempotent retries, reuse the same key and check how long the provider honors it.

Once the provider confirms a decline, the customer can try another card with a new attempt ID. If the first charge is still unresolved, check its status before starting another attempt. Both cards could otherwise end up charged.

Handle a success that comes in twice

The webhook handler and the reconciliation worker may hear about the same successful charge a few seconds apart. Have both use the same database operation to move PAY-7 from pending to succeeded. Record the webhook event IDs you've processed and update the attempt only if it's still pending. In that transaction, also record the work that should follow, such as releasing the order for fulfillment.

You can queue that work. Have the fulfillment handler check whether it already acted on the order. A worker can send the instruction and then crash before acknowledging the message.

For the first test, have the provider simulator record a successful charge and drop the connection before replying. Retry from the client while two copies of the success webhook come in. Check that the customer sees one successful payment and the warehouse gets one fulfillment instruction.

Top comments (3)

Collapse
 
mickyarun profile image
arun rajkumar •

Saving PAY-7 and the idempotency key before you send the charge request is the sentence the rest of this rests on, and it is the one people skip, because the natural place to write a payment attempt is wherever you handle the response.

Two things from the card side that would tighten the retry paragraph.

A timeout is not a decline and it is not a retry. The schemes made that a separate message: an acquirer that sends an authorisation and gets no answer sends a reversal, which actively releases the hold rather than asking again. Open banking has the same shape with a different verb, where you poll the payment by its id and there is no way to re-send the initiation at all. So reuse the same key is doing two jobs in your post that are worth splitting: finding out what happened, and undoing what might have happened. Most HTTP payment APIs give you the first and nothing for the second, which is why the pending attempt sits there.

And the honouring window you tell people to check is the thing to pin rather than check once. Stripe's idempotency keys are good for 24 hours. Past that the same key is a new request, so a worker that retries a two-day-old attempt because it is still pending will create a second charge using the key it believes is protecting it.

Which is the gap I would add to the design: pending needs an expiry and an owner. You keep the attempt pending until you can confirm what happened, and in the bad case you never can, because your record and the provider's disagree and nothing in the system is obliged to notice. An authorisation expires on its own after about a week and the hold drops, which is the rail refusing to hold an unresolved state open forever. Give the attempt a point at which pending becomes a named failure in a reconciliation queue with a human attached, rather than a row still reading as in-flight two months later.

Your first test is the right one. The second I would add is the one where the status read times out too, because then the new-attempt guard has to hold without knowing the old attempt's state.

Collapse
 
nilu_mukherjee profile image
Nilu Mukherjee •

Thanks, Arun. I appreciate the distinction you drew between checking what happened and asking the provider to reverse an authorization. I also should have been clearer about what happens when the idempotency window ends. I'd stop automatic retries at that point and move the unresolved attempt to a reconciliation queue with an owner and a deadline. I'd mark it for review so it's clear the payment outcome is still unknown.

Your second test is one I'd add. If the status lookup also times out, I'd check that checkout still shows the original attempt as unresolved and blocks a fresh charge.

Collapse
 
challan116ux profile image
challan116-ux •

Your point about saving the attempt and idempotency key before sending the charge is the whole game — in B2B document exchange we live the same rule, because a retried transmission that gets processed twice is a duplicate order, not a harmless retry. I also agree with Arun's point that pending needs an owner and an expiry; an attempt stuck in pending forever is just a dispute waiting for a customer to notice it first. The status page you suggest is underrated too — giving the customer a place to watch beats letting them hammer Pay again.