Payments after the integration: late IPNs, reconciliation and a failure-mode table
The operations side of integrating a VNPay-style gateway: a case study of a successful IPN arriving after inventory was released, the transaction-query job, reconciling the gateway statement with the database and the ledger, BDD-style sandbox testing, and a complete failure-mode table.
On this page
An integration that "works" still meets IPNs that arrive late, IPNs that never arrive, and numbers that disagree with the gateway. This post handles the late IPN, designs reconciliation, and tests every failure mode. Read first: part 1, and the booking and ledger post for the ledger.
1. The problem
Seats are held for 15 minutes. A customer opens the gateway at minute 13 and is charged at minute 16, but the job released the seat at minute 15 to someone else. The IPN arrives, the handler sees an EXPIRED booking and ignores it: the customer loses 3,000,000 VND and nobody knows.
2. Key idea
The IPN is the bank's letter; the query job is phoning when the letter hasn't come; reconciliation is comparing your books with the statement at day's end.
| Layer against late IPNs | What it does |
|---|---|
| 1. Prevent it | vnp_ExpireDate 15 minutes, hold 20 minutes; the expiry job skips bookings with a PENDING payment |
| 2. Ask before releasing | release only once the gateway confirms it's unpaid |
| 3. The late IPN still arrives | record SUCCEEDED; same-class seat available → PAID, otherwise REFUND_PENDING |
Add a metric "successful IPNs for expired bookings": anything above 0 means the time windows are wrong. Engineers make it rare and never silent.
2.1 What does reconciliation produce?
| Outcome | Meaning | Action |
|---|---|---|
MATCHED |
matches | nothing |
NOT_PAID_HERE |
the gateway collected, we still have PENDING/EXPIRED |
run apply with statement data, investigate |
UNKNOWN_TO_US |
the gateway has a transaction we didn't create | alarm: wrong merchant, leaked key |
OURS_ONLY |
we recorded success, the gateway has nothing | worst: tickets may have shipped unpaid |
AMOUNT_MISMATCH |
amounts differ | lock the order, investigate |
2.2 Wait for IPNs or ask the gateway?
Both. IPNs can vanish (endpoint down, wrong URL after a domain change), so a job asks the gateway about overdue payments. Duplicates are harmless because both go through the same apply.
3. In Java
@Scheduled(fixedDelay = 60_000)
void reconcilePending() {
List<Payment> batch = tx.execute(s -> payments.lockStale(50)); // FOR UPDATE SKIP LOCKED, set last_queried_at
for (Payment p : batch) { // network calls AFTER commit, no locks held
rateLimiter.acquire(); // respect the gateway's quota
gateway.queryDr(p.txnRef()).ifPresent(paymentService::apply);
}
}
@Transactional
public void apply(GatewayResult r) {
int n = payments.markResult(r.txnRef(), r.status()); // WHERE status IN ('PENDING','EXPIRED')
if (n == 0) return; // already handled: duplicate IPN gets "02"
if (!bookings.markPaid(r.bookingId())) // booking already EXPIRED
payments.markRefundPending(r.txnRef()); // + refund outbox, key = txn_ref
}Reconciliation filters the day with paid_at >= '2026-11-18 00:00+07' AND paid_at < '2026-11-19 00:00+07', not paid_at::date.
4. Working with AI
Give the AI: the FULL JOIN reconciliation query, BDD scenarios for IPNs, CI tests that sign IPNs with a test key (no sandbox dependency).
Check in AI-written code:
- The IPN handler never ignores a successful IPN for an expired booking.
- The expiry job doesn't release bookings with a
PENDINGpayment. - IPNs and the query job call the same
applywith a conditional update. - Reconciliation filters days in
+07, not with::date.
5. Exercises
Exercise 1: Mismatches every morning
Every morning the reconciliation job reports dozens of OURS_ONLY and the same number of UNKNOWN_TO_US, all between 00:00 and 07:00. Finance checks by hand and they match. The AI suggests "ignore mismatches under 50 transactions". Diagnose and fix.
Solution to Exercise 1
The statement splits days in Vietnam time, while the query uses paid_at::date, which cuts in the session's zone (UTC). Transactions between 00:00 and 07:00 Vietnam time fall into "the day before" on our side.
Fix: filter with an explicit +07 range, and also check the neighbouring day's statement for transactions near midnight before concluding OURS_ONLY. No ignore threshold: it hides real mismatches too.
Exercise 2: Paying from two tabs
A customer opens two tabs, clicks pay in both, and is charged twice. Design it so seats aren't double-sold and no extra money is kept.
Solution to Exercise 2
- At most one
PENDINGpayment per booking: the second tab gets the existing URL (a unique partial index onbooking_id WHERE status = 'PENDING'). - If two successes still happen (the old URL expired and a new attempt was created), for the second one
applysees the booking isPAIDand moves the payment toREFUND_PENDING, with a refund outbox keyed bytxn_ref.
Summary
- A late successful IPN is still recorded as
SUCCEEDED, then the seat is kept or refunded. - URL expiry shorter than the hold; never release a booking with an open payment.
- IPNs and the query job go through one idempotent
apply. - Reconciliation
FULL JOINgives 5 outcomes;OURS_ONLYis the worst. - Filter days with a
+07range, not::date.
Read more: Integrating the VNPay gateway.
Comments
Community rules