Feature Specification: Billing & Quotas
1. Overview & Vision
Billing & Quotas is the platform's monetization and resource-governance layer. It bridges the gap between commercial plans and technical limits, ensuring organizations are billed accurately for their usage while providing a seamless way to upgrade or manage subscriptions.
2. Personas & Stakeholders
| Persona | Goal |
|---|---|
| Owner | Upgrade plans, manage payment methods, and view invoices. |
| Admin | Monitor organization usage vs. plan limits. |
| System Admin | Define global product tiers and monitor revenue. |
3. User Stories
- As an owner, I want to upgrade to the "Pro" plan so I can add more than 5 members.
- As an admin, I want to see how much storage we have left in Drive before uploading large files.
4. Functional Requirements (FR)
- REQ-BILL-001: Support for subscription tiers (Hobby, Pro, Enterprise).
- REQ-BILL-002: Stripe Checkout integration for plan upgrades.
- REQ-BILL-003: Stripe Customer Portal for invoice and payment management.
- REQ-BILL-004: Real-time quota tracking (Storage, Users, AI Queries).
5. Non-Functional Requirements (NFR)
- Accuracy: Usage tracking must be atomic and eventually consistent within 1 minute.
- Security: Zero storage of raw credit card data (PCI compliance via Stripe).
6. Business Logic & Rules
- Hard vs. Soft Limits: Storage is a hard limit (stop uploads); AI queries are soft limits (metered overage).
- Grace Periods: 3-day grace period for failed payments before account locking.
- Pro-rated Billing: Stripe handles automated pro-ration for mid-cycle upgrades.
7. User Interface (UI/UX)
- "Plans & Pricing" comparison matrix.
- "Usage Dashboard" with progress bars for each quota.
- In-app banners for "Limit Approaching" or "Payment Failed" states.
8. Information Architecture
- "Billing" section within Organization Settings.
- Quick usage stats in the Shell sidebar (optional).
9. Data Model & Persistence
- Tables:
subscription_plans,plan_limits,plan_usage. - External: Stripe Products, Prices, and Customers.
10. API & Service Layer
GET /api/subscriptions/usagePOST /api/subscriptions/upgradePOST /api/subscriptions/webhook
11. Integration Patterns
- Stripe Webhooks: For syncing payment status and lifecycle events.
- Quota Hooks: Satellite modules increment usage counters in the Shell via internal API calls.
12. Security & Permissions
- Only organization
Ownercan access billing settings and checkout. - Quota enforcement is checked at the API layer of each satellite module.
13. Error Handling & Resilience
- Graceful recovery from Stripe API timeouts.
- "Emergency Mode" (Read-only) when an account is locked for non-payment.
14. Performance & Scalability
- Quota counters are cached in Redis for high-frequency checks.
- Usage aggregation is performed asynchronously.
15. Globalization & i18n
- Support for multiple currencies (USD, VND).
- Region-specific tax calculation (delegated to Stripe).
16. Accessibility (a11y)
- Accessible pricing tables (aria-describedby for plan features).
17. Observability & Analytics
- Tracking of "Churn Rate" and "Upgrade Conversion" metrics.
- Monitoring of high-usage organizations for potential sales leads.
18. Testing & Quality
- Integration tests using Stripe CLI for webhook simulation.
- Unit tests for pro-ration and quota logic.
19. Constraints & Assumptions
- Assumes a valid Stripe account is configured in the environment.
20. Future Enhancements
- Usage-based (metered) billing for AI tokens.
- Add-on resource packs (e.g., "+10GB Storage").