-
Notifications
You must be signed in to change notification settings - Fork 177
fix(billing): refresh plan after returning from checkout #1215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b121d83
fix(billing): refresh plan after returning from checkout
RhysSullivan 17a52e1
test(e2e): consume @executor-js/emulate 0.9.0 for the trial-checkout …
RhysSullivan e5896ce
Merge remote-tracking branch 'origin/main' into autumn-trial-stale
RhysSullivan e47df14
fix(billing): show purchased plan as activating on checkout return
RhysSullivan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Cloud-only (billing, browser): completing the Team free-trial checkout should | ||
| // leave the billing page showing the new plan WITHOUT a manual reload. | ||
| // | ||
| // Repro of a reported bug: a user starts the free trial, completes Stripe | ||
| // checkout, and is redirected back to the plans page, which STILL shows the | ||
| // upgrade/"Start free trial" call to action. A manual reload then shows the | ||
| // active trial. The cause is client-side: autumn-js fetches the customer once | ||
| // on load (staleTime 60s, refetchOnWindowFocus off) and the redirect back from | ||
| // Stripe lands before Autumn has processed Stripe's webhook, so that single | ||
| // fetch sees the old plan and the page never refetches on its own. | ||
| // | ||
| // The emulator models this faithfully: completing the hosted checkout redirects | ||
| // back immediately but does NOT activate the subscription; the activation lands | ||
| // only when the webhook settles (autumn.settleCheckout). The reload control | ||
| // proves the backend is consistent, isolating the failure to the stale client. | ||
| import { expect } from "@effect/vitest"; | ||
| import { Effect } from "effect"; | ||
|
|
||
| import { scenario } from "../src/scenario"; | ||
| import { Autumn, Billing, Browser, Target } from "../src/services"; | ||
|
|
||
| scenario( | ||
| "Billing · completing the trial checkout shows the new plan without a reload", | ||
| { timeout: 120_000 }, | ||
| Effect.gen(function* () { | ||
| // Gates: billing enforced here AND the Autumn emulator is observable (so we | ||
| // can land the checkout webhook). Yield before any work so a target missing | ||
| // either capability skips cleanly rather than failing. | ||
| yield* Billing; | ||
| const autumn = yield* Autumn; | ||
| const target = yield* Target; | ||
| const browser = yield* Browser; | ||
|
|
||
| const identity = yield* target.newIdentity(); | ||
|
|
||
| yield* browser.session(identity, async ({ page, step }) => { | ||
| const teamCard = page | ||
| .getByText("Team", { exact: true }) | ||
| .locator("xpath=ancestor::div[contains(@class,'rounded-xl')][1]"); | ||
| const startTrial = teamCard.getByRole("button", { name: "Start free trial" }); | ||
|
|
||
| await step("A fresh org is offered the Team free trial", async () => { | ||
| // Billing requests are org-scoped via the URL slug header, so reach the | ||
| // plans page through the org-scoped URL (a bare /billing/plans would fire | ||
| // the first fetch before the slug resolves and 401). Land on "/" to | ||
| // canonicalize, then open the slug-scoped plans page. | ||
| await page.goto("/", { waitUntil: "networkidle" }); | ||
| const slug = new URL(page.url()).pathname.split("/").filter(Boolean)[0]; | ||
| await page.goto(`/${slug}/billing/plans`, { waitUntil: "networkidle" }); | ||
| await page.getByRole("heading", { name: "Choose a plan" }).waitFor(); | ||
| await startTrial.waitFor(); | ||
| }); | ||
|
|
||
| let sessionId = ""; | ||
| await step("Start the trial and land on the hosted checkout", async () => { | ||
| await startTrial.click(); | ||
| // attach() redirects the whole page to the checkout URL. | ||
| await page.waitForURL(/\/checkout\//, { timeout: 30_000 }); | ||
| sessionId = new URL(page.url()).pathname.split("/").filter(Boolean).pop() ?? ""; | ||
| expect(sessionId, "captured the checkout session id").toMatch(/^cs_/); | ||
| await page.locator("button.checkout-pay-btn").waitFor(); | ||
| }); | ||
|
|
||
| await step("Complete checkout and return to the plans page", async () => { | ||
| await page.locator("button.checkout-pay-btn").click(); | ||
| // Checkout completes and redirects back to the success_url (the plans | ||
| // page). The webhook has NOT landed yet, so the trial is still offered: | ||
| // exactly the window the bug lives in. | ||
| await page.waitForURL(/billing\/plans/, { timeout: 30_000 }); | ||
| await startTrial.waitFor(); | ||
| }); | ||
|
|
||
| // The Stripe webhook reaches Autumn: the customer is now on the Team trial. | ||
| // From here on the billing backend is authoritative-consistent. | ||
| await Effect.runPromise(autumn.settleCheckout(sessionId)); | ||
|
|
||
| // The page the user was returned to never reloads. If the UI refetched | ||
| // after returning from checkout it would drop the trial CTA within a few | ||
| // seconds; today it does not, so this stays visible. | ||
| const reflectedWithoutReload = await startTrial | ||
| .waitFor({ state: "hidden", timeout: 8_000 }) | ||
| .then(() => true) | ||
| .catch(() => false); | ||
|
|
||
| // Control: a manual reload surfaces the active trial, proving the backend | ||
| // was consistent all along and the only thing missing was a client refetch. | ||
| await step("A manual reload shows the active trial", async () => { | ||
| await page.reload({ waitUntil: "networkidle" }); | ||
| await teamCard.getByText("Current plan").waitFor({ timeout: 15_000 }); | ||
| }); | ||
|
|
||
| expect( | ||
| reflectedWithoutReload, | ||
| "the plans page reflects the completed checkout without a manual reload", | ||
| ).toBe(true); | ||
| }); | ||
| }), | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // The plan catalog the Autumn emulator advertises, derived from the deploy-time | ||
| // source of truth (repo-root `autumn.config.ts`). In production Autumn learns | ||
| // these plans via `atmn` sync; the emulator has no such sync, so the e2e boot | ||
| // seeds them. Keeping this derived from autumn.config.ts means plans, prices, | ||
| // and the Team free trial can never drift from what the app actually ships. | ||
| import { free, team, enterprise } from "../../autumn.config"; | ||
|
|
||
| type AtmnPlan = { | ||
| id: string; | ||
| name: string; | ||
| addOn?: boolean; | ||
| autoEnable?: boolean; | ||
| price?: { amount: number; interval: string }; | ||
| freeTrial?: { durationLength: number; durationType: string; cardRequired?: boolean }; | ||
| items?: ReadonlyArray<{ featureId: string; included?: number; unlimited?: boolean }>; | ||
| }; | ||
|
|
||
| export interface AutumnSeedPlan { | ||
| id: string; | ||
| name: string; | ||
| add_on: boolean; | ||
| auto_enable: boolean; | ||
| price: { amount: number; interval: string } | null; | ||
| free_trial: { duration_length: number; duration_type: string; card_required: boolean } | null; | ||
| items: Array<{ feature_id: string; included?: number; unlimited?: boolean }>; | ||
| } | ||
|
|
||
| const toSeed = (plan: AtmnPlan): AutumnSeedPlan => ({ | ||
| id: plan.id, | ||
| name: plan.name, | ||
| add_on: plan.addOn ?? false, | ||
| auto_enable: plan.autoEnable ?? false, | ||
| price: plan.price ? { amount: plan.price.amount, interval: plan.price.interval } : null, | ||
| free_trial: plan.freeTrial | ||
| ? { | ||
| duration_length: plan.freeTrial.durationLength, | ||
| duration_type: plan.freeTrial.durationType, | ||
| card_required: plan.freeTrial.cardRequired ?? false, | ||
| } | ||
| : null, | ||
| items: (plan.items ?? []).map((it) => ({ | ||
| feature_id: it.featureId, | ||
| included: it.included, | ||
| unlimited: it.unlimited, | ||
| })), | ||
| }); | ||
|
|
||
| /** Free, Team (card-required 14-day trial), and Enterprise, in display order. */ | ||
| export const AUTUMN_PLAN_SEED: AutumnSeedPlan[] = [free, team, enterprise].map((p) => | ||
| toSeed(p as AtmnPlan), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.