Add Stripe customer portal for Pro billing management

Merged
LA
5mo ago
+771-36

Customers need self-serve cancel/manage before the pricing flag flips on. GET /api/billing/portal creates a Stripe billing portal session for the current Stack user (customer id strictly from the stripe_customers row for the resolved stackUserId, anonymous purchaser fallback included) and redirects; failures land on safe /pricing banners with Sentry capture. Pro users get Manage billing links on the success page, /app-pricing, the localized pricing page, and the native Settings Account card via the shared presenter, with the portal URL following the same CMUX_BILLING_WWW_ORIGIN-then-app-web-origin resolution as checkout.

/fable loop: two judge rounds (origin-override parity + anonymous-fallback test coverage), 377 web tests green in sorted order, typecheck clean, en+ja localization audited.

🤖 Generated with Claude Code


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


[!NOTE]
Medium Risk
Touches billing auth, Stripe customer lookup, and entitlement-driven UI across web and native; failures are mostly redirects, but wrong customer mapping or plan detection would affect who can manage subscriptions.

Overview
Adds self-serve Stripe billing management via a new GET /api/billing/portal route that resolves the Stack user (including anonymous purchaser fallback), loads stripe_customers for that user only, creates a portal session, and redirects—or sends users to /pricing?billing=… on failure with Sentry context.

Web pricing is now plan-aware: localized /pricing and /app-pricing use resolveProPlanStatus to show a current-plan badge, disable upgrade CTAs for Pro, and link Manage billing to the portal; post-checkout /billing/success gets the same link. New currentPlan / manageBilling strings (en + ja).

macOS: AccountFlow gains isProActive, refreshBillingPlan() (api/billing/plan), and openBillingPortal(); ProUpgradeCard refreshes on identity change and switches copy/button between Upgrade and Manage billing. AuthEnvironment.billingPortalURL mirrors checkout origin rules (CMUX_BILLING_WWW_ORIGIN); ProUpgradePresenter.presentBillingPortal() opens it in the default browser.

Tests cover the portal route (signed-in, anonymous fallback, misconfig), pricing pages, and portal URL resolution; billing skill doc updated.

Reviewed by Cursor Bugbot for commit cb8d10210a54eddcd671bbb08f27fec339ae3007. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Adds a self-serve Stripe customer portal so Pro users can manage billing or cancel. Includes a new API route, “Manage billing” links across web and macOS, and safe fallbacks with correct origin resolution.

  • New Features

    • Added GET /api/billing/portal: resolves the current Stack user (with anonymous purchaser fallback), looks up their stripe_customers by stackUserId, creates a Stripe Billing Portal session with return to /pricing, and 302s; unavailable/error paths redirect to /pricing?billing=… with error capture.
    • Web: For active Pro, show “Current plan” and a “Manage billing” link on /billing/success, /app-pricing, and localized /[locale]/pricing (server-rendered via resolveProPlanStatus).
    • macOS: AccountFlow adds refreshBillingPlan(), openBillingPortal(), and isProActive; ProUpgradeCard auto-refreshes plan state and switches Upgrade vs Manage; ProUpgradePresenter.presentBillingPortal() opens AuthEnvironment.billingPortalURL.
    • Origin parity: AuthEnvironment resolves the portal URL with CMUX_BILLING_WWW_ORIGIN override (else app web origin), matching checkout; tests cover origin override and anonymous fallback. Localized strings updated (en/ja).
  • Migration

    • Enable Stripe Billing Portal and set STRIPE_SECRET_KEY.
    • Optionally set CMUX_BILLING_WWW_ORIGIN if the billing host differs; otherwise the app’s web origin is used.

Summary by CodeRabbit

  • New Features
    • Added “Manage billing” for active Pro users, with a billing portal link on pricing and the billing success screen.
    • Pricing now reflects the user’s current plan, showing a current-plan state for active Pro accounts and updating CTAs accordingly.
    • macOS settings now detects active Pro status and refreshes billing state automatically.
    • Added new English/Japanese UI copy for current-plan and manage-billing labels.
  • Bug Fixes
    • Billing links now route to the correct destination (portal vs Pro upgrade) based on active subscription status.