Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/billing-web/src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
66 changes: 66 additions & 0 deletions apps/billing-web/src/views/CheckoutView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
11 changes: 10 additions & 1 deletion apps/billing-web/src/views/CheckoutView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions packages/account-core/src/core/billing/subscriptionCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading