diff --git a/apps/billing-web/e2e/full-page-endings.spec.ts b/apps/billing-web/e2e/full-page-endings.spec.ts index d91a0bd3255..47cedcc76fa 100644 --- a/apps/billing-web/e2e/full-page-endings.spec.ts +++ b/apps/billing-web/e2e/full-page-endings.spec.ts @@ -113,6 +113,50 @@ test('a reload after its own Pay went through renders Already completed on every expect(subscribeRequests(cloud)).toHaveLength(1) }) +/** How the real preview route refuses a link: a 400 with its own code and sentence. */ +function refuseQuote(cloud: MockCloud, message: string) { + cloud.reply('POST', '/billing/preview-subscribe', () => ({ + status: 400, + body: { code: 'TRANSITION_NOT_ALLOWED', message } + })) +} + +test('E2: reopening the checkout link after its own Pay went through is Already completed, even when the quote refuses it with a 400', async ({ + page, + cloud, + signIn +}) => { + await signIn(CHECKOUT) + await payButton(page).click() + await expect(heading(page, "You're all set")).toBeVisible() + refuseQuote(cloud, 'the selected plan is already the current plan') + + await page.goto(CHECKOUT) + + await expect(heading(page, 'Already completed')).toBeVisible() + await expect(code(page)).toHaveText('op_subscribe') + await expect(heading(page, "Couldn't load your checkout")).toBeHidden() + await expect(payButton(page)).toBeHidden() + expect(subscribeRequests(cloud)).toHaveLength(1) +}) + +test("a coded quote refusal on a link nothing here paid is Checkout not available in the server's words", async ({ + page, + cloud, + signIn +}) => { + refuseQuote(cloud, 'the selected plan is already the current plan') + await signIn(CHECKOUT) + + await expect(heading(page, 'Checkout not available')).toBeVisible() + await expect( + page.getByText('the selected plan is already the current plan') + ).toBeVisible() + await expect(code(page)).toHaveText('TRANSITION_NOT_ALLOWED') + await expect(page.getByRole('button', { name: 'Try again' })).toBeHidden() + await expect(contactSupport(page)).toBeVisible() +}) + const RECEIPT_PLAN = { slug: 'pro_monthly', duration: 'MONTHLY' } as const test('77-4068: a Pay that goes through counts the credits the server says it added', async ({ @@ -652,6 +696,37 @@ test('433-6840: a team link without its commit stop is an invalid link, and View ) }) +test('G7: a catalog team plan linked without its commit stop, which the quote refuses with a 400, is Plan not available and View plans opens the Team tab', async ({ + page, + cloud, + signIn +}) => { + const [listed] = cloud.scenario.plans.plans + cloud.scenario.plans = { + ...cloud.scenario.plans, + plans: [ + ...cloud.scenario.plans.plans, + { ...listed, slug: 'team_per_credit_monthly', tier: 'TEAM' } + ] + } + refuseQuote( + cloud, + 'team_credit_stop_id is required for the per-credit Team plan' + ) + await signIn(entryPath('checkout', { plan: 'team_per_credit_monthly' })) + + await expect(heading(page, "This plan isn't available")).toBeVisible() + await expect(code(page)).toHaveText('CHECKOUT_LINK_INVALID') + await expect(heading(page, "Couldn't load your checkout")).toBeHidden() + + await page.getByRole('button', { name: 'View plans' }).click() + + await expect(heading(page, 'Host app')).toBeVisible() + await expect(page).toHaveURL( + 'https://testcloud.comfy.org/?pricing=team&workspace=ws_e2e' + ) +}) + test('433-6840: a checkout link that names no plan, routed once the tab is signed in on the flag, is Plan not available, and View plans opens the pricing table', async ({ page, signIn diff --git a/apps/billing-web/src/checkout/checkoutPage.test.ts b/apps/billing-web/src/checkout/checkoutPage.test.ts index c607d005a90..6733cc3a505 100644 --- a/apps/billing-web/src/checkout/checkoutPage.test.ts +++ b/apps/billing-web/src/checkout/checkoutPage.test.ts @@ -646,6 +646,14 @@ const changed = ( }) const settledOnTheSpot: CheckoutPageEvent = { type: 'paySettled' } const notAllowed: CheckoutPageEvent = { type: 'notAllowed' } +const ALREADY_CURRENT = { + code: 'TRANSITION_NOT_ALLOWED', + message: 'the selected plan is already the current plan' +} +const refusedByServer: CheckoutPageEvent = { + type: 'notAllowed', + server: ALREADY_CURRENT +} const declinedElsewhere: OperationOutcome = { kind: 'declined', reason: 'card_declined', @@ -1176,6 +1184,29 @@ describe("reduceCheckoutPage over this tab's own operation", () => { events: [reconciled(undefined), notAllowed], expected: { kind: 'refused', reason: 'unspecified' } }, + { + name: 'a quote the server refuses with its own code, with nothing of this tab settled, keeps that code and sentence', + events: [reconciled(undefined), refusedByServer], + expected: { kind: 'refused', server: ALREADY_CURRENT } + }, + { + name: 'a success found on mount that the server refuses with its own code is Already completed', + events: [reconciled(succeededOperation()), refusedByServer], + expected: { + kind: 'terminal', + operation: succeededOperation(), + attribution: 'settled' + } + }, + { + name: 'a coded refusal read after Already completed leaves it standing', + events: [reconciled(succeededOperation()), notAllowed, refusedByServer], + expected: { + kind: 'terminal', + operation: succeededOperation(), + attribution: 'settled' + } + }, { name: 'its own payment found settled on a return is Success, whatever the quote says', events: [reconciled(awaited(succeededOperation())), quoted(0)], diff --git a/apps/billing-web/src/checkout/checkoutPage.ts b/apps/billing-web/src/checkout/checkoutPage.ts index a4e86df5ad5..f9b9d55eef8 100644 --- a/apps/billing-web/src/checkout/checkoutPage.ts +++ b/apps/billing-web/src/checkout/checkoutPage.ts @@ -17,6 +17,12 @@ export interface ScheduledChange { readonly effectiveAt: string } +/** A quote the server refused in its own words: its code, and the sentence it wrote for the customer. */ +export interface ServerRefusal { + readonly code: string + readonly message?: string +} + /** `none` means the read succeeded and the workspace has no saved method. */ type SavedStatus = 'loading' | 'ready' | 'none' | 'failed' @@ -159,6 +165,7 @@ export type CheckoutPage = readonly reason: CapabilityDenialReason readonly scheduled?: ScheduledChange } + | { readonly kind: 'refused'; readonly server: ServerRefusal } | { readonly kind: 'unavailable' readonly cause: LoadFailure @@ -214,8 +221,11 @@ export type CheckoutPageEvent = /** The lifecycle could not say what the workspace is waiting on. */ | { readonly type: 'recheckFailed'; readonly code: string } | { readonly type: 'planUnavailable'; readonly reason: PlanUnavailableReason } - /** The quote answered `allowed: false`; its free-text reason is never read. */ - | { readonly type: 'notAllowed' } + /** + * The server will not sell this link: a quote answering `allowed: false`, + * whose free-text reason is never read, or a coded 4xx refusal (`server`). + */ + | { readonly type: 'notAllowed'; readonly server?: ServerRefusal } /** The server's catalog named the plan a settled payment bought. */ | { readonly type: 'settledPlanRead'; readonly plan: SettledPlan } /** Try again on a checkout that could not load. */ @@ -394,7 +404,7 @@ function isStopEvent(event: CheckoutPageEvent): event is StopEvent { /** * The page a read that ends resolving leaves behind. A refused quote over a * success this tab already saw is that success revisited; any other is a - * checkout the server will not sell, for a reason it gives no code for. + * checkout the server will not sell, in its own words when it coded them. */ function stoppedOn( page: Extract, @@ -402,9 +412,7 @@ function stoppedOn( ): CheckoutPage { switch (event.type) { case 'notAllowed': - return page.settled === undefined - ? { kind: 'refused', reason: 'unspecified' } - : { kind: 'terminal', operation: page.settled, attribution: 'settled' } + return notSoldOn(page, event) case 'refused': return { kind: 'refused', @@ -422,6 +430,18 @@ function stoppedOn( } } +/** A link the server will not sell is the success this tab already saw, revisited, or a refusal. */ +function notSoldOn( + page: Extract, + event: Extract +): CheckoutPage { + if (page.settled !== undefined) + return { kind: 'terminal', operation: page.settled, attribution: 'settled' } + return event.server === undefined + ? { kind: 'refused', reason: 'unspecified' } + : { kind: 'refused', server: event.server } +} + /** An event that means nothing in the current state returns it untouched. */ export function reduceCheckoutPage( page: CheckoutPage, diff --git a/apps/billing-web/src/checkout/endingScreen.test.ts b/apps/billing-web/src/checkout/endingScreen.test.ts index 1e55bb8ab42..b11643fbc32 100644 --- a/apps/billing-web/src/checkout/endingScreen.test.ts +++ b/apps/billing-web/src/checkout/endingScreen.test.ts @@ -100,6 +100,31 @@ describe('endingOf', () => { page: { kind: 'terminal', attribution: 'settled' }, screen: { kind: 'already_completed' } }, + { + name: 'a quote the server refused with its own code and sentence', + page: { + kind: 'refused', + server: { + code: 'TRANSITION_NOT_ALLOWED', + message: 'the selected plan is already the current plan' + } + }, + screen: { + kind: 'refused', + code: 'TRANSITION_NOT_ALLOWED', + copy: 'unknown', + serverMessage: 'the selected plan is already the current plan' + } + }, + { + name: 'a quote the server refused with its own code and no sentence', + page: { kind: 'refused', server: { code: 'TRANSITION_NOT_ALLOWED' } }, + screen: { + kind: 'refused', + code: 'TRANSITION_NOT_ALLOWED', + copy: 'unknown' + } + }, { name: 'a checkout that could not load', page: { kind: 'unavailable', cause: 'quote', code: 'REQUEST_FAILED' }, diff --git a/apps/billing-web/src/checkout/endingScreen.ts b/apps/billing-web/src/checkout/endingScreen.ts index 89c4a2f82be..80a5ce5248c 100644 --- a/apps/billing-web/src/checkout/endingScreen.ts +++ b/apps/billing-web/src/checkout/endingScreen.ts @@ -79,6 +79,8 @@ export type EndingScreen = readonly kind: 'refused' readonly code: string readonly copy: RefusalCopy + /** The sentence the server wrote for a quote it refused, shown in place of the copy. */ + readonly serverMessage?: string } | { readonly kind: 'refused' @@ -126,6 +128,15 @@ export function endingOf(page: CheckoutPage): EndingScreen | undefined { function refusedEnding( page: Extract ): EndingScreen { + if ('server' in page) { + const { code, message } = page.server + return { + kind: 'refused', + code, + copy: 'unknown', + ...(message === undefined ? {} : { serverMessage: message }) + } + } const code = page.reason.toUpperCase() return page.scheduled === undefined ? { kind: 'refused', code, copy: REFUSAL_COPY[page.reason] } diff --git a/apps/billing-web/src/components/fullPage/CheckoutEnding.vue b/apps/billing-web/src/components/fullPage/CheckoutEnding.vue index 5437d240dfc..681d3c9b8cf 100644 --- a/apps/billing-web/src/components/fullPage/CheckoutEnding.vue +++ b/apps/billing-web/src/components/fullPage/CheckoutEnding.vue @@ -122,6 +122,12 @@ const bodyParams = computed(() => } : { workspace } ) +/** A refusal the server worded reads in its words. */ +const body = computed( + () => + ('serverMessage' in screen ? screen.serverMessage : undefined) ?? + t(bodyKey.value, bodyParams.value) +) const code = computed(() => ('code' in screen ? screen.code : undefined)) const receipt = computed(() => endingReceipt(screen)) /** A plan card stands in for the reference; without the plan's listing the code stays. */ @@ -188,7 +194,7 @@ function act() { {{ title }}

- {{ t(bodyKey, bodyParams) }} + {{ body }}

= 500 || + httpStatus === RATE_LIMITED + ) + return undefined + return { + code: unwrapServerCode(failure.serverCode), + ...(serverMessage === undefined ? {} : { message: serverMessage }) + } +} + /** A capability read that ends the page before any quote: unreadable, or refused. */ function capabilityStop( allowed: BillingResult @@ -382,11 +411,7 @@ export function useFullPageCheckout() { if (expiredPromo !== undefined) promo.expire() const stopped = capabilityStop(allowed) if (stopped !== undefined) return withScheduledChange(stopped) - if (quoted.status === 'error') - return 'serverCode' in quoted && - matchesServerCode(quoted, UNKNOWN_PLAN_SERVER_CODE) - ? { type: 'planUnavailable', reason: 'retired' } - : { type: 'unavailable', code: quoted.code, ...withHttpStatus(quoted) } + if (quoted.status === 'error') return quoteFailureEvent(quoted, arrival) const unquotable = quotedStop(quoted.value, arrival) if (unquotable !== undefined) return unquotable const facts = { @@ -400,6 +425,43 @@ export function useFullPageCheckout() { } } + /** + * A quote the server would not give: a slug its catalog lacks, a team + * plan its catalog lists but the link names without a stop, a refusal in + * its own words, or a read that failed. + */ + async function quoteFailureEvent( + failure: SubscriptionCommandFailure, + arrival: PlannedEntry + ): Promise { + if ( + 'serverCode' in failure && + matchesServerCode(failure, UNKNOWN_PLAN_SERVER_CODE) + ) + return { type: 'planUnavailable', reason: 'retired' } + const server = refusalOf(failure) + if (server === undefined) + return { + type: 'unavailable', + code: failure.code, + ...withHttpStatus(failure) + } + if (await namesTeamWithoutStop(arrival)) + return { type: 'planUnavailable', reason: 'team_stop_missing' } + return { type: 'notAllowed', server } + } + + async function namesTeamWithoutStop(arrival: PlannedEntry) { + if (arrival.teamCreditStopId !== undefined) return false + const catalog = await plans.read() + return ( + catalog.status === 'ok' && + catalog.value.data.plans.some( + (plan) => plan.slug === arrival.plan && plan.tier === 'TEAM' + ) + ) + } + /** Capture never renders before reconciliation has answered (rule 3). */ async function readCapture() { const arrival = entry.value diff --git a/apps/billing-web/src/views/FullPageCheckoutView.test.ts b/apps/billing-web/src/views/FullPageCheckoutView.test.ts index e364dc0b4ac..15f3a1da36a 100644 --- a/apps/billing-web/src/views/FullPageCheckoutView.test.ts +++ b/apps/billing-web/src/views/FullPageCheckoutView.test.ts @@ -221,6 +221,19 @@ async function renderCheckout( const payButton = () => screen.getByRole('button', { name: 'Pay and subscribe' }) +/** A 400 the server coded, the way the preview route refuses a link. */ +const refusedQuote = ( + serverCode: string, + message: string, + httpStatus = 400 +): PreviewSubscribeResult => ({ + status: 'error', + code: 'REQUEST_FAILED', + httpStatus, + serverCode: readBillingErrorCode({ code: serverCode, message }), + serverMessage: message +}) + afterEach(() => { sessionStorage.clear() }) @@ -584,26 +597,34 @@ describe('FullPageCheckoutView', () => { ]) }) - it.for<{ name: string; serverCode: string; heading: string }>([ + it.for<{ name: string; preview: PreviewSubscribeResult; heading: string }>([ { name: 'a plan the catalog lacks is Plan not available', - serverCode: 'INVALID_PLAN', + preview: refusedQuote('INVALID_PLAN', 'no'), heading: "This plan isn't available" }, { - name: 'any other refused quote is a load failure', - serverCode: 'TRANSITION_NOT_ALLOWED', + name: 'any other coded refusal is Checkout not available', + preview: refusedQuote('TRANSITION_NOT_ALLOWED', 'no'), + heading: 'Checkout not available' + }, + { + name: 'a quote the server could not answer is a load failure', + preview: { status: 'error', code: 'REQUEST_FAILED', httpStatus: 503 }, + heading: "Couldn't load your checkout" + }, + { + name: 'a coded rate limit is a load failure, since Try again can still get the quote', + preview: refusedQuote('RATE_LIMITED', 'slow down', 429), + heading: "Couldn't load your checkout" + }, + { + name: 'a quote that never reached the server is a load failure', + preview: { status: 'error', code: 'REQUEST_FAILED' }, heading: "Couldn't load your checkout" } - ])('$name', async ({ serverCode, heading }) => { - await renderCheckout({ - preview: { - status: 'error', - code: 'REQUEST_FAILED', - httpStatus: 400, - serverCode: readBillingErrorCode({ code: serverCode, message: 'no' }) - } - }) + ])('$name', async ({ preview, heading }) => { + await renderCheckout({ preview }) expect( await screen.findByRole('heading', { name: heading }) @@ -611,6 +632,29 @@ describe('FullPageCheckoutView', () => { expect(form.mounts).toBe(0) }) + it("ends a coded refusal on the server's code and sentence, never a retry", async () => { + const fake = await renderCheckout({ + preview: refusedQuote( + 'TRANSITION_NOT_ALLOWED', + 'the selected plan is already the current plan' + ) + }) + + expect( + await screen.findByRole('heading', { name: 'Checkout not available' }) + ).toBeInTheDocument() + expect( + screen.getByText('the selected plan is already the current plan') + ).toBeInTheDocument() + expect(screen.getByTestId('checkout-ending-code')).toHaveTextContent( + 'TRANSITION_NOT_ALLOWED' + ) + expect( + screen.queryByRole('button', { name: 'Try again' }) + ).not.toBeInTheDocument() + expect(fake.subscribe).not.toHaveBeenCalled() + }) + it.for<{ name: string path: string @@ -666,6 +710,27 @@ describe('FullPageCheckoutView', () => { code: 'CHECKOUT_LINK_INVALID', plans: 'https://testcloud.comfy.org/?pricing=team&workspace=ws-team' }, + { + name: 'a catalog team plan named without its commit stop, which the quote refuses', + path: '/v1/checkout?product=comfyui&return_to=comfyui_workspace&plan=team_per_credit_monthly', + options: { + preview: refusedQuote( + 'TRANSITION_NOT_ALLOWED', + 'team_credit_stop_id is required for the per-credit Team plan' + ), + plans: { + status: 'ok', + value: { + plans: [ + planOf(), + planOf({ slug: 'team_per_credit_monthly', tier: 'TEAM' }) + ] + } + } + }, + code: 'CHECKOUT_LINK_INVALID', + plans: 'https://testcloud.comfy.org/?pricing=team&workspace=ws-team' + }, { name: 'a link the contract cannot read', path: '/v1/checkout?product=comfyui&return_to=comfyui_workspace&plan=creator/monthly', @@ -1572,6 +1637,23 @@ describe('FullPageCheckoutView mount reconciliation', () => { expect(form.mounts).toBe(0) }) + it('lands on Already completed for a settled payment whose link the quote now refuses with its own code', async () => { + await renderCheckout({ + recover: { status: 'ok', value: succeededOperation('op_done') }, + preview: refusedQuote( + 'TRANSITION_NOT_ALLOWED', + 'the selected plan is already the current plan' + ) + }) + + expect( + await screen.findByRole('heading', { name: 'Already completed' }) + ).toBeInTheDocument() + expect(screen.getByTestId('checkout-ending-code')).toHaveTextContent( + 'op_done' + ) + }) + it('opens the form over a settled payment when the quote still sells this link', async () => { await renderCheckout({ recover: { status: 'ok', value: succeededOperation('op_done') }