logo
Parslinks
Back to blog
Guides5 min read

Payment callbacks: no double charges, no lost orders

The customer paid, but there's no order. Or they paid once and got two. Both bugs start in the same place.

Ehsan Yaghmori

Founder & CTO, Parslinks

An online flower shop does most of its yearly revenue on two days: Valentine's Day and Mother's Day. One year, the morning after, support gets three tickets in a row: "My card was charged but I have no order." Then another: "I got two confirmation emails and you sent two bouquets."

Both come from the same piece of code: what runs when the customer comes back from the payment page.

The flow, step by step

  1. Your site creates a payment with the gateway and gets a token.
  2. The customer is redirected to the bank or gateway page and authorizes the payment.
  3. The gateway sends the customer back to your callback URL.
  4. Your server verifies the payment with the gateway.
  5. The order is confirmed.

Between steps 2 and 5, anything can happen. The customer closes the tab. Their connection drops. They double-click "return to merchant". They refresh the callback page.

What can go wrong, and the right response

Rule one: don't trust callback parameters

Anyone can edit a URL. A status=success in the query string means nothing until your server calls the gateway directly and verifies the payment. Read the expected amount from your own database and compare it with the verified amount, never from request parameters.

Where the gateway also sends server-to-server webhooks, treat them as another entry into the same verification path, not a separate one.

Rule two: exactly once

That's idempotency: running the callback twice must have the same effect as running it once. The simplest approach is a status column and a conditional update:

payment-callback.ts
// claim the payment only if it's still pending
const claimed = await db.payment.updateMany({
  where: { id: paymentId, status: 'pending' },
  data: { status: 'verifying' },
});
if (claimed.count === 0) {
  // another request already took this payment
  return redirect(`/orders/${orderId}`);
}
const result = await gateway.verify({ token, amount: payment.amount });
await db.$transaction([
  db.payment.update({ where: { id: paymentId }, data: { status: result.ok ? 'paid' : 'failed', refId: result.refId } }),
  ...(result.ok ? [db.order.update({ where: { id: orderId }, data: { status: 'confirmed' } })] : []),
]);
ts14 lines

A conditional update is atomic: if two requests race, only one gets count = 1. The other sees the work is in progress and simply redirects. Tie confirmation emails and fulfilment to that status change, not to the callback running.

Rule three: the customer who never comes back

If someone pays and closes the tab, your callback never runs. Money is taken, the order is stuck in pending, and depending on the gateway it may be auto-reversed later, but the customer is already annoyed.

Add a scheduled job that, every few minutes, finds pending payments older than ten minutes and asks the gateway for their status. If your gateway offers an inquiry endpoint, this recovers most "lost" orders. See our guide to scheduled jobs.

Reconcile daily

Even with all this, compare the gateway's daily settlement report with your paid orders every day. Any mismatch is a failure path you haven't found yet.

After adding the conditional update, automatic inquiries and daily reconciliation, the flower shop got through the next Mother's Day without a single "charged but no order" ticket.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.