A customer writes in: they were charged twice. You open the logs and find two POST /checkout requests, identical bodies, both answered 200, both perfectly valid. Nothing failed. The code did exactly what it was asked, twice.
The usual diagnosis is a double click, and the usual fix is to disable the button on submit. That fix is real, and it does nothing about one of the commonest ways the second request gets sent, because that one never touches your page at all.
The second request your JavaScript never sees
When a form POST is answered with a page directly, that page only exists as the result of the POST. Press back to it, or forward, or reload it, and the browser cannot show it again without sending the form again. So it asks. Chrome's error page for the back-and-forward case reads, in Chromium's own source:
This webpage requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.
Reload, and the dialog is the familiar "Confirm Form Resubmission". Either way, if the person confirms, the browser replays the stored request: same URL, same body. Your submit handler does not run, the disabled button is not involved, and no debounce can intercept it. From the server's side it is indistinguishable from the first submission.
The person rarely reads the warning as "this will charge you again". They read it as an obstacle between them and the page they wanted, and they click through.
How to tell which one you have
The two requests look the same, but the gap between them does not.
- A double click puts the second request within a second of the first, with nothing in between.
- A resubmission comes later: seconds or minutes, often with the person's other requests in between, because they went somewhere else and came back.
That is as far as the logs go. They tell you two requests arrived; they cannot tell you the person pressed back, saw a dialog and pressed the button on it. The customer usually cannot tell you either, because to them it was one purchase and an annoying popup.
The fix that actually holds, and the version of it that does not
Two parts, and the second is the one that matters for money.
Stop producing the prompt. Answer the POST with a redirect to a page loaded with GET (post, redirect, get). Back and reload then repeat the GET, which is harmless, and the dialog never appears.
Make the second request harmless anyway, because redirects get forgotten on one route, and retries come from places other than the back button. That means an idempotency key, and where you generate it decides whether it works.
Stripe's documentation describes the mechanism: a key the client generates, which "the server uses to recognize subsequent retries of the same request". Retries with the same key return the saved response instead of acting twice.
The trap is generating the key in the request handler:
// Looks safe. Is not: every submission, including a resubmitted one,
// arrives here and gets a brand new key.
app.post("/checkout", async (req, res) => {
const key = crypto.randomUUID();
await stripe.paymentIntents.create(params, { idempotencyKey: key });
});
A resubmitted form arrives as a new request, gets a new key, and Stripe correctly treats it as a new payment. The key has to be created when the form is rendered and travel inside the form, because the browser resends the form body exactly as it was:
// Rendering the form: the key is fixed here, once.
res.render("checkout", { orderToken: crypto.randomUUID() });
// <input type="hidden" name="order_token" value="<%= orderToken %>">
app.post("/checkout", async (req, res) => {
const token = req.body.order_token;
// A unique index on order_token: a second request finds the
// existing order instead of inserting another, however it was produced.
const order = await Orders.createOnce({ token, ...items });
await stripe.paymentIntents.create(params, { idempotencyKey: token });
res.redirect(303, `/orders/${order.id}`);
});
Now a double click, a resubmission and a network retry all carry the same token, and all land on the same order.
One limit worth knowing: Stripe's API v1 documentation says keys can be removed once they are at least 24 hours old, after which a reused key is a new request. Your own unique index is what covers the tab somebody leaves open overnight.
What the report needed
"I was charged twice" is a complete description from the customer's side and nearly useless from yours. The thing that separates a double click from a resubmission from a retry is what the person did between the two requests, and that is exactly the part nobody writes down. Fix the endpoint so it no longer matters; until then, the sequence of what they clicked is the only evidence there is.
Top comments (0)