Skip to Content
DevelopersBuildingPlans and billing

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:

  1. createPlanBill finds the plan product variant by SKU plan-<plan>-<interval> (for example plan-growth-month).
  2. It then creates a draft, converts it, and creates a payment attempt with the sales store’s first enabled gateway.
  3. The bill’s URL is the signed thank-you page on the sales storefront, which shows Pay now for an unstarted attempt.
  4. When the order is paid, the outbox relay (order.paid on the sales store) calls markPlanBillPaid. That makes the plan active, starts the period at max(start, paid time), and sets ai_settings.plan. A refund or cancel calls cancelPlanBill.

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_plan is 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 marked plan_payment.prorated (migration 0246, with credit_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) and feeOrderRows in billing/usage-fee.ts. Only orders inside a paid plan_payment period 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') in billing/usage-fee-service.ts (not draft, not cancelled, not test, not refunded). feeEstimate is this month so far for Plan & billing.
  • Bills: usage_bill (migration 0179), one row per store and month. runUsageBilling runs 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 is carried; otherwise a sales order with one usage-fee-<plan> line per rate plus usage-fee-carried, each with a computed unitPricePaise.
  • Paid / cancelled: the relay calls markUsageBillPaid / cancelUsageBill (usage_bill_store). An unpaid fee bill never changes store_plan.status: createPlanBill adds every due fee bill to the next plan bill as a usage-fee-carried line, marks them merged and 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.

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.list attaches it as billing, for the Installed apps list and the install flow. The App Store website prices apps from the same APP_PRICING (@commerce/core/app-catalogue).
  • apps.install refuses a buy app with FORBIDDEN and data.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 → markAppBillPaid makes 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 for GRACE_DAYS, then lapses.
  • Stop renewing: marks the purchase cancelled. Access lasts until periodEnd.

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 from PLANS.
  • 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 from APP_PRICING.
  • AI credits: a product with SKU ai-credits-1000 at ₹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.

  • startAutopay creates a Razorpay plan (the plan price + GST) and a subscription, and stores an autopay_mandate row (migration 0134). The owner approves the UPI mandate on the subscription’s short_url.
  • subscription.charged → handleAutopayWebhook bills the period (createPlanBill), then captures that sales order’s payment with applyPaymentStatus. order.paid then 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.

Was this helpful?
Last updated on