(Note · · 7 min read)

Your checkout is leaving behind orders nobody paid for

Every payment integration eventually produces two piles. One is orders nobody paid for, sitting unpaid and holding stock. The other is worse and quieter: money taken with nothing in your system to show for it. Both come from the same thing — deciding what is true from the wrong signal.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Do not create anything until the gateway agrees to begin

The first pile comes from writing the record before the payment exists. The user clicks pay, the application creates the order, and only then asks the gateway for a payment page. If the gateway refuses — bad configuration, a rejected amount, a disabled account — the order stays behind, unpaid and probably holding inventory.

On a hotel booking platform I took over, this was exactly the failure: a payment refused by the gateway surfaced to guests as a generic error and left an unpaid booking behind. The repair was to reverse the order of operations — ask the gateway for a payment page first, and only create the reservation once one comes back. Nothing is written for a payment that never started.

While you are there, pass on what the gateway actually said. A generic failure message trains your support team to guess, and the guest to try again with the same card and the same result. The gateway's real reason is information both of them need.

But you do need a record before the user leaves

Having moved the write later, do not move it too far. Once the gateway has agreed to a session and the user is being sent off to pay, there must be something in your database keyed to that session's reference before they go.

Otherwise you create the second pile. The payment succeeds, the provider tells you about it, and the notification arrives referring to a transaction your system has never heard of — with no user, no cart and no order to attach it to. Money in, nothing recorded.

So the sequence is: ask the gateway, get a session, write a pending record carrying that session's reference, then redirect. Pending is not an order; it is a claim that something is in flight, and it is what every later signal attaches itself to.

The redirect back is a hint, not the truth

The most common mistake is confirming the order on the success URL the gateway redirects to. That redirect is a browser event, and browsers are not reliable narrators. The customer closes the tab on the bank's page, the phone drops the connection, the app switches away and never comes back. The payment still happened; your success handler never ran.

Treat the redirect as presentation only: show the user something reassuring, and have the page ask your own backend what the state is rather than asserting it. The same goes for the failure and cancellation URLs — useful for sending someone back to the right place, never the basis for deciding what happened.

The server-to-server notification decides

The provider's webhook is the signal that actually determines state, because it does not depend on the customer's browser surviving. Two properties of it shape everything you build around it.

  • It is delivered at least once, not exactly once. The same event can arrive twice, or ten times. Every handler must be safe to run repeatedly.
  • It can arrive out of order, and late. A pending notification can land after the final one, so applying events blindly can move a paid order back to unpaid.

Both are handled the same way: make the write conditional rather than absolute. Update the record only where it is still in the state that transition expects, and ignore anything that would move it backwards. Returning a 200 means you received the message, not that the order is correct — so acknowledge quickly, then do the work where you can see it fail.

Verify the message is genuinely from the provider, and check the amount and currency against what you expected rather than trusting the payload to describe your own order back to you.

Then reconcile, because notifications go missing

Webhooks are delivered over the same internet as everything else. Yours will be down for ten minutes one day, and some events will not survive the retries.

So nothing should be allowed to sit pending forever. Run a job that finds records still in flight past a sensible age, asks the provider directly about each stored reference, and settles them against the authoritative answer. This is the piece most integrations skip, and it is the one that stops a silent gap becoming a customer complaint weeks later.

One handler, many accounts

If you take payments for several businesses, brands or properties, each will eventually need its own merchant account — money has to land in the right bank account, and the reconciliation downstream is somebody's actual job.

The tempting shortcut is to copy the payment code per brand. It works twice and becomes a liability on the third, because a fix to the refund path now has to be made everywhere, and will not be. On that hotel platform the new properties were each brought up on their own merchant account through one parameterised payment handler rather than another copied one, and success, failure and cancellation each return the guest to their own hotel with the booking and inventory updated.

Keep the credentials as configuration belonging to the tenant, not as branches in the code. The logic is the same for every one of them; only the account differs.

The order to build it in

  1. Ask the gateway to open a payment session. Write nothing yet.
  2. If it refuses, show the user the gateway's actual reason and stop.
  3. Create a pending record carrying the session reference, then redirect.
  4. Treat return URLs as presentation; ask your own backend for the state.
  5. Decide state from the verified webhook, with conditional writes that cannot move backwards or double-apply.
  6. Check amount and currency against what you expected, not against the payload's own claims.
  7. Reconcile anything still pending past a time limit by asking the provider directly.

Common questions

Why does my checkout leave unpaid orders behind?

Because the order is written before the payment exists. If the gateway then refuses to open a payment session, the record stays behind unpaid and often holding stock. Ask the gateway for a payment page first and only create the record once it agrees to start.

Should I create the order before or after taking payment?

Neither extreme works. Create nothing until the gateway agrees to open a session, but do write a pending record carrying that session's reference before redirecting the user — otherwise a successful payment arrives referring to a transaction your system has never heard of.

Can I confirm an order on the payment gateway's success redirect?

No. The redirect is a browser event, and the browser may never come back — a closed tab or a dropped connection still leaves the payment completed. Use return URLs for presentation only and have the page ask your own backend what the state is.

Why must payment webhook handlers be idempotent?

Because providers guarantee at-least-once delivery, so the same event can arrive repeatedly, and events can arrive late or out of order. Make writes conditional on the record still being in the expected state and ignore anything that would move a paid order backwards.

What happens if a payment webhook never arrives?

The order sits pending and the customer believes they paid. Run a reconciliation job that finds records in flight past a time limit, queries the provider using the stored reference, and settles them against the authoritative answer. Most integrations skip this step.

How do I take payments into a separate account per brand or property?

Keep one parameterised payment handler and treat the merchant credentials as configuration belonging to each tenant. Copying the payment code per brand means every later fix has to be repeated in each copy, and eventually will not be.

Need something like this built?