Prompt
How do I build a billing system with monthly and annual plans?
Latest observation
Here’s a practical way to build a billing system that supports monthly and annual plans.
1. Define your billing model
Start by deciding what you bill for:
- Plans: e.g. Basic, Pro, Enterprise
- Billing intervals: monthly, annual
- Prices: one price per plan per interval
- Billing behavior:
- auto-renew or manual renewal
- prorations when upgrading/downgrading
- trials
- taxes/VAT
- coupons/discounts
- refunds
A simple model is:
- Plan
- Price
- Subscription
- Invoice
- Payment
- Customer
2. Suggested database tables
A common schema:
plans
idnamedescriptionactive
prices
idplan_idcurrencyinterval(monthoryear)amount_centsactive
customers
idemailnamebilling_addresstax_id
subscriptions
idcustomer_idprice_idstatus(trialing,active,past_due,canceled)current_period_startcurrent_period_endcancel_at_period_endtrial_endrenewal_date
invoices
idcustomer_idsubscription_idamount_centscurrencystatus(draft,open,paid,failed,void)due_dateperiod_startperiod_end
payments
idinvoice_idproviderprovider_payment_idamount_centsstatuspaid_at
3. Core billing flow
A. Customer chooses a plan
Example:
- Basic Monthly: $10/month
- Basic Annual: $100/year
You create a subscription linked to the selected price.
B. Generate invoice
On subscription start or renewal:
- create an invoice for the next billing period
- calculate taxes/discounts
- attempt payment automatically if stored payment method exists
C. Process payment
If payment succeeds:
- mark invoice as
paid - extend subscription period by 1 month or 1 year
If payment fails:
- mark invoice as
failed - retry according to a dunning policy
- possibly move subscription to
past_due
4. Monthly vs annual logic
The main difference is the billing interval and renewal period.
Monthly plan
- invoice every 1 month
current_period_end = current_period_start + 1 month
Annual plan
- invoice every 1 year
current_period_end = current_period_start + 1 year
Use a date library to handle:
- leap years
- month-end edge cases
- timezone consistency
5. Handle upgrades and downgrades
When a customer switches plans:
- If switching mid-cycle, decide whether to:
- prorate the difference
- apply changes next renewal
- Common approach:
- compute unused value of old plan
- compute remaining value of new plan
- invoice/refund the difference
Example:
- Monthly plan upgraded halfway through the month to annual
- You charge annual price minus remaining monthly credit
6. Build a billing scheduler
You need a background job that runs daily/hourly:
- find subscriptions whose
current_period_endis near or past due - generate renewal invoices
- charge payment method
- update subscription dates
- send receipts and failure emails
This can be done with:
- cron jobs
- queue workers
- scheduled tasks
7. Payment provider integration
Usually you should not store raw card data yourself. Use:
- Stripe
- Paddle
- Braintree
- Adyen
The provider handles:
- PCI compliance
- card tokenization
- recurring payments
- webhooks for success/failure
You should listen for webhooks like:
invoice.paidinvoice.payment_failedcustomer.subscription.updatedcharge.refunded
8. Important business rules
Decide in advance:
- Do annual plans discount compared to monthly x 12?
- Are renewals automatic?
- Can users switch between monthly and annual anytime?
- Do cancellations take effect immediately or end of period?
- Do you allow partial refunds?
- How do taxes work by country?
- What happens if payment fails multiple times?
9. Example subscription state machine
Typical states:
trialingactivepast_duecanceledunpaid
Transitions:
trialing -> activeactive -> past_dueon failed paymentpast_due -> activeon successful retryactive -> canceled
10. Minimal implementation approach
If you want something simple, implement:
- Plan and price tables
- Customer table
- Subscription table
- Invoice generation job
- Payment provider integration
- Webhook handler
- Renewal scheduler
- Email notifications
11. Example pseudo-flow
User signs up
→ choose Monthly or Annual
→ create subscription
→ create invoice
→ charge payment method
→ if success:
subscription active
set next renewal date
→ if failure:
mark past_due
retry later
12. Recommended approach if you’re starting from scratch
If possible:
- use Stripe Billing or a similar service
- keep your own internal tables for app access, entitlements, and subscription status
- let the billing provider handle invoicing, proration, retries, and taxes where possible
This reduces complexity a lot.
If you want, I can also give you:
- a database schema in SQL,
- a backend design in Node/Python/Ruby, or
- a Stripe-based implementation example.