We didn't have an incident. Nothing was on fire. We just decided to sit down and audit our own payments and refunds code the way we'd audit someone else's — assuming it was wrong somewhere, and trying to prove it.
It was wrong in three places. Here's what we found.
Bug 1: cancelling an order quietly leaked everything
Checkout deducts stock, credits used gift card balance against the order, and records coupon usage — all inside one transaction, which is correct, because checkout really is one atomic operation.
The problem showed up later. If that order got cancelled before it was ever paid — which happens constantly, people abandon pending COD orders, staff cancel stale carts — none of that got reversed. No restock. No gift card credit given back. No coupon usage released. The inventory number just silently drifted further from reality every time this happened, forever, with nothing in the logs to tell you it was happening.
The fix was two parts.…