|
| 1 | +# Billing Module |
| 2 | + |
| 3 | +Stripe-based billing with per-plan quota management. |
| 4 | + |
| 5 | +## Quota System |
| 6 | + |
| 7 | +### Configuration |
| 8 | + |
| 9 | +Each downstream project defines its quotas in the billing config: |
| 10 | + |
| 11 | +```js |
| 12 | +// config/defaults/development.config.js |
| 13 | +billing: { |
| 14 | + quotas: { |
| 15 | + free: { documents: { create: 10, export: 50 } }, |
| 16 | + starter: { documents: { create: 100, export: 500 } }, |
| 17 | + pro: { documents: { create: Infinity, export: Infinity } }, |
| 18 | + }, |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +### Middleware — `requireQuota(resource, action)` |
| 23 | + |
| 24 | +Enforces per-plan limits on routes. Returns 429 when quota exceeded. |
| 25 | + |
| 26 | +```js |
| 27 | +import requireQuota from '../billing/middlewares/billing.requireQuota.js'; |
| 28 | + |
| 29 | +app.route('/api/documents') |
| 30 | + .post(passport.authenticate('jwt', { session: false }), |
| 31 | + organization.resolveOrganization, |
| 32 | + requireQuota('documents', 'create'), |
| 33 | + documents.create); |
| 34 | +``` |
| 35 | + |
| 36 | +### Tracking Usage — `BillingUsageService` |
| 37 | + |
| 38 | +Increment counters after successful operations: |
| 39 | + |
| 40 | +```js |
| 41 | +import BillingUsageService from '../billing/services/billing.usage.service.js'; |
| 42 | + |
| 43 | +// After creating a document |
| 44 | +await BillingUsageService.increment(organizationId, 'documents.create', 1); |
| 45 | +``` |
| 46 | + |
| 47 | +### Usage Endpoint |
| 48 | + |
| 49 | +`GET /api/billing/usage` — returns current usage, limits, and plan for the authenticated org. |
| 50 | + |
| 51 | +### Events |
| 52 | + |
| 53 | +Listen for plan changes in downstream modules: |
| 54 | + |
| 55 | +```js |
| 56 | +import { billingEvents } from '../billing/lib/events.js'; |
| 57 | + |
| 58 | +billingEvents.on('plan.changed', ({ organizationId, previousPlan, newPlan, isDowngrade }) => { |
| 59 | + if (isDowngrade) { |
| 60 | + // Handle downgrade (e.g. disable premium features) |
| 61 | + } |
| 62 | +}); |
| 63 | +``` |
0 commit comments