Suppose a store answers the payment gateway’s webhook in 400 milliseconds on an ordinary Tuesday, and retries almost never happen. In the busiest hour of Black Friday, with the database under load, the same response takes 12 seconds, and the gateway’s timeout is 10 seconds.

If 900 orders come in that hour and 5% run past the timeout, that’s 45 payment notifications the gateway treats as lost and resends. If the store creates an order for every notification it receives, 45 customers end up with two orders and one payment. Three hours like that in a day add up to 135 extra orders, and at an average order of $60, about $8,100 in merchandise that would ship twice.

The gateway delivers the notification at least once

When a payment is approved, the gateway notifies the store with an HTTP call, the webhook, and considers the notification delivered if the store answers with a 2xx within the timeout. If it gets any other code, or no response, it sends the notification again, with longer and longer waits, for hours or even days. Each gateway sets its own timeout and retry schedule, and both are in its documentation.

From the outside, the gateway can’t tell whether the store already created the order or crashed halfway, so it resends until it gets a 2xx. Recognizing the repeated notification falls to the store.

The second notification arrives while the first is still running

In many stores the endpoint does everything before it answers: it validates the notification, creates the order, reserves stock, generates the invoice, and notifies the ERP. In the example that takes 12 seconds. At 10, the gateway resends, and the second notification comes in while the first is still working.

Each one asks whether an order already exists for that payment. Since neither has finished saving its own, both get a “no” and both create the order. The check alone leaves a gap between asking and saving, and the second notification fits in that gap.

A unique index on the payment ID

The store saves the payment ID of every notification in a table of processed notifications, with a unique index on that column. When a notification arrives, it opens a transaction and tries to insert the ID before doing anything else. If the index rejects the insert, the notification was already processed: the store answers 200 and creates nothing. If the index accepts it, the store creates the order inside the same transaction and commits at the end.

That way the database makes the two notifications take turns. In MySQL with InnoDB and in PostgreSQL, the second insert waits for the first transaction to finish. If it commits, the second insert fails and the store answers 200. If it rolls back because something went wrong, the insert goes through and the second notification creates the order the first one never got to create.

Answering 200 for the duplicate is necessary. If the store returned an error, the gateway would keep resending the same notification.

When the gateway sends several notifications per payment, such as authorized, captured, and refunded, the key is the pair made of the payment ID and the notification type. With the ID alone, the capture notification would be mistaken for a duplicate of the authorization and dropped.

Answering 200 as soon as the notification is saved and creating the order in the background, with a worker, cuts retries sharply, because the response takes milliseconds even in the busiest hour. The index is still needed, because the gateway also resends after a network failure or a store deploy in the middle of a delivery.

How to find the orders that were already created

A query that groups the last seven days of orders by payment ID shows the duplicates:

SELECT payment_id,
       COUNT(*) AS order_count,
       MIN(created_at) AS first_seen,
       MAX(created_at) AS last_seen
FROM orders
WHERE created_at >= NOW() - INTERVAL 7 DAY
GROUP BY payment_id
HAVING COUNT(*) > 1
ORDER BY first_seen;

The syntax is MySQL’s, and table and column names vary by platform. In Adobe Commerce, where the ID lives depends on the payment gateway module.

Each row is a payment with more than one order. The gap between first_seen and last_seen is usually a few seconds, and the time of the first duplicate shows when the store started running past the timeout. The gateway’s webhook delivery log shows every retry with its time and the code the store returned, which confirms it.

Before shipping, keep the oldest order of each pair and cancel the other if it hasn’t left the warehouse. The duplicate also reserved stock and may have reached the ERP and the WMS, so cancel it in all three. There’s only one payment, so there’s nothing to refund.

A double charge has a different cause

Two orders with the same payment ID are one charge. A double charge looks like two payments with different IDs, from the same customer for the same amount, a few minutes apart. The customer may have clicked “Pay” again because the page was slow, or the store may have repeated its call to the gateway’s API after a timeout, without the idempotency key that many gateways offer in their API for that case.

To find them, group the orders by customer email and amount, and look at the pairs less than 15 minutes apart. The second charge is refunded from the gateway’s dashboard and the repeated order is canceled. To keep it from happening again, the charge call carries an idempotency key tied to the cart and reused on every retry, and the button is disabled on the first click.

How to tell whether the store is exposed

It’s enough to compare the gateway’s timeout with how long the webhook endpoint takes in last year’s busiest hour, and to run the query above. If the endpoint takes longer than the timeout in that hour, retries are happening, and the unique index is what keeps each one from turning into an order.

It’s worth logging every notification that arrives a second time and putting an alert on it, because each one marks a response that ran past the timeout. That alert is part of what we call observability.

The rest of the event checks are in the Black Friday guide.