From 2502db645744a8dbaf3675c9ff3d1a2c4ef2a542 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 30 Sep 2026 06:22:28 -0400 Subject: [PATCH] fix(billing-web): restore a promo this page applied, and keep discount rows in server order A code applied on the checkout page is kept in sessionStorage, tagged with the checkout it belongs to (workspace, product, plan, team stop), so a reload or a provider return re-applies it through the capture quote; a refusal lands on the existing promo-expired card. It never reaches a URL. Removing the code, an expiry, a finished checkout, or another checkout clears it. The summary renders every promotion row in the order the quote lists them, dropping the separate entered-code slot and the never-populated Subtotal. --- apps/billing-web/e2e/full-page-promo.spec.ts | 106 ++++++++++++++ .../src/checkout/promoEntry.test.ts | 15 ++ apps/billing-web/src/checkout/promoEntry.ts | 10 +- .../src/checkout/promoMemory.test.ts | 128 +++++++++++++++++ apps/billing-web/src/checkout/promoMemory.ts | 108 ++++++++++++++ .../src/checkout/summaryLedger.test.ts | 85 +++++------ .../billing-web/src/checkout/summaryLedger.ts | 56 +++----- .../fullPage/CheckoutSummaryColumn.test.ts | 52 +++---- .../fullPage/summary/CheckoutLedger.vue | 28 +--- .../src/composables/useCheckoutPromo.test.ts | 3 +- .../src/composables/useCheckoutPromo.ts | 17 ++- .../src/composables/useFullPageCheckout.ts | 14 ++ apps/billing-web/src/locales/en/main.json | 1 - .../src/views/FullPageCheckoutView.test.ts | 135 ++++++++++++++++++ 14 files changed, 616 insertions(+), 142 deletions(-) create mode 100644 apps/billing-web/src/checkout/promoMemory.test.ts create mode 100644 apps/billing-web/src/checkout/promoMemory.ts diff --git a/apps/billing-web/e2e/full-page-promo.spec.ts b/apps/billing-web/e2e/full-page-promo.spec.ts index 7a6278c8c9e..36d8dd88095 100644 --- a/apps/billing-web/e2e/full-page-promo.spec.ts +++ b/apps/billing-web/e2e/full-page-promo.spec.ts @@ -223,3 +223,109 @@ test('a code that lapses before Pay returns to capture with the expired card', a ).toBeEnabled() await expect(page.getByRole('link', { name: 'Contact support' })).toBeHidden() }) + +test('a code applied here survives a reload, still applied at the discounted price', async ({ + page, + cloud, + signIn +}) => { + quoteCodes(cloud) + await signIn(CHECKOUT) + await enterCode(page, 'LAUNCH20') + await expect(summary(page)).toContainText('Total due today$40.00') + + await page.reload() + + await expect(summary(page)).toContainText('Promo code−$10.00') + await expect(summary(page)).toContainText('Total due today$40.00') + await expect( + page.getByRole('button', { name: 'Remove LAUNCH20' }) + ).toBeVisible() +}) + +test('a restored code the server now refuses shows the expired card at the full price', async ({ + page, + cloud, + signIn +}) => { + let stillValid = true + quoteCodes(cloud, () => stillValid) + await signIn(CHECKOUT) + await enterCode(page, 'LAUNCH20') + await expect(summary(page)).toContainText('Total due today$40.00') + stillValid = false + + await page.reload() + + const card = page.getByRole('alert') + await expect(card).toContainText('Your promo code expired') + await expect(card).toContainText( + 'The LAUNCH20 code expired, so the total was updated.' + ) + await expect(summary(page)).toContainText('Total due today$50.00') + await expect( + page.getByRole('button', { name: 'Add promo code' }) + ).toBeEnabled() +}) + +test('a copied checkout URL carries no applied code', async ({ + page, + context, + cloud, + signIn +}) => { + quoteCodes(cloud) + await signIn(CHECKOUT) + await enterCode(page, 'LAUNCH20') + await expect(summary(page)).toContainText('Total due today$40.00') + + const copied = page.url() + expect(copied).not.toMatch(/launch20/i) + const pasted = await context.newPage() + await pasted.goto(copied) + + await expect(pasted.getByText(EYEBROW)).toBeVisible() + await expect(summary(pasted)).toContainText('Total due today$50.00') + await expect( + pasted.getByRole('button', { name: 'Add promo code' }) + ).toBeVisible() +}) + +test('discount rows follow the server order, the entered code first when it comes first', async ({ + page, + cloud, + signIn +}) => { + cloud.reply('POST', '/billing/preview-subscribe', ({ body }) => { + const entered = + typeof body === 'object' && body !== null && 'promotion_code' in body + if (!entered) return { body: cloud.scenario.preview } + return { + body: { + ...cloud.scenario.preview, + quote_id: 'quote_launch20', + amount_due_cents: 3000, + promotion_code: 'LAUNCH20', + discounts: [ + { kind: 'promotion', code: 'LAUNCH20', amount_off_cents: 1000 }, + { + kind: 'promotion', + code: 'COMFY-EDU', + name: 'Education discount', + amount_off_cents: 1000 + } + ] + } + } + }) + await signIn(CHECKOUT) + + await enterCode(page, 'LAUNCH20') + + await expect(summary(page)).toContainText( + 'Promo code−$10.00Education discount−$10.00' + ) + await expect( + page.getByRole('button', { name: 'Remove LAUNCH20' }) + ).toBeVisible() +}) diff --git a/apps/billing-web/src/checkout/promoEntry.test.ts b/apps/billing-web/src/checkout/promoEntry.test.ts index 6abf4dc6fdf..d2a7d3c5c9c 100644 --- a/apps/billing-web/src/checkout/promoEntry.test.ts +++ b/apps/billing-web/src/checkout/promoEntry.test.ts @@ -38,6 +38,21 @@ describe('reducePromoEntry', () => { events: [], expected: editing('LAUNCH20') }, + { + name: 'a code this page applied before a reload comes back applied', + start: initialPromoEntry(undefined, 'LAUNCH20'), + events: [], + expected: applied('LAUNCH20') + }, + { + name: 'a code this page applied outranks the link code, typed or unreadable', + start: initialPromoEntry( + { promotionCode: 'OTHER', unreadablePromotionCode: 'SAVE 20' }, + 'LAUNCH20' + ), + events: [], + expected: applied('LAUNCH20') + }, { name: 'no URL code starts collapsed', start: initialPromoEntry(undefined), diff --git a/apps/billing-web/src/checkout/promoEntry.ts b/apps/billing-web/src/checkout/promoEntry.ts index d9bf84b6530..6bb60a5d444 100644 --- a/apps/billing-web/src/checkout/promoEntry.ts +++ b/apps/billing-web/src/checkout/promoEntry.ts @@ -51,12 +51,16 @@ export type PromoPrefill = Pick< > /** - * A code carried in on the entry URL opens the field with it typed, never - * applied. One the link could not carry opens refused, as typed. + * A code this page applied before a reload or a provider return comes back + * applied, for the capture read to price again. A code carried in on the + * entry URL opens the field with it typed, never applied. One the link could + * not carry opens refused, as typed. */ export function initialPromoEntry( - prefill: PromoPrefill | undefined + prefill: PromoPrefill | undefined, + restored?: string ): PromoEntry { + if (restored !== undefined) return { kind: 'applied', code: restored } if (prefill?.promotionCode !== undefined) return { kind: 'editing', draft: prefill.promotionCode } const unreadable = prefill?.unreadablePromotionCode diff --git a/apps/billing-web/src/checkout/promoMemory.test.ts b/apps/billing-web/src/checkout/promoMemory.test.ts new file mode 100644 index 00000000000..4893f36feba --- /dev/null +++ b/apps/billing-web/src/checkout/promoMemory.test.ts @@ -0,0 +1,128 @@ +import type { PromoEntry } from '@/checkout/promoEntry' +import { + checkoutIdentity, + codeToRemember, + createPromoMemory +} from '@/checkout/promoMemory' + +const PRO = checkoutIdentity( + { product: 'comfyui', plan: 'pro_monthly' }, + 'ws-team' +) + +afterEach(() => { + sessionStorage.clear() +}) + +describe('checkoutIdentity', () => { + it.for<{ + name: string + other: Parameters + }>([ + { + name: 'another workspace', + other: [{ product: 'comfyui', plan: 'pro_monthly' }, 'ws-other'] + }, + { + name: 'another plan', + other: [{ product: 'comfyui', plan: 'creator_monthly' }, 'ws-team'] + }, + { + name: 'another team commitment', + other: [ + { product: 'comfyui', plan: 'pro_monthly', teamCreditStopId: 'stop_2' }, + 'ws-team' + ] + }, + { + name: 'an unbound workspace', + other: [{ product: 'comfyui', plan: 'pro_monthly' }, undefined] + } + ])('names $name as a different checkout', ({ other }) => { + expect(checkoutIdentity(...other)).not.toBe(PRO) + }) +}) + +describe('codeToRemember', () => { + it.for<{ entry: PromoEntry; code: string | undefined }>([ + { entry: { kind: 'applied', code: 'LAUNCH20' }, code: 'LAUNCH20' }, + { entry: { kind: 'removing', code: 'LAUNCH20' }, code: 'LAUNCH20' }, + { entry: { kind: 'idle' }, code: undefined }, + { entry: { kind: 'editing', draft: 'LAUNCH20' }, code: undefined }, + { entry: { kind: 'applying', draft: 'LAUNCH20' }, code: undefined }, + { + entry: { kind: 'rejected', draft: 'LAUNCH20', reason: 'invalid' }, + code: undefined + } + ])('$entry.kind remembers $code', ({ entry, code }) => { + expect(codeToRemember(entry)).toBe(code) + }) +}) + +describe('createPromoMemory', () => { + it('recalls the code this checkout kept, across a fresh memory', () => { + createPromoMemory(() => PRO).keep('LAUNCH20') + + expect(createPromoMemory(() => PRO).recall()).toBe('LAUNCH20') + }) + + it('forgets the code once it is kept as nothing', () => { + const memory = createPromoMemory(() => PRO) + memory.keep('LAUNCH20') + + memory.keep(undefined) + + expect(createPromoMemory(() => PRO).recall()).toBeUndefined() + }) + + it('gives another checkout nothing, and drops the stale code', () => { + createPromoMemory(() => PRO).keep('LAUNCH20') + const other = checkoutIdentity( + { product: 'comfyui', plan: 'creator_monthly' }, + 'ws-team' + ) + + expect(createPromoMemory(() => other).recall()).toBeUndefined() + expect(createPromoMemory(() => PRO).recall()).toBeUndefined() + }) + + it.for([ + 'not json', + '{"identity":1,"code":"LAUNCH20"}', + '{"code":"LAUNCH20"}', + 'null' + ])('reads a malformed record %s as nothing', (raw) => { + sessionStorage.setItem('comfy.billing-web.checkout-promo.v1', raw) + + expect(createPromoMemory(() => PRO).recall()).toBeUndefined() + }) + + it('works as an empty memory where storage throws', () => { + const blocked = () => { + throw new DOMException('blocked', 'SecurityError') + } + const memory = createPromoMemory(() => PRO, blocked) + + expect(() => memory.keep('LAUNCH20')).not.toThrow() + expect(memory.recall()).toBeUndefined() + }) + + it('survives a storage that refuses writes', () => { + const full: Storage = Object.assign(Object.create(sessionStorage), { + getItem: () => null, + setItem: () => { + throw new DOMException('full', 'QuotaExceededError') + }, + removeItem: () => { + throw new DOMException('full', 'QuotaExceededError') + } + }) + const memory = createPromoMemory( + () => PRO, + () => full + ) + + expect(() => memory.keep('LAUNCH20')).not.toThrow() + expect(() => memory.keep(undefined)).not.toThrow() + }) +}) diff --git a/apps/billing-web/src/checkout/promoMemory.ts b/apps/billing-web/src/checkout/promoMemory.ts new file mode 100644 index 00000000000..5e7a311b0e6 --- /dev/null +++ b/apps/billing-web/src/checkout/promoMemory.ts @@ -0,0 +1,108 @@ +/** + * The promo code this page applied, kept in the tab's sessionStorage so a + * reload or a return from a provider's site re-applies it. It is never + * written to a URL, so a copied link carries only what the link itself did. + * One record at a time, tagged with the checkout it belongs to: another + * checkout reads nothing and drops it. + */ +import type { BillingEntry } from '@comfyorg/billing-contract' + +import type { PromoEntry } from '@/checkout/promoEntry' + +const STORAGE_KEY = 'comfy.billing-web.checkout-promo.v1' + +/** Who pays, and for what: a code applied to one checkout belongs to it alone. */ +export function checkoutIdentity( + entry: + | Pick + | undefined, + workspaceId: string | undefined +): string { + return JSON.stringify([ + workspaceId ?? null, + entry?.product ?? null, + entry?.plan ?? null, + entry?.teamCreditStopId ?? null + ]) +} + +/** A code the server priced for this page, until the customer takes it off. */ +export function codeToRemember(entry: PromoEntry): string | undefined { + return entry.kind === 'applied' || entry.kind === 'removing' + ? entry.code + : undefined +} + +interface Remembered { + readonly identity: string + readonly code: string +} + +function isRemembered(value: unknown): value is Remembered { + return ( + typeof value === 'object' && + value !== null && + 'identity' in value && + 'code' in value && + typeof value.identity === 'string' && + typeof value.code === 'string' + ) +} + +function parseRemembered(raw: string | null): Remembered | undefined { + if (raw === null) return undefined + try { + const value: unknown = JSON.parse(raw) + return isRemembered(value) ? value : undefined + } catch { + return undefined + } +} + +export interface PromoMemory { + readonly recall: () => string | undefined + /** Nothing forgets the code. */ + readonly keep: (code: string | undefined) => void +} + +/** A tab without storage, or one that refuses it, remembers nothing and still checks out. */ +function withStorage( + storage: () => Storage, + use: (store: Storage) => T, + fallback: T +): T { + try { + return use(storage()) + } catch { + return fallback + } +} + +export function createPromoMemory( + identity: () => string, + storage: () => Storage = () => globalThis.sessionStorage +): PromoMemory { + const forget = () => + withStorage(storage, (store) => store.removeItem(STORAGE_KEY), undefined) + return { + recall: () => { + const remembered = withStorage( + storage, + (store) => parseRemembered(store.getItem(STORAGE_KEY)), + undefined + ) + if (remembered?.identity === identity()) return remembered.code + if (remembered !== undefined) forget() + return undefined + }, + keep: (code) => { + if (code === undefined) return forget() + const record: Remembered = { identity: identity(), code } + withStorage( + storage, + (store) => store.setItem(STORAGE_KEY, JSON.stringify(record)), + undefined + ) + } + } +} diff --git a/apps/billing-web/src/checkout/summaryLedger.test.ts b/apps/billing-web/src/checkout/summaryLedger.test.ts index 2e02fc9f66d..8a716dc31b3 100644 --- a/apps/billing-web/src/checkout/summaryLedger.test.ts +++ b/apps/billing-web/src/checkout/summaryLedger.test.ts @@ -79,7 +79,7 @@ describe('buildSummaryLedger', () => { sublines: ['$700 /mo, billed monthly'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$700.00', @@ -110,7 +110,7 @@ describe('buildSummaryLedger', () => { sublines: ['Billed yearly'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$7,560.00', @@ -135,7 +135,7 @@ describe('buildSummaryLedger', () => { headline: { amount: '$0', currency: 'USD' }, credits: { count: '1,772,400', qualifier: 'credits per year' }, items: [], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$0.00', @@ -177,7 +177,7 @@ describe('buildSummaryLedger', () => { ] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$32.50', @@ -218,7 +218,7 @@ describe('buildSummaryLedger', () => { ] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$32.50', @@ -253,7 +253,7 @@ describe('buildSummaryLedger', () => { sublines: ['Billed yearly', 'Credits refill to 88,620 each year'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$336.00', @@ -290,7 +290,7 @@ describe('buildSummaryLedger', () => { sublines: ['Billed yearly', 'Credits refill to 253,200 each year'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$960.00', @@ -330,7 +330,7 @@ describe('buildSummaryLedger', () => { sublines: ['Starts July 28, 2026, billed monthly'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: false, total: '$0.00', @@ -366,7 +366,7 @@ describe('buildSummaryLedger', () => { sublines: ['Starts June 28, 2027, billed monthly'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: false, total: '$0.00', @@ -402,7 +402,7 @@ describe('buildSummaryLedger', () => { sublines: ['Starts July 28, 2026, billed monthly'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: false, total: '$0.00', @@ -441,7 +441,7 @@ describe('buildSummaryLedger', () => { ] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$123.45', @@ -586,7 +586,7 @@ function discountSlotsOf( promotionCode?: string, amountDueCents = 0 ) { - const { adjustments, subtotal, promo, chips } = ledgerOf({ + const ledger = ledgerOf({ transition_type: 'new_subscription', amount_due_cents: amountDueCents, cost_today_cents: 756_000, @@ -594,7 +594,7 @@ function discountSlotsOf( discounts, ...(promotionCode === undefined ? {} : { promotion_code: promotionCode }) }) - return { adjustments, subtotal, promo, chips } + return { discounts: ledger.discounts, chips: ledger.chips } } describe('buildSummaryLedger discounts', () => { @@ -609,21 +609,14 @@ describe('buildSummaryLedger discounts', () => { name: 'a catalog coupon folds into the price: no row, no chip', discounts: [ANNUAL_RATE], amountDueCents: 756_000, - slots: { - adjustments: [], - subtotal: undefined, - promo: undefined, - chips: [] - } + slots: { discounts: [], chips: [] } }, { name: 'an unnamed entered code reads "Promo code"; the code sits only on its removable chip', discounts: [ANNUAL_RATE, entered('COMFYFREE', 756_000)], promotionCode: 'COMFYFREE', slots: { - adjustments: [], - subtotal: undefined, - promo: { label: 'Promo code', amount: '−$7,560.00' }, + discounts: [{ label: 'Promo code', amount: '−$7,560.00' }], chips: [{ code: 'COMFYFREE', removable: true }] } }, @@ -633,9 +626,7 @@ describe('buildSummaryLedger discounts', () => { promotionCode: 'LAUNCH20', amountDueCents: 604_800, slots: { - adjustments: [], - subtotal: undefined, - promo: { label: 'Launch week', amount: '−$1,512.00' }, + discounts: [{ label: 'Launch week', amount: '−$1,512.00' }], chips: [{ code: 'LAUNCH20', removable: true }] } }, @@ -645,32 +636,29 @@ describe('buildSummaryLedger discounts', () => { promotionCode: 'LAUNCH20', amountDueCents: 604_800, slots: { - adjustments: [], - subtotal: undefined, - promo: { label: 'Promo code', amount: '−$1,512.00' }, + discounts: [{ label: 'Promo code', amount: '−$1,512.00' }], chips: [{ code: 'LAUNCH20', removable: true }] } }, { - name: 'a held discount is a pre-applied row whose chip has no remove', + name: 'a held discount is a row whose chip has no remove', discounts: [EDUCATION], amountDueCents: 604_800, slots: { - adjustments: [{ label: 'Education discount', amount: '−$1,512.00' }], - subtotal: undefined, - promo: undefined, + discounts: [{ label: 'Education discount', amount: '−$1,512.00' }], chips: [{ code: 'COMFY-EDU', removable: false }] } }, { - name: 'a held discount before an entered code still leaves Subtotal out: the quote reports no pre-discount base', + name: 'a held discount the server lists first renders first', discounts: [ANNUAL_RATE, EDUCATION, entered('COMFY50', 302_400)], promotionCode: 'COMFY50', amountDueCents: 302_400, slots: { - adjustments: [{ label: 'Education discount', amount: '−$1,512.00' }], - subtotal: undefined, - promo: { label: 'Promo code', amount: '−$3,024.00' }, + discounts: [ + { label: 'Education discount', amount: '−$1,512.00' }, + { label: 'Promo code', amount: '−$3,024.00' } + ], chips: [ { code: 'COMFY-EDU', removable: false }, { code: 'COMFY50', removable: true } @@ -678,14 +666,31 @@ describe('buildSummaryLedger discounts', () => { } }, { - name: 'an entered code with no reported amount keeps its row and no Subtotal', + name: 'an entered code the server lists first renders first', + discounts: [entered('COMFY50', 302_400), ANNUAL_RATE, EDUCATION], + promotionCode: 'COMFY50', + amountDueCents: 302_400, + slots: { + discounts: [ + { label: 'Promo code', amount: '−$3,024.00' }, + { label: 'Education discount', amount: '−$1,512.00' } + ], + chips: [ + { code: 'COMFY-EDU', removable: false }, + { code: 'COMFY50', removable: true } + ] + } + }, + { + name: 'an entered code with no reported amount keeps its row, amount left out', discounts: [EDUCATION, entered('COMFY50')], promotionCode: 'COMFY50', amountDueCents: 302_400, slots: { - adjustments: [{ label: 'Education discount', amount: '−$1,512.00' }], - subtotal: undefined, - promo: { label: 'Promo code' }, + discounts: [ + { label: 'Education discount', amount: '−$1,512.00' }, + { label: 'Promo code' } + ], chips: [ { code: 'COMFY-EDU', removable: false }, { code: 'COMFY50', removable: true } diff --git a/apps/billing-web/src/checkout/summaryLedger.ts b/apps/billing-web/src/checkout/summaryLedger.ts index 5103c07cb61..e989b9f88ed 100644 --- a/apps/billing-web/src/checkout/summaryLedger.ts +++ b/apps/billing-web/src/checkout/summaryLedger.ts @@ -46,16 +46,8 @@ export interface SummaryLedger { readonly credits?: { readonly count: string; readonly qualifier: string } /** Money rows the total reconciles with; see `moneyItems`. */ readonly items: readonly LedgerRow[] - /** Discounts the customer already holds, priced before any entered code. */ - readonly adjustments: readonly DiscountRow[] - /** - * The pre-discount base an entered code applied to. The quote carries - * no such field today, so no builder populates this; it stays typed for - * when the server reports one, and the row renders only then. - */ - readonly subtotal?: string - /** The row for the code the customer entered. */ - readonly promo?: DiscountRow + /** Every promotion the quote applied, held or entered, in the server's order. */ + readonly discounts: readonly DiscountRow[] readonly chips: readonly PromoChip[] /** Codes apply to a charge made today, so only those families take one. */ readonly acceptsPromo: boolean @@ -63,10 +55,7 @@ export interface SummaryLedger { readonly trailing: readonly string[] } -type DiscountSlots = Pick< - SummaryLedger, - 'adjustments' | 'subtotal' | 'promo' | 'chips' | 'acceptsPromo' -> +type DiscountSlots = Pick type FamilyLedger = Omit @@ -445,21 +434,8 @@ export function acceptsPromoCode(quote: SubscriptionPreview): boolean { type Discount = NonNullable[number] -/** - * The `promotion` discount matching the quote's `promotion_code` is the - * customer's entered code, any other is one the account already holds. - * Subtotal would name the base the entered code applied to, but the quote - * carries no pre-discount total, so this builder never sets it. Computing - * one as today's charge plus what the code took would be a frontend guess - * at a number the server is supposed to report. - */ -function discountSlots(r: QuoteReading): DiscountSlots { - const enteredCode = r.quote.promotion_code - const entered = r.promotions.find( - (discount) => discount.code.toUpperCase() === enteredCode?.toUpperCase() - ) - const held = r.promotions.filter((discount) => discount !== entered) - const rowOf = (discount: Discount) => ({ +function discountRow(r: QuoteReading, discount: Discount): DiscountRow { + return { label: discount.name ?? r.t(`${S}.discount.fallbackLabel`, {}), ...(discount.amount_off_cents === undefined ? {} @@ -468,15 +444,27 @@ function discountSlots(r: QuoteReading): DiscountSlots { amount: r.money(discount.amount_off_cents) }) }) - }) + } +} + +/** + * Every promotion gets a row, in the order the server listed them and at the + * amount it reported. The `promotion` discount matching the quote's + * `promotion_code` is the customer's entered code, the only chip that comes + * off; any other is one the account already holds. + */ +function discountSlots(r: QuoteReading): DiscountSlots { + const enteredCode = r.quote.promotion_code?.toUpperCase() + const held = r.promotions.filter( + (discount) => discount.code.toUpperCase() !== enteredCode + ) return { - adjustments: held.map(rowOf), - ...(entered === undefined ? {} : { promo: rowOf(entered) }), + discounts: r.promotions.map((discount) => discountRow(r, discount)), chips: [ ...held.map(({ code }) => ({ code, removable: false })), - ...(enteredCode === undefined + ...(r.quote.promotion_code === undefined ? [] - : [{ code: enteredCode, removable: true }]) + : [{ code: r.quote.promotion_code, removable: true }]) ], acceptsPromo: acceptsPromoCode(r.quote) } diff --git a/apps/billing-web/src/components/fullPage/CheckoutSummaryColumn.test.ts b/apps/billing-web/src/components/fullPage/CheckoutSummaryColumn.test.ts index 56d8f6a6c3b..fad9ede3df2 100644 --- a/apps/billing-web/src/components/fullPage/CheckoutSummaryColumn.test.ts +++ b/apps/billing-web/src/components/fullPage/CheckoutSummaryColumn.test.ts @@ -24,7 +24,7 @@ const UPGRADE: SummaryLedger = { sublines: ['Unused time from Creator plan'] } ], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$32.50', @@ -35,7 +35,6 @@ const LANDMARKS = [ 'Pro Plan - Prorated', '$50.00', 'Unused time from Creator plan', - 'Subtotal', 'Total due today', 'Renews at $100.00 on July 28, 2026' ] @@ -93,22 +92,6 @@ describe('CheckoutSummaryColumn', () => { ]) }) - it('divides above a Subtotal when the ledger has one', () => { - renderColumn({ ...UPGRADE, subtotal: '$32.50' }) - - expect(ledgerOutline()).toEqual([ - '---', - 'Pro Plan - Prorated', - '$50.00', - 'Unused time from Creator plan', - '---', - 'Subtotal', - '---', - 'Total due today', - 'Renews at $100.00 on July 28, 2026' - ]) - }) - it('renders a $0 due quote as the headline and the total, with no row between', () => { renderColumn({ family: 'charge_now', @@ -116,7 +99,7 @@ describe('CheckoutSummaryColumn', () => { headline: { amount: '$0', currency: 'USD' }, credits: { count: '1,772,400', qualifier: 'credits per year' }, items: [], - adjustments: [], + discounts: [], chips: [], acceptsPromo: true, total: '$0.00', @@ -131,15 +114,16 @@ describe('CheckoutSummaryColumn', () => { expect(screen.getByText('$0.00')).toBeInTheDocument() }) - it('prices a held discount, then the Subtotal an entered code applied to', () => { + it('lists every discount row in the ledger order, above the chips', () => { render(CheckoutSummaryColumn, { props: { ledger: { ...UPGRADE, items: [UPGRADE.items[0]], - adjustments: [{ label: 'Education discount', amount: '−$10.00' }], - subtotal: '$40.00', - promo: { label: 'Promo code', amount: '−$7.50' } + discounts: [ + { label: 'Promo code', amount: '−$7.50' }, + { label: 'Education discount', amount: '−$10.00' } + ] } }, slots: { default: '

chips and entry

' }, @@ -148,12 +132,10 @@ describe('CheckoutSummaryColumn', () => { const expected = [ 'Pro Plan - Prorated', - 'Education discount', - '−$10.00', - 'Subtotal', - '$40.00', 'Promo code', '−$7.50', + 'Education discount', + '−$10.00', 'chips and entry', 'Total due today' ] @@ -178,33 +160,33 @@ describe('CheckoutSummaryColumn', () => { }) }) -describe('CheckoutSummaryColumn held discounts', () => { - const held = (...rows: [string, string][]): SummaryLedger => ({ +describe('CheckoutSummaryColumn discount rows', () => { + const discounted = (...rows: [string, string][]): SummaryLedger => ({ ...UPGRADE, items: [UPGRADE.items[0]], - adjustments: rows.map(([label, amount]) => ({ label, amount })) + discounts: rows.map(([label, amount]) => ({ label, amount })) }) - const heldRows = () => + const discountRows = () => screen .getAllByRole('listitem') .map((row) => row.textContent.trim()) .filter((text) => /discount|Promo code/.test(text)) - it('re-prices two unnamed held discounts as rows of their own', async () => { + it('re-prices two unnamed discounts as rows of their own', async () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) const { rerender } = renderColumn( - held(['Promo code', '−$10.00'], ['Education discount', '−$4.00']) + discounted(['Promo code', '−$10.00'], ['Education discount', '−$4.00']) ) await rerender({ - ledger: held( + ledger: discounted( ['Education discount', '−$4.00'], ['Promo code', '−$10.00'], ['Promo code', '−$5.00'] ) }) - expect(heldRows()).toEqual([ + expect(discountRows()).toEqual([ 'Education discount−$4.00', 'Promo code−$10.00', 'Promo code−$5.00' diff --git a/apps/billing-web/src/components/fullPage/summary/CheckoutLedger.vue b/apps/billing-web/src/components/fullPage/summary/CheckoutLedger.vue index ec3781fa289..0b44edab30f 100644 --- a/apps/billing-web/src/components/fullPage/summary/CheckoutLedger.vue +++ b/apps/billing-web/src/components/fullPage/summary/CheckoutLedger.vue @@ -32,7 +32,7 @@ const { t } = useI18n()

-