Testing Billing Webhooks for Idempotency Without Live Charges
Testing Billing Webhooks for Idempotency Without Live Charges
Testing Billing Webhooks for Idempotency Without Live Charges
Billing webhooks are the part of a payment integration that fails quietly. Your checkout works, your test card succeeds, and nothing looks wrong — until a customer gets two subscriptions, or a credit lands twice, or the same invoice email arrives three times. The cause is almost always the same: a handler that assumes each event arrives exactly once, in order, and only after the previous one finished.
None of that is true. You can test for it thoroughly without ever charging a real card.
Why Idempotency Is the Whole Ballgame
Payment providers deliver events at least once. They retry on timeouts and non-2xx responses, and they make no promise about ordering. So your handler will see the same event more than once, and sometimes two events describing the same state change.
Idempotency is the property that makes those retries harmless: processing an event twice produces the same end state as processing it once. The standard implementation is a table of processed event IDs with a unique constraint, written in the same transaction as the state change. If the insert conflicts, you return 200 and do nothing else.
The failure modes when that's missing are all customer-visible. A duplicated subscription. A credit granted twice. Two invoice lines for one month of service. A provisioning call that runs again and overwrites a user's configuration. These are the bugs that generate support tickets and refunds, and they're the ones normal happy-path testing never surfaces.
Build a Replay Harness Instead of Charging Cards
You don't need live charges to test any of this. You need captured payloads, a fake endpoint, and a way to replay.
Capture fixtures. Use the provider's test mode to trigger each event type once, then save the raw JSON as files with descriptive names — invoice.paid.json, subscription.updated.json. Keep the signature header alongside each payload so you can verify signing logic too.
Stand up a local endpoint. It should verify the signature using the test webhook secret, then write to a throwaway database. This is the same handler code you run in production, pointed at disposable infrastructure.
Write a replay script. It should be able to post the same payload sequentially, concurrently, and with configurable delays. For example, you could fire one invoice.paid payload five times in parallel with a shell loop and curl, then query the database and assert that exactly one invoice, one ledger entry, and one queued email exist.
That single test catches more real bugs than a full suite of happy-path integration tests, and it costs nothing to run in CI.
The Adversarial Cases Most Teams Skip
Sequential duplicates are the easy case. These are the ones that break handlers in production:
- Concurrent duplicates. Two identical requests arriving milliseconds apart is a different test from the same request arriving twice a second apart. Without a unique constraint or a row lock, both requests can pass the "have I seen this?" check before either writes. - Distinct event IDs, same logical change. Providers sometimes emit two different events for one state transition. Deduplicating on event ID alone won't save you; you may also need a natural key such as subscription ID plus billing period. - Out-of-order delivery. An update event can arrive before the creation event. Handlers that assume creation happened first will throw, or worse, write a partial record. - Retry after commit, before response. Your transaction committed, your process died before returning 200, and the provider retries. This punishes any side effect performed outside the transaction. - Partial failure. The database write succeeds and the downstream email or provisioning call fails. On retry, the write must be a no-op while the downstream call is retried — not repeated blindly.
A useful habit: after every replay run, assert invariants rather than status codes. "Exactly one active subscription per customer" is a far stronger test than "returned 200."
Running the Loop With a Multi-Agent Setup
This is where multi-agent collaboration earns its place, because the work splits into roles that don't need the same context.
Imagine assigning one agent to read the webhook handler and the surrounding repository, and list every side effect that happens outside a transaction. A second agent takes the captured fixtures and generates the adversarial matrix — concurrent duplicates, out-of-order pairs, retries after simulated crashes. A third inspects the database and logs after each run and reports which invariants broke, along with the exact event sequence that broke them.
That's the shape of VIBA: several AI models — ChatGPT, Claude, Gemini and others — running in one shared session, each assigned a role, with you approving the key steps and seeing cost per agent. For developer QA, the useful part isn't that AI tools can write tests. It's that generation, code review, and verification happen in the same session with a shared record of what was already tried, so findings don't get lost between tools. VIBA also covers UI testing (does the billing portal show one subscription or two?), repo testing, beta testing, and ranked repair reports when something breaks.
Treat the agents as reviewers and generators, not authorities. You still decide which invariants matter and which fixes get merged.
Conclusion
Idempotency bugs in billing are expensive precisely because they're invisible until a customer notices. You can catch most of them with three things: captured fixtures, a fake endpoint, and a replay script willing to be rude to your handler. Add agents for the parts that require reading a lot of code and generating a lot of cases, keep a human on the approval step, and you'll find the duplicate-charge bug before a customer does.
If you want to run that workflow with multiple models in one session, start at https://viba.guru.
See VIBA: https://viba.guru