I built Simuno , a service that sells temporary phone numbers for receiving SMS verification codes. This post is about the one part I found hardest: making the refund automatic, so a customer never has to ask for their money back.
The problem
Codes don't always arrive. The number may already be used on that app, or the app may refuse that number range. On most services like this you then argue with support. I wanted the opposite: if the number expires with no code, the full amount returns to the wallet by itself.
That sounds simple. It isn't, because three things can try to settle the same order at the same moment: a background worker that expires orders, a webhook from the supplier, and the user pressing cancel.
Decision 1: a ledger, not a balance
Every balance change is an entry in an append-only ledger. The cached balance on the user is updated in the same database transaction as the entry and the order status change.…