Billing state models
Gives billing concepts separate, explicit lifecycles so payment success, access rights, subscription status and invoice issuance cannot be confused.
billing-domain-state-machines
Team members using this skill
When is it useful?
Use it when one billing event succeeds while another fails, or several providers update the same commercial relationship asynchronously.
What does it help you do?
- Separates payments, subscriptions, entitlements and invoices.
- Uses exact monetary units and defined rounding rules.
- Preserves history through corrective entries and ordered transitions.
Try asking this
Model a paid subscription whose invoice generation fails temporarily. Keep paid access active while the invoice enters a visible retry state. Recovery should create one invoice linked to the original payment.
Detailed documentation
Read the skill’s original instructions, examples and checks in their authored language.
Show less
Billing Domain & State Machines
The single most destructive mistake in billing architecture is collapsing Payment, Subscription, Entitlement (Access) and Invoice (Legal Tax Document) into a single database table or boolean flag (user.is_paid = true). These four concepts have different lifecycles, different failure modes and often different authoritative systems.
1. The Four Distinct Domain Boundaries
Model and persist these four domains separately, linked by explicit correlation IDs:
- Payment / Charge (
PaymentIntent,PaymentAttempt):- Represents a specific money movement attempt (
created -> requires_action -> processing -> succeeded | failed | canceled | refunded | disputed). - Authoritative source: Payment Service Provider (PSP) or Merchant of Record (MoR).
- Represents a specific money movement attempt (
- Subscription / Commercial Agreement (
Subscription):- Represents the recurring commercial contract (
trialing -> active -> past_due -> paused -> canceled -> expired), billing cadence, plan/price version, current period bounds (current_period_start,current_period_end) and cancellation schedule (cancel_at_period_end).
- Represents the recurring commercial contract (
- Product Entitlement / Access (
Entitlement,QuotaGrant):- Represents what features, seats or usage limits the customer can exercise right now.
- Decoupled from raw payment state: an account can have active entitlements without a payment (trials, comped accounts, enterprise wire agreements, grace periods during
past_due, or remaining prepaid period aftercancel_at_period_end).
- Legal Invoice / Receipt (
Invoice,CreditNote):- Represents the immutable accounting and tax document (
draft -> issued -> paid | voided | credited). - Retrying a failed invoice via an invoicing API (Billingo, Számlázz.hu) must never re-charge the customer's card, and an invoice API failure must not revoke paid product access.
- Represents the immutable accounting and tax document (
2. Monetary Representation & Arithmetic Invariants
- Never use IEEE 754 floating-point (
number/float/double) for money:0.1 + 0.2 !== 0.3. - Minor integer units or fixed-point decimal:
- Store amounts as integer minor units (
amount_minor: bigint | int, e.g., cents/fillér) paired with an explicit ISO 4217 currency code (currency: 'USD' | 'EUR' | 'HUF' | 'JPY'). - Watch zero-decimal and special-exponent currencies:
JPY,KRWhave 0 decimal places;HUFhas minor units in some PSP APIs (Stripe expects HUF multiplied by 100 as two-decimal minor units, whereas local Hungarian gateways often take whole forints or decimals). Verify the exact exponent rule per provider! - For unit prices requiring sub-cent precision (e.g.,
$0.0004per API token), use exact fixed-pointDECIMAL(19, 6)/BigIntscaled arithmetic and define explicit rounding rules (HALF_EVEN/ banker's rounding orHALF_UP) at invoice line-item aggregation time.
- Store amounts as integer minor units (
3. Monotonic State Transitions & Out-of-Order Events
External webhooks arrive out of order (e.g., customer.subscription.updated arriving before customer.subscription.created, or a stale payment_intent.processing arriving after payment_intent.succeeded):
- Define an explicit state transition graph; reject or ignore transitions from a terminal or newer state back to an older state.
- Track provider resource timestamps/versions or fetch the canonical current resource from the provider API when event ordering is ambiguous.
- Immutable financial history: never
DELETEor overwrite historical ledger rows, charges or issued invoices; record refunds, reversals, credit notes and balance adjustments as append-only entries.
Included files 1
SKILL.md
