Most billing code has the same bug hiding in it: it stores the account balance as a number and mutates it — balance += amount. It works fine, right up until a payment gets entered twice, or you have to fix last month's reading, and suddenly nobody can explain how an account reached its current total.
I learned the right way to do this building a real utility billing system (a client project that fell through — so I turned the engine into a product). The one decision that made it trustworthy: treat money as an append-only ledger and derive every balance from it. Never store a number you can recompute.
If you're building anything that touches money — invoicing, subscriptions, metered/usage billing, wallets — this is the part worth getting right.
The trap: the balance column
Most billing code starts like this:






