Skip to content
Draft
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
158 changes: 158 additions & 0 deletions browser_tests/fixtures/helpers/LiveCloudTopup.ts
Original file line number Diff line number Diff line change
@@ -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<typeof zCreateTopupResponse.parse>,
balanceBeforeCents: number,
extra: Record<string, string> = {}
) {
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)
}
}
27 changes: 27 additions & 0 deletions browser_tests/fixtures/liveCloudPaidBillingFixture.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
16 changes: 16 additions & 0 deletions browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
5 changes: 4 additions & 1 deletion browser_tests/fixtures/utils/liveCloudBillingPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down
9 changes: 9 additions & 0 deletions browser_tests/tests/liveCloud/paid/savedCardTopup.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
9 changes: 9 additions & 0 deletions browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
8 changes: 8 additions & 0 deletions docs/testing/cloud-billing-e2e.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 4 additions & 1 deletion src/composables/billing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateTopupResponse | void>
topup: (
amountCents: number,
idempotencyKey?: string
) => Promise<CreateTopupResponse | void>
fetchPlans: () => Promise<void>
/**
* Ensures billing is initialized and subscription is active.
Expand Down
7 changes: 5 additions & 2 deletions src/composables/billing/useBillingContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
4 changes: 2 additions & 2 deletions src/composables/billing/useBillingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' })
Expand Down Expand Up @@ -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()
Expand Down
Loading
Loading