Subscription & Billing Guide π‘ BETA
Billing is implemented by the botbilling crate over a feature-gated product catalog in botproducts.
Overview
Billing is implemented in the botbilling crate alongside the product catalog in botproducts; both are feature-gated by billing, which the saas feature enables (root AGENTS.md, Cloud SaaS Product Architecture).
Plan Structure
The plan manager reads a configuration keyed by plan id. The three configured ids are free, shared and private-cloud; shared is the default offer in the admin upgrade handlers.
Default Plans
| Plan id | Notes from source |
|---|---|
free |
Lowest quota tier in botbilling/src/plans/ |
shared |
Shared tier; used as the default in admin upgrade handlers |
private-cloud |
Unlimited-tier quotas in the plan tests |
The SaaS catalog in botproducts seeds additional SKUs (plans, VMs, GPU, storage, communication and LLM tokens), all with unlimited stock.
Plan Configuration
Plan definitions live in the botbilling configuration, not in this book. Changing a limit means changing the configuration and its tests; the exact numbers are read from source, not copied into prose.
Stripe Integration
The crate has a stripe_integration module with customer creation, payment-method attachment and webhook handling. Credentials belong in Vault, never in .env or in documentation.
Configuration
Payment providers are configured outside the repository. This page records the modules that exist; it does not publish endpoints or secret names that the source does not expose.
Webhook Events
Webhook handling is implemented in botbilling/src/stripe_integration/handle_webhook.rs. Event names are defined by the provider; no local list is maintained here.
Setting Up Webhooks
- Configure the provider outside the repository.
- Store credentials in Vault under the documented secret path.
- Point the provider at the deployed host through the proxy.
- Verify a test event reaches the handler before enabling live events.
Subscription Lifecycle
Lifecycle events are recorded in botbilling/src/lifecycle/. Subscription state is scoped by branch_id, the same scope used by products and other SaaS domains.
Creating a Subscription
Subscriptions are created through the billing UI handlers or the seeded SaaS flow after signup. The signup path seeds products for the new branch (root AGENTS.md, Product Seeding).
Upgrading/Downgrading
Admin handlers accept an offer / plan_id field and fall back to shared when it is absent. The upgrade handler lives in billing_admin/handle_subscription_upgrade.rs.
Cancellation
Cancellation is handled in billing_ui/handle_subscription_cancel.rs. It changes subscription state; it does not delete tenant data.
Pausing a Subscription
No pause handler is present in the billing modules listed above; do not assume a pause API exists.
Resuming a Subscription
No resume handler is present in the billing modules listed above; re-subscribe through the upgrade flow instead.
Quota Management
Quota checks and alerts are in botbilling/src/quotas/: quota_manager, check_and_increment, with_thresholds and alert_message.
Quota Types
The tests reference UsageMetric::Messages, UsageMetric::StorageBytes and UsageMetric::Bots. Other metrics are read from the enum in source.
Checking Quotas
Call the quota manager with a plan id and a metric; it returns whether the limit is exceeded. The tests exercise the boundary values.
Usage Alerts
Alert messages are built in quotas/alert_message.rs and delivered through the billing alert notification module.
Configuring Alerts
Thresholds come from the quota configuration; no alert address is hardcoded in this book.
Grace Period
No grace-period implementation is present in the modules listed above; do not assume one.
Usage Metering
Usage increments are recorded through the quota module. Invoices are assembled from recorded line items.
Recording Usage
Usage is recorded by check_and_increment; the calling route owns the metric choice.
Querying Usage
Usage is read back through the quota manager; the invoice handlers aggregate the billed items.
Invoices
Invoice assembly, totals, line items and HTML rendering live in botbilling/src/invoice/.
Listing Invoices
The billing UI exposes an invoices handler. The response shape belongs to the generated API reference, not to prose.
Downloading Invoice PDF
The crate renders invoice HTML; no PDF generator is present in the invoice module. Do not document a PDF endpoint that does not exist.
Payment Methods
Attachment is implemented in stripe_integration/attach_payment_method.rs through the provider SDK.
Adding a Payment Method
Use the provider-backed attach flow; the method is stored by the provider, not by this crate.
Listing Payment Methods
Listing is provider-side; this crate does not own a local payment-method table.
Removing a Payment Method
Removal is provider-side; no detach handler is present in the listed modules.
White-Label Billing
Product branding is read from the root Cargo.toml version and the product configuration file; the chapter overview covers the configuration keys.
Custom Branding
Branding keys are documented in the chapter overview, not in billing prose.
Reseller Mode
No reseller-only billing flow is present in the billing modules listed above.
Custom Plan Pricing
Plan prices are read from configuration; do not copy prices into this page.
Self-Hosted Mode
A self-hosted deployment runs without the SaaS feature chain, so product seeding and cloud subscription routes are not active (root AGENTS.md, Feature Gates & Dependency Chain).
Events and Webhooks
Billing events are recorded locally; provider webhooks are handled by the Stripe integration module.
Subscription Events
Events are written by lifecycle/record_event.rs.
Configuring Webhooks
Provider webhooks are configured outside the repository; only the handler is in source.
Best Practices
- Keep plan ids stable; they are keys in configuration and in tests.
- Store provider credentials in Vault and rotate them with the CLI.
- Scope every billing query by
branch_id; tenant isolation is enforced by tests. - Do not restate prices or quotas in documentation β read them from source.
For SaaS Operators
Use the global catalog seeded with Uuid::nil() before signup; a new branch gets its own seeded scope.
For Enterprise Deployments
Enable billing without saas when only the billing modules are needed; the feature chain is saas β billing β botproducts.
For Self-Hosted
Do not expect cloud subscription routes in a build without the SaaS features.
Troubleshooting
An empty product grid in SaaS mode is a known failure mode when get_default_bot is None; the correct closure is Some(|_c| (nil, "default")) (root AGENTS.md, get_default_bot Resolver).
Payment Failed
Provider-side failures surface through the webhook handler; inspect provider logs before changing local state.
Quota Exceeded
Check the metric and plan id passed to check_and_increment; the boundary is defined by plan configuration.
Invoice Issues
Verify line items and totals in the invoice module and the stored billing events.
Related Topics
Authority: botserver/crates/botbilling/src/, botserver/crates/botproducts/ and root AGENTS.md (Cloud SaaS Product Architecture).
Limits
- No price, quota value or provider endpoint is published here; all are configuration or provider-owned.
pause,resume, PDF download and detach flows are not present in the modules cited above.- Billing scope follows
branch_id; cross-tenant queries are a regression, not a feature. - Documentation never contains credentials β Vault is the single source of truth.
Related
- Instructions for Logging Issues β how to report a billing defect.
- Chapter 12: Ecosystem & Reference