Skip to content

Commit 5585060

Browse files
docs(billing): add module README with quota system guide
1 parent 6df1249 commit 5585060

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

modules/billing/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
```

modules/billing/config/billing.development.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ const config = {
22
billing: {
33
// Plans available for subscriptions — extend as needed
44
plans: ['free', 'starter', 'pro', 'enterprise'],
5+
// Quotas — downstream projects override these per plan:
6+
// quotas: {
7+
// free: { documents: { create: 10, export: 50 } },
8+
// starter: { documents: { create: 100, export: 500 } },
9+
// pro: { documents: { create: Infinity, export: Infinity } },
10+
// },
511
// Stripe subscription statuses — see https://docs.stripe.com/api/subscriptions/object#subscription_object-status
612
statuses: [
713
'incomplete',

0 commit comments

Comments
 (0)