Repository navigation
fix(billing): start the AI usage from 0 as soon as a trial is bought - #1804
Conversation
Buying a trial now (trial_end: now) starts the first paid period that day, and the monthly quotas, AI usage among them, count from that period (quotaPeriodStart, from subscription_ends_at). Only the webhook wrote the new period end, so until it landed the meter read the whole trial's usage against the plan's full budget. - PATCH /api/stripe/subscription writes subscription_ends_at with the plan and status, as the webhook does. - Billing reads the AI usage again after a plan change: the header meter (and its banners) and the "This period's AI usage" card. - Tests: the buy-now write, the webhook's trialing -> active update, the quota window starting at the purchase, and both reads again. - FAQ: buying during the trial starts the AI usage again from 0%. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
gkhngyk
left a comment
There was a problem hiding this comment.
Small, well-aimed fix. The meter's window already followed subscription_ends_at; the gap was only that PATCH didn't mirror it, so the trial's usage stayed on screen until the webhook came (and forever locally). The PR now writes it the way the webhook does, and both places that show usage read it again. Approving as is.
What I checked
- Period math, server side.
quotaPeriodStartreadssubscription_ends_atfrom the database on every call (no cache), and the Chat AI budget (aiBudgetOf) and every other monthly quota use the same function. So once the row has the new end, the next read anywhere starts from the purchase. No other code in web computes the window. - Double reset / idempotence. Nothing is counted or zeroed. The window is derived from the period end, so
PATCHand the webhook writing the same value, in either order, give the same result. A replayed webhook for the same change writes the same value again. - Plan switched mid-period. For a same-interval upgrade or downgrade, Stripe keeps the billing anchor, so
PATCHwrites the unchanged end: no reset. A monthly ↔ annual switch starts a new period in Stripe, so the window restarts there. That's what the webhook already did, and now it happens at once. A change during the trial (no Buy now) keeps the trial's end: no reset. - Annual Buy now. The end is a year away, and the monthly walk back lands on the purchase day.
- Client.
useAiUsagefalls back to a no-op context outside the provider, and Billing is rendered insideAiUsageProvider, so nothing breaks when it's used elsewhere. The 80% toast's mark includesperiodStart, so a new period can warn again later.AiUsageCard key={planChanges}is a clean way to re-read. - Writes go through the service role, same as the webhook, so the organization column guard is unaffected.
Tests
- On the PR merged with current main: the four Stripe route tests,
plan-switcher,billing-section,ai-usage-cardandlib/guards/plan-row.test.ts(the client-import guard) give 9 files and 108 tests, all passing. Serverquota-period,plan-guard*androutes/billinggive 7 files and 88 tests, all passing.tsc, eslint and prettier on the changed web files are clean. - With main's
route.ts,plan-switcher.tsxandbilling-section.tsxunder the PR's tests, the three new assertions fail. So they do guard the fix.
Non-blocking notes
- The month-end edge is wider for annual plans.
anchoredPeriodStartwalks back one month at a time from the shifted date, so the day clamps add up. For an annual purchase on the 29th–31st of any month, the walk crosses February and the window opens on the 28th, 1–3 days before the purchase. For example, an annual plan bought on Oct 31 opens on Oct 28. Thecurrent_period_startfollow-up you mention fixes both cases. It may be worth saying "monthly: before a shorter month; annual: any 29th–31st" in that PR. - Ordering with other billing PRs. #1818 is stacked on this one. #1823 and #1806 merge cleanly with it. One thing for #1823: Buy now also sends
cancel_at_period_end: falseto Stripe, so once #1823's column lands, this optimistic update could writecancel_at_period_end: falsetoo. Otherwise a cancelled trial that's then bought keeps the "plan ends" band until the webhook comes. - Nit: if a header read is already running when
refreshAiUsage()fires, the provider drops the refresh (reading.current), so the meter can show the old figure until the next stale read. That's rare and not worth changing here.
No migration. Merges cleanly on current main.
gkhngyk
left a comment
There was a problem hiding this comment.
Merging in order with the other approved PRs. This head merges current main; its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build.
…et admins start the plan now (#1818) * fix(billing): start the AI usage from 0 as soon as a trial is bought Buying a trial now (trial_end: now) starts the first paid period that day, and the monthly quotas, AI usage among them, count from that period (quotaPeriodStart, from subscription_ends_at). Only the webhook wrote the new period end, so until it landed the meter read the whole trial's usage against the plan's full budget. - PATCH /api/stripe/subscription writes subscription_ends_at with the plan and status, as the webhook does. - Billing reads the AI usage again after a plan change: the header meter (and its banners) and the "This period's AI usage" card. - Tests: the buy-now write, the webhook's trialing -> active update, the quota window starting at the purchase, and both reads again. - FAQ: buying during the trial starts the AI usage again from 0%. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(billing): show the free trial's AI credit without a share, and start the plan from it In the free trial, one Content Editor page reads as a large share of the small trial AI budget, so the trial seemed to allow only a few pages (decided 2026-10-29). The trial's AI credit now shows no percentage, and an admin can start the plan when it runs low. - The header meter reads "Trial credit", then "Almost used" from 80% and "Used up". Its popover, Billing's AI usage card and Chat's plan card show the same, with when the trial ends. They show no share, no bar and no per-feature shares. Improve's tooltip names none either. - From 80% the band and the toast say "Your trial AI credit is almost used". - Start my plan now opens a dialog that names the plan, what is charged today and that the free trial ends now; nothing to type, the card is on file. It sits in the band, the popover and the Billing card, and does what Billing's Buy now does (PATCH /api/stripe/subscription, buyNow, the first-month code on a monthly plan). A turned-down code shows the full price to confirm again; a declined card keeps the trial and offers Update card. The usage starts from 0 (#1804). Billing reads its plan and AI usage cards again (PLAN_STARTED_EVENT). - In the popover the dialog lives outside it, since the popover closes as the dialog opens. - Admins only, as the route checks. Other members read "To start the plan now, ask an admin of your workspace." - Help: settings/trial-ai-credit. Co-Authored-By: Claude Opus 5.5 <[email protected]> * style(billing): format the AI usage card's trial tests Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(brands): mock only the plan helpers the page uses The page now reaches Start my plan now, which reads FIRST_MONTH_OFFER when it loads, so the plans mock keeps the module's other exports. --------- Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Gökhan Geyik <[email protected]> Co-authored-by: Gökhan Geyik <[email protected]>
Summary
Reported while testing: buying during the free trial should start AI usage from 0.
How the period works today:
quotaPeriodStart(server/src/lib/plan-guard.js), one month back fromorganizations.subscription_ends_at, which is Stripe's period end.trial_end: 'now') restarts Stripe's period at once, and the webhook writes the new end. So the server-side reset already worked once the webhook arrived.The gaps fixed here:
PATCH /api/stripe/subscriptionwrote only the plan and status, so until the webhook came the meter set the trial's usage against the full plan budget. Locally the webhook never comes, so it stayed that way. It now writessubscription_ends_attoo.plan-switcher.tsx), and neither was Billing's AI usage card (billing-section.tsx). Both now re-read, so the old percentage doesn't stay on screen.docs/faq.mdxgets one line: buying during the trial starts AI usage from 0.No migration.
Known edge, not fixed here: a purchase on the 29th–31st, before a shorter month, opens the window 1–3 days before the purchase. This comes from the month-end arithmetic (#500). The exact fix stores Stripe's
current_period_start(a column and a migration). I'll propose it as a separate PR.Related issue
None. Product owner's request from onboarding testing.
Type of change
fix— Bug fixHow to test
plan-switcherandbilling-section(4 files, 54 tests);quota-period.Checklist
quota-periodtest passes (full suite left to CI)🤖 Generated with Claude Code