diff --git a/src/platform/telemetry/types.ts b/src/platform/telemetry/types.ts index 81297fa431c..6498f821fcb 100644 --- a/src/platform/telemetry/types.ts +++ b/src/platform/telemetry/types.ts @@ -1342,6 +1342,12 @@ type BillingTimedOut = { failure_category: 'poll_timeout' } +/** The stage one attempt at a billing operation settled on. */ +export type BillingOperationTerminal = + | BillingSucceeded + | (BillingFailed & { decline_reason?: BillingDeclineReason }) + | BillingTimedOut + type SubscriptionCheckoutBillingEvent = { operation: 'subscription_checkout' billing_op_id?: string @@ -1386,12 +1392,7 @@ type BillingOperationBillingEvent = { * true duration. */ duration_ms?: number -} & ( - | BillingStarted - | BillingSucceeded - | (BillingFailed & { decline_reason?: BillingDeclineReason }) - | BillingTimedOut -) +} & (BillingStarted | BillingOperationTerminal) type ResubscribeBillingEvent = { operation: 'resubscribe' diff --git a/src/platform/telemetry/utils/billingFailureCategory.ts b/src/platform/telemetry/utils/billingFailureCategory.ts index 7d973d1b9e5..c8f50a42012 100644 --- a/src/platform/telemetry/utils/billingFailureCategory.ts +++ b/src/platform/telemetry/utils/billingFailureCategory.ts @@ -4,7 +4,8 @@ import { AuthStoreError } from '@/stores/authStore' import type { BillingFailure } from '../types' /** - * A `WorkspaceApiError`/`AuthStoreError` with no `status` never reached the + * A `WorkspaceApiError` that names its category keeps it. Otherwise a + * `WorkspaceApiError`/`AuthStoreError` with no `status` never reached the * backend (`network`); a `TypeError` naming fetch/network/load is what `fetch` * throws for connectivity failures. */ @@ -12,6 +13,7 @@ export function categorizeBillingApiError( err: unknown ): BillingFailure['failure_category'] { if (err instanceof WorkspaceApiError) { + if (err.failureCategory) return err.failureCategory return err.status === undefined ? 'network' : 'api_rejected' } if (err instanceof AuthStoreError) { diff --git a/src/platform/workspace/api/workspaceApiError.ts b/src/platform/workspace/api/workspaceApiError.ts index 3b3e67bddf2..89bdeb8524e 100644 --- a/src/platform/workspace/api/workspaceApiError.ts +++ b/src/platform/workspace/api/workspaceApiError.ts @@ -1,3 +1,5 @@ +import type { BillingFailure } from '@/platform/telemetry/types' + /** * A leaf module, so a subclass can be declared wherever the class is needed * without pulling the workspace client and its stores into the import graph. @@ -6,7 +8,9 @@ export class WorkspaceApiError extends Error { constructor( message: string, public readonly status?: number, - public readonly code?: string + public readonly code?: string, + /** For a failure `status` cannot classify, such as a billing SDK refusal. */ + public readonly failureCategory?: BillingFailure['failure_category'] ) { super(message) this.name = 'WorkspaceApiError' diff --git a/src/platform/workspace/billing/sdk/topupOperationView.test.ts b/src/platform/workspace/billing/sdk/topupOperationView.test.ts index 8bf7ff24413..40bc237e5f6 100644 --- a/src/platform/workspace/billing/sdk/topupOperationView.test.ts +++ b/src/platform/workspace/billing/sdk/topupOperationView.test.ts @@ -89,13 +89,13 @@ describe('projectTopupResult', () => { ).toMatchObject({ billing_op_id: 'op-1', status: 'failed' }) }) - it('reports nothing for a purchase this tab stopped observing', () => { + it('reports a purchase this tab stopped observing as still pending', () => { expect( projectTopupResult( { status: 'unsettled', operation: settledTopup('timed_out') }, 1000 ) - ).toBeUndefined() + ).toMatchObject({ billing_op_id: 'op-1', status: 'pending' }) }) it('throws the API error whose code the dialog already handles', () => { diff --git a/src/platform/workspace/billing/sdk/topupOperationView.ts b/src/platform/workspace/billing/sdk/topupOperationView.ts index 3ac45920304..f4af702f226 100644 --- a/src/platform/workspace/billing/sdk/topupOperationView.ts +++ b/src/platform/workspace/billing/sdk/topupOperationView.ts @@ -1,8 +1,8 @@ /** * Projections from the SDK's operation state and top-up result onto the two * shapes the top-up dialog already reads: the poller's operation record and - * the `CreateTopupResponse` the legacy call returned. The dialog is untouched; - * these give it the same inputs from the other rail. + * the `CreateTopupResponse` the legacy call returned, plus the terminal the + * SDK observed where those shapes cannot hold it. */ import type { BillingDeclineReason, @@ -16,6 +16,10 @@ import { } from '@comfyorg/account-core/billing' import { t } from '@/i18n' +import type { + BillingFailure, + BillingOperationTerminal +} from '@/platform/telemetry/types' import type { BillingAuthenticationState, BillingOperationPhase, @@ -72,36 +76,98 @@ export function projectTopupOperation( } } +/** + * A purchase the SDK settled without crediting, as the response the dialog + * handles, carrying the terminal its status cannot: the decline reason, or + * that this tab stopped watching, or that support must reconcile it. + */ +export class UncreditedTopupResponse implements CreateTopupResponse { + readonly topup_id = '' + + constructor( + readonly billing_op_id: string, + readonly status: 'failed' | 'pending', + readonly amount_cents: number, + readonly terminal: BillingOperationTerminal + ) {} +} + +/** + * Most SDK refusals carry no HTTP status, which a status-less API error + * would otherwise report as a network failure, so each names its category. + */ function topupFailureError(failure: TopupFailure): WorkspaceApiError { const serverCode = 'serverCode' in failure ? failure.serverCode : undefined return new WorkspaceApiError( t('credits.topUp.unknownError'), 'httpStatus' in failure ? failure.httpStatus : undefined, - serverCode === undefined ? failure.code : unwrapServerCode(serverCode) + serverCode === undefined ? failure.code : unwrapServerCode(serverCode), + topupFailureCategory(failure) ) } +function topupFailureCategory( + failure: TopupFailure +): BillingFailure['failure_category'] { + switch (failure.code) { + case 'REQUEST_FAILED': + return failure.httpStatus === undefined ? 'network' : 'api_rejected' + case 'SUPERSEDED': + return 'stale_operation' + case 'INVALID_AMOUNT': + return 'validation' + default: + return 'api_rejected' + } +} + /** - * A settled result as the response the dialog handles today. `unsettled` has - * no counterpart: the dialog reads an undefined response as "nothing to - * report" and the operation stays visible through `projectTopupOperation`. - * `topup_id` is not surfaced by the SDK and nothing reads it. + * A settled result as the response the dialog handles today. `unsettled` is + * the pending response: the server may still settle it, and the operation + * stays visible through `projectTopupOperation`. `topup_id` is not surfaced + * by the SDK and nothing reads it. */ export function projectTopupResult( result: TopupResult, amountCents: number -): CreateTopupResponse | undefined { +): CreateTopupResponse { switch (result.status) { case 'ok': - case 'declined': return { billing_op_id: result.operation.id, topup_id: '', - status: result.status === 'ok' ? 'completed' : 'failed', + status: 'completed', amount_cents: amountCents } + case 'declined': + return new UncreditedTopupResponse( + result.operation.id, + 'failed', + amountCents, + { + stage: 'failed', + outcome: 'failure', + failure_category: 'provider_decline', + decline_reason: result.operation.declineReason + } + ) case 'unsettled': - return undefined + return new UncreditedTopupResponse( + result.operation.id, + 'pending', + amountCents, + result.operation.phase === 'timed_out' + ? { + stage: 'timeout', + outcome: 'failure', + failure_category: 'poll_timeout' + } + : { + stage: 'failed', + outcome: 'failure', + failure_category: 'reconciliation_needed' + } + ) case 'error': throw topupFailureError(result) } diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts index b3b87bd394b..5962a0c22a8 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts @@ -1,3 +1,13 @@ +import type { TopupResult } from '@comfyorg/account-core/billing' + +import { useFeatureFlags } from '@/composables/useFeatureFlags' +import type { BillingTelemetryEvent } from '@/platform/telemetry/types' +import { + failedTopup, + fakeBillingSdk, + settledTopup +} from '@/platform/workspace/billing/sdk/billingSdkTestUtils' +import type { BillingSdk } from '@/platform/workspace/billing/sdk/createBillingSdk' import { useBillingOperationStore } from '@/platform/workspace/stores/billingOperationStore' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { @@ -65,6 +75,13 @@ vi.mock(import('@/platform/settings/composables/useSettingsDialog')) vi.mock(import('@/platform/telemetry')) +vi.mock(import('@/composables/useFeatureFlags')) + +const mockCreateBillingSdk = vi.hoisted(() => vi.fn<() => BillingSdk>()) +vi.mock(import('@/platform/workspace/billing/sdk/createBillingSdk'), () => ({ + createBillingSdk: mockCreateBillingSdk +})) + vi.mock(import('firebase/auth'), { spy: true }) const mockClearPendingTopup = vi.hoisted(() => vi.fn()) vi.mock(import('@/composables/billing/usePendingTopup'), () => ({ @@ -142,6 +159,27 @@ function setHasSavedPaymentMethod(value: boolean | null) { mockHasSavedPaymentMethod.ref.value = value } +const SPARSE_BILLING_FIELDS: ReadonlySet = new Set([ + 'stage', + 'operation_type', + 'billing_op_id', + 'failure_category', + 'decline_reason' +]) + +function billingEvents(operation: BillingTelemetryEvent['operation']) { + return vi + .mocked(useTelemetry()!.trackBillingEvent) + .mock.calls.filter(([event]) => event.operation === operation) + .map(([event]) => + Object.fromEntries( + Object.entries(event).filter(([field]) => + SPARSE_BILLING_FIELDS.has(field) + ) + ) + ) +} + async function clickAddCredits() { const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Add credits' })) @@ -1128,4 +1166,136 @@ describe('TopUpCreditsDialogContentWorkspace', () => { expect(mockToastAdd).not.toHaveBeenCalled() expect(useDialogStore().closeDialog).not.toHaveBeenCalled() }) + + describe('on the billing SDK rail', () => { + it.for<{ + outcome: string + result: TopupResult + operationTerminal: Record + topupTerminal: Record + }>([ + { + outcome: 'a settled purchase', + result: { + status: 'ok', + operation: settledTopup('succeeded'), + creditsReconciled: true + }, + operationTerminal: { stage: 'succeeded', billing_op_id: 'op-1' }, + topupTerminal: { stage: 'succeeded', billing_op_id: 'op-1' } + }, + { + outcome: 'a purchase this tab stopped watching', + result: { status: 'unsettled', operation: settledTopup('timed_out') }, + operationTerminal: { + stage: 'timeout', + billing_op_id: 'op-1', + failure_category: 'poll_timeout' + }, + topupTerminal: { + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'poll_timeout' + } + }, + { + outcome: 'a decline', + result: { + status: 'declined', + operation: failedTopup('insufficient_funds') + }, + operationTerminal: { + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'provider_decline', + decline_reason: 'insufficient_funds' + }, + topupTerminal: { + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'provider_decline' + } + }, + { + outcome: 'a purchase support must reconcile', + result: { + status: 'unsettled', + operation: settledTopup('reconciliation_needed') + }, + operationTerminal: { + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'reconciliation_needed' + }, + topupTerminal: { + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'reconciliation_needed' + } + }, + { + outcome: 'a refusal that carries no HTTP status', + result: { + status: 'error', + code: 'NO_PAYMENT_METHOD', + recoveryAction: 'replace_payment_method' + }, + operationTerminal: { + stage: 'failed', + failure_category: 'api_rejected' + }, + topupTerminal: { stage: 'failed', failure_category: 'api_rejected' } + }, + { + outcome: 'a purchase the scope moved out from under', + result: { status: 'error', code: 'SUPERSEDED' }, + operationTerminal: { + stage: 'failed', + failure_category: 'stale_operation' + }, + topupTerminal: { stage: 'failed', failure_category: 'stale_operation' } + }, + { + outcome: 'an amount the request contract refused', + result: { status: 'error', code: 'INVALID_AMOUNT' }, + operationTerminal: { stage: 'failed', failure_category: 'validation' }, + topupTerminal: { stage: 'failed', failure_category: 'validation' } + }, + { + outcome: 'a request that never reached the server', + result: { status: 'error', code: 'REQUEST_FAILED' }, + operationTerminal: { stage: 'failed', failure_category: 'network' }, + topupTerminal: { stage: 'failed', failure_category: 'network' } + } + ])( + 'reports $outcome as one terminal carrying what the SDK settled', + async ({ result, operationTerminal, topupTerminal }) => { + const harness = fakeBillingSdk() + mockCreateBillingSdk.mockReturnValue(harness.sdk) + vi.mocked(harness.sdk.topup.createTopupCheckout).mockResolvedValue( + result + ) + vi.mocked(useFeatureFlags().flags).billingSdkTopupRailEnabled = true + vi.spyOn(console, 'error').mockImplementation(() => {}) + + renderDialog() + await clickAddCredits() + await userEvent.click( + screen.getByRole('button', { name: 'Pay $50.00' }) + ) + + await waitFor(() => + expect(billingEvents('operation')).toEqual([ + { stage: 'started', operation_type: 'topup' }, + { operation_type: 'topup', ...operationTerminal } + ]) + ) + expect(billingEvents('topup')).toEqual([ + { stage: 'intent' }, + { stage: 'started' }, + topupTerminal + ]) + } + ) + }) }) diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue index 0cf58c04d19..63cb6c16ac8 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue @@ -303,14 +303,17 @@ import { useTelemetry } from '@/platform/telemetry' import { usePendingTopup } from '@/composables/billing/usePendingTopup' import { isCloud } from '@/platform/distribution/types' import type { + BillingOperationTerminal, CheckoutJourneyPhaseEvent, PaymentIntentSource } from '@/platform/telemetry/types' import { categorizeBillingApiError } from '@/platform/telemetry/utils/billingFailureCategory' import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog' import { reportError } from '@/platform/telemetry/reportError' +import type { CreateTopupResponse } from '@/platform/workspace/api/workspaceApi' import { WorkspaceApiError } from '@/platform/workspace/api/workspaceApi' import { isBlockedOnCustomerPhase } from '@/platform/workspace/billing/customerAttention' +import { UncreditedTopupResponse } from '@/platform/workspace/billing/sdk/topupOperationView' import { useBillingCapabilities } from '@/platform/workspace/composables/useBillingCapabilities' import { useHasSavedPaymentMethod } from '@/platform/workspace/composables/useHasSavedPaymentMethod' import { useTopupOperation } from '@/platform/workspace/composables/useTopupOperation' @@ -623,21 +626,10 @@ async function handleBuy() { const response = await topup(amountCents) if (!response) { if (isCurrentAttempt()) paymentSubmitted.value = false - telemetry?.trackBillingEvent({ - operation: 'topup', + reportTerminal(attemptStartedAt, { stage: 'failed', outcome: 'failure', - payment_intent_source: source, - failure_category: 'unknown', - duration_ms: Date.now() - attemptStartedAt - }) - telemetry?.trackBillingEvent({ - operation: 'operation', - stage: 'failed', - outcome: 'failure', - operation_type: 'topup', - failure_category: 'unknown', - duration_ms: Date.now() - attemptStartedAt + failure_category: 'unknown' }) return } @@ -660,28 +652,14 @@ async function handleBuy() { } } + reportResponseTerminal(response, attemptStartedAt) + if (response.status === 'completed') { if ( getActiveCheckoutJourney()?.billing_op_id === response.billing_op_id ) { clearCheckoutJourney() } - telemetry?.trackBillingEvent({ - operation: 'topup', - stage: 'succeeded', - outcome: 'success', - billing_op_id: response.billing_op_id, - payment_intent_source: source, - duration_ms: Date.now() - attemptStartedAt - }) - telemetry?.trackBillingEvent({ - operation: 'operation', - stage: 'succeeded', - outcome: 'success', - operation_type: 'topup', - billing_op_id: response.billing_op_id, - duration_ms: Date.now() - attemptStartedAt - }) toast.add({ severity: 'success', summary: t('credits.topUp.purchaseSuccess'), @@ -708,26 +686,7 @@ async function handleBuy() { ) }) } else { - // Synchronous 'failed' here means the charge was declined, not rejected pre-attempt. if (isCurrentAttempt()) paymentSubmitted.value = false - telemetry?.trackBillingEvent({ - operation: 'topup', - stage: 'failed', - outcome: 'failure', - billing_op_id: response.billing_op_id, - payment_intent_source: source, - failure_category: 'provider_decline', - duration_ms: Date.now() - attemptStartedAt - }) - telemetry?.trackBillingEvent({ - operation: 'operation', - stage: 'failed', - outcome: 'failure', - operation_type: 'topup', - billing_op_id: response.billing_op_id, - failure_category: 'provider_decline', - duration_ms: Date.now() - attemptStartedAt - }) toast.add({ severity: 'error', summary: t('credits.topUp.purchaseError'), @@ -750,30 +709,87 @@ function reportPurchaseError( if (currentAttempt) paymentSubmitted.value = false console.error('Purchase failed', ...(error === undefined ? [] : [error])) - telemetry?.trackBillingEvent({ - operation: 'topup', + reportTerminal( + attemptStartedAt, + { + stage: 'failed', + outcome: 'failure', + failure_category: + error === undefined ? 'unknown' : categorizeBillingApiError(error) + }, + billingOpId + ) + toast.add({ + severity: 'error', + summary: t('credits.topUp.purchaseError'), + detail: purchaseErrorDetail(error) + }) +} + +/** + * The terminal a response's status implies. A synchronous `failed` is a + * declined charge, not a pre-attempt rejection; a `pending` one is the + * poller's to report once it settles. + */ +const RESPONSE_TERMINALS: Record< + CreateTopupResponse['status'], + BillingOperationTerminal | undefined +> = { + completed: { stage: 'succeeded', outcome: 'success' }, + failed: { stage: 'failed', outcome: 'failure', + failure_category: 'provider_decline' + }, + pending: undefined +} + +function reportResponseTerminal( + response: CreateTopupResponse, + attemptStartedAt: number +) { + const terminal = + response instanceof UncreditedTopupResponse + ? response.terminal + : RESPONSE_TERMINALS[response.status] + if (terminal) { + reportTerminal(attemptStartedAt, terminal, response.billing_op_id) + } +} + +function reportTerminal( + attemptStartedAt: number, + terminal: BillingOperationTerminal, + billingOpId?: string +) { + const attempt = { ...(billingOpId ? { billing_op_id: billingOpId } : {}), - payment_intent_source: source, - failure_category: - error === undefined ? 'unknown' : categorizeBillingApiError(error), duration_ms: Date.now() - attemptStartedAt - }) + } + // No timeout stage on the top-up funnel; the poller reports it as failed too. + telemetry?.trackBillingEvent( + terminal.stage === 'succeeded' + ? { + operation: 'topup', + stage: 'succeeded', + outcome: 'success', + payment_intent_source: source, + ...attempt + } + : { + operation: 'topup', + stage: 'failed', + outcome: 'failure', + payment_intent_source: source, + failure_category: terminal.failure_category, + ...attempt + } + ) telemetry?.trackBillingEvent({ operation: 'operation', - stage: 'failed', - outcome: 'failure', operation_type: 'topup', - ...(billingOpId ? { billing_op_id: billingOpId } : {}), - failure_category: - error === undefined ? 'unknown' : categorizeBillingApiError(error), - duration_ms: Date.now() - attemptStartedAt - }) - toast.add({ - severity: 'error', - summary: t('credits.topUp.purchaseError'), - detail: purchaseErrorDetail(error) + ...terminal, + ...attempt }) }