State machines
Every status machine the code keeps as a transition table, rendered from that table.
Each diagram below is generated from the table or guard named in its first line, so it shows exactly the moves the code allows. An arrow into the end marker means the status has no way out. Labels on the billing-engine machines are the engine's internal billing events; the webhooks you receive are listed under Webhooks.
Invoice
The billing engine's invoice lifecycle (lifecycle.InvoiceMachine). An invoice starts as
draft and is finalized to open. An open invoice becomes paid, void or
uncollectible, and an uncollectible invoice can still become paid or void. paid
and void are final. Through the API, mark_paid and void work on open and
uncollectible invoices; a draft is deleted rather than voided.
Status writes from providers and updates
invoice.CanApplyStatus guards status writes that come from provider sync and invoice updates.
It only lets an invoice move forward, so a replayed or out-of-order event cannot reopen a closed
invoice. Unlike the engine's machine, it also lets a provider move a draft straight to paid,
void or uncollectible. Writing the same status again is allowed.
Subscription
The billing engine's subscription machine (lifecycle.SubscriptionMachine). A subscription
starts as incomplete or trialing; one Paycose bills with charge_automatically stays
incomplete until its first invoice is paid. A failed payment moves an active or
trialing subscription to past_due, and only a past_due subscription can become
unpaid. The engine applies it to trial end and cancellation; payment outcomes move the
status as shown in Subscription renewal.
Credit note
The billing engine's credit note machine (lifecycle.CreditNoteMachine). A draft is issued or
canceled; an issued note is applied or voided, and voiding sends credit_note.voided. The
API shows the voided status as void and the applied status as issued.
Refund
refund.CanRefundTransition is checked on every refund status write. succeeded and failed
can still swap when the provider later proves the other outcome. Every status also allows a
same-status write for field changes; those loops are not drawn. rejected is legacy and is
never produced again.
Quote
quotation.CanApplyQuoteStatus only moves a quote forward, so replayed webhooks never regress
it. accepted and canceled are final.
Balance transaction
Ledger legs (balance.CanTransition). A pending leg completes or is voided; a held leg settles,
is released, expires or fails settlement; a completed leg can be voided, disputed or refunded.
Not drawn here
Some statuses are derived rather than moved, so there is no table to draw:
- Payment intent status is computed from its charge (
payment_intent.DeriveStatus). - Payout and payout batch statuses are computed from their flags.
Charges, disputes, checkout sessions and test clocks change status through individual guarded updates rather than one table. Their flows are in the sequence diagrams.