A billing agent is a program that decides to move money and then asks other systems to do it. It writes to a ledger (your record of what a customer owes) and it calls a processor (the system that actually charges a card or bank account). Those two systems cannot be updated in one atomic transaction, so the operation is eventually consistent: it proceeds through steps, and the overall state is briefly inconsistent until all steps finish compensating transaction pattern.
Four failure modes matter, and each gets its own test.
- Retry after timeout. The agent sends a charge, the processor commits it, and the response times out. The agent does not know whether the charge happened. If it retries without a reusable idempotency key, the customer is charged twice.
- Duplicate webhook. The processor fires a success event and the delivery fails, so it retries. Your handler sees the same event twice. Providers deliver on an at-least-once basis, meaning you will eventually receive the same webhook more than once webhook idempotency guide.
- Partial write. The ledger records the invoice change but the processor call fails, or the reverse: the processor charges but your database write rolls back.
- Out-of-order events. A refund webhook arrives before the payment succeeded webhook, and your state machine takes an illegal transition.
Enumerate these on paper first, because each one needs a different injection point in the fake processor. A suite that only covers the happy path will return 200 all day and still double-charge a customer. Status codes prove delivery acknowledgement, not financial correctness payment webhook testing.
There is a second reason to write the failure list down: it doubles as your definition of "usage" for any metered component of the bill. If a failed or retried charge counts as billable, you need to say so explicitly, because vague definitions on retries are a common source of disputes later usage event design. Finding your own answer to "are retries free?" is easier in a test file than in a customer email.



