BILLING & CREDITS
Frontend subscription & credit UX, admin support endpoints, and architecture study of Stripe webhooks, idempotency guards & dual pools.
Ownership & Role Scope
Shared UI/admin/pricing-PRD contribution alongside CTO-owned core billing-engine infrastructure
Stack & Modules
Architecture inspected read-only in billing-engine (built by CTO Nilesh; Vishank contributed billing UI alignment, admin support endpoints & pricing PRD).
Signed Stripe Ingress
Subscription Checkout
Attaches accountUserId, tier, interval & creditsPerPeriod metadata
express.raw + Timing-Safe
Preserves req.rawBody for constructEvent & HMAC verification
4-Layer Idempotency Guard
Processed Event + Atomic Upsert
Webhook lookup + $setOnInsert dedup + deterministic boost:<id> keys
CreditHold 11000 Guard
Unique CreditHold.idempotencyKey prevents double-charge holds
Dual Pool Waterfall
$expr Atomic Deduction
Consumes expiring creditBalance FIRST, addonCreditBalance SECOND
Billing UI + Admin Endpoints
Vishank: billing history/subscription UI, adminController.ts & v3 PRD
Core billing-engine commits are team/CTO-owned; shown strictly to document verified system boundaries.
Frontend subscription & credit UX, admin support endpoints, and architecture study of Stripe webhooks, idempotency guards & dual pools.
Subscription Allowances Paired With Persistent Add-On Credits
AI company research and enrichment carry variable per-call compute costs. MagiQ AI separates expiring monthly/annual subscription credits (`creditBalance`) from purchased top-up packs (`addonCreditBalance`), while tracking reserved credits on active in-flight holds (`reservedCredits`).
Webhook Verification & Multi-Layer Idempotency Guard
To prevent duplicate credit grants during webhook retries, `/webhooks/stripe` mounts raw JSON buffering before body parsing for byte-exact `stripe.webhooks.constructEvent` validation, while payment webhooks verify HMAC-SHA256 signatures with `crypto.timingSafeEqual`.
Processed webhook records, deterministic `boost:<sessionId>` keys, atomic `$setOnInsert` upserts, and unique `CreditHold.idempotencyKey` constraints enforce safe replay handling across retries.
Waterfall Deduction Order & Pre-Action UX Clarity
Inside MongoDB transactions guarded by an `$expr` available-credit constraint (`reservedCredits + credits <= creditBalance + addonCreditBalance`), deductions consume expiring subscription `creditBalance` first and persistent `addonCreditBalance` second—backed on the frontend by pre-action credit labels and contextual HTTP `402/403` limit modals.
Vishank's Contributions
- Frontend subscription, plan comparison, billing history, and `UsageLimitModal` / pre-action credit cost clarity UI updates (`frontend-service`).
- Admin support credit endpoints commit (`billing-engine` `adminController.ts`, `creditsController.ts`) and authoring `PRD-Pricing-and-Billing-Plan_v3.md`.
Team-Owned Core Engine (Nilesh Kanawade)
- Core `billing-engine` architecture—Stripe Checkout session orchestration, raw-body webhook signature verification, multi-layer idempotency guards, atomic `$expr` dual-pool ledgering, and v3 feature quotas—was built by the CTO; documented here from read-only repository inspection without overclaiming authorship.
Subscription Allowances Paired With Persistent Add-On Credits
AI company research and enrichment carry variable per-call compute costs. MagiQ AI separates expiring monthly/annual subscription credits (`creditBalance`) from purchased top-up packs (`addonCreditBalance`), while tracking reserved credits on active in-flight holds (`reservedCredits`).
Webhook Verification & Multi-Layer Idempotency Guard
To prevent duplicate credit grants during webhook retries, `/webhooks/stripe` mounts raw JSON buffering before body parsing for byte-exact `stripe.webhooks.constructEvent` validation, while payment webhooks verify HMAC-SHA256 signatures with `crypto.timingSafeEqual`.
Processed webhook records, deterministic `boost:<sessionId>` keys, atomic `$setOnInsert` upserts, and unique `CreditHold.idempotencyKey` constraints enforce safe replay handling across retries.
Waterfall Deduction Order & Pre-Action UX Clarity
Inside MongoDB transactions guarded by an `$expr` available-credit constraint (`reservedCredits + credits <= creditBalance + addonCreditBalance`), deductions consume expiring subscription `creditBalance` first and persistent `addonCreditBalance` second—backed on the frontend by pre-action credit labels and contextual HTTP `402/403` limit modals.