diff --git a/browser_tests/fixtures/helpers/LiveCloudTopup.ts b/browser_tests/fixtures/helpers/LiveCloudTopup.ts new file mode 100644 index 00000000000..8ba73cb6fa1 --- /dev/null +++ b/browser_tests/fixtures/helpers/LiveCloudTopup.ts @@ -0,0 +1,158 @@ +import { + zBillingBalanceResponse, + zBillingOpStatusResponse, + zBillingStatusResponse, + zCreateTopupRequest, + zCreateTopupResponse +} from '@comfyorg/ingest-types/zod' +import { expect } from '@playwright/test' +import type { TestInfo } from '@playwright/test' + +import type { ComfyPage } from '@e2e/fixtures/ComfyPage' +import type { LiveCloudBillingSession } from '@e2e/fixtures/helpers/LiveCloudBillingSession' + +export class LiveCloudTopup { + private readonly dialog + + constructor( + private readonly comfyPage: ComfyPage, + private readonly session: LiveCloudBillingSession, + private readonly frontend: string + ) { + this.dialog = comfyPage.page.getByRole('dialog') + } + + async open() { + const status = await this.session.read( + '/api/billing/status', + zBillingStatusResponse + ) + expect(status.is_active).toBe(true) + expect(status.subscription_tier).toBe('CREATOR') + await this.comfyPage.page.goto(`${this.frontend}/?topup=1`) + await this.dialog.getByRole('button', { name: '$10', exact: true }).click() + await this.dialog + .getByRole('button', { name: 'Add credits', exact: true }) + .click() + await expect( + this.dialog.getByText( + 'Credits are added to this workspace as soon as payment completes.', + { exact: true } + ) + ).toBeVisible() + } + + async verifyPurchase(testInfo: TestInfo) { + const before = await this.session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + const { topup } = await this.submitPurchase() + await this.attachPurchase(testInfo, topup, before.amount_micros) + await expect( + this.comfyPage.page.getByText('Credits added successfully', { + exact: true + }) + ).toBeVisible() + await this.expectPurchaseComplete( + topup.billing_op_id, + before.amount_micros + Number(topup.amount_cents) + ) + await this.comfyPage.attachScreenshot('saved-card-topup.png', { + runInCI: true + }) + } + + async verifyIdempotentRetry(testInfo: TestInfo) { + const before = await this.session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + const { request, topup } = await this.submitPurchase() + expect(request.idempotency_key).toBeTruthy() + if (!request.idempotency_key) { + throw new Error('Missing top-up idempotency key') + } + const retry = await this.session.post( + '/api/billing/topup', + { + amount_cents: Number(request.amount_cents), + idempotency_key: request.idempotency_key + }, + zCreateTopupResponse + ) + expect(retry.billing_op_id).toBe(topup.billing_op_id) + expect(retry.topup_id).toBe(topup.topup_id) + await this.attachPurchase(testInfo, topup, before.amount_micros, { + retryOperationId: retry.billing_op_id, + retryTopupId: retry.topup_id + }) + await this.expectPurchaseComplete( + topup.billing_op_id, + before.amount_micros + Number(topup.amount_cents) + ) + } + + private async submitPurchase() { + const [response] = await Promise.all([ + this.comfyPage.page.waitForResponse( + (response) => + response.url() === `${this.frontend}/api/billing/topup` && + response.request().method() === 'POST' + ), + this.dialog + .getByRole('button', { name: 'Pay $10.00', exact: true }) + .click() + ]) + expect(response.status()).toBe(200) + const request = zCreateTopupRequest.parse(response.request().postDataJSON()) + const topup = zCreateTopupResponse.parse(await response.json()) + expect(topup.amount_cents).toBe(1000n) + expect(topup.billing_op_id).not.toBe('') + return { request, topup } + } + + private async attachPurchase( + testInfo: TestInfo, + topup: ReturnType, + balanceBeforeCents: number, + extra: Record = {} + ) { + await testInfo.attach('topup.json', { + body: JSON.stringify({ + operationId: topup.billing_op_id, + topupId: topup.topup_id, + amountCents: Number(topup.amount_cents), + balanceBeforeCents, + ...extra + }), + contentType: 'application/json' + }) + } + + private async expectPurchaseComplete( + operationId: string, + expectedBalanceCents: number + ) { + await expect + .poll(async () => { + const operation = await this.session.read( + `/api/billing/ops/${encodeURIComponent(operationId)}`, + zBillingOpStatusResponse + ) + return { id: operation.id, status: operation.status } + }) + .toMatchObject({ id: operationId, status: 'succeeded' }) + await expect + .poll( + async () => + ( + await this.session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + ).amount_micros + ) + .toBe(expectedBalanceCents) + } +} diff --git a/browser_tests/fixtures/liveCloudPaidBillingFixture.ts b/browser_tests/fixtures/liveCloudPaidBillingFixture.ts new file mode 100644 index 00000000000..5746ae1940b --- /dev/null +++ b/browser_tests/fixtures/liveCloudPaidBillingFixture.ts @@ -0,0 +1,27 @@ +import { LiveCloudTopup } from '@e2e/fixtures/helpers/LiveCloudTopup' +import { liveCloudDisposableBillingFixture } from '@e2e/fixtures/liveCloudDisposableBillingFixture' + +export const liveCloudPaidBillingFixture = + liveCloudDisposableBillingFixture.extend<{ + topup: LiveCloudTopup + }>({ + topup: async ( + { comfyPage, disposableCheckout, liveCloudBillingConfig }, + use, + testInfo + ) => { + if (!liveCloudBillingConfig) + throw new Error('Live billing config required') + await disposableCheckout.checkout.completeCheckout( + disposableCheckout.billingSession, + testInfo + ) + const topup = new LiveCloudTopup( + comfyPage, + disposableCheckout.billingSession, + liveCloudBillingConfig.PLAYWRIGHT_TEST_URL + ) + await topup.open() + await use(topup) + } + }) diff --git a/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts b/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts index fbdea395459..9d35f49e3e5 100644 --- a/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts +++ b/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts @@ -383,3 +383,19 @@ describe('Disposable payment permissions', () => { ).toBe(false) }) }) + +describe('paid top-up environment boundary', () => { + it.for([ + { backend: 'https://stagingcloud.comfy.org', allowed: true }, + { backend: 'https://testcloud.comfy.org', allowed: true }, + { backend: 'https://cloud.comfy.org', allowed: false } + ])('$backend top-up allowed=$allowed', ({ backend, allowed }) => { + expect( + isLiveCloudMutationAllowed( + new URL('/api/billing/topup', backend), + 'POST', + { ...config, PLAYWRIGHT_SETUP_API_URL: backend, allowPayments: true } + ) + ).toBe(allowed) + }) +}) diff --git a/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts b/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts index 1ddcee00a41..63e99c417bd 100644 --- a/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts +++ b/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts @@ -123,7 +123,10 @@ function isPaymentPostAllowed(url: URL, backend: string): boolean { url.pathname === '/ajax/metrics_batch' ) return true - if (url.origin === backend && url.pathname === '/api/billing/payment-portal') + if ( + url.origin === backend && + ['/api/billing/payment-portal', '/api/billing/topup'].includes(url.pathname) + ) return true return ( url.origin === 'https://api.stripe.com' && diff --git a/browser_tests/tests/liveCloud/paid/savedCardTopup.spec.ts b/browser_tests/tests/liveCloud/paid/savedCardTopup.spec.ts new file mode 100644 index 00000000000..e582df49d07 --- /dev/null +++ b/browser_tests/tests/liveCloud/paid/savedCardTopup.spec.ts @@ -0,0 +1,9 @@ +import { liveCloudPaidBillingFixture as test } from '@e2e/fixtures/liveCloudPaidBillingFixture' + +test.describe('Real Cloud saved-card billing', { tag: ['@cloud-live'] }, () => { + test('purchases credits with the saved test card', async ({ + topup + }, testInfo) => { + await topup.verifyPurchase(testInfo) + }) +}) diff --git a/browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts b/browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts new file mode 100644 index 00000000000..ae09f759943 --- /dev/null +++ b/browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts @@ -0,0 +1,9 @@ +import { liveCloudPaidBillingFixture as test } from '@e2e/fixtures/liveCloudPaidBillingFixture' + +test.describe('Real Cloud saved-card billing', { tag: ['@cloud-live'] }, () => { + test('retries one top-up without granting credits twice', async ({ + topup + }, testInfo) => { + await topup.verifyIdempotentRetry(testInfo) + }) +}) diff --git a/docs/testing/cloud-billing-e2e.md b/docs/testing/cloud-billing-e2e.md index 96bdcab10e3..9d18653929a 100644 --- a/docs/testing/cloud-billing-e2e.md +++ b/docs/testing/cloud-billing-e2e.md @@ -82,3 +82,11 @@ Traces start after permanent-account sign-in and contain authenticated network traffic. Keep reports private. Disposable accounts require no reusable credentials. Completed-payment assertions verify saved cards through the real hosted billing portal, because Staging disables the embedded-checkout saved-card API. + +## Saved-card top-ups + +PR #17538 depends only on #17481. Run `--project=cloud-live-paid`. Each test +creates a disposable account and completes a real Creator checkout with a saved +Stripe test card before purchasing credits. The top-up driver, exact +balance/operation assertions, retry-key fix, and top-up endpoint permission belong +to this PR. Payment tests reject production and require no permanent account credentials. diff --git a/src/composables/billing/types.ts b/src/composables/billing/types.ts index 84719722b10..31a7da5cce5 100644 --- a/src/composables/billing/types.ts +++ b/src/composables/billing/types.ts @@ -77,7 +77,10 @@ export interface BillingActions { * Pass-through by design: the caller owns the completed/pending follow-up * (balance refresh or billing-op polling), so this does not refresh. */ - topup: (amountCents: number) => Promise + topup: ( + amountCents: number, + idempotencyKey?: string + ) => Promise fetchPlans: () => Promise /** * Ensures billing is initialized and subscription is active. diff --git a/src/composables/billing/useBillingContext.test.ts b/src/composables/billing/useBillingContext.test.ts index dd19f58c693..99e9aca64d9 100644 --- a/src/composables/billing/useBillingContext.test.ts +++ b/src/composables/billing/useBillingContext.test.ts @@ -347,9 +347,12 @@ describe('useBillingContext', () => { vi.clearAllMocks() expect(context.type.value).toBe('workspace') - await context.topup(500) + await context.topup(500, 'topup-attempt-1') - expect(workspaceApi.createTopup).toHaveBeenCalledWith(500) + expect(workspaceApi.createTopup).toHaveBeenCalledWith( + 500, + 'topup-attempt-1' + ) expect(useAuthActions().purchaseCredits).not.toHaveBeenCalled() }) diff --git a/src/composables/billing/useBillingContext.ts b/src/composables/billing/useBillingContext.ts index 4d25ba306ff..ccb9b29b0ea 100644 --- a/src/composables/billing/useBillingContext.ts +++ b/src/composables/billing/useBillingContext.ts @@ -315,7 +315,7 @@ function useBillingContextInternal(): BillingContext { return activeContext.value.resubscribe(options) } - async function topup(amountCents: number) { + async function topup(amountCents: number, idempotencyKey?: string) { if ( !Number.isInteger(amountCents) || amountCents <= 0 || @@ -325,7 +325,7 @@ function useBillingContextInternal(): BillingContext { 'Top-up amount must be a positive whole-dollar cent value' ) } - return activeContext.value.topup(amountCents) + return activeContext.value.topup(amountCents, idempotencyKey) } async function fetchPlans() { diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts index f19e7f233d8..35bad024fdd 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts @@ -32,6 +32,9 @@ import { mockBillingContext } from '@/utils/__tests__/mockBillingContext' import TopUpCreditsDialogContentWorkspace from './TopUpCreditsDialogContentWorkspace.vue' const mockReportError = vi.hoisted(() => vi.fn()) +const mockCreateUuidv4 = vi.hoisted(() => vi.fn()) + +vi.mock(import('@/utils/uuid'), () => ({ createUuidv4: mockCreateUuidv4 })) vi.mock(import('@/platform/telemetry/reportError'), () => ({ reportError: mockReportError @@ -182,6 +185,7 @@ beforeEach(() => { }) beforeEach(() => { + mockCreateUuidv4.mockReturnValue('topup-attempt-1') vi.mocked(useDialogStore().closeDialog).mockImplementation(() => {}) Object.assign(useAuthStore(), { userId: 'user-1' }) Object.assign(useTeamWorkspaceStore(), { activeWorkspaceId: 'workspace-1' }) @@ -908,6 +912,59 @@ describe('TopUpCreditsDialogContentWorkspace', () => { ) }) + it('reuses the idempotency key after an uncertain request failure', async () => { + vi.mocked(mockBillingContext().topup) + .mockRejectedValueOnce(new Error('connection lost')) + .mockResolvedValueOnce(topupResponse('pending')) + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) + + renderDialog() + await clickAddCredits() + const payButton = screen.getByRole('button', { name: 'Pay $50.00' }) + await userEvent.click(payButton) + await waitFor(() => expect(payButton).toBeEnabled()) + await userEvent.click(payButton) + + expect(vi.mocked(mockBillingContext().topup)).toHaveBeenNthCalledWith( + 1, + 5000, + 'topup-attempt-1' + ) + expect(vi.mocked(mockBillingContext().topup)).toHaveBeenNthCalledWith( + 2, + 5000, + 'topup-attempt-1' + ) + consoleError.mockRestore() + }) + + it('uses a new idempotency key after a definitive failure', async () => { + mockCreateUuidv4 + .mockReturnValueOnce('topup-attempt-1') + .mockReturnValueOnce('topup-attempt-2') + vi.mocked(mockBillingContext().topup) + .mockResolvedValueOnce(topupResponse('failed')) + .mockResolvedValueOnce(topupResponse('pending')) + + renderDialog() + await clickAddCredits() + const payButton = screen.getByRole('button', { name: 'Pay $50.00' }) + await userEvent.click(payButton) + await waitFor(() => expect(payButton).toBeEnabled()) + await userEvent.click(payButton) + + expect(vi.mocked(mockBillingContext().topup)).toHaveBeenNthCalledWith( + 1, + 5000, + 'topup-attempt-1' + ) + expect(vi.mocked(mockBillingContext().topup)).toHaveBeenNthCalledWith( + 2, + 5000, + 'topup-attempt-2' + ) + }) + it('does not top up after the server capability is revoked', async () => { renderDialog() await clickAddCredits() diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue index 6b5b927625a..1fe029704a2 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue @@ -319,6 +319,7 @@ import type { CheckoutJourneyRecord } from '@/platform/workspace/utils/checkoutJ import { api } from '@/scripts/api' import { useAuthStore } from '@/stores/authStore' import { useDialogStore } from '@/stores/dialogStore' +import { createUuidv4 } from '@/utils/uuid' import { cn } from '@comfyorg/tailwind-utils' const { isInsufficientCredits = false } = defineProps<{ @@ -406,6 +407,7 @@ const payAmount = ref(50) const showCeilingWarning = ref(false) const loading = ref(false) const paymentSubmitted = ref(false) +let topupIdempotencyKey: string | undefined const step = ref<'amount' | 'confirm' | 'verifying'>( topupOperation.value && canTopUp.value ? 'verifying' : 'amount' ) @@ -428,6 +430,7 @@ const creditsModel = computed({ set: (newCredits: number) => { payAmount.value = Math.round(creditsToUsd(newCredits)) selectedPreset.value = null + topupIdempotencyKey = undefined } }) @@ -490,12 +493,14 @@ function handlePayAmountChange(value: number) { payAmount.value = value selectedPreset.value = null showCeilingWarning.value = false + topupIdempotencyKey = undefined } function handlePresetClick(amount: number) { showCeilingWarning.value = false payAmount.value = amount selectedPreset.value = amount + topupIdempotencyKey = undefined } function handlePrimaryAction() { @@ -533,6 +538,7 @@ function startOverTopup() { const operation = topupOperation.value if (operation) dismissOperation(operation.opId) paymentSubmitted.value = false + topupIdempotencyKey = undefined step.value = 'amount' } @@ -543,6 +549,11 @@ function handleClose(clearTracking = true) { dialogStore.closeDialog({ key: 'top-up-credits' }) } +function submitTopup(amountCents: number) { + topupIdempotencyKey ??= createUuidv4() + return topup(amountCents, topupIdempotencyKey) +} + async function handleBuy() { if (paymentLocked.value || !isValidAmount.value || !canTopUp.value) { return @@ -574,7 +585,7 @@ async function handleBuy() { } const amountCents = payAmount.value * 100 - const response = await topup(amountCents) + const response = await submitTopup(amountCents) if (!response) { if (isCurrentAttempt()) paymentSubmitted.value = false telemetry?.trackBillingEvent({ @@ -662,7 +673,10 @@ async function handleBuy() { }) } else { // Synchronous 'failed' here means the charge was declined, not rejected pre-attempt. - if (isCurrentAttempt()) paymentSubmitted.value = false + if (isCurrentAttempt()) { + paymentSubmitted.value = false + topupIdempotencyKey = undefined + } telemetry?.trackBillingEvent({ operation: 'topup', stage: 'failed', diff --git a/src/platform/workspace/composables/useTopupOperation.test.ts b/src/platform/workspace/composables/useTopupOperation.test.ts index d9a4c8f5523..404d1c9f94d 100644 --- a/src/platform/workspace/composables/useTopupOperation.test.ts +++ b/src/platform/workspace/composables/useTopupOperation.test.ts @@ -126,7 +126,10 @@ describe('useTopupOperation', () => { } mockContextTopup.mockResolvedValue(response) - await expect(useTopupOperation().topup(1000)).resolves.toBe(response) + await expect(useTopupOperation().topup(1000, 'retry-key')).resolves.toBe( + response + ) + expect(mockContextTopup).toHaveBeenCalledWith(1000, 'retry-key') expect(mockCreateBillingSdk).not.toHaveBeenCalled() }) diff --git a/src/platform/workspace/composables/useTopupOperation.ts b/src/platform/workspace/composables/useTopupOperation.ts index 6a4e3fdc174..b56a8864a8c 100644 --- a/src/platform/workspace/composables/useTopupOperation.ts +++ b/src/platform/workspace/composables/useTopupOperation.ts @@ -29,9 +29,12 @@ export function useTopupOperation() { : operationStore.topupActionOperation ) - function topup(amountCents: number): Promise { + function topup( + amountCents: number, + idempotencyKey?: string + ): Promise { if (sdkStore) return sdkStore.createTopup(amountCents) - return billingContext.topup(amountCents) + return billingContext.topup(amountCents, idempotencyKey) } function retryPaymentAuthentication(operationId: string): Promise { diff --git a/src/platform/workspace/composables/useWorkspaceBilling.test.ts b/src/platform/workspace/composables/useWorkspaceBilling.test.ts index 64abb06bb4e..3adf8cc087f 100644 --- a/src/platform/workspace/composables/useWorkspaceBilling.test.ts +++ b/src/platform/workspace/composables/useWorkspaceBilling.test.ts @@ -1543,9 +1543,12 @@ describe('useWorkspaceBilling', () => { mockWorkspaceApi.createTopup.mockResolvedValue(topupResponse) const billing = setupBilling() - const result = await billing.topup(500) + const result = await billing.topup(500, 'topup-attempt-1') - expect(mockWorkspaceApi.createTopup).toHaveBeenCalledWith(500) + expect(mockWorkspaceApi.createTopup).toHaveBeenCalledWith( + 500, + 'topup-attempt-1' + ) expect(result).toBe(topupResponse) expect(mockWorkspaceApi.getBillingStatus).not.toHaveBeenCalled() expect(mockWorkspaceApi.getBillingBalance).not.toHaveBeenCalled() @@ -1630,9 +1633,12 @@ describe('useWorkspaceBilling', () => { mockWorkspaceApi.createTopup.mockResolvedValue(topupResponse) const billing = setupBilling() - const result = await billing.topup(500) + const result = await billing.topup(500, 'topup-attempt-1') - expect(mockWorkspaceApi.createTopup).toHaveBeenCalledWith(500) + expect(mockWorkspaceApi.createTopup).toHaveBeenCalledWith( + 500, + 'topup-attempt-1' + ) expect(result).toBe(topupResponse) expect(mockWorkspaceApi.getBillingStatus).not.toHaveBeenCalled() expect(mockWorkspaceApi.getBillingBalance).not.toHaveBeenCalled() diff --git a/src/platform/workspace/composables/useWorkspaceBilling.ts b/src/platform/workspace/composables/useWorkspaceBilling.ts index 6c0a3159033..3b13bb7cea7 100644 --- a/src/platform/workspace/composables/useWorkspaceBilling.ts +++ b/src/platform/workspace/composables/useWorkspaceBilling.ts @@ -560,12 +560,13 @@ export function useWorkspaceBilling(): BillingState & BillingActions { } async function topup( - amountCents: number + amountCents: number, + idempotencyKey?: string ): Promise { isLoading.value = true error.value = null try { - return await workspaceApi.createTopup(amountCents) + return await workspaceApi.createTopup(amountCents, idempotencyKey) } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to top up credits'