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.
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
- Your site creates a payment with the gateway and gets a token.
- The customer is redirected to the bank or gateway page and authorizes the payment.
- The gateway sends the customer back to your callback URL.
- Your server verifies the payment with the gateway.
- 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.
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:
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.
Related posts
- Guides
Yalda night sales and Nowruz traffic: getting your site ready for the hour everyone shows up
The sale SMS goes out at 9pm, and in the first five minutes you get ten times a normal day's traffic. This post covers what breaks first under that load, how to load-test properly, and what to do from two weeks before until the morning after.
- Guides
Environment variables and secrets: from a .env on your laptop to production
Why does a key you marked "hidden" in your dashboard show up in your site's JavaScript? The difference between build-time and runtime variables, separate values for each environment, and what to do in the first minutes after a key leaks.
- Guides
Monitoring and logs for a small team: four numbers that are enough to start
The first person to notice your site is down shouldn't be a customer. This post explains the four basic signals (latency, traffic, error rate and saturation), suggests sensible alert thresholds for each, and shows how to write logs that help on incident day.
