← Back to all products
$29
Subscription Billing Engine
Subscription billing engine with recurring payments, proration, dunning, and plan management.
JSONMarkdownPython
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 9 files
subscription-billing/
├── LICENSE
├── README.md
├── examples/
│ └── plans_config.json
├── free-sample.zip
├── guide/
│ ├── 01_features.md
│ ├── 02_cli-reference.md
│ └── 03_license.md
├── index.html
└── src/
└── subscription_billing.py
📖 Documentation Preview README excerpt
Subscription Billing Engine
Part of the Payment Stack by CodeVault
A complete subscription lifecycle manager: plan creation, trial periods, proration, grace periods, and dunning workflows. Understand every billing pattern before integrating with Stripe Billing.
Features
- Plan management (create, list, activate/deactivate)
- Subscription lifecycle: trial → active → past_due → canceled
- Mid-cycle plan changes with automatic proration
- Trial periods with configurable duration
- Grace periods for failed payments
- Dunning workflow: retry → email → final warning → cancel
- Billing cycle calculations (monthly, yearly, weekly)
- Cancel at period end or immediately
- Full event logging for audit trails
- Python stdlib only — zero dependencies
Quick Start
# Run the full lifecycle demo
python src/subscription_billing.py --action demo
# Create plans
python src/subscription_billing.py --action create-plan --name "Pro" --price 4999 --interval month
# Create a subscription with trial
python src/subscription_billing.py --action subscribe --customer cus_001 --plan plan_pro
# Upgrade plan with proration
python src/subscription_billing.py --action change-plan --sub-id sub_abc --new-plan plan_enterprise
# View dunning schedule
python src/subscription_billing.py --action dunning-schedule
CLI Reference
| Flag | Description |
|---|---|
--action, -a | Action to perform (required) |
--name | Plan name |
--price | Plan price in cents (4999 = $49.99) |
--interval | Billing interval: month, year, week |
--customer | Customer ID |
--plan | Plan ID |
--sub-id | Subscription ID |
--new-plan | New plan ID for upgrades/downgrades |
--no-trial | Skip trial period |
--immediate | Cancel immediately instead of at period end |
--reason | Cancellation reason |
--data-dir | Data storage directory (default: ./billing_data) |
Dunning Schedule
| Day | Action | Description |
|---|---|---|
| 0 | retry | Immediate retry — often temporary |
... continues with setup instructions, usage examples, and more.
📄 Code Sample .py preview
src/subscription_billing.py
#!/usr/bin/env python3
"""
Subscription Billing Engine — Payment Stack by DataNest
A complete subscription lifecycle manager: plan creation, customer subscriptions,
proration calculations, trial periods, grace periods, dunning workflows, and
billing cycle management. All logic runs locally with JSON file persistence.
Why this exists:
Subscription billing is deceptively complex. You need to handle trial → active
transitions, mid-cycle plan changes (proration), failed payment retries
(dunning), grace periods before cancellation, and annual vs monthly billing
cycles. This engine implements all of that so you understand the patterns
before integrating with Stripe Billing or building your own system.
Usage:
python subscription_billing.py --action create-plan --name "Pro" --price 4999 --interval month
python subscription_billing.py --action subscribe --customer cus_001 --plan plan_pro
python subscription_billing.py --action change-plan --sub-id sub_abc --new-plan plan_enterprise
python subscription_billing.py --action demo
License: MIT
"""
from __future__ import annotations
import argparse
import json
import logging
import sys
import uuid
from dataclasses import dataclass, field, asdict
from datetime import datetime, date, timedelta, timezone
from enum import Enum
from pathlib import Path
from typing import Any, Optional
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
# How many days a trial lasts by default
DEFAULT_TRIAL_DAYS = 14
# Grace period: days after a failed payment before cancellation
DEFAULT_GRACE_PERIOD_DAYS = 7
# Maximum dunning attempts before giving up
MAX_DUNNING_ATTEMPTS = 4
# ... 709 more lines ...