← Back to all products
$29
Billing Integration
Python billing system for SaaS with subscription plans, usage metering, and invoicing.
PythonMarkdown
📄 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 10 files
billing-integration/
├── LICENSE
├── README.md
├── examples/
│ └── basic_example.py
├── free-sample.zip
├── guide/
│ ├── 01_features.md
│ ├── 02_quick-start.md
│ ├── 03_configuration.md
│ └── 04_project-structure.md
├── index.html
└── src/
└── main.py
📖 Documentation Preview README excerpt
Billing Integration
A complete Python billing system for SaaS applications. Handles subscription plans, customer management, usage metering, invoice generation, and webhook processing — all built on Python's standard library.
Features
- Subscription plans — Free, flat-rate, and metered billing with monthly/yearly intervals
- Customer management — Create customers, link to your internal user/tenant IDs
- Usage metering — Record API calls, storage, compute with idempotency protection
- Invoice generation — Automatic line items for base plan + metered usage
- Webhook processing — Verify HMAC-SHA256 signatures, dispatch to typed handlers
- Trial support — Configurable trial periods per subscription
- Graceful cancellation — Cancel immediately or at period end
- JSON persistence — Save/load state for development and testing
Requirements
- Python 3.10+
- No external dependencies (stdlib only)
Quick Start
# Start the billing server with demo data
python src/main.py --init-demo
# Start on a custom port
python src/main.py --port 8001
Then try the API:
# List available plans
curl http://localhost:8001/api/plans
# Create a customer
curl -X POST http://localhost:8001/api/customers \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "name": "Acme Corp", "external_id": "tenant_123"}'
# Subscribe to a plan (use plan ID from /api/plans response)
curl -X POST http://localhost:8001/api/subscriptions \
-H "Content-Type: application/json" \
-d '{"customer_id": "cus_xxx", "plan_id": "plan_xxx", "trial_days": 14}'
# Record usage
curl -X POST http://localhost:8001/api/usage \
-H "Content-Type: application/json" \
-d '{"subscription_id": "sub_xxx", "quantity": 100, "idempotency_key": "req_001"}'
# Generate an invoice
curl -X POST http://localhost:8001/api/invoices/generate \
-H "Content-Type: application/json" \
-d '{"subscription_id": "sub_xxx"}'
API Endpoints
| Method | Path | Description |
... continues with setup instructions, usage examples, and more.
📄 Code Sample .py preview
src/main.py
#!/usr/bin/env python3
"""
Billing Integration — Stripe-Style Billing System
===================================================
A complete billing integration module for SaaS applications. Handles
subscription plans, customer management, usage metering, invoice generation,
and webhook processing — all without external dependencies.
This module provides the *business logic layer* for billing. It's designed
to sit between your SaaS application and a payment provider (Stripe, Paddle,
etc.). The included mock provider lets you develop and test billing flows
before connecting to a real payment API.
Zero dependencies. Just run: python3 main.py
Part of the SaaS Starter collection by DataNest.
License: MIT
"""
from __future__ import annotations
import argparse
import hashlib
import hmac
import json
import logging
import os
import time
import uuid
from dataclasses import asdict, dataclass, field
from datetime import datetime, timezone, timedelta
from enum import Enum
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path
from typing import Any, Optional
from urllib.parse import parse_qs, urlparse
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = 8001
DATA_DIR = Path("./billing-data")
# Webhook signing secret — in production, get this from your payment provider
WEBHOOK_SECRET = os.environ.get("BILLING_WEBHOOK_SECRET", "whsec_test_secret_key_here")
logging.basicConfig(
# ... 720 more lines ...