From be362682d1576b765ba74f1915c63d8f782e5ec5 Mon Sep 17 00:00:00 2001 From: Dante Date: Tue, 29 Sep 2026 19:15:29 +0000 Subject: [PATCH] FE-3056 fix(billing-web): re-quote a stale subscribe quote the way the app does (#19126) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Linear: FE-3056 When the server refuses a subscribe because the quote went stale, the embedded checkout re-quotes and tells the customer "Your quote changed. Review the updated amount and try again." billing-web instead shows "We couldn't reach the billing service. Please try again." and keeps the old quote, so every retry is refused the same way. Seen live on testcloud with a Team monthly → annual change (`POST /api/billing/subscribe` → 400 `SUBSCRIPTION_QUOTE_STALE`). `issueSubscribe` in `packages/account-core/src/core/billing/subscriptionCommands.ts` maps only `REACTIVATION_CONFIRMATION_REQUIRED`; any other coded 4xx reaches the host as a generic failure. `CheckoutView.pay` in billing-web has no branch for a stale quote, so the failure copy is the transport one. The app's legacy rail handles the code in `useSubscriptionCheckout.recoverStaleQuote`. - account-core: `SUBSCRIPTION_QUOTE_STALE` becomes the command code `QUOTE_STALE`, the same way the reactivation code is mapped. - billing-web: on `QUOTE_STALE`, re-quote the plan and show the app's copy ("Your quote changed. Review the updated amount and try again."); if the re-quote itself fails, show the app's "quote refresh failed" copy. - Tests: an account-core case for the mapping and a CheckoutView case for the re-quote and copy. Both fail before the fix. - Red: the two new tests fail locally on edd132405d (the CI run for that commit was cancelled by the fix push); green: CI on 2ca20d8d35. The stale quotes themselves come from the server (non-deterministic `renewal_at` on preview for a monthly → annual change); that was reported to backend separately. **AS IS:** billing-web monthly → annual shows "We couldn't reach the billing service" (server 400 `SUBSCRIPTION_QUOTE_STALE`); the same change in the embedded checkout then goes through. https://github.com/user-attachments/assets/b31ba7c9-0db0-4a84-a329-804ed28dac4f **TO BE:** Unit and view tests (CheckoutView re-quotes and shows "Your quote changed. Review the updated amount and try again."). The stale quote is intermittent server-side, so a live TO BE capture is pending the backend fix. | PR | Title | |---|---| | #19097 | FE-3045 drop the billing-web plan grid | | #19098 | FE-3046 A buttonVariants into design-system | | #19099 | FE-3046 B account-ui checkout surfaces | | #19100 | FE-3046 C app renders account-ui checkout | | #19101 | FE-3046 D billing-web shared checkout | | #19104 | FE-3046 E invite from the success step | | #19105 | FE-3047 F clear decline toasts on success | | #19106 | FE-3048 G return closes the tab | | #19115 | FE-3050 H CTA reachable | | **this PR** | **FE-3056 I stale quote re-quoted** | ```mermaid graph LR P97[#19097] --> A[#19098] --> B[#19099] --> C[#19100] --> D[#19101] --> E[#19104] --> F[#19105] --> G[#19106] --> H[#19115] --> I[FE-3056]:::me classDef me fill:#f9d71c,stroke:#333,color:#000 ``` --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> (cherry picked from commit e352169b7bf4f5a79a9dd148d981cb1b41542576) --- apps/billing-web/src/locales/en/main.json | 4 +- .../src/views/CheckoutView.test.ts | 66 +++++++++++++++++++ apps/billing-web/src/views/CheckoutView.vue | 11 +++- .../core/billing/subscriptionCommands.test.ts | 22 +++++++ .../src/core/billing/subscriptionCommands.ts | 18 +++-- 5 files changed, 114 insertions(+), 7 deletions(-) diff --git a/apps/billing-web/src/locales/en/main.json b/apps/billing-web/src/locales/en/main.json index 8708fdc3511..db401bfee7c 100644 --- a/apps/billing-web/src/locales/en/main.json +++ b/apps/billing-web/src/locales/en/main.json @@ -226,7 +226,9 @@ "REACTIVATION_CONFIRMATION_REQUIRED": "Confirm the reactivation charge before continuing.", "CONFLICTING_PAYMENT_METHOD": "That request named two payment methods; choose one.", "MISSING_PAYMENT_METHOD_URL": "The billing service asked for a payment step but didn't say where.", - "OPERATION_ALREADY_PENDING": "A payment you started earlier is still going through. It has to finish before you can choose a different plan." + "OPERATION_ALREADY_PENDING": "A payment you started earlier is still going through. It has to finish before you can choose a different plan.", + "QUOTE_STALE": "Your quote changed. Review the updated amount and try again.", + "QUOTE_REFRESH_FAILED": "Your quote expired and couldn't be refreshed. Choose your plan again." }, "tier": { "unknown": "Plan", diff --git a/apps/billing-web/src/views/CheckoutView.test.ts b/apps/billing-web/src/views/CheckoutView.test.ts index 864f648b4a3..9361b594f4e 100644 --- a/apps/billing-web/src/views/CheckoutView.test.ts +++ b/apps/billing-web/src/views/CheckoutView.test.ts @@ -936,6 +936,72 @@ describe('CheckoutView', () => { ) }) + it('re-quotes a stale quote and pays against the replacement', async () => { + const fake = await renderCheckout(CHECKOUT_PATH, { + preview: { status: 'ok', value: upgradeQuote() } + }) + fake.subscribe.mockResolvedValueOnce({ + status: 'error', + code: 'QUOTE_STALE' + }) + fake.previewSubscribe.mockResolvedValueOnce({ + status: 'ok', + value: upgradeQuote({ + quote_id: 'q_2', + quote_version: 4, + amount_due_cents: 3100 + }) + }) + + await userEvent.click( + await screen.findByRole('button', { name: 'Confirm upgrade' }) + ) + + expect(await screen.findByRole('alert')).toHaveTextContent( + 'Your quote changed. Review the updated amount and try again.' + ) + expect(fake.previewSubscribe).toHaveBeenCalledTimes(2) + expect(await screen.findByText('$31.00')).toBeInTheDocument() + expect(screen.queryByText('$28.00')).not.toBeInTheDocument() + + await userEvent.click( + screen.getByRole('button', { name: 'Confirm upgrade' }) + ) + + await waitFor(() => expect(fake.subscribe).toHaveBeenCalledTimes(2)) + expect(fake.subscribe).toHaveBeenLastCalledWith( + expect.objectContaining({ quote_id: 'q_2', quote_version: 4 }) + ) + expect(fake.subscribe).not.toHaveBeenLastCalledWith( + expect.objectContaining({ quote_id: 'q_1' }) + ) + }) + + it('keeps Confirm closed on the stale quote when its refresh fails', async () => { + const fake = await renderCheckout(CHECKOUT_PATH, { + preview: { status: 'ok', value: upgradeQuote() } + }) + fake.subscribe.mockResolvedValueOnce({ + status: 'error', + code: 'QUOTE_STALE' + }) + fake.previewSubscribe.mockResolvedValueOnce({ + status: 'error', + code: 'REQUEST_FAILED' + }) + + await userEvent.click( + await screen.findByRole('button', { name: 'Confirm upgrade' }) + ) + + expect(await screen.findByRole('alert')).toHaveTextContent( + "Your quote expired and couldn't be refreshed. Choose your plan again." + ) + expect( + screen.getByRole('button', { name: 'Confirm upgrade' }) + ).toBeDisabled() + }) + it('tells the customer when the subscribe itself was refused and keeps the form', async () => { await renderCheckout(CHECKOUT_PATH, { subscribe: { status: 'error', code: 'REQUEST_FAILED' } diff --git a/apps/billing-web/src/views/CheckoutView.vue b/apps/billing-web/src/views/CheckoutView.vue index f08bd9d2716..bdf557bc9e9 100644 --- a/apps/billing-web/src/views/CheckoutView.vue +++ b/apps/billing-web/src/views/CheckoutView.vue @@ -58,7 +58,7 @@ import { createDeferredStripeChallengePort } from '@/session/stripeChallengePort import { useWorkspaceInvites } from '@/session/workspaceInvites' const { locale, t } = useI18n() -const { refusal } = useHostedCopy() +const { coded, refusal } = useHostedCopy() const { copy, successCopy, inviteCopy, tierName } = useCheckoutCopy() const invites = useWorkspaceInvites() const { entry } = useBillingEntry() @@ -504,6 +504,15 @@ async function pay(choice: PaymentChoice) { await quotePlan(planSlug.value, teamCreditStopId.value) return } + if (result.code === 'QUOTE_STALE') { + quoteIsCurrent.value = false + const requoted = await quotePlan(planSlug.value, teamCreditStopId.value) + submitFailure.value = coded( + 'failure', + requoted?.status === 'ok' ? 'QUOTE_STALE' : 'QUOTE_REFRESH_FAILED' + ) + return + } submitFailure.value = refusal(result) } diff --git a/packages/account-core/src/core/billing/subscriptionCommands.test.ts b/packages/account-core/src/core/billing/subscriptionCommands.test.ts index f6f7b331872..271138cdc2e 100644 --- a/packages/account-core/src/core/billing/subscriptionCommands.test.ts +++ b/packages/account-core/src/core/billing/subscriptionCommands.test.ts @@ -1170,6 +1170,28 @@ describe('createBillingCommands', () => { expect(h.readCredits).not.toHaveBeenCalled() }) + it.for([ + { httpStatus: 400, expected: 'QUOTE_STALE' }, + { httpStatus: 503, expected: 'REQUEST_FAILED' } + ])( + 'a subscribe answered $httpStatus SUBSCRIPTION_QUOTE_STALE is $expected', + async ({ httpStatus, expected }) => { + const h = harness({ + status: FREE, + script: { + [POST_SUBSCRIBE]: [ + serverError(httpStatus, 'SUBSCRIPTION_QUOTE_STALE') + ] + } + }) + + await expect(h.commands.subscribe(PLAN)).resolves.toMatchObject({ + status: 'error', + code: expected + }) + } + ) + it('surfaces REACTIVATION_CONFIRMATION_REQUIRED for the host to re-preview', async () => { const h = harness({ status: FREE, diff --git a/packages/account-core/src/core/billing/subscriptionCommands.ts b/packages/account-core/src/core/billing/subscriptionCommands.ts index 36ed235bd72..70fd4c48b08 100644 --- a/packages/account-core/src/core/billing/subscriptionCommands.ts +++ b/packages/account-core/src/core/billing/subscriptionCommands.ts @@ -54,6 +54,7 @@ const REACTIVATION_CONFIRMATION_REQUIRED_SERVER_CODE = const NOT_SCHEDULED_FOR_CANCELLATION_SERVER_CODE = 'NOT_SCHEDULED_FOR_CANCELLATION' const ALREADY_CANCELED_SERVER_CODE = 'ALREADY_CANCELED' +const SUBSCRIPTION_QUOTE_STALE_SERVER_CODE = 'SUBSCRIPTION_QUOTE_STALE' const SUBSCRIPTION_CHANGE_IN_PROGRESS_SERVER_CODE = 'SUBSCRIPTION_CHANGE_IN_PROGRESS' @@ -75,6 +76,8 @@ export type SubscriptionCommandCode = | 'NO_ACTIVE_SUBSCRIPTION' /** The server asked for a hosted payment step but offered no page for it. */ | 'MISSING_PAYMENT_METHOD_URL' + /** The quote no longer matches what the server would charge; re-preview. */ + | 'QUOTE_STALE' export type SubscriptionCommandFailure = | BillingFailure @@ -362,11 +365,16 @@ export function createBillingCommands( key ) if (response.status === 'error') { - return matchesServerCode( - response, - REACTIVATION_CONFIRMATION_REQUIRED_SERVER_CODE - ) - ? coded('REACTIVATION_CONFIRMATION_REQUIRED') + if ( + matchesServerCode( + response, + REACTIVATION_CONFIRMATION_REQUIRED_SERVER_CODE + ) + ) { + return coded('REACTIVATION_CONFIRMATION_REQUIRED') + } + return refusedWith(response, SUBSCRIPTION_QUOTE_STALE_SERVER_CODE) + ? coded('QUOTE_STALE') : refusedWhilePending(response) } const { billing_op_id, status, payment_method_url } = response.value.data