6 min read
Plans and billing
A store’s plan lives in store_plan (one row per store). Its bills live in plan_payment
(migration 0123). Prices, limits and the trial are in @commerce/core/billing (PLANS,
TRIAL_DAYS = 14, RENEW_AHEAD_DAYS = 3, GRACE_DAYS = 7). The decisions behind them are in
memory-bank/pricing.md.
Bills are orders
Each bill is an order on the platform’s sales store (PLATFORM_SALES_STORE_ID), so the
existing checkout, payment gateways, thank-you page and GST-invoice app do the work:
createPlanBillfinds the plan product variant by SKUplan-<plan>-<interval>(for exampleplan-growth-month).- It then creates a draft, converts it, and creates a payment attempt with the sales store’s first enabled gateway.
- The bill’s URL is the signed thank-you page on the sales storefront, which shows Pay now for an unstarted attempt.
- When the order is paid, the outbox relay (
order.paidon the sales store) callsmarkPlanBillPaid. That makes the planactive, starts the period at max(start, paid time), and setsai_settings.plan. A refund or cancel callscancelPlanBill.
States
trialing → active (paid) → due (renewal unpaid past the end) → paused (after the grace
days). A downgrade on an active plan sets next_plan and takes effect at the period end.
The worker’s hourly {billing}-sweep (runBillingSweep, stores_with_plan_due):
- bills 3 days ahead;
- marks plans due;
- pauses them after grace.
It is off while PLATFORM_SALES_STORE_ID is unset.
Changing plans (proration)
quotePlanChange (billing/proration.ts, pure) decides what picking a plan costs; the admin’s
billing.quote shows it (“Move to Pro?”) and billing.choose bills the same quote
(choosePlan in billing/plan-change.ts), so Pay now is the sales order’s total.
- Downgrade with paid time left (smaller plan, or the same plan yearly → monthly): nothing
charged or refunded;
next_planis set and applies at the period end. - Trialing, due, paused, or no paid time left: the new plan’s full price for one interval from now, at the plan variant’s price (as before).
- Upgrade, same interval: the new plan for the days left (today counts as unused) minus the
old plan for the same days, each
round(price × daysLeft / periodDays); GST is 18% of the net, rounded to the paisa; never below 0. The bill covers[now, period end)and is markedplan_payment.prorated(migration0246, withcredit_paise); paying it keeps the period end, so renewals stay on the same date. Paid only after that end, it gives its days from the payment. - Monthly → yearly: a full year from now at the yearly price, less the month’s unused days.
- Yearly → a bigger monthly plan: the rest of the year at twelve times the monthly price, pro rata, less the unused days; monthly renewals start when the year would have ended.
- A prorated or credited bill’s plan line is placed with
unitPricePaise= net + GST (sales-store prices hold their GST, which the order extracts back to exactly the quoted GST). Unpaid fee bills still ride along as their own line. - Choosing the plan and interval the store is already on, with paid time left, is refused
(
AlreadyOnPlan).
The Plan & billing page’s Next bill is projectNextBill (billing/next-bill.ts): the renewing
plan, this month’s per-order fees so far, renewing bought apps the plan does not include, and 18%
GST. The usage nudge (upgradeNudge) and Change plan’s “at your pace” lines project this month’s
orders at today’s daily rate and use monthlyCost; the nudge shows only when a bigger plan is at
least ₹100 a month cheaper.
Per-order fees
Each plan includes PLANS[plan].orders orders a month; every order after that carries
feeBasisPoints of its total (100 / 75 / 50 / 0, Scale charges none). The rules are in
docs/superpowers/specs/2026-10-28-pricing-revamp-design.md.
- Pure maths:
monthFee(orders, periods)andfeeOrderRowsinbilling/usage-fee.ts. Only orders inside a paidplan_paymentperiod count, so trial, grace and paused days carry no fee, and each order’s rate is the period covering it (after an upgrade, the later one). - Inputs:
loadMonthFeeInputs(tx, storeId, 'YYYY-MM')inbilling/usage-fee-service.ts(not draft, not cancelled, not test, notrefunded).feeEstimateis this month so far for Plan & billing. - Bills:
usage_bill(migration0179), one row per store and month.runUsageBillingruns in the hourly sweep and bills last month once (stores_with_paid_period). Under ₹100 (USAGE_BILL_MIN_PAISE, with what was carried in) the row iscarried; otherwise a sales order with oneusage-fee-<plan>line per rate plususage-fee-carried, each with a computedunitPricePaise. - Paid / cancelled: the relay calls
markUsageBillPaid/cancelUsageBill(usage_bill_store). An unpaid fee bill never changesstore_plan.status:createPlanBilladds everyduefee bill to the next plan bill as ausage-fee-carriedline, marks themmergedand cancels their own orders; paying the plan bill pays them. - AutoPay charges a fixed amount, so fee bills stay pay links.
runFairUseCheck (same sweep) compares the two finished months’ marketing_session counts with
visitsPerMonth; two months over sets store_plan.fair_use_notice_month and emails the owner
and support. Nothing is blocked. The beacon starts no session for a bot user agent.
The pricing page’s calculator uses monthlyCost / bestPlan (billing/cost-estimate.ts).
Prices outside India are stored in billing/regional-prices.ts and stay off
(REGIONAL_PRICING_LIVE = false) until a non-Indian gateway exists.
Paid apps
APP_PRICING (@commerce/core/billing) lists each paid first-party app, the plan that includes
it, and its monthly price. Apps not listed are free.
storeAppAccess gives each app one of four states: free, included, bought (until a date) or
buy.
apps.listattaches it asbilling, for the Installed apps list and the install flow. The App Store website prices apps from the sameAPP_PRICING(@commerce/core/app-catalogue).apps.installrefuses abuyapp withFORBIDDENanddata.reason = 'app_not_in_plan'.
A purchase is an app_purchase row (migration 0132) whose bill is a sales-store order with SKU
app-<pluginId>-month, placed by the same placeSalesOrder as plan bills.
- Paid:
order.paid→markAppBillPaidmakes the month active and installs the app. - Renewals: the same hourly sweep bills 3 days ahead (
runAppRenewals,stores_with_app_renewals). An unpaid renewal keeps the app forGRACE_DAYS, then lapses. - Stop renewing: marks the purchase
cancelled. Access lasts untilperiodEnd.
Moving to a smaller plan doesn’t uninstall anything: apps already installed keep working.
AI credits
Every AI call is metered in credits: 1 credit is ₹0.10 of model cost. creditMillisFor(tier, in, out) in @commerce/ai prices tokens with TIER_PRICES_INR, and recordAiUsage adds
them to ai_usage.credit_millis. checkQuota refuses a call once the month’s credits reach
PLAN_QUOTAS[plan].credits plus the store’s bought credits (ai_settings.extra_credit_millis).
Whatever a call spends past the allowance comes out of the bought credits (spendExtraCredits).
A top-up (AI_CREDIT_PACK: 1,000 credits for ₹199, SKU ai-credits-1000) is an
ai_credit_topup row (migration 0133). On order.paid, markAiTopupPaid adds the credits.
Update TIER_PRICES_INR whenever the model or its price changes.
Unlimited stores
Koomerce’s own stores (Waimers) run on an unlimited plan: store_plan.unlimited, set only with
bun run platform:unlimited <slug> (--off to undo; setStoreUnlimited in
core/billing/unlimited.ts). On the server, run it in the worker container from
/repo/apps/worker: bun ops/platform-unlimited.ts <slug> (see
Deployment). The store
keeps Scale’s features with no staff or storage limit, no
per-order fee, no renewals, due or pause, and no fair-use notice; choosePlan refuses with
UnlimitedPlan. AI moves to the internal tier, a 50,000-credit monthly safety cap.
Bill emails and GSTIN
- The billing sweep emails the store’s contact email (platform template
plan_bill,sendPlanBillEmail) when it bills a plan or app renewal, and once when a plan goes overdue. - Plan, app and credit orders carry the buying store’s GSTIN (
buyer_gstin, validated) and name, so the GST invoice is a B2B one.
Setting up the sales store
- Create products with SKUs
plan-starter|growth|pro-month|year: active, untracked inventory, GST-inclusive prices fromPLANS. - For paid apps, one product per app with SKU
app-<pluginId>-month(reviews,back-in-stock,forms,affiliates,emi-pay-later,google-search-console), priced fromAPP_PRICING. - AI credits: a product with SKU
ai-credits-1000at ₹199 + GST. - Enable one payment gateway on the sales store.
- Run migrations
0121–0134.
UPI AutoPay
Set PLATFORM_RAZORPAY_KEY_ID, _KEY_SECRET and _WEBHOOK_SECRET (the platform’s own Razorpay
account, with Subscriptions enabled) to turn it on. The webhook is
<ADMIN_URL>/api/billing/autopay-webhook, for subscription.* events.
startAutopaycreates a Razorpay plan (the plan price + GST) and a subscription, and stores anautopay_mandaterow (migration0134). The owner approves the UPI mandate on the subscription’sshort_url.subscription.charged→handleAutopayWebhookbills the period (createPlanBill), then captures that sales order’s payment withapplyPaymentStatus.order.paidthen runs the same path as a pay-link bill: plan active, GST invoice issued. A repeated webhook for the same payment changes nothing (last_payment_id).- While a mandate is
active, the sweep sends no pay-link renewals. A halted, cancelled or turned-off mandate goes back to pay links.
Not live-tested yet: it needs a Razorpay account with Subscriptions enabled.
Paused shows a banner only and blocks nothing.