diff --git a/apps/billing-web/e2e/checkout.spec.ts b/apps/billing-web/e2e/checkout.spec.ts index 6c5b92eb20e..afbcb61d3ac 100644 --- a/apps/billing-web/e2e/checkout.spec.ts +++ b/apps/billing-web/e2e/checkout.spec.ts @@ -7,8 +7,11 @@ import type { BillingOpStatusResponse } from '@comfyorg/ingest-types' import type { MockCloud } from './fixtures/cloud' -import { E2E_USER } from './fixtures/env' -import { expectStraightToHost } from './fixtures/planless' +import { CLOUD_ORIGIN, E2E_USER } from './fixtures/env' +import { + expectPlanlessLinkToLeaveFor, + expectStraightToPricingTable +} from './fixtures/planless' import { challengeRequiredOperation, contactSupportOperation, @@ -418,8 +421,44 @@ test('a checkout link naming a team credit stop quotes it along with the plan', }) test.describe('a checkout link that names no plan goes back to the host to choose one', () => { + test('on the Team tab when the link names a team', async ({ page }) => { + await expectPlanlessLinkToLeaveFor( + page, + { workspace: 'ws_team_e2e', team_credit_stop_id: 'stop_700' }, + `${CLOUD_ORIGIN}/?pricing=team&workspace=ws_team_e2e` + ) + }) + + test("to a platform link's own return_to when billing may follow it", async ({ + page + }) => { + await expectPlanlessLinkToLeaveFor( + page, + { product: 'platform', workspace: 'ws_e2e' }, + `${CLOUD_ORIGIN}/?workspace=ws_e2e` + ) + }) + + test('never to the cloud for a platform link this deployment has no platform page for', async ({ + page, + signIn + }) => { + await signIn(CHECKOUT) + const platformLink = entryPath('checkout', { + product: 'platform', + return_to: 'nowhere' + }) + + await page.goto(platformLink) + + await expect( + page.getByText("That link doesn't name a place we can send you back to.") + ).toBeVisible() + await expect(page).toHaveURL(platformLink) + }) + test('for a signed-out visitor', async ({ page }) => { - await expectStraightToHost(page, 'ws_team_e2e') + await expectStraightToPricingTable(page, 'ws_team_e2e') }) test('for a signed-in customer, in the tab they signed in on', async ({ @@ -431,7 +470,7 @@ test.describe('a checkout link that names no plan goes back to the host to choos page.getByRole('button', { name: 'Pay and subscribe' }) ).toBeVisible() - await expectStraightToHost(page, 'ws_e2e') + await expectStraightToPricingTable(page, 'ws_e2e') }) test('for a signed-in customer the host opens a new tab for, before that tab has a session', async ({ @@ -440,7 +479,7 @@ test.describe('a checkout link that names no plan goes back to the host to choos }) => { await signIn(CHECKOUT) - await expectStraightToHost(await context.newPage(), 'ws_e2e') + await expectStraightToPricingTable(await context.newPage(), 'ws_e2e') }) test('for a signed-in customer whose link names a workspace they cannot manage, never the refusal', async ({ @@ -455,7 +494,7 @@ test.describe('a checkout link that names no plan goes back to the host to choos })) const tab = await context.newPage() - await expectStraightToHost(tab, 'ws_not_a_member') + await expectStraightToPricingTable(tab, 'ws_not_a_member') await expect(tab.getByRole('alert')).toHaveCount(0) }) }) diff --git a/apps/billing-web/e2e/fixtures/planless.ts b/apps/billing-web/e2e/fixtures/planless.ts index 5858852ff4b..6c7bdf43a1d 100644 --- a/apps/billing-web/e2e/fixtures/planless.ts +++ b/apps/billing-web/e2e/fixtures/planless.ts @@ -1,18 +1,16 @@ import type { Page } from '@playwright/test' +import { CLOUD_ORIGIN } from './env' import { entryPath, expect } from './test' -const planlessCheckout = (workspace: string) => - entryPath('checkout', { workspace }) - /** - * Opens a checkout link that names no plan and expects what the embedded - * checkout always did with it: straight back to the host, the link's - * workspace echoed, with no sign-in page shown and no session minted. + * Opens a checkout link that names no plan and expects it to leave for + * `destination` at once, with no sign-in page shown and no session minted. */ -export async function expectStraightToHost( +export async function expectPlanlessLinkToLeaveFor( tab: Page, - workspace: string + link: Record, + destination: string ): Promise { const shown: string[] = [] const mints: string[] = [] @@ -24,12 +22,22 @@ export async function expectStraightToHost( mints.push(request.url()) }) - await tab.goto(planlessCheckout(workspace)) + await tab.goto(entryPath('checkout', link)) - await expect(tab).toHaveURL( - `https://testcloud.comfy.org/?workspace=${workspace}` - ) + await expect(tab).toHaveURL(destination) await expect(tab.getByRole('heading', { name: 'Host app' })).toBeVisible() expect(shown).not.toContain('/sign-in') expect(mints, 'a link that goes back mints no session').toEqual([]) } + +/** The host's pricing table, in the link's workspace: where a plan is picked. */ +export async function expectStraightToPricingTable( + tab: Page, + workspace: string +): Promise { + await expectPlanlessLinkToLeaveFor( + tab, + { workspace }, + `${CLOUD_ORIGIN}/?pricing=1&workspace=${workspace}` + ) +} diff --git a/apps/billing-web/e2e/full-page-endings.spec.ts b/apps/billing-web/e2e/full-page-endings.spec.ts index f15e8aa894c..d8fc26ec32e 100644 --- a/apps/billing-web/e2e/full-page-endings.spec.ts +++ b/apps/billing-web/e2e/full-page-endings.spec.ts @@ -11,7 +11,7 @@ import { pendingOperation, succeededOperation } from './fixtures/scenario' -import { expectStraightToHost } from './fixtures/planless' +import { expectStraightToPricingTable } from './fixtures/planless' import { installFakeStripe } from './fixtures/stripe' import { entryPath, expect, test as base } from './fixtures/test' @@ -435,7 +435,7 @@ test('433-6840: a checkout link that names no plan, routed once the tab is signe test('a signed-out visitor on the flag still goes back to the host for a checkout link that names no plan', async ({ page }) => { - await expectStraightToHost(page, 'ws_team_e2e') + await expectStraightToPricingTable(page, 'ws_team_e2e') }) test('a checkout link that names no plan, opened by the host in a new tab, goes back to the host even for a signed-in customer on the flag', async ({ @@ -444,7 +444,7 @@ test('a checkout link that names no plan, opened by the host in a new tab, goes }) => { await signIn(CHECKOUT) - await expectStraightToHost(await context.newPage(), 'ws_e2e') + await expectStraightToPricingTable(await context.newPage(), 'ws_e2e') }) test("433-6840: a checkout link the contract cannot read is the checkout's 404 on the full page, and still the entry error on the embedded one", async ({ diff --git a/apps/billing-web/src/router.planless.test.ts b/apps/billing-web/src/router.planless.test.ts index 7aef8d2354c..cfed20ba4e2 100644 --- a/apps/billing-web/src/router.planless.test.ts +++ b/apps/billing-web/src/router.planless.test.ts @@ -1,5 +1,7 @@ import { createMemoryHistory } from 'vue-router' +import type { BillingEnvironment } from '@comfyorg/billing-contract' + import type { BillingWebSessionPhase } from '@/router' const h = vi.hoisted(() => ({ @@ -48,7 +50,7 @@ function flagAnswers(variant: string) { const PLANLESS = '/v1/checkout?product=comfyui&return_to=comfyui_workspace&workspace=ws-team' -const HOST = 'https://testcloud.comfy.org/?workspace=ws-team' +const PRICING_TABLE = 'https://testcloud.comfy.org/?pricing=1&workspace=ws-team' beforeEach(() => { vi.resetModules() @@ -57,10 +59,13 @@ beforeEach(() => { h.bind.mockReset() }) -async function openPlanless(visitor: { - livePhase: BillingWebSessionPhase | undefined - flag: string -}) { +async function openPlanless( + visitor: { + livePhase: BillingWebSessionPhase | undefined + flag: string + }, + path: string = PLANLESS +) { h.livePhase = visitor.livePhase flagAnswers(visitor.flag) const { createBillingRouter } = await import('@/router') @@ -72,7 +77,7 @@ async function openPlanless(visitor: { undefined, leave ) - await router.push(PLANLESS) + await router.push(path) return { router, leave, ...useBillingEntry() } } @@ -113,11 +118,11 @@ describe('a checkout link that names no plan', () => { flag: 'full_page' } ])( - 'sends $name straight back to the host, without asking for a session or the flag', + 'sends $name straight to the pricing table, without asking for a session or the flag', async (visitor) => { const { router, leave } = await openPlanless(visitor) - expect(leave).toHaveBeenCalledExactlyOnceWith(HOST) + expect(leave).toHaveBeenCalledExactlyOnceWith(PRICING_TABLE) expect(router.currentRoute.value.path).not.toBe('/sign-in') expect(h.bind).not.toHaveBeenCalled() expect(fetchMock).not.toHaveBeenCalled() @@ -125,14 +130,14 @@ describe('a checkout link that names no plan', () => { ) it.for(['embedded', 'unreachable'])( - 'sends a signed-in customer whose flag reads %s back to the host', + 'sends a signed-in customer whose flag reads %s to the pricing table', async (flag) => { const { router, leave } = await openPlanless({ livePhase: 'authenticated', flag }) - expect(leave).toHaveBeenCalledExactlyOnceWith(HOST) + expect(leave).toHaveBeenCalledExactlyOnceWith(PRICING_TABLE) expect(router.currentRoute.value.path).not.toBe('/sign-in') expect(h.bind).not.toHaveBeenCalled() } @@ -156,6 +161,85 @@ describe('a checkout link that names no plan', () => { }) }) +describe('where a checkout link that names no plan sends the customer to pick one', () => { + const SIGNED_OUT = { livePhase: 'signed-out', flag: 'embedded' } as const + + it.for<{ + name: string + env: BillingEnvironment + query: string + destination: string + }>([ + { + name: 'a cloud link to the pricing table', + env: 'staging', + query: 'product=comfyui&return_to=comfyui_workspace&workspace=ws-1', + destination: 'https://stagingcloud.comfy.org/?pricing=1&workspace=ws-1' + }, + { + name: 'a cloud link without a return_to to the pricing table', + env: 'production', + query: 'product=comfyui', + destination: 'https://cloud.comfy.org/?pricing=1' + }, + { + name: 'a cloud link that names a team to the Team tab', + env: 'staging', + query: + 'product=comfyui&return_to=comfyui_workspace&workspace=ws-1&team_credit_stop_id=stop_700', + destination: 'https://stagingcloud.comfy.org/?pricing=team&workspace=ws-1' + }, + { + name: 'a platform link to its return_to', + env: 'staging', + query: 'product=platform&return_to=platform_account&workspace=ws-1', + destination: 'https://stagingplatform.comfy.org/?workspace=ws-1' + }, + { + name: 'a platform link to a return_to in another product', + env: 'production', + query: 'product=platform&return_to=comfyui_credits', + destination: 'https://cloud.comfy.org/?settings=plan-credits' + }, + { + name: 'a platform link with an unregistered return_to to the platform billing page', + env: 'staging', + query: 'product=platform&return_to=https://evil.test&workspace=ws-1', + destination: + 'https://stagingplatform.comfy.org/profile/billing?workspace=ws-1' + }, + { + name: 'a platform link without a return_to to the platform billing page', + env: 'production', + query: 'product=platform', + destination: 'https://platform.comfy.org/profile/billing' + } + ])('sends $name', async ({ env, query, destination }) => { + vi.stubEnv('VITE_BILLING_ENV', env) + + const { router, leave } = await openPlanless( + SIGNED_OUT, + `/v1/checkout?${query}` + ) + + expect(leave).toHaveBeenCalledExactlyOnceWith(destination) + expect(router.currentRoute.value.path).not.toBe('/sign-in') + expect(h.bind).not.toHaveBeenCalled() + expect(fetchMock).not.toHaveBeenCalled() + }) + + it('explains a platform link where this deployment has no platform to send it to', async () => { + const { leave, entry, error } = await openPlanless( + SIGNED_OUT, + '/v1/checkout?product=platform&return_to=platform_account' + ) + + expect(leave).not.toHaveBeenCalled() + expect(entry.value).toBeUndefined() + expect(error.value).toBe('UNKNOWN_RETURN_TARGET') + }) +}) + describe('a planless checkout link the customer navigates away from', () => { const LATER = '/v1/subscription?product=comfyui&return_to=comfyui_workspace&workspace=ws-other' diff --git a/apps/billing-web/src/router.test.ts b/apps/billing-web/src/router.test.ts index de9d6ace53d..011a9a09fd7 100644 --- a/apps/billing-web/src/router.test.ts +++ b/apps/billing-web/src/router.test.ts @@ -93,19 +93,26 @@ describe('plan selection, which the host app owns', () => { } it.for([ - `/v1/pricing?${ENTRY_QUERY}&workspace=ws-team`, - `/v1/checkout?${ENTRY_QUERY}&workspace=ws-team` - ])('sends %s back to the host without rebinding the tab', async (path) => { - const { router, onEntryWorkspace, leave } = hostBoundRouter() + [ + `/v1/pricing?${ENTRY_QUERY}&workspace=ws-team`, + 'https://testcloud.comfy.org/?workspace=ws-team' + ], + [ + `/v1/checkout?${ENTRY_QUERY}&workspace=ws-team`, + 'https://testcloud.comfy.org/?pricing=1&workspace=ws-team' + ] + ])( + 'sends %s to %s without rebinding the tab', + async ([path, destination]) => { + const { router, onEntryWorkspace, leave } = hostBoundRouter() - await router.push(path) + await router.push(path) - expect(leave).toHaveBeenCalledExactlyOnceWith( - 'https://testcloud.comfy.org/?workspace=ws-team' - ) - expect(onEntryWorkspace).not.toHaveBeenCalled() - expect(router.currentRoute.value.path).not.toBe('/sign-in') - }) + expect(leave).toHaveBeenCalledExactlyOnceWith(destination) + expect(onEntryWorkspace).not.toHaveBeenCalled() + expect(router.currentRoute.value.path).not.toBe('/sign-in') + } + ) it('keeps a checkout that names a plan', async () => { const { router, leave } = hostBoundRouter('authenticated') diff --git a/apps/billing-web/src/router.ts b/apps/billing-web/src/router.ts index 52f83f49674..39c7f5c6350 100644 --- a/apps/billing-web/src/router.ts +++ b/apps/billing-web/src/router.ts @@ -16,6 +16,7 @@ import { resolveReturnTarget } from '@comfyorg/billing-contract' +import { pricingTableUrl } from '@/checkout/cloudLinks' import { awaitCheckoutUiVariant } from '@/config/checkoutUi' import { BILLING_WEB_ENV } from '@/config/env' import { recordBillingEntry } from '@/entry/billingEntry' @@ -90,9 +91,12 @@ function fullPageKeepsPlanless(): false | Promise { } /** Echoes the host's own workspace back, leaving this tab's binding alone. */ -function hostReturnHref(entry: BillingEntry): string | undefined { +function returnHref( + target: ReturnTarget, + entry: BillingEntry +): string | undefined { return buildReturnUrl({ - target: entry.returnTo, + target, environment: BILLING_WEB_ENV, workspace: entry.workspaceId })?.href @@ -101,21 +105,46 @@ function hostReturnHref(entry: BillingEntry): string | undefined { const CHECKOUT_PATH = billingIntentPath('checkout') const CHECKOUT_FALLBACK_RETURN: ReturnTarget = 'comfyui_credits' +const linkUrl = (fullPath: string) => + new URL(fullPath, 'https://billing.invalid') + +function followsLinkReturn(url: URL): boolean { + const target = url.searchParams.get('return_to') + return ( + target !== null && + resolveReturnTarget(target, BILLING_WEB_ENV) !== undefined + ) +} + /** * A checkout's `return_to` is optional. One that is missing, outside the * registry, or without a destination in this family is ignored, never * followed, and the checkout returns to Plan & Credits instead. */ function withCheckoutReturn(fullPath: string): string { - const url = new URL(fullPath, 'https://billing.invalid') - if (url.pathname !== CHECKOUT_PATH) return fullPath - const target = url.searchParams.get('return_to') - if (target !== null && resolveReturnTarget(target, BILLING_WEB_ENV)) - return fullPath + const url = linkUrl(fullPath) + if (url.pathname !== CHECKOUT_PATH || followsLinkReturn(url)) return fullPath url.searchParams.set('return_to', CHECKOUT_FALLBACK_RETURN) return `${url.pathname}${url.search}` } +/** + * Where a checkout that names no plan goes to have one picked. Platform has + * no pricing table, so its link goes back to its own `return_to`, or to + * Platform's billing page when that is not one billing may follow. + */ +function planPickerHref( + entry: BillingEntry, + followsReturn: boolean +): string | undefined { + if (entry.product !== 'platform') + return pricingTableUrl( + entry.teamCreditStopId === undefined ? 'default' : 'team', + entry.workspaceId + ) + return returnHref(followsReturn ? entry.returnTo : 'platform_billing', entry) +} + function leaveForHost(href: string): void { window.location.replace(href) } @@ -130,7 +159,7 @@ function leaveForHost(href: string): void { * and signing in would not repair it. The sign-in page is the one route that * leaves the entry alone, because it is where that visitor was sent. The app's * own entry path carries no product request and clears what a previous link - * left behind. A link that still needs a plan chosen goes back to its host + * left behind. A link that still needs a plan chosen goes where one is picked * before any session is asked for, unless the full-page flag of a tab that is * already signed in keeps a planless checkout to explain it; with nowhere to * go back to, it is an entry error. The phase is read on every route, sign-in @@ -153,8 +182,7 @@ export function createBillingRouter( } /** False once the link has left this tab for its host. */ - function sendToHost(entry: BillingEntry): boolean { - const href = hostReturnHref(entry) + function sendToHost(href: string | undefined): boolean { if (href === undefined) return recordUnknownReturn() leave(href) return false @@ -170,13 +198,16 @@ export function createBillingRouter( * The tab stays unbound to the link's workspace until the flag answers, and * an answer that arrives after a newer navigation started acts on nothing. */ - function readPlanless(entry: BillingEntry): boolean | Promise { + function readPlanless( + entry: BillingEntry, + pickPlanAt: string | undefined + ): boolean | Promise { const kept = fullPageKeepsPlanless() - if (kept === false) return sendToHost(entry) + if (kept === false) return sendToHost(pickPlanAt) const navigation = latestNavigation return kept.then((full) => { if (navigation !== latestNavigation) return false - return full ? admitEntry(entry) : sendToHost(entry) + return full ? admitEntry(entry) : sendToHost(pickPlanAt) }) } @@ -187,8 +218,13 @@ export function createBillingRouter( return true } const { entry } = result - if (entry.intent === 'pricing') return sendToHost(entry) - if (isPlanlessCheckout(entry)) return readPlanless(entry) + if (entry.intent === 'pricing') + return sendToHost(returnHref(entry.returnTo, entry)) + if (isPlanlessCheckout(entry)) + return readPlanless( + entry, + planPickerHref(entry, followsLinkReturn(linkUrl(fullPath))) + ) return admitEntry(entry) } diff --git a/packages/billing-contract/src/returnTargets.test.ts b/packages/billing-contract/src/returnTargets.test.ts index 3073d0df39e..7956b95adb0 100644 --- a/packages/billing-contract/src/returnTargets.test.ts +++ b/packages/billing-contract/src/returnTargets.test.ts @@ -23,13 +23,24 @@ describe('resolveReturnTarget', () => { 'https://testcloud.comfy.org/?settings=plan-credits' ], ['platform_account', 'production', 'https://platform.comfy.org/'], - ['platform_account', 'staging', 'https://stagingplatform.comfy.org/'] + ['platform_account', 'staging', 'https://stagingplatform.comfy.org/'], + [ + 'platform_billing', + 'production', + 'https://platform.comfy.org/profile/billing' + ], + [ + 'platform_billing', + 'staging', + 'https://stagingplatform.comfy.org/profile/billing' + ] ] as const)('resolves %s in %s', ([target, environment, expected]) => { expect(resolveReturnTarget(target, environment)?.href).toBe(expected) }) it.for([ ['platform_account', 'test'], + ['platform_billing', 'test'], ['workshop_credits', 'production'], ['attacker_site', 'production'], ['https://attacker.example', 'production'] diff --git a/packages/billing-contract/src/returnTargets.ts b/packages/billing-contract/src/returnTargets.ts index 73de0a10925..f585b9d44b1 100644 --- a/packages/billing-contract/src/returnTargets.ts +++ b/packages/billing-contract/src/returnTargets.ts @@ -19,7 +19,8 @@ import { parseUrl } from './url.js' export const RETURN_TARGETS = [ 'comfyui_workspace', 'comfyui_credits', - 'platform_account' + 'platform_account', + 'platform_billing' ] as const export type ReturnTarget = (typeof RETURN_TARGETS)[number] @@ -48,6 +49,11 @@ const RETURN_TARGET_DESTINATIONS: Readonly< platform_account: { production: 'https://platform.comfy.org/', staging: 'https://stagingplatform.comfy.org/' + }, + // Platform's own billing page, `pages/profile/billing.vue` in that app. + platform_billing: { + production: 'https://platform.comfy.org/profile/billing', + staging: 'https://stagingplatform.comfy.org/profile/billing' } }