Skip to content

feat(billing): show the trial's AI credit without a percentage, and let admins start the plan now - #1818

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/trial-ai-credit
Oct 30, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/trial-ai-credit

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

From onboarding testing: one article used about 5.7% of the $2 trial AI budget. A trial user then reads that as "about 17 articles on this plan", though on Growth the same article is about 0.5%. When the trial budget ran out, the trial went on but the AI couldn't be used. The product owner approved this in a local preview.

Stacked on #1804 (AI usage restarts at 0 on purchase). The diff against main includes #1804 until it merges.

No percentage during the trial

  • Header meter: reads AI · Trial credit, then Almost used from 80% (amber) and Used up (red), with no bar or %. Its popover says the trial credit state, a short note, and "the trial ends {date}"; feature and page shares are hidden.
  • Other places: Billing's AI usage card, Chat's "Your plan" card and the Improve hint follow the same rule.
  • From 80%: a band and a toast: "Your trial AI credit is almost used. Starting your plan now gives its full monthly AI usage."
  • At 100%: the band turns red. It keeps Use your own key for Agent Chat.

Start my plan now

  • Where: in the 80% and 100% bands, the meter's popover, and Billing's AI card (from 80%).
  • Who: admins only. Other roles read "To start the plan now, ask an admin of your workspace."
  • Confirmation:
    • It shows the plan ("Growth, monthly"), what's charged today, and Free trial: Ends now. There's no payment entry, since the card is on file.
    • A monthly plan gets the first-month offer, as on Billing (FIRST_MONTH_CODE): half today, "then $X a month".
    • If Stripe refuses the code, the full price is shown and asked again; it's never charged silently.
  • On confirm: PATCH /api/stripe/subscription { newPlanId, interval, buyNow, promoCode? }, then router.refresh, the AI usage is read again, and PLAN_STARTED_EVENT refreshes Billing's plan and AI cards. With fix(billing): start the AI usage from 0 as soon as a trial is bought #1804, usage starts from 0.
  • A declined card keeps the trial running and offers Update card.

Help: new help/settings/trial-ai-credit.md (approved: false).

Related issue

None. Product owner's onboarding feedback.

Type of change

  • feat — New feature

How to test

  1. On a trialing workspace (cloud), use at least 80% of the trial budget:
    • the header reads Almost used;
    • the amber band and Billing's card offer Start my plan now;
    • no percentage shows anywhere.
  2. At 100%: Used up and the red band.
  3. Start my plan now (test Stripe): the confirmation shows plan, today's charge and Ends now. Confirm, and the plan is active with usage at 0.
  4. As a non-admin: the "ask an admin" line instead of the button.
  5. Tests: ai-usage, the billing card and section, plan-switcher, prompt-improve, chat-access and Help validation (11 files, 118 tests).

Local preview, light and dark, at 80% and 100%: shown to and approved by the product owner. Locally the confirmation can't read the plan without a Stripe key; on staging it shows the charge.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: eslint/prettier and the related tests pass (typecheck left to CI)
  • Tests added
  • Help article updated
  • Test-Stripe run of Start my plan now: the maintainer's

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 30, 2026 00:19
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]>
…tart 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 (ansvisor#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]>
gkhngyk
gkhngyk previously approved these changes Oct 30, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed what this PR adds on top of #1804 (0b10d6e4..979e3e5d). Approving.

What I checked

  • Buying stays on the server's checks. Start my plan now reuses PATCH /api/stripe/subscription with buyNow, the same path as Billing's Buy now. The route requires an admin of the caller's own organization (403 admin_only otherwise), takes the organization from the session rather than the body, checks the plan and period with planRequestSchema + planChange (Starter/Growth only, buy-now only while trialing, the promo code only on a monthly plan), and uses payment_behavior: 'error_if_incomplete', so a declined card or one that needs 3DS changes nothing and the trial carries on. Hiding the button for non-admins is only the UI side of that.
  • Double clicks. The confirm button is disabled while the request runs, and the dialog can't close mid-request. A second request after the first one went through is refused (Only a free trial can be bought now), and reopening the dialog reads the subscription again and shows "Your plan has already started."
  • Dialog vs charge. The amount shown uses the same periodPrice / firstMonthPrice / FIRST_MONTH_CODE as Billing, sends the trial's own plan and period, and when the code is gone it shows the full price and asks again instead of charging it.
  • No percentage in the trial: the header meter (label, aria-label, title, popover; the page and feature shares are hidden), the banner and toast (banner.warningTrial has no {percent}), Billing's AI usage card, Chat's Your plan card and the Improve hint's title. Paid plans look the same as before.
  • Other members: "ask an admin to start the plan" in the band, popover and card. No Start button and no dialog.
  • Billing refresh: PLAN_STARTED_EVENT updates the plan card and reloads the AI usage card. router.refresh() and refreshAiUsage() take care of the header.
  • No migrations, no new Supabase queries or embeds, no new routes. The read-only gate coverage passes.

Tests (PR head, which already contains current main)

  • plan-row.test.ts (client import walk), ai-usage components, the AI usage card, billing section, plan switcher, the Stripe subscription and webhook routes, chat-access, prompt-improve, lib/billing, subscription-guard coverage: 27 files, 256 passed.
  • Help validation: 5 passed. Web tsc --noEmit is clean, and eslint and prettier are clean on the touched files.

Merge status: clean on main. It conflicts only with #1846 (billing-section.tsx and its test, adjacent imports/JSX, easy to resolve). #1806, #1823 and #1845 merge cleanly with it.

Non-blocking, for later

  1. Cancelled trial + #1823. If a trial set to cancel at period end is started now, Stripe clears cancel_at_period_end. The buy-now path's optimistic organization update doesn't, though. Once #1823 adds its organizations.cancel_at_period_end mirror, the "plan ends" band would stay until the webhook arrives. Whichever of the two merges second should add cancel_at_period_end: false to the buy-now update.
  2. A code Stripe refuses on the update itself (for example a per-customer or first-time restriction on the coupon) currently comes back as payment_failed ("Update card"), because only a missing or inactive code returns invalid_promo_code. This comes from main's route, not from this PR.
  3. Two tabs at once. An idempotency key on the buy-now update (for example the subscription id + its trial_end) would make two simultaneous submits from separate tabs a single Stripe call. Also main's route.
  4. Popover button timing. The popover shows Start my plan now at any level, but the bands and the card show it only from 80%. The help article says "once the credit is almost or all used". Either is fine, as long as the two say the same.
  5. #1846 trial line. Its "Trials can't buy packs. Choose a plan" could point to Start my plan now once both are in.

gkhngyk and others added 2 commits October 30, 2026 20:38
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.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging in order with the other approved PRs. One more commit: the brands page test mocks @/config/plans partially, because the page now reaches Start my plan now, which reads FIRST_MONTH_OFFER when it loads (the web CI failed on it).

@gkhngyk
gkhngyk merged commit fbc98f0 into ansvisor:main Oct 30, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants