From 024d424e9feed1bf48c12f5092236fe138102f1c Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Mon, 28 Sep 2026 14:01:11 -0400 Subject: [PATCH 1/4] feat(account-ui): export CheckoutSavedMethods from the checkout entry --- packages/account-ui/src/billing/checkout/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/account-ui/src/billing/checkout/index.ts b/packages/account-ui/src/billing/checkout/index.ts index c8ae25e66a8..61ca4189aca 100644 --- a/packages/account-ui/src/billing/checkout/index.ts +++ b/packages/account-ui/src/billing/checkout/index.ts @@ -23,6 +23,7 @@ export { resolveRenewalDate } from './checkoutQuote' export { default as CheckoutPaymentForm } from './CheckoutPaymentForm.vue' +export { default as CheckoutSavedMethods } from './CheckoutSavedMethods.vue' export { default as CheckoutSubscribeConfirm } from './CheckoutSubscribeConfirm.vue' export { default as CheckoutSuccess } from './CheckoutSuccess.vue' export { default as CheckoutTermsNote } from './CheckoutTermsNote.vue' From 9f5f1205d460d2d1e01173a285dd2f894ba95b0a Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Mon, 28 Sep 2026 14:01:38 -0400 Subject: [PATCH 2/4] feat(billing-web): saved-method tabs and the payment-rail failure taxonomy on the full-page checkout --- .../e2e/full-page-checkout.spec.ts | 120 ++++++++- .../src/checkout/checkoutPage.test.ts | 246 +++++++++++++++--- apps/billing-web/src/checkout/checkoutPage.ts | 130 +++++++-- .../src/checkout/subscribeRequest.test.ts | 17 ++ .../src/checkout/subscribeRequest.ts | 5 + .../fullPage/CheckoutPaymentColumn.vue | 121 ++++++--- .../components/fullPage/PaymentFormError.vue | 15 +- .../components/fullPage/PaymentMethodTabs.vue | 44 ++++ .../components/fullPage/PaymentTabsRail.vue | 49 ++++ .../components/fullPage/SavedMethodPanel.vue | 67 +++++ .../src/composables/useFullPageCheckout.ts | 76 +++++- apps/billing-web/src/locales/en/main.json | 27 +- .../billing-web/src/test/fakeBillingClient.ts | 8 +- .../src/views/FullPageCheckoutView.test.ts | 219 ++++++++++++++-- .../src/views/FullPageCheckoutView.vue | 10 +- 15 files changed, 1011 insertions(+), 143 deletions(-) create mode 100644 apps/billing-web/src/components/fullPage/PaymentMethodTabs.vue create mode 100644 apps/billing-web/src/components/fullPage/PaymentTabsRail.vue create mode 100644 apps/billing-web/src/components/fullPage/SavedMethodPanel.vue diff --git a/apps/billing-web/e2e/full-page-checkout.spec.ts b/apps/billing-web/e2e/full-page-checkout.spec.ts index 2f68309b58e..b0e52dd85b2 100644 --- a/apps/billing-web/e2e/full-page-checkout.spec.ts +++ b/apps/billing-web/e2e/full-page-checkout.spec.ts @@ -1,3 +1,5 @@ +import type { Page } from '@playwright/test' + import { installFakeStripe } from './fixtures/stripe' import { entryPath, expect, test as base } from './fixtures/test' @@ -18,18 +20,31 @@ const test = base.extend<{ fullPage: void }>({ const CHECKOUT = entryPath('checkout', { plan: 'pro_monthly' }) const EYEBROW = 'Subscribe to Pro Plan · Personal' +const FORM_FAILED = "The payment form couldn't load" +const SAVED_FAILED = "Your saved payment methods couldn't load" + +const payButton = (page: Page) => + page.getByRole('button', { name: 'Pay and subscribe' }) +const tab = (page: Page, name: 'Saved' | 'Add new payment') => + page.getByRole('tab', { name }) + +function failPaymentElementLoads(page: Page) { + return page.addInitScript(() => { + Object.assign(window, { __e2eStripeLoadErrors: 1 }) + }) +} test('names the plan and the workspace, and enables Pay once the form is ready', async ({ page, cloud, signIn }) => { + cloud.scenario.paymentMethods = [] await signIn(CHECKOUT) await expect(page.getByText(EYEBROW)).toBeVisible() - await expect( - page.getByRole('button', { name: 'Pay and subscribe' }) - ).toBeEnabled() + await expect(payButton(page)).toBeEnabled() + await expect(tab(page, 'Saved')).toBeHidden() await expect( page.getByRole('heading', { name: 'Confirm your payment' }) ).toBeHidden() @@ -38,25 +53,102 @@ test('names the plan and the workspace, and enables Pay once the form is ready', ).toBe(true) }) -test('a payment form that fails to load is replaced, and Try again remounts it', async ({ +test('360-4874: with no saved method a failed form takes the column, and Try again remounts it', async ({ page, + cloud, signIn }) => { - await page.addInitScript(() => { - Object.assign(window, { __e2eStripeLoadErrors: 1 }) - }) + cloud.scenario.paymentMethods = [] + await failPaymentElementLoads(page) await signIn(CHECKOUT) - await expect(page.getByText("The payment form couldn't load")).toBeVisible() - await expect( - page.getByRole('button', { name: 'Pay and subscribe' }) - ).toBeHidden() + await expect(page.getByText(FORM_FAILED)).toBeVisible() + await expect(payButton(page)).toBeHidden() await expect(page.getByText(EYEBROW)).toBeVisible() await page.getByRole('button', { name: 'Try again' }).click() + await expect(payButton(page)).toBeEnabled() + await expect(page.getByText(FORM_FAILED)).toBeHidden() +}) + +test('opens on the saved method and subscribes with it, no card token', async ({ + page, + cloud, + signIn +}) => { + await signIn(CHECKOUT) + + await expect(tab(page, 'Saved')).toHaveAttribute('aria-selected', 'true') + await expect(page.getByText('visa •••• 4242')).toBeVisible() + await expect(page.getByText('Billing address')).toBeHidden() + await payButton(page).click() + + await expect + .poll(() => + cloud.requests.find((request) => request.path === '/billing/subscribe') + ) + .toMatchObject({ body: { saved_payment_method_id: 'pm_e2e' } }) + const subscribe = cloud.requests.find( + (request) => request.path === '/billing/subscribe' + ) + expect(subscribe?.body).not.toHaveProperty('confirmation_token') +}) + +test('368-15319: a failed form beside a saved method stays inside Add new, and Saved stays payable', async ({ + page, + signIn +}) => { + await failPaymentElementLoads(page) + await signIn(CHECKOUT) + + await expect(tab(page, 'Saved')).toHaveAttribute('aria-selected', 'true') + await expect(payButton(page)).toBeEnabled() + await expect(page.getByText(FORM_FAILED)).toBeHidden() + + await tab(page, 'Add new payment').click() + + await expect(page.getByText(FORM_FAILED)).toBeVisible() await expect( - page.getByRole('button', { name: 'Pay and subscribe' }) - ).toBeEnabled() - await expect(page.getByText("The payment form couldn't load")).toBeHidden() + page.getByText('or pay with a saved payment method.', { exact: false }) + ).toBeVisible() + await expect(payButton(page)).toBeHidden() + + await page.getByRole('button', { name: 'Try again' }).click() + + await expect(payButton(page)).toBeEnabled() + await expect(tab(page, 'Add new payment')).toHaveAttribute( + 'aria-selected', + 'true' + ) +}) + +test('368-15401: a failed saved-methods read errors on Saved only, and Add new stays live', async ({ + page, + cloud, + signIn +}) => { + let healthy = false + cloud.reply('GET', '/billing/payment-methods', () => + healthy + ? { body: cloud.scenario.paymentMethods } + : { status: 500, body: { code: 'INTERNAL', message: 'boom' } } + ) + await signIn(CHECKOUT) + + await expect(tab(page, 'Saved')).toHaveAttribute('aria-selected', 'true') + await expect(page.getByText(SAVED_FAILED)).toBeVisible() + await expect(page.getByText('Billing address')).toBeHidden() + await expect(payButton(page)).toBeHidden() + + await tab(page, 'Add new payment').click() + await expect(payButton(page)).toBeEnabled() + + await tab(page, 'Saved').click() + healthy = true + await page.getByRole('button', { name: 'Try again' }).click() + + await expect(page.getByText('visa •••• 4242')).toBeVisible() + await expect(page.getByText(SAVED_FAILED)).toBeHidden() + await expect(payButton(page)).toBeEnabled() }) diff --git a/apps/billing-web/src/checkout/checkoutPage.test.ts b/apps/billing-web/src/checkout/checkoutPage.test.ts index 5762d3583cb..bad07fad1b3 100644 --- a/apps/billing-web/src/checkout/checkoutPage.test.ts +++ b/apps/billing-web/src/checkout/checkoutPage.test.ts @@ -1,11 +1,22 @@ -import type { CheckoutPage, CheckoutPageEvent } from '@/checkout/checkoutPage' +import type { + CheckoutPage, + CheckoutPageEvent, + PaymentTab, + RailView, + SavedArrival +} from '@/checkout/checkoutPage' import { RESOLVING, railAcceptsPay, + railView, reduceCheckoutPage } from '@/checkout/checkoutPage' -const quotedCard: CheckoutPageEvent = { type: 'quoted', method: 'new_card' } +const quoted = (saved: SavedArrival): CheckoutPageEvent => ({ + type: 'quoted', + method: 'collect', + saved +}) const quotedOnFile: CheckoutPageEvent = { type: 'quoted', method: 'on_file' } const refused: CheckoutPageEvent = { type: 'refused', @@ -18,16 +29,37 @@ const unavailable: CheckoutPageEvent = { const ready: CheckoutPageEvent = { type: 'elementReady' } const failed: CheckoutPageEvent = { type: 'elementFailed' } const retried: CheckoutPageEvent = { type: 'elementRetried' } +const savedLoaded = (count: number): CheckoutPageEvent => ({ + type: 'savedLoaded', + count +}) +const savedFailed: CheckoutPageEvent = { type: 'savedFailed' } +const savedRetried: CheckoutPageEvent = { type: 'savedRetried' } +const select = (tab: PaymentTab): CheckoutPageEvent => ({ + type: 'tabSelected', + tab +}) + +type Element = 'loading' | 'ready' | 'failed' +type Saved = 'loading' | 'ready' | 'none' | 'failed' -const cardWith = (element: 'loading' | 'ready' | 'failed'): CheckoutPage => ({ +const collect = ( + element: Element, + saved: Saved = 'none', + tab: PaymentTab = saved === 'none' ? 'new' : 'saved' +): CheckoutPage => ({ kind: 'capture', - rail: { method: 'new_card', element } + rail: { method: 'collect', element, saved, tab } }) function replay(events: readonly CheckoutPageEvent[]): CheckoutPage { return events.reduce(reduceCheckoutPage, RESOLVING) } +function viewOf(page: CheckoutPage): RailView | undefined { + return page.kind === 'capture' ? railView(page.rail) : undefined +} + describe('reduceCheckoutPage', () => { it.for<{ name: string @@ -49,15 +81,39 @@ describe('reduceCheckoutPage', () => { pay: false }, { - name: 'a quote for a new card', - events: [quotedCard], - expected: cardWith('loading'), + name: 'a quote with no saved method, on Add new', + events: [quoted(0)], + expected: collect('loading', 'none', 'new'), + pay: false + }, + { + name: 'a quote with saved methods, on Saved, payable before the element', + events: [quoted(2)], + expected: collect('loading', 'ready', 'saved'), + pay: true + }, + { + name: 'a quote whose saved read failed, on Saved', + events: [quoted('failed')], + expected: collect('loading', 'failed', 'saved'), + pay: false + }, + { + name: 'a ready element with no saved method', + events: [quoted(0), ready], + expected: collect('ready'), + pay: true + }, + { + name: 'Add new chosen before the element is ready', + events: [quoted(1), select('new')], + expected: collect('loading', 'ready', 'new'), pay: false }, { - name: 'a quote then a ready element', - events: [quotedCard, ready], - expected: cardWith('ready'), + name: 'Add new chosen once the element is ready', + events: [quoted(1), ready, select('new')], + expected: collect('ready', 'ready', 'new'), pay: true }, { @@ -67,27 +123,39 @@ describe('reduceCheckoutPage', () => { pay: true }, { - name: 'an element that fails before ready', - events: [quotedCard, failed], - expected: cardWith('failed'), + name: 'an element that fails after ready', + events: [quoted(0), ready, failed], + expected: collect('failed'), pay: false }, { - name: 'an element that fails after ready', - events: [quotedCard, ready, failed], - expected: cardWith('failed'), + name: 'an element retry, which re-disables Pay until the remount is ready', + events: [quoted(0), failed, retried], + expected: collect('loading'), pay: false }, { - name: 'a retry, which re-disables Pay until the remount is ready', - events: [quotedCard, failed, retried], - expected: cardWith('loading'), + name: 'a saved retry, which stays on Saved while it reads', + events: [quoted('failed'), savedRetried], + expected: collect('loading', 'loading', 'saved'), pay: false }, { - name: 'a retry that comes back ready', - events: [quotedCard, failed, retried, ready], - expected: cardWith('ready'), + name: 'a saved retry that finds methods', + events: [quoted('failed'), savedRetried, savedLoaded(1)], + expected: collect('loading', 'ready', 'saved'), + pay: true + }, + { + name: 'a saved retry that fails again', + events: [quoted('failed'), savedRetried, savedFailed], + expected: collect('loading', 'failed', 'saved'), + pay: false + }, + { + name: 'a saved retry that finds none, leaving only Add new', + events: [quoted('failed'), ready, savedRetried, savedLoaded(0)], + expected: collect('ready', 'none', 'new'), pay: true } ])('$name', ({ events, expected, pay }) => { @@ -101,42 +169,156 @@ describe('reduceCheckoutPage', () => { { name: 'element ready while resolving', from: RESOLVING, event: ready }, { name: 'element failed while resolving', from: RESOLVING, event: failed }, { name: 'retry while resolving', from: RESOLVING, event: retried }, + { + name: 'a saved read while resolving', + from: RESOLVING, + event: savedLoaded(1) + }, + { name: 'a tab while resolving', from: RESOLVING, event: select('new') }, { name: 'a late quote after a refusal', from: replay([refused]), - event: quotedCard + event: quoted(0) }, { name: 'a late refusal after capture', - from: cardWith('ready'), + from: collect('ready'), event: refused }, { name: 'a late failure after capture', - from: cardWith('loading'), + from: collect('loading'), event: unavailable }, { name: 'a second quote during capture', - from: cardWith('ready'), + from: collect('ready'), event: quotedOnFile }, - { - name: 'retry of a live element', - from: cardWith('ready'), - event: retried - }, + { name: 'retry of a live element', from: collect('ready'), event: retried }, { name: 'ready from a failed element', - from: cardWith('failed'), + from: collect('failed'), event: ready }, + { + name: 'a saved retry of a live list', + from: collect('ready', 'ready'), + event: savedRetried + }, + { + name: 'a saved answer nobody asked for', + from: collect('ready', 'failed'), + event: savedLoaded(3) + }, + { + name: 'the Saved tab with nothing saved', + from: collect('ready', 'none', 'new'), + event: select('saved') + }, + { + name: 'the tab already selected', + from: collect('ready', 'ready', 'saved'), + event: select('saved') + }, { name: 'element events on a method on file', from: replay([quotedOnFile]), event: failed + }, + { + name: 'a tab on a method on file', + from: replay([quotedOnFile]), + event: select('new') } ])('ignores $name', ({ from, event }) => { expect(reduceCheckoutPage(from, event)).toBe(from) }) + + it.for<{ tab: PaymentTab; event: CheckoutPageEvent }>([ + { tab: 'saved', event: failed }, + { tab: 'new', event: failed }, + { tab: 'saved', event: retried }, + { tab: 'new', event: retried }, + { tab: 'saved', event: ready }, + { tab: 'new', event: ready }, + { tab: 'saved', event: savedFailed }, + { tab: 'new', event: savedFailed }, + { tab: 'saved', event: savedRetried }, + { tab: 'new', event: savedRetried }, + { tab: 'saved', event: savedLoaded(2) }, + { tab: 'new', event: savedLoaded(2) } + ])('never moves the customer off $tab on $event.type', ({ tab, event }) => { + const pages = [ + collect('loading', 'loading', tab), + collect('ready', 'ready', tab), + collect('failed', 'failed', tab), + collect('failed', 'ready', tab) + ] + for (const page of pages) { + const next = reduceCheckoutPage(page, event) + expect(next.kind === 'capture' && next.rail).toMatchObject({ tab }) + } + }) +}) + +describe('railView', () => { + it.for<{ + name: string + events: CheckoutPageEvent[] + expected: RailView + }>([ + { + name: '360-4874: the element fails with no saved method', + events: [quoted(0), failed], + expected: { kind: 'column_error' } + }, + { + name: '368-15319: the element fails beside saved methods', + events: [quoted(1), failed, select('new')], + expected: { + kind: 'tabs', + tab: 'new', + element: 'failed', + saved: 'ready' + } + }, + { + name: '368-15319: the element fails while the customer is on Saved', + events: [quoted(1), failed], + expected: { + kind: 'tabs', + tab: 'saved', + element: 'failed', + saved: 'ready' + } + }, + { + name: '368-15401: the saved read fails beside a live element', + events: [quoted('failed'), ready], + expected: { + kind: 'tabs', + tab: 'saved', + element: 'ready', + saved: 'failed' + } + }, + { + name: 'both rails down', + events: [quoted('failed'), failed], + expected: { kind: 'column_error' } + }, + { + name: 'no saved method, element live', + events: [quoted(0), ready], + expected: { kind: 'element_only', element: 'ready' } + }, + { + name: 'a method on file', + events: [quotedOnFile], + expected: { kind: 'on_file' } + } + ])('$name', ({ events, expected }) => { + expect(viewOf(replay(events))).toEqual(expected) + }) }) diff --git a/apps/billing-web/src/checkout/checkoutPage.ts b/apps/billing-web/src/checkout/checkoutPage.ts index 7ec916b1745..d07e689645f 100644 --- a/apps/billing-web/src/checkout/checkoutPage.ts +++ b/apps/billing-web/src/checkout/checkoutPage.ts @@ -2,14 +2,31 @@ import type { CapabilityDenialReason } from '@comfyorg/account-core/billing' type ElementStatus = 'loading' | 'ready' | 'failed' +/** `none` means the read succeeded and the workspace has no saved method. */ +type SavedStatus = 'loading' | 'ready' | 'none' | 'failed' + +export type PaymentTab = 'saved' | 'new' + +/** A saved-methods read as it arrives: how many methods, or that it failed. */ +export type SavedArrival = number | 'failed' + /** * How capture collects the money. `on_file` is a plan change, which the * server charges to the method already on file, so no card form mounts. + * `collect` is a new subscription: the Stripe element and the saved methods + * load independently, and `tab` is only ever changed by the customer. */ type PaymentRail = - | { readonly method: 'new_card'; readonly element: ElementStatus } + | { + readonly method: 'collect' + readonly element: ElementStatus + readonly saved: SavedStatus + readonly tab: PaymentTab + } | { readonly method: 'on_file' } +type CollectRail = Extract + /** The full-page checkout, one state at a time. `resolving` renders the capture skeleton. */ export type CheckoutPage = | { readonly kind: 'resolving' } @@ -20,21 +37,48 @@ export type CheckoutPage = export type CheckoutPageEvent = | { readonly type: 'refused'; readonly reason: CapabilityDenialReason } | { readonly type: 'unavailable'; readonly code: string } - | { readonly type: 'quoted'; readonly method: PaymentRail['method'] } + | { + readonly type: 'quoted' + readonly method: 'collect' + readonly saved: SavedArrival + } + | { readonly type: 'quoted'; readonly method: 'on_file' } | { readonly type: 'elementReady' } | { readonly type: 'elementFailed' } | { readonly type: 'elementRetried' } + | { readonly type: 'savedLoaded'; readonly count: number } + | { readonly type: 'savedFailed' } + | { readonly type: 'savedRetried' } + | { readonly type: 'tabSelected'; readonly tab: PaymentTab } export const RESOLVING: CheckoutPage = { kind: 'resolving' } -function withElement( +/** The first read picks the tab: Saved whenever the tab row shows at all. */ +function arrivedRail(saved: SavedArrival): CollectRail { + if (saved === 'failed') + return { method: 'collect', element: 'loading', saved, tab: 'saved' } + return saved > 0 + ? { method: 'collect', element: 'loading', saved: 'ready', tab: 'saved' } + : { method: 'collect', element: 'loading', saved: 'none', tab: 'new' } +} + +function withCollect( page: CheckoutPage, - from: readonly ElementStatus[], - to: ElementStatus + change: (rail: CollectRail) => CollectRail | undefined ): CheckoutPage { - if (page.kind !== 'capture' || page.rail.method !== 'new_card') return page - if (!from.includes(page.rail.element)) return page - return { kind: 'capture', rail: { method: 'new_card', element: to } } + if (page.kind !== 'capture' || page.rail.method !== 'collect') return page + const next = change(page.rail) + return next === undefined ? page : { ...page, rail: next } +} + +/** + * A re-read that finds nothing hides the tab row, so the one rail left is + * Add new; with methods on file the customer's tab stands. + */ +function settledSaved(rail: CollectRail, count: number): CollectRail { + return count > 0 + ? { ...rail, saved: 'ready' } + : { ...rail, saved: 'none', tab: 'new' } } /** An event that means nothing in the current state returns it untouched. */ @@ -56,21 +100,77 @@ export function reduceCheckoutPage( return { kind: 'capture', rail: - event.method === 'new_card' - ? { method: 'new_card', element: 'loading' } + event.method === 'collect' + ? arrivedRail(event.saved) : { method: 'on_file' } } case 'elementReady': - return withElement(page, ['loading'], 'ready') + return withCollect(page, (rail) => + rail.element === 'loading' ? { ...rail, element: 'ready' } : undefined + ) case 'elementFailed': - return withElement(page, ['loading', 'ready'], 'failed') + return withCollect(page, (rail) => + rail.element === 'failed' ? undefined : { ...rail, element: 'failed' } + ) case 'elementRetried': - return withElement(page, ['failed'], 'loading') + return withCollect(page, (rail) => + rail.element === 'failed' ? { ...rail, element: 'loading' } : undefined + ) + case 'savedLoaded': + return withCollect(page, (rail) => + rail.saved === 'loading' ? settledSaved(rail, event.count) : undefined + ) + case 'savedFailed': + return withCollect(page, (rail) => + rail.saved === 'loading' ? { ...rail, saved: 'failed' } : undefined + ) + case 'savedRetried': + return withCollect(page, (rail) => + rail.saved === 'failed' ? { ...rail, saved: 'loading' } : undefined + ) + case 'tabSelected': + return withCollect(page, (rail) => + rail.saved === 'none' || rail.tab === event.tab + ? undefined + : { ...rail, tab: event.tab } + ) } } -/** Pay waits for the quote and, when a card is collected, for the element to be ready. */ +/** + * What the payment column shows for a rail. With no saved method to fall + * back on, a failed element takes the whole column; otherwise each failure + * stays inside its own tab and the other tab stays live. + */ +export type RailView = + | { readonly kind: 'column_error' } + | { readonly kind: 'on_file' } + | { readonly kind: 'element_only'; readonly element: ElementStatus } + | { + readonly kind: 'tabs' + readonly tab: PaymentTab + readonly element: ElementStatus + readonly saved: Exclude + } + +export function railView(rail: PaymentRail): RailView { + if (rail.method === 'on_file') return { kind: 'on_file' } + const { element, saved, tab } = rail + const savedDown = saved === 'none' || saved === 'failed' + if (element === 'failed' && savedDown) return { kind: 'column_error' } + if (saved === 'none') return { kind: 'element_only', element } + return { kind: 'tabs', tab, element, saved } +} + +/** + * Pay waits for the quote and for the rail the customer is on: the ready + * element on Add new, a loaded list on Saved, nothing for a method on file. + */ export function railAcceptsPay(page: CheckoutPage): boolean { if (page.kind !== 'capture') return false - return page.rail.method === 'on_file' || page.rail.element === 'ready' + const { rail } = page + if (rail.method === 'on_file') return true + return rail.tab === 'saved' + ? rail.saved === 'ready' + : rail.element === 'ready' } diff --git a/apps/billing-web/src/checkout/subscribeRequest.test.ts b/apps/billing-web/src/checkout/subscribeRequest.test.ts index a84edc3d959..959e126f8e5 100644 --- a/apps/billing-web/src/checkout/subscribeRequest.test.ts +++ b/apps/billing-web/src/checkout/subscribeRequest.test.ts @@ -28,6 +28,7 @@ describe('buildSubscribeRequest', () => { plan: string quoted: SubscriptionPreview confirmationToken: string | undefined + savedPaymentMethodId?: string confirmReactivation: boolean returnUrl: string | undefined expected: SubscribeInput @@ -48,6 +49,20 @@ describe('buildSubscribeRequest', () => { return_url: 'https://billing.test/v1/result' } }, + { + name: 'a new subscription charged to a chosen saved method', + arrival, + plan: 'creator_monthly', + quoted: previewOf(), + confirmationToken: undefined, + savedPaymentMethodId: 'pm_saved', + confirmReactivation: false, + returnUrl: undefined, + expected: { + plan_slug: 'creator_monthly', + saved_payment_method_id: 'pm_saved' + } + }, { name: 'a plan change against the saved method, no token', arrival, @@ -144,6 +159,7 @@ describe('buildSubscribeRequest', () => { plan, quoted, confirmationToken, + savedPaymentMethodId, confirmReactivation, returnUrl, expected @@ -154,6 +170,7 @@ describe('buildSubscribeRequest', () => { plan, quoted, confirmationToken, + savedPaymentMethodId, confirmReactivation, returnUrl }) diff --git a/apps/billing-web/src/checkout/subscribeRequest.ts b/apps/billing-web/src/checkout/subscribeRequest.ts index 998309f905f..cb409103943 100644 --- a/apps/billing-web/src/checkout/subscribeRequest.ts +++ b/apps/billing-web/src/checkout/subscribeRequest.ts @@ -34,6 +34,7 @@ export function buildSubscribeRequest({ plan, quoted, confirmationToken, + savedPaymentMethodId, confirmReactivation, returnUrl }: { @@ -41,6 +42,7 @@ export function buildSubscribeRequest({ plan: string quoted: SubscriptionPreview confirmationToken: string | undefined + savedPaymentMethodId?: string confirmReactivation: boolean returnUrl: string | undefined }): SubscribeInput { @@ -49,6 +51,9 @@ export function buildSubscribeRequest({ ...(confirmationToken === undefined ? {} : { confirmation_token: confirmationToken }), + ...(savedPaymentMethodId === undefined + ? {} + : { saved_payment_method_id: savedPaymentMethodId }), ...(arrival.teamCreditStopId === undefined ? {} : { team_credit_stop_id: arrival.teamCreditStopId }), diff --git a/apps/billing-web/src/components/fullPage/CheckoutPaymentColumn.vue b/apps/billing-web/src/components/fullPage/CheckoutPaymentColumn.vue index 5cc9409e6cf..69d075c2744 100644 --- a/apps/billing-web/src/components/fullPage/CheckoutPaymentColumn.vue +++ b/apps/billing-web/src/components/fullPage/CheckoutPaymentColumn.vue @@ -2,15 +2,19 @@ import { computed } from 'vue' import { useI18n } from 'vue-i18n' +import type { SavedPaymentMethod } from '@comfyorg/account-core/billing' import type { StripePaymentCopy, StripePaymentPhase } from '@comfyorg/account-ui/billing/stripe' import { StripePaymentForm } from '@comfyorg/account-ui/billing/stripe' -import type { CheckoutPage } from '@/checkout/checkoutPage' +import type { CheckoutPage, PaymentTab } from '@/checkout/checkoutPage' +import { railView } from '@/checkout/checkoutPage' import CheckoutPayAction from '@/components/fullPage/CheckoutPayAction.vue' import PaymentFormError from '@/components/fullPage/PaymentFormError.vue' +import PaymentTabsRail from '@/components/fullPage/PaymentTabsRail.vue' +import type { PayChoice } from '@/composables/useFullPageCheckout' export interface CheckoutCharge { readonly amountCents: number @@ -18,26 +22,53 @@ export interface CheckoutCharge { readonly paymentMethodConfigurationId: string } -const { page, charge, publishableKey, canPay, submitting, failure } = - defineProps<{ - page: Extract - charge?: CheckoutCharge - publishableKey: string - canPay: boolean - submitting: boolean - failure?: string - }>() +const { + page, + charge, + publishableKey, + canPay, + submitting, + failure, + savedMethods = [] +} = defineProps<{ + page: Extract + charge?: CheckoutCharge + publishableKey: string + canPay: boolean + submitting: boolean + failure?: string + savedMethods?: readonly SavedPaymentMethod[] +}>() const emit = defineEmits<{ phase: [phase: StripePaymentPhase] - pay: [confirmationToken?: string] - retry: [] + pay: [choice: PayChoice] + retryElement: [] + retrySaved: [] + retryColumn: [] + selectTab: [tab: PaymentTab] }>() const { t } = useI18n() +const view = computed(() => + page.kind === 'capture' ? railView(page.rail) : undefined +) + +const tabbed = computed(() => + view.value?.kind === 'tabs' ? view.value : undefined +) + +const elementLive = computed(() => { + const current = view.value + return ( + (current?.kind === 'tabs' || current?.kind === 'element_only') && + current.element !== 'failed' + ) +}) + const copy = computed(() => ({ - paymentMethod: t('checkout.paymentMethod'), + paymentMethod: tabbed.value ? '' : t('checkout.paymentMethod'), methodChoice: t('checkout.methodChoice'), billingAddress: t('checkout.billingAddress'), alipayRenewalNote: t('checkout.alipayRenewalNote'), @@ -49,7 +80,7 @@ const copy = computed(() => ({ diff --git a/apps/billing-web/src/components/fullPage/PaymentFormError.vue b/apps/billing-web/src/components/fullPage/PaymentFormError.vue index 37453a1726a..8611d85f71b 100644 --- a/apps/billing-web/src/components/fullPage/PaymentFormError.vue +++ b/apps/billing-web/src/components/fullPage/PaymentFormError.vue @@ -1,6 +1,15 @@ + + diff --git a/apps/billing-web/src/components/fullPage/PaymentTabsRail.vue b/apps/billing-web/src/components/fullPage/PaymentTabsRail.vue new file mode 100644 index 00000000000..cbd9c661f06 --- /dev/null +++ b/apps/billing-web/src/components/fullPage/PaymentTabsRail.vue @@ -0,0 +1,49 @@ + + + diff --git a/apps/billing-web/src/components/fullPage/SavedMethodPanel.vue b/apps/billing-web/src/components/fullPage/SavedMethodPanel.vue new file mode 100644 index 00000000000..162bab8f423 --- /dev/null +++ b/apps/billing-web/src/components/fullPage/SavedMethodPanel.vue @@ -0,0 +1,67 @@ + + + diff --git a/apps/billing-web/src/composables/useFullPageCheckout.ts b/apps/billing-web/src/composables/useFullPageCheckout.ts index 4d60a8583cd..8c7f06442d2 100644 --- a/apps/billing-web/src/composables/useFullPageCheckout.ts +++ b/apps/billing-web/src/composables/useFullPageCheckout.ts @@ -3,13 +3,19 @@ import { computed, shallowReadonly, shallowRef } from 'vue' import { useBillingClient, useCheckout, + usePaymentMethods, usePreviewSubscribe } from '@comfyorg/account-ui/billing' import type { StripePaymentPhase } from '@comfyorg/account-ui/billing/stripe' import type { BillingEntry } from '@comfyorg/billing-contract' import { buildReturnUrl } from '@comfyorg/billing-contract' -import type { CheckoutPage, CheckoutPageEvent } from '@/checkout/checkoutPage' +import type { + CheckoutPage, + CheckoutPageEvent, + PaymentTab, + SavedArrival +} from '@/checkout/checkoutPage' import { RESOLVING, railAcceptsPay, @@ -26,10 +32,16 @@ import { awaitBillingWebStripeKey } from '@/config/stripeKey' import { useBillingEntry } from '@/entry/billingEntry' import { createDeferredStripeChallengePort } from '@/session/stripeChallengePort' +/** What Pay charges: a new card's token, a saved method, or the method on file. */ +export type PayChoice = + | { readonly confirmationToken: string } + | { readonly savedMethodId: string } + | undefined + /** * The full-page checkout's effects around one `CheckoutPage` state: the - * capabilities read, the quote and the Stripe key on arrival, the payment - * element's readiness, and the subscribe on Pay. Every change goes through + * capabilities read, the quote, the saved methods and the Stripe key on + * arrival, the payment element's readiness, and the subscribe on Pay. Every change goes through * `reduceCheckoutPage`; the quote itself stays with `usePreviewSubscribe`. * The key is awaited because the payment form reads it only when it mounts. */ @@ -38,6 +50,7 @@ export function useFullPageCheckout() { const billedWorkspace = useBilledWorkspace() const { capabilities } = useBillingClient<'capabilities'>(undefined) const { preview, quote } = usePreviewSubscribe() + const saved = usePaymentMethods({ immediate: false }) const checkout = useCheckout({ openUrl: (url) => window.location.assign(url), navigationMode: 'redirect', @@ -52,7 +65,7 @@ export function useFullPageCheckout() { async function resolve(arrival: BillingEntry) { if (arrival.plan === undefined) return - const [allowed, quoted] = await Promise.all([ + const [allowed, quoted, methods] = await Promise.all([ capabilities.read(), quote({ planSlug: arrival.plan, @@ -60,6 +73,7 @@ export function useFullPageCheckout() { ? {} : { teamCreditStopId: arrival.teamCreditStopId }) }), + saved.refresh(), awaitBillingWebStripeKey() ]) if (allowed.status === 'error') { @@ -71,17 +85,42 @@ export function useFullPageCheckout() { }) } else if (quoted.status === 'error') { dispatch({ type: 'unavailable', code: quoted.code }) + } else if (quoted.value.transition_type === 'new_subscription') { + dispatch({ type: 'quoted', method: 'collect', saved: arrivalOf(methods) }) } else { - dispatch({ - type: 'quoted', - method: - quoted.value.transition_type === 'new_subscription' - ? 'new_card' - : 'on_file' - }) + dispatch({ type: 'quoted', method: 'on_file' }) } } + function arrivalOf( + read: Awaited> + ): SavedArrival { + return read.status === 'ok' ? read.value.methods.length : 'failed' + } + + async function retrySaved() { + dispatch({ type: 'savedRetried' }) + const read = await saved.refresh() + const arrival = arrivalOf(read) + dispatch( + arrival === 'failed' + ? { type: 'savedFailed' } + : { type: 'savedLoaded', count: arrival } + ) + } + + /** The whole-column error retries every rail that is down. */ + function retryColumn() { + dispatch({ type: 'elementRetried' }) + const current = page.value + if ( + current.kind === 'capture' && + current.rail.method === 'collect' && + current.rail.saved === 'failed' + ) + void retrySaved() + } + if (entry.value !== undefined) void resolve(entry.value) function onPaymentPhase(phase: StripePaymentPhase) { @@ -119,7 +158,7 @@ export function useFullPageCheckout() { return host ?? planCreditsSettingsUrl(workspace) }) - async function pay(confirmationToken?: string) { + async function pay(choice: PayChoice) { const arrival = entry.value const quoted = preview.value if (arrival?.plan === undefined || !quoted || !canPay.value) return @@ -128,7 +167,13 @@ export function useFullPageCheckout() { arrival, plan: arrival.plan, quoted, - confirmationToken, + confirmationToken: + choice && 'confirmationToken' in choice + ? choice.confirmationToken + : undefined, + ...(choice && 'savedMethodId' in choice + ? { savedPaymentMethodId: choice.savedMethodId } + : {}), confirmReactivation: false, returnUrl: checkoutResultUrl( arrival, @@ -147,7 +192,12 @@ export function useFullPageCheckout() { payFailure, returnLink, onPaymentPhase, + savedMethods: saved.methods, + defaultSavedMethod: saved.defaultMethod, retryElement: () => dispatch({ type: 'elementRetried' }), + retrySaved: () => void retrySaved(), + retryColumn, + selectTab: (tab: PaymentTab) => dispatch({ type: 'tabSelected', tab }), pay } } diff --git a/apps/billing-web/src/locales/en/main.json b/apps/billing-web/src/locales/en/main.json index fa416f09964..cb79f4e7db4 100644 --- a/apps/billing-web/src/locales/en/main.json +++ b/apps/billing-web/src/locales/en/main.json @@ -179,9 +179,30 @@ "terms": "Terms", "privacyPolicy": "Privacy Policy" }, - "formFailed": { - "title": "The payment form couldn't load", - "body": "Nothing has been charged. Check your connection and try again — your order details are unaffected.", + "tabs": { + "saved": "Saved", + "new": "Add new payment" + }, + "saved": { + "label": "Saved payment method", + "change": "Change", + "addNew": "Add new payment method", + "alipay": "Alipay", + "selectLabel": "Choose a saved payment method" + }, + "railFailed": { + "column": { + "title": "The payment form couldn't load", + "body": "Nothing has been charged. Check your connection and try again — your order details are unaffected." + }, + "element": { + "title": "The payment form couldn't load", + "body": "Nothing has been charged. Check your connection and try again, or pay with a saved payment method." + }, + "saved": { + "title": "Your saved payment methods couldn't load", + "body": "Nothing has been charged. Try again, or pay with a new payment method." + }, "retry": "Try again" }, "refused": { diff --git a/apps/billing-web/src/test/fakeBillingClient.ts b/apps/billing-web/src/test/fakeBillingClient.ts index 08a2c4a4a71..257ab5c8d13 100644 --- a/apps/billing-web/src/test/fakeBillingClient.ts +++ b/apps/billing-web/src/test/fakeBillingClient.ts @@ -64,8 +64,8 @@ export interface FakeBillingClientOptions { export interface FakeBillingClient { readonly client: BillingClient readonly readPlans: () => Promise> - readonly readPaymentMethods: () => Promise< - BillingResult + readonly readPaymentMethods: Mock< + () => Promise> > readonly invalidatePaymentMethods: () => void readonly previewSubscribe: Mock @@ -136,7 +136,9 @@ export function createFakeBillingClient( } satisfies BillingResult) : plans ) - const readPaymentMethods = vi.fn(async () => + const readPaymentMethods: Mock< + () => Promise> + > = vi.fn(async () => paymentMethods.status === 'ok' ? ({ status: 'ok', diff --git a/apps/billing-web/src/views/FullPageCheckoutView.test.ts b/apps/billing-web/src/views/FullPageCheckoutView.test.ts index 16701ee254c..8b50d0e7820 100644 --- a/apps/billing-web/src/views/FullPageCheckoutView.test.ts +++ b/apps/billing-web/src/views/FullPageCheckoutView.test.ts @@ -1,8 +1,12 @@ import userEvent from '@testing-library/user-event' import { render, screen, waitFor } from '@testing-library/vue' import { nextTick, ref } from 'vue' +import type { VNode } from 'vue' -import type { PreviewSubscribeResult } from '@comfyorg/account-core/billing' +import type { + PreviewSubscribeResult, + SavedPaymentMethod +} from '@comfyorg/account-core/billing' import type { AccountCredential } from '@comfyorg/account-core/session' import { BILLING_CLIENT_KEY } from '@comfyorg/account-ui/billing' import type { StripePaymentPhase } from '@comfyorg/account-ui/billing/stripe' @@ -72,31 +76,40 @@ const form = vi.hoisted(() => ({ emit: (() => {}) as (event: string, payload: unknown) => void })) -vi.mock(import('@comfyorg/account-ui/billing/stripe'), () => ({ - StripePaymentForm: { - name: 'StripePaymentForm', - props: { - canSubmit: { type: Boolean, default: true }, - isLoading: { type: Boolean, default: false } - }, - emits: ['confirm', 'phase'], - setup( - props: { canSubmit: boolean; isLoading: boolean }, - { - emit, - slots - }: { - emit: (event: string, payload: unknown) => void - slots: { submit?: (slotProps: Record) => unknown } +vi.mock(import('@comfyorg/account-ui/billing/stripe'), async () => { + const { h } = await import('vue') + return { + StripePaymentForm: { + name: 'StripePaymentForm', + props: { + canSubmit: { type: Boolean, default: true }, + isLoading: { type: Boolean, default: false } + }, + emits: ['confirm', 'phase'], + setup( + props: { canSubmit: boolean; isLoading: boolean }, + { + emit, + slots + }: { + emit: (event: string, payload: unknown) => void + slots: { submit?: (slotProps: Record) => VNode[] } + } + ) { + form.mounts += 1 + form.emit = emit + return () => + h( + 'div', + slots.submit?.({ + disabled: !props.canSubmit, + loading: props.isLoading + }) + ) } - ) { - form.mounts += 1 - form.emit = emit - return () => - slots.submit?.({ disabled: !props.canSubmit, loading: props.isLoading }) } } -})) +}) function reportPhase(phase: StripePaymentPhase) { form.emit('phase', phase) @@ -357,3 +370,163 @@ describe('FullPageCheckoutView', () => { expect(assign).toHaveBeenCalledWith(href) }) }) + +const VISA: SavedPaymentMethod = { + id: 'pm_visa', + type: 'card', + brand: 'visa', + last4: '4242', + is_default: false +} +const MASTERCARD: SavedPaymentMethod = { + id: 'pm_mastercard', + type: 'card', + brand: 'mastercard', + last4: '4402', + is_default: true +} + +const tab = (name: 'Saved' | 'Add new payment') => + screen.getByRole('tab', { name }) + +async function renderQuoted(options: FakeBillingClientOptions = {}) { + const fake = await renderCheckout(options) + await screen.findByText('Subscribe to Creator Plan · Acme Team') + return fake +} + +describe('FullPageCheckoutView saved methods and rail failures', () => { + beforeEach(() => { + form.mounts = 0 + }) + + it('opens on Saved with the default method and charges it without a card token', async () => { + const fake = await renderQuoted({ + paymentMethods: { status: 'ok', value: [VISA, MASTERCARD] } + }) + + expect(tab('Saved')).toHaveAttribute('aria-selected', 'true') + expect(screen.getByRole('combobox')).toBeInTheDocument() + await userEvent.click(payButton()) + + await waitFor(() => expect(fake.subscribe).toHaveBeenCalledOnce()) + const request = fake.subscribe.mock.calls[0][0] + expect(request).toMatchObject({ saved_payment_method_id: 'pm_mastercard' }) + expect(request).not.toHaveProperty('confirmation_token') + }) + + it('shows a single saved method as a row, not a picker', async () => { + await renderQuoted({ paymentMethods: { status: 'ok', value: [VISA] } }) + + expect(screen.getByText('visa •••• 4242')).toBeInTheDocument() + expect(screen.queryByRole('combobox')).not.toBeInTheDocument() + }) + + it('keeps the card form mounted across tab switches, so typed details survive', async () => { + const fake = await renderQuoted({ + paymentMethods: { status: 'ok', value: [VISA] } + }) + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + + await userEvent.click(tab('Add new payment')) + await userEvent.click(tab('Saved')) + await userEvent.click(tab('Add new payment')) + form.emit('confirm', 'ctoken_new') + + expect(form.mounts).toBe(1) + await waitFor(() => + expect(fake.subscribe).toHaveBeenCalledWith( + expect.objectContaining({ confirmation_token: 'ctoken_new' }) + ) + ) + expect(fake.subscribe.mock.calls[0][0]).not.toHaveProperty( + 'saved_payment_method_id' + ) + }) + + it('368-15401: a failed saved read errors on Saved only, hides the address, and Try again reads again', async () => { + const fake = await renderQuoted({ + paymentMethods: { status: 'error', code: 'REQUEST_FAILED' } + }) + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + + expect(tab('Saved')).toHaveAttribute('aria-selected', 'true') + expect( + screen.getByText("Your saved payment methods couldn't load") + ).toBeInTheDocument() + expect( + screen.queryByRole('button', { name: 'Pay and subscribe' }) + ).not.toBeInTheDocument() + + await userEvent.click(tab('Add new payment')) + expect(payButton()).toBeEnabled() + + await userEvent.click(tab('Saved')) + fake.readPaymentMethods.mockResolvedValueOnce({ + status: 'ok', + value: { + scope: { userId: 'uid-1', workspaceId: 'ws-1', role: 'owner' }, + methods: [VISA], + readAt: 0 + } + }) + await userEvent.click(screen.getByRole('button', { name: 'Try again' })) + + expect(await screen.findByText('visa •••• 4242')).toBeInTheDocument() + expect(tab('Saved')).toHaveAttribute('aria-selected', 'true') + expect(payButton()).toBeEnabled() + expect(form.mounts).toBe(1) + }) + + it('368-15319: a failed element beside saved methods errors on Add new only, and Saved stays payable', async () => { + await renderQuoted({ paymentMethods: { status: 'ok', value: [VISA] } }) + reportPhase({ + phase: 'payment_element_failed', + element: 'payment', + element_phase: 'mount' + }) + await nextTick() + + expect(tab('Saved')).toHaveAttribute('aria-selected', 'true') + expect(payButton()).toBeEnabled() + + await userEvent.click(tab('Add new payment')) + expect( + screen.getByText( + 'Nothing has been charged. Check your connection and try again, or pay with a saved payment method.' + ) + ).toBeInTheDocument() + + await userEvent.click(screen.getByRole('button', { name: 'Try again' })) + + expect(form.mounts).toBe(2) + expect(tab('Add new payment')).toHaveAttribute('aria-selected', 'true') + expect(payButton()).toBeDisabled() + }) + + it('takes the whole column when both rails are down, and Try again retries both', async () => { + const fake = await renderQuoted({ + paymentMethods: { status: 'error', code: 'REQUEST_FAILED' } + }) + reportPhase({ + phase: 'payment_element_failed', + element: 'payment', + element_phase: 'init' + }) + + expect( + await screen.findByText( + 'Nothing has been charged. Check your connection and try again — your order details are unaffected.' + ) + ).toBeInTheDocument() + expect(screen.queryByRole('tab')).not.toBeInTheDocument() + expect(fake.readPaymentMethods).toHaveBeenCalledOnce() + + await userEvent.click(screen.getByRole('button', { name: 'Try again' })) + + expect(form.mounts).toBe(2) + await waitFor(() => + expect(fake.readPaymentMethods).toHaveBeenCalledTimes(2) + ) + }) +}) diff --git a/apps/billing-web/src/views/FullPageCheckoutView.vue b/apps/billing-web/src/views/FullPageCheckoutView.vue index 759524ed711..cf36831b83a 100644 --- a/apps/billing-web/src/views/FullPageCheckoutView.vue +++ b/apps/billing-web/src/views/FullPageCheckoutView.vue @@ -27,7 +27,11 @@ const { payFailure, returnLink, onPaymentPhase, + savedMethods, retryElement, + retrySaved, + retryColumn, + selectTab, pay } = useFullPageCheckout() @@ -122,9 +126,13 @@ function returnToProduct() { :can-pay="canPay" :submitting :failure="payFailureCopy" + :saved-methods="savedMethods" @phase="onPaymentPhase" @pay="pay" - @retry="retryElement" + @retry-element="retryElement" + @retry-saved="retrySaved" + @retry-column="retryColumn" + @select-tab="selectTab" /> From 69eca0f45b9539af2837b477092030a28bcd6593 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Mon, 28 Sep 2026 19:14:38 -0400 Subject: [PATCH 3/4] style(billing-web): give the payment-rail failure card the shared notice style --- .../src/components/fullPage/PaymentFormError.vue | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/billing-web/src/components/fullPage/PaymentFormError.vue b/apps/billing-web/src/components/fullPage/PaymentFormError.vue index 8611d85f71b..f1e90ae08a6 100644 --- a/apps/billing-web/src/components/fullPage/PaymentFormError.vue +++ b/apps/billing-web/src/components/fullPage/PaymentFormError.vue @@ -17,9 +17,12 @@ const { t } = useI18n()