From 8a7041c369ebaf437058d6d0dd9d442f9192ce23 Mon Sep 17 00:00:00 2001 From: comfydesigner Date: Mon, 31 Aug 2026 12:45:16 -0700 Subject: [PATCH 1/6] feat(billing): top-up success and declined outcome states Replace the success toast plus redirect into settings with the designed success screen, showing previous balance, credits added, and new balance. Replace the decline toast with the declined screen, which carries the Stripe reason when the operation reports one and offers a way back to the charge or on to the billing portal. --- src/locales/en/main.json | 9 + ...TopUpCreditsDialogContentWorkspace.test.ts | 75 +++++--- .../TopUpCreditsDialogContentWorkspace.vue | 171 +++++++++++++++--- 3 files changed, 210 insertions(+), 45 deletions(-) diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 3e3a2d4c690..6cb4022c688 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -2711,6 +2711,15 @@ "cancelPayment": "Cancel payment", "cancelUnavailable": "This payment is already processing and can't be canceled.", "paymentCanceledNotice": "Payment canceled. Nothing was charged.", + "successTitle": "You're all set", + "successBody": "View this charge anytime in Billing & invoices in your workspace settings.", + "previousBalance": "Previous balance", + "added": "Added", + "newBalance": "New balance", + "declinedTitle": "Payment declined", + "declinedBody": "Your card couldn't be charged. Try another card, or contact your bank if this looks wrong.", + "declinedReasonLabel": "Stripe reasoning:", + "updatePaymentMethod": "Update payment method", "verifyTitle": "Verify your payment", "viewPricing": "View pricing details", "youPay": "Amount (USD)", diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts index 706eda392db..2dba5624577 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts @@ -1,7 +1,7 @@ import { render, screen, waitFor } from '@testing-library/vue' import userEvent from '@testing-library/user-event' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { nextTick } from 'vue' +import { nextTick, ref } from 'vue' import { createI18n } from 'vue-i18n' import enMessages from '@/locales/en/main.json' with { type: 'json' } @@ -11,6 +11,7 @@ import type { CreateTopupResponse } from '@/platform/workspace/api/workspaceApi' import TopUpCreditsDialogContentWorkspace from './TopUpCreditsDialogContentWorkspace.vue' +const mockBalance = ref<{ amountMicros: number } | null>({ amountMicros: 0 }) const mockFetchBalance = vi.fn() const mockFetchStatus = vi.fn() const mockManageSubscription = vi.fn<() => Promise>() @@ -23,7 +24,6 @@ const mockTopup = vi.fn<(amountCents: number) => Promise>() const mockStartOperation = vi.fn() const mockRetryPaymentAuthentication = vi.fn() -const mockShowSettings = vi.fn() const mockToastAdd = vi.fn() const mockCloseDialog = vi.fn() const mockTrackTopUpPurchase = vi.fn() @@ -31,10 +31,6 @@ const mockTrackBillingEvent = vi.fn() const mockCanTopUp = vi.hoisted(() => ({ ref: undefined as { value: boolean } | undefined })) -const mockDistributionTypes = vi.hoisted(() => ({ isCloud: true })) - -vi.mock('@/platform/distribution/types', () => mockDistributionTypes) - const mockHasSavedPaymentMethod = vi.hoisted(() => ({ ref: undefined as { value: boolean | null } | undefined })) @@ -73,6 +69,7 @@ const mockBillingOperationState = vi.hoisted(() => ({ vi.mock('@/composables/billing/useBillingContext', () => ({ useBillingContext: () => ({ + balance: mockBalance, fetchBalance: mockFetchBalance, fetchStatus: mockFetchStatus, manageSubscription: mockManageSubscription, @@ -108,10 +105,6 @@ vi.mock('@/platform/workspace/composables/useBillingCapabilities', async () => { } }) -vi.mock('@/platform/settings/composables/useSettingsDialog', () => ({ - useSettingsDialog: () => ({ show: mockShowSettings }) -})) - vi.mock('@/stores/dialogStore', () => ({ useDialogStore: () => ({ closeDialog: mockCloseDialog }) })) @@ -140,6 +133,7 @@ vi.mock('primevue/usetoast', () => ({ })) vi.mock('@/base/credits/comfyCredits', () => ({ + centsToCredits: (cents: number) => cents, creditsToUsd: (credits: number) => credits, usdToCredits: (usd: number) => usd })) @@ -254,7 +248,6 @@ describe('TopUpCreditsDialogContentWorkspace', () => { }) beforeEach(() => { - mockDistributionTypes.isCloud = true setCanTopUp(true) setIsAddingCredits(false) setTopupActionOperation(undefined) @@ -597,7 +590,6 @@ describe('TopUpCreditsDialogContentWorkspace', () => { expect(mockFetchBalance).toHaveBeenCalledOnce() expect(mockFetchStatus).toHaveBeenCalledOnce() - expect(mockShowSettings).toHaveBeenCalledWith('workspace') expect(mockTrackBillingEvent).toHaveBeenCalledWith({ operation: 'topup', stage: 'succeeded', @@ -624,17 +616,6 @@ describe('TopUpCreditsDialogContentWorkspace', () => { expect(mockCloseDialog).toHaveBeenCalled() }) - it('opens Credits settings after a completed local top-up', async () => { - mockDistributionTypes.isCloud = false - mockTopup.mockResolvedValue(topupResponse('completed')) - - renderDialog() - await clickAddCredits() - await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) - - expect(mockShowSettings).toHaveBeenCalledWith('credits') - }) - it('keeps completed top-up telemetry successful when refresh fails', async () => { mockTopup.mockResolvedValue(topupResponse('completed')) mockFetchBalance.mockRejectedValueOnce(new Error('balance unavailable')) @@ -652,7 +633,53 @@ describe('TopUpCreditsDialogContentWorkspace', () => { billing_op_id: 'op-1', duration_ms: expect.any(Number) }) - expect(mockShowSettings).toHaveBeenCalledWith('workspace') + }) + + describe('outcome states', () => { + it('breaks down the balance after a completed charge', async () => { + mockBalance.value = { amountMicros: 46_450 } + mockTopup.mockResolvedValue(topupResponse('completed')) + + renderDialog() + await clickAddCredits() + await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) + + expect(screen.getByText("You're all set")).toBeInTheDocument() + expect(screen.getByText('46,450')).toBeInTheDocument() + expect(screen.getByText('+50')).toBeInTheDocument() + expect(screen.getByText('46,500')).toBeInTheDocument() + }) + + it('offers a way back to the charge after a decline', async () => { + mockTopup.mockResolvedValue(topupResponse('failed')) + + renderDialog() + await clickAddCredits() + await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) + + expect(screen.getByText('Payment declined')).toBeInTheDocument() + expect(screen.queryByText('Stripe reasoning:')).not.toBeInTheDocument() + + await userEvent.click(screen.getByRole('button', { name: 'Back' })) + + expect( + screen.getByRole('button', { name: 'Pay $50.00' }) + ).toBeInTheDocument() + }) + + it('sends a declined customer to the billing portal', async () => { + mockTopup.mockResolvedValue(topupResponse('failed')) + mockManageSubscription.mockResolvedValue(undefined) + + renderDialog() + await clickAddCredits() + await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) + await userEvent.click( + screen.getByRole('button', { name: 'Update payment method' }) + ) + + expect(mockManageSubscription).toHaveBeenCalledOnce() + }) }) it('does not refresh balance or status for a pending top-up', async () => { diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue index c1f364c5e33..e5e225aed23 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue @@ -15,7 +15,7 @@

{{ @@ -108,6 +108,79 @@ + + + +
-
+
+ +
+
+ + +
+
@@ -118,9 +118,20 @@ > {{ $t('credits.topUp.successTitle') }}

-

- {{ $t('credits.topUp.successBody') }} -

+ + +
{{ $t('credits.topUp.newBalance') }} - + + {{ formatNumber(newBalanceCredits) }}
@@ -296,7 +310,7 @@ {{ $t('g.close') }} -
+
-