Billing & Monetization Documentation
๐ฐ Overviewโ
The Billing Service (src/services/billingService.ts) manages subscriptions, usage tracking (Credit System), and invoicing. It is designed to support a Hybrid Pricing Model (Subscription + Usage-Based Overages).
๐ท๏ธ Pricing Tiers (Hardcoded Configuration)โ
| Tier | Base Price (IDR) | Token Limit | Overage Rate |
|---|---|---|---|
| FREE | Rp 0 | 500 | - |
| PRO | Rp 299.000 | 50,000 | Rp 10 / 1k tokens |
| ENTERPRISE | Rp 999.000 | 500,000 | Rp 10 / 1k tokens |
Configuration is located in
billingService.tsconstantPRICING.
๐ณ Credit System (AI Tokens)โ
The system tracks AI usage via aiUsageCount on the teams table.
Usage Tracking Flowโ
- Check Quota: Before any AI operation,
BillingService.checkQuota(teamId, cost)is called. - Execute Operation: If checking passes, the AI task runs.
- Deduct/Track:
BillingService.trackUsage(teamId, tokensUsed)increments the counter.- Also updates/creates a
usage_billingsrecord for the current month. - Triggers "Upsell" notification if usage approaches the limit.
- Also updates/creates a
๐งพ Monthly Billing Cycleโ
- Background Job:
processMonthlyBilling()(Usually triggered via Cron/Inngest). - Logic:
- Calculates
overageTokens = usage - limit. usageAmount = overageTokens * rate.totalAmount = baseAmount + usageAmount.- Generates a database record in
usage_billings. - Sends email notification to Team Admins.
- Calculates
๐ Stripe Integrationโ
- Webhooks:
handleSubscriptionChangeupdatesteam.tierandteam.subscriptionStatus. - Payment Failure: Automatically downgrades status to
past_dueand blocks AI access viacheckQuota.
๐ Usage Exampleโ
import { BillingService } from '@/services/billingService';
const teamId = "team_123";
const estimatedCost = 100;
// 1. Gatekeeping
const canProceed = await BillingService.checkQuota(teamId, estimatedCost);
if (!canProceed) throw new Error("Quota exceeded or Payment failed");
// 2. Perform Expensive Action...
// ...
// 3. Billing
await BillingService.trackUsage(teamId, estimatedCost);