Billing Automation
Master automated billing systems including recurring billing, invoice generation, dunning management, proration, and tax calculation.
When to Use This Skill
- Implementing SaaS subscription billing
- Automating invoice generation and delivery
- Managing failed payment recovery (dunning)
- Calculating prorated charges for plan changes
- Handling sales tax, VAT, and GST
- Processing usage-based billing
- Managing billing cycles and renewals
Core Concepts
1. Billing Cycles
Common Intervals:
- Monthly (most common for SaaS)
- Annual (discounted long-term)
- Quarterly
- Weekly
- Custom (usage-based, per-seat)
2. Subscription States
trial → active → past_due → canceled
→ paused → resumed
3. Dunning Management
Automated process to recover failed payments through:
- Retry schedules
- Customer notifications
- Grace periods
- Account restrictions
4. Proration
Adjusting charges when:
- Upgrading/downgrading mid-cycle
- Adding/removing seats
- Changing billing frequency
Quick Start
from billing import BillingEngine, Subscription
# Initialize billing engine
billing = BillingEngine()
# Create subscription
subscription = billing.create_subscription(
customer_id="cus_123",
plan_id="plan_pro_monthly",
billing_cycle_anchor=datetime.now(),
trial_days=14
)
# Process billing cycle
billing.process_billing_cycle(subscription.id)
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Implementation Checklist
Before building or changing billing logic, capture:
- Product catalog: plan ids, prices, currency, billing interval, trial rules, and included usage.
- Customer lifecycle: signup, trial conversion, renewal, pause, resume, cancellation, and reactivation.
- Payment failure policy: retry cadence, email/SMS notices, grace period, account restrictions, and final cancellation behavior.
- Proration rules: upgrades, downgrades, seat changes, coupons, taxes, refunds, and invoice credits.
- Ledger model: immutable invoice records, payment attempts, adjustments, and audit metadata.
- Compliance constraints: tax invoices, receipts, PCI boundaries, refund approvals, and data-retention requirements.
Verification Scenarios
Test billing changes with concrete timelines:
- Trial user converts to paid plan before trial end.
- Monthly subscriber upgrades mid-cycle and receives prorated credit.
- Payment fails three times, enters dunning, then recovers.
- Annual customer cancels with service continuing through paid period.
- Seat count changes immediately before renewal.
Record expected invoices, subscription state transitions, customer notifications, and ledger entries for each scenario. Do not ship billing automation with only happy-path tests.
Risk Controls
Billing changes need stronger safeguards than ordinary workflow automation:
- Use idempotency keys for invoice creation, payment attempts, webhook processing, and retry jobs.
- Store external payment processor ids separately from internal subscription ids.
- Treat webhooks as eventually consistent; never assume event order is guaranteed.
- Make retry jobs safe to run more than once.
- Keep a manual override path for support teams, but log who changed what and why.
- Separate preview calculations from committed ledger writes.
- Add alerts for abnormal retry volume, invoice creation failures, tax calculation errors, and webhook backlog.
User-facing Summary
When reporting billing automation work, include the business effect:
Changed:
Risk reduced:
Customer impact:
Accounting impact:
Tests run:
Open rollout concern:
This helps reviewers evaluate both code correctness and revenue operations risk.
1---2name: billing-automation3description: Build automated billing systems for recurring payments, invoicing, subscription lifecycle, and dunning management. Use when implementing subscription billing, automating invoicing, or managing recurring payment systems.4license: MIT5---6
7# Billing Automation
8
9Master automated billing systems including recurring billing, invoice generation, dunning management, proration, and tax calculation.
10
11## When to Use This Skill
12
13- Implementing SaaS subscription billing
14- Automating invoice generation and delivery
15- Managing failed payment recovery (dunning)
16- Calculating prorated charges for plan changes
17- Handling sales tax, VAT, and GST
18- Processing usage-based billing
19- Managing billing cycles and renewals
20
21## Core Concepts
22
23### 1. Billing Cycles
24
25**Common Intervals:**
26
27- Monthly (most common for SaaS)
28- Annual (discounted long-term)
29- Quarterly
30- Weekly
31- Custom (usage-based, per-seat)
32
33### 2. Subscription States
34
35```
36trial → active → past_due → canceled
37 → paused → resumed
38```
39
40### 3. Dunning Management
41
42Automated process to recover failed payments through:
43
44- Retry schedules
45- Customer notifications
46- Grace periods
47- Account restrictions
48
49### 4. Proration
50
51Adjusting charges when:
52
53- Upgrading/downgrading mid-cycle
54- Adding/removing seats
55- Changing billing frequency
56
57## Quick Start
58
59```python
60from billing import BillingEngine, Subscription
61
62# Initialize billing engine
63billing = BillingEngine()
64
65# Create subscription
66subscription = billing.create_subscription(
67 customer_id="cus_123",
68 plan_id="plan_pro_monthly",
69 billing_cycle_anchor=datetime.now(),
70 trial_days=14
71)
72
73# Process billing cycle
74billing.process_billing_cycle(subscription.id)
75```
76
77## Detailed patterns and worked examples
78
79Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
80
81<!-- LOCAL-CURATION-SUPPLEMENT:START -->
82## Implementation Checklist
83
84Before building or changing billing logic, capture:
85
86- Product catalog: plan ids, prices, currency, billing interval, trial rules, and included usage.
87- Customer lifecycle: signup, trial conversion, renewal, pause, resume, cancellation, and reactivation.
88- Payment failure policy: retry cadence, email/SMS notices, grace period, account restrictions, and final cancellation behavior.
89- Proration rules: upgrades, downgrades, seat changes, coupons, taxes, refunds, and invoice credits.
90- Ledger model: immutable invoice records, payment attempts, adjustments, and audit metadata.
91- Compliance constraints: tax invoices, receipts, PCI boundaries, refund approvals, and data-retention requirements.
92
93## Verification Scenarios
94
95Test billing changes with concrete timelines:
96
971. Trial user converts to paid plan before trial end.
982. Monthly subscriber upgrades mid-cycle and receives prorated credit.
993. Payment fails three times, enters dunning, then recovers.
1004. Annual customer cancels with service continuing through paid period.
1015. Seat count changes immediately before renewal.
102
103Record expected invoices, subscription state transitions, customer notifications, and ledger entries for each scenario. Do not ship billing automation with only happy-path tests.
104
105## Risk Controls
106
107Billing changes need stronger safeguards than ordinary workflow automation:
108
109- Use idempotency keys for invoice creation, payment attempts, webhook processing, and retry jobs.
110- Store external payment processor ids separately from internal subscription ids.
111- Treat webhooks as eventually consistent; never assume event order is guaranteed.
112- Make retry jobs safe to run more than once.
113- Keep a manual override path for support teams, but log who changed what and why.
114- Separate preview calculations from committed ledger writes.
115- Add alerts for abnormal retry volume, invoice creation failures, tax calculation errors, and webhook backlog.
116
117## User-facing Summary
118
119When reporting billing automation work, include the business effect:
120
121```text
122Changed:
123Risk reduced:
124Customer impact:
125Accounting impact:
126Tests run:
127Open rollout concern:
128```
129
130This helps reviewers evaluate both code correctness and revenue operations risk.
131<!-- LOCAL-CURATION-SUPPLEMENT:END -->