From 5ae4c8d1b6483092c1d63d32e397ae85d4b9ff64 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Fri, 2 Oct 2026 17:55:39 -0400 Subject: [PATCH 1/4] feat(billing): add the checkout challenge events to the shared telemetry contract --- .../account-core/src/core/billing/index.ts | 5 +- .../src/core/billing/operationLifecycle.ts | 12 +++- .../telemetry/billingTelemetryEvent.ts | 2 + .../telemetry/checkoutChallengeEvent.ts | 27 ++++++++ .../telemetry/operationLifecycleEvent.test.ts | 62 +++++++++++++++++++ .../telemetry/operationLifecycleEvent.ts | 28 ++++++++- packages/account-core/src/telemetry.ts | 10 +++ 7 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 packages/account-core/src/core/billing/telemetry/checkoutChallengeEvent.ts diff --git a/packages/account-core/src/core/billing/index.ts b/packages/account-core/src/core/billing/index.ts index 8a6e1905a0c..0c4843c662c 100644 --- a/packages/account-core/src/core/billing/index.ts +++ b/packages/account-core/src/core/billing/index.ts @@ -169,7 +169,10 @@ export { createBillingOperationLifecycle, operationRoute } from './operationLifecycle.js' -export { BILLING_OPERATION_TELEMETRY_EVENT } from '../../telemetry.js' +export { + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT, + BILLING_OPERATION_TELEMETRY_EVENT +} from '../../telemetry.js' export * from './telemetry/index.js' export type { EmbeddedChallengeOutcome, diff --git a/packages/account-core/src/core/billing/operationLifecycle.ts b/packages/account-core/src/core/billing/operationLifecycle.ts index 27323ac6b12..2844f8ee91a 100644 --- a/packages/account-core/src/core/billing/operationLifecycle.ts +++ b/packages/account-core/src/core/billing/operationLifecycle.ts @@ -17,7 +17,11 @@ * contract is new. */ -import { BILLING_OPERATION_TELEMETRY_EVENT } from '../../telemetry.js' +import type { + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT} from '../../telemetry.js'; +import { + BILLING_OPERATION_TELEMETRY_EVENT +} from '../../telemetry.js' import type { BillingFailure, BillingResult, @@ -90,8 +94,12 @@ export type BillingOperationFailureCategory = | 'reconciliation_needed' | 'stale_operation' +type BillingOperationTelemetryEventName = + | (typeof BILLING_OPERATION_TELEMETRY_EVENT)[keyof typeof BILLING_OPERATION_TELEMETRY_EVENT] + | (typeof BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT)[keyof typeof BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT] + export interface BillingOperationTelemetryEvent { - readonly name: (typeof BILLING_OPERATION_TELEMETRY_EVENT)[keyof typeof BILLING_OPERATION_TELEMETRY_EVENT] + readonly name: BillingOperationTelemetryEventName readonly billing_op_id: string readonly operation_type: BillingOperationKind readonly presentation: BillingPresentation diff --git a/packages/account-core/src/core/billing/telemetry/billingTelemetryEvent.ts b/packages/account-core/src/core/billing/telemetry/billingTelemetryEvent.ts index 631b65aecae..e12def8cdb0 100644 --- a/packages/account-core/src/core/billing/telemetry/billingTelemetryEvent.ts +++ b/packages/account-core/src/core/billing/telemetry/billingTelemetryEvent.ts @@ -1,4 +1,5 @@ import type { CapabilityReadBillingEvent } from './capabilityReadEvent.js' +import type { CheckoutChallengeBillingEvent } from './checkoutChallengeEvent.js' import type { DowngradeToPersonalBillingEvent } from './downgradeToPersonalEvent.js' import type { BillingOperationBillingEvent } from './operationEvent.js' import type { ResubscribeBillingEvent } from './resubscribeEvent.js' @@ -27,6 +28,7 @@ export type BillingTelemetryEvent = { | WebHandoffBillingEvent | WebReturnBillingEvent | WebSessionBillingEvent + | CheckoutChallengeBillingEvent ) type BillingTelemetryEventNameFor = diff --git a/packages/account-core/src/core/billing/telemetry/checkoutChallengeEvent.ts b/packages/account-core/src/core/billing/telemetry/checkoutChallengeEvent.ts new file mode 100644 index 00000000000..70d32359b9f --- /dev/null +++ b/packages/account-core/src/core/billing/telemetry/checkoutChallengeEvent.ts @@ -0,0 +1,27 @@ +import type { + BillingDeclineReason, + BillingOperationKind, + BillingPresentation +} from '../operationState.js' + +/** + * Payment friction inside one operation: a bank challenge the customer must + * complete, the verdict this tab saw, and each retryable decline the server + * reports while the operation stays open. None of these settle the + * operation, so every stage stays `pending`. A failure with a + * `decline_reason` is the server's retryable decline; one without is this + * tab's own challenge verdict. + */ +export type CheckoutChallengeBillingEvent = { + operation: 'checkout' + outcome: 'pending' + billing_op_id: string + operation_type: BillingOperationKind + presentation: BillingPresentation + /** True when this tab reattached to an operation it did not issue. */ + resumed?: boolean +} & ( + | { stage: 'challenge_required' } + | { stage: 'challenge_completed' } + | { stage: 'challenge_failed'; decline_reason?: BillingDeclineReason } +) diff --git a/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.test.ts b/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.test.ts index 33c55d5908b..1b8a67f7f41 100644 --- a/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.test.ts +++ b/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.test.ts @@ -124,6 +124,68 @@ describe('toBillingTelemetryEvent', () => { failure_category: 'poll_timeout', duration_ms: 600_000 } + }, + { + name: 'a required challenge onto the checkout, never as an operation stage', + event: { + name: 'billing.checkout.challenge_required', + billing_op_id: 'op-challenge', + operation_type: 'subscription', + presentation: 'embedded', + resumed: false + }, + expected: { + operation: 'checkout', + billing_client: 'sdk', + stage: 'challenge_required', + outcome: 'pending', + operation_type: 'subscription', + billing_op_id: 'op-challenge', + presentation: 'embedded', + resumed: false + } + }, + { + name: 'a completed challenge as still pending, since the server settles the payment', + event: { + name: 'billing.checkout.challenge_completed', + billing_op_id: 'op-challenge', + operation_type: 'topup', + presentation: 'embedded', + resumed: true + }, + expected: { + operation: 'checkout', + billing_client: 'sdk', + stage: 'challenge_completed', + outcome: 'pending', + operation_type: 'topup', + billing_op_id: 'op-challenge', + presentation: 'embedded', + resumed: true + } + }, + { + name: 'a retryable decline with its reason', + event: { + name: 'billing.checkout.challenge_failed', + billing_op_id: 'op-retry', + operation_type: 'subscription', + presentation: 'hosted', + resumed: false, + decline_reason: 'card_declined' + }, + expected: { + operation: 'checkout', + billing_client: 'sdk', + stage: 'challenge_failed', + outcome: 'pending', + operation_type: 'subscription', + billing_op_id: 'op-retry', + presentation: 'hosted', + resumed: false, + decline_reason: 'card_declined' + } } ])('maps $name', ({ event, expected }) => { expect(toBillingTelemetryEvent(event)).toEqual(expected) diff --git a/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.ts b/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.ts index f98e33ba7d7..0df58612b3e 100644 --- a/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.ts +++ b/packages/account-core/src/core/billing/telemetry/operationLifecycleEvent.ts @@ -1,15 +1,27 @@ -import { BILLING_OPERATION_TELEMETRY_EVENT } from '../../../telemetry.js' +import { + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT, + BILLING_OPERATION_TELEMETRY_EVENT +} from '../../../telemetry.js' import type { BillingOperationTelemetryEvent } from '../operationLifecycle.js' import type { BillingTelemetryEvent } from './billingTelemetryEvent.js' /** * The lifecycle's event onto the poller's `billing.operation.*` stages, so * both rails count in one funnel; only this rail sets `presentation` and - * `resumed`. + * `resumed`. Friction inside the operation maps onto `billing.checkout.*`. */ export function toBillingTelemetryEvent( event: BillingOperationTelemetryEvent ): BillingTelemetryEvent { + const friction = { + operation: 'checkout', + outcome: 'pending', + billing_client: 'sdk', + operation_type: event.operation_type, + billing_op_id: event.billing_op_id, + presentation: event.presentation, + resumed: event.resumed + } as const const shared = { operation: 'operation', billing_client: 'sdk', @@ -42,5 +54,17 @@ export function toBillingTelemetryEvent( failure_category: event.failure_category ?? 'provider_decline', decline_reason: event.decline_reason } + case BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeRequired: + return { ...friction, stage: 'challenge_required' } + case BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeCompleted: + return { ...friction, stage: 'challenge_completed' } + case BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeFailed: + return { + ...friction, + stage: 'challenge_failed', + ...(event.decline_reason === undefined + ? {} + : { decline_reason: event.decline_reason }) + } } } diff --git a/packages/account-core/src/telemetry.ts b/packages/account-core/src/telemetry.ts index 7497b3f872a..a373874ff5a 100644 --- a/packages/account-core/src/telemetry.ts +++ b/packages/account-core/src/telemetry.ts @@ -21,6 +21,16 @@ export const BILLING_OPERATION_TELEMETRY_EVENT = { timeout: 'billing.operation.timeout' } as const +/** + * Payment friction the lifecycle observes inside one operation: a bank + * challenge and its verdict, and each retryable decline. + */ +export const BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT = { + challengeRequired: 'billing.checkout.challenge_required', + challengeCompleted: 'billing.checkout.challenge_completed', + challengeFailed: 'billing.checkout.challenge_failed' +} as const + export const AUTH_TELEMETRY_EVENT = { signUpOpened: 'app:user_sign_up_opened', authCompleted: 'app:user_auth_completed', From c8d44b5db0b5e85deb379010dddb247dbaa438a5 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Fri, 2 Oct 2026 17:56:42 -0400 Subject: [PATCH 2/4] test(billing): expect the lifecycle to report challenges and retryable declines --- .../core/billing/operationLifecycle.test.ts | 186 ++++++++++++++++++ .../billing/sdk/billingSdkStore.test.ts | 28 +++ 2 files changed, 214 insertions(+) diff --git a/packages/account-core/src/core/billing/operationLifecycle.test.ts b/packages/account-core/src/core/billing/operationLifecycle.test.ts index 1a41c674c9a..12439defd66 100644 --- a/packages/account-core/src/core/billing/operationLifecycle.test.ts +++ b/packages/account-core/src/core/billing/operationLifecycle.test.ts @@ -1553,3 +1553,189 @@ describe('createBillingOperationLifecycle', () => { }) }) }) + +describe('payment friction telemetry', () => { + function frictionOf(telemetry: BillingOperationTelemetryEvent[]) { + return telemetry + .filter((event) => event.name.startsWith('billing.checkout.')) + .map(({ name, presentation, decline_reason }) => + decline_reason === undefined + ? { name, presentation } + : { name, presentation, decline_reason } + ) + } + + const challengeRequired = (clientSecret: string) => + httpOk( + opStatus({ + authentication_state: 'requires_action', + payment_intent_client_secret: clientSecret + }) + ) + + it('reports a challenge the issued command already carried, with its operation', async () => { + const { lifecycle, telemetry } = harness({ + embedded: true, + answers: [challengeRequired('pi_secret')] + }) + + await lifecycle.begin( + 'subscription', + issued({ operationId: 'op-1', clientSecret: 'pi_secret' }) + ) + await flush() + + expect( + telemetry.filter((event) => event.name.startsWith('billing.checkout.')) + ).toEqual([ + { + name: 'billing.checkout.challenge_required', + billing_op_id: 'op-1', + operation_type: 'subscription', + presentation: 'embedded', + resumed: false + } + ]) + }) + + it('reports a challenge a status read surfaces once, however often it is echoed', async () => { + const { lifecycle, telemetry } = harness({ + embedded: true, + answers: [httpOk(opStatus()), challengeRequired('pi_secret')] + }) + + await lifecycle.begin('topup', issued()) + await flush() + await vi.advanceTimersByTimeAsync(OPERATION_POLL_TIMING.parkedMs * 3) + + expect(frictionOf(telemetry)).toEqual([ + { name: 'billing.checkout.challenge_required', presentation: 'embedded' } + ]) + }) + + it.for([ + { outcome: 'completed', expected: 'billing.checkout.challenge_completed' }, + { outcome: 'failed', expected: 'billing.checkout.challenge_failed' } + ] as const)( + 'reports the challenge this tab drove as $outcome', + async ({ outcome, expected }) => { + const { lifecycle, telemetry } = harness({ + embedded: true, + answers: [challengeRequired('pi_secret')] + }) + await lifecycle.begin( + 'topup', + issued({ operationId: 'op-1', clientSecret: 'pi_secret' }) + ) + await flush() + + lifecycle.reportChallengeStarted('op-1') + lifecycle.reportChallengeSettled('op-1', outcome) + + expect(frictionOf(telemetry)).toEqual([ + { + name: 'billing.checkout.challenge_required', + presentation: 'embedded' + }, + { name: expected, presentation: 'embedded' } + ]) + } + ) + + it('reports a failed challenge once when the server then echoes it as a retryable decline', async () => { + const { lifecycle, telemetry } = harness({ + embedded: true, + answers: [ + challengeRequired('pi_secret'), + httpOk( + opStatus({ + authentication_state: 'failed_retryable', + decline_reason: 'authentication_failed', + payment_intent_client_secret: 'pi_secret' + }) + ) + ] + }) + await lifecycle.begin( + 'topup', + issued({ operationId: 'op-1', clientSecret: 'pi_secret' }) + ) + await flush() + + lifecycle.reportChallengeStarted('op-1') + lifecycle.reportChallengeSettled('op-1', 'failed') + await vi.advanceTimersByTimeAsync(OPERATION_POLL_TIMING.parkedMs * 2) + + expect(lifecycle.get('op-1')).toMatchObject({ + declineReason: 'authentication_failed' + }) + expect(frictionOf(telemetry)).toEqual([ + { name: 'billing.checkout.challenge_required', presentation: 'embedded' }, + { name: 'billing.checkout.challenge_failed', presentation: 'embedded' } + ]) + }) + + it('reports each retryable decline inside one hosted operation with its reason, once per decline', async () => { + const declined = (reason: BillingOpStatus['decline_reason']) => + httpOk( + opStatus({ + authentication_state: 'failed_retryable', + decline_reason: reason, + action_url: 'https://billing.example/continue' + }) + ) + const { lifecycle, telemetry } = harness({ + answers: [ + declined('card_declined'), + declined('card_declined'), + declined('insufficient_funds') + ] + }) + + await lifecycle.begin('subscription', issued()) + await flush() + await vi.advanceTimersByTimeAsync(OPERATION_POLL_TIMING.parkedMs * 3) + + expect(frictionOf(telemetry)).toEqual([ + { + name: 'billing.checkout.challenge_failed', + presentation: 'hosted', + decline_reason: 'card_declined' + }, + { + name: 'billing.checkout.challenge_failed', + presentation: 'hosted', + decline_reason: 'insufficient_funds' + } + ]) + }) + + it('does not report the same challenge again when it returns from the hosted page', async () => { + const { lifecycle, telemetry } = harness({ + embedded: true, + answers: [ + challengeRequired('pi_secret'), + httpOk( + opStatus({ + authentication_state: 'requires_action', + payment_intent_client_secret: 'pi_secret', + action_url: 'https://billing.example/continue' + }) + ) + ] + }) + await lifecycle.begin( + 'topup', + issued({ operationId: 'op-1', clientSecret: 'pi_secret' }) + ) + await flush() + await vi.advanceTimersByTimeAsync(OPERATION_POLL_TIMING.parkedMs) + + lifecycle.switchPresentation('op-1', 'hosted') + lifecycle.switchPresentation('op-1', 'embedded') + + expect(frictionOf(telemetry)).toEqual([ + { name: 'billing.checkout.challenge_required', presentation: 'embedded' } + ]) + }) +}) diff --git a/src/platform/workspace/billing/sdk/billingSdkStore.test.ts b/src/platform/workspace/billing/sdk/billingSdkStore.test.ts index edfa60172ed..5781915da84 100644 --- a/src/platform/workspace/billing/sdk/billingSdkStore.test.ts +++ b/src/platform/workspace/billing/sdk/billingSdkStore.test.ts @@ -1070,6 +1070,34 @@ describe('useBillingSdkStore telemetry ownership', () => { } ) + it.for(['topup', 'subscription'] as const)( + 'reports the payment friction of a %s this tab issued, which no dialog reports', + (kind) => { + useBillingSdkStore() + + options.onTelemetry({ + name: 'billing.checkout.challenge_failed', + billing_op_id: 'op-1', + operation_type: kind, + presentation: 'embedded', + resumed: false, + decline_reason: 'authentication_failed' + }) + + expect(useTelemetry()?.trackBillingEvent).toHaveBeenCalledWith({ + operation: 'checkout', + billing_client: 'sdk', + stage: 'challenge_failed', + outcome: 'pending', + operation_type: kind, + billing_op_id: 'op-1', + presentation: 'embedded', + resumed: false, + decline_reason: 'authentication_failed' + }) + } + ) + it('reports a started that arrives once the announced command has settled', async () => { const store = useBillingSdkStore() vi.mocked(harness.sdk.commands.cancelSubscription).mockResolvedValue( From 8c37a94b48c63875b491c6b4862d7a295dece98f Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Fri, 2 Oct 2026 17:59:29 -0400 Subject: [PATCH 3/4] feat(billing): report 3DS challenges and retryable declines from the operation lifecycle --- .../src/session/billingWebClient.test.ts | 30 ++++++++++ .../src/core/billing/operationLifecycle.ts | 39 ++++++++++-- .../src/core/billing/paymentFriction.ts | 60 +++++++++++++++++++ .../workspace/billing/sdk/billingSdkStore.ts | 14 +++++ 4 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 packages/account-core/src/core/billing/paymentFriction.ts diff --git a/apps/billing-web/src/session/billingWebClient.test.ts b/apps/billing-web/src/session/billingWebClient.test.ts index a521cb932db..34fae25016c 100644 --- a/apps/billing-web/src/session/billingWebClient.test.ts +++ b/apps/billing-web/src/session/billingWebClient.test.ts @@ -252,6 +252,36 @@ describe('SDK operation telemetry', () => { } ) + it('reports a retryable decline inside an issued operation, as the SDK on billing web', async () => { + stubBillingRoutes({ + status: 'pending', + authentication_state: 'failed_retryable', + decline_reason: 'card_declined' + }) + const client = createBillingWebClient(authenticatedSession()) + onTestFinished(() => disposeBillingClient(client)) + + await client.lifecycle.begin('subscription', issueOperation) + + await vi.waitFor(() => + expect(datadogRum.addAction).toHaveBeenCalledWith( + 'billing.checkout.challenge_failed', + { + operation: 'checkout', + stage: 'challenge_failed', + outcome: 'pending', + operation_type: 'subscription', + billing_op_id: 'op_1', + presentation: 'hosted', + resumed: false, + decline_reason: 'card_declined', + billing_client: 'sdk', + billing_surface: 'billing_web' + } + ) + ) + }) + it.for<{ name: string run: (client: BillingClient) => Promise diff --git a/packages/account-core/src/core/billing/operationLifecycle.ts b/packages/account-core/src/core/billing/operationLifecycle.ts index 2844f8ee91a..fe1503427f7 100644 --- a/packages/account-core/src/core/billing/operationLifecycle.ts +++ b/packages/account-core/src/core/billing/operationLifecycle.ts @@ -17,9 +17,8 @@ * contract is new. */ -import type { - BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT} from '../../telemetry.js'; import { + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT, BILLING_OPERATION_TELEMETRY_EVENT } from '../../telemetry.js' import type { @@ -65,6 +64,8 @@ import { reduceBillingOperation, validateActionUrl } from './operationState.js' +import type { PaymentFrictionSignal } from './paymentFriction.js' +import { paymentFrictionBetween } from './paymentFriction.js' import { selectBillingPresentation } from './presentation.js' import { readValidatedBillingResponse } from './sharedRead.js' import type { BillingStatusData, BillingStatusReader } from './status.js' @@ -319,6 +320,14 @@ function failureCategoryFor( } } +const FRICTION_EVENT_NAME = { + challenge_required: + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeRequired, + challenge_completed: + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeCompleted, + challenge_failed: BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeFailed +} as const satisfies Record + /** The pause is this tab's own challenge on screen; a hosted page never pauses. */ function isDrivingChallenge(state: PendingBillingOperation): boolean { return ( @@ -398,10 +407,31 @@ export function createBillingOperationLifecycle( }) } + function emitFrictionTelemetry( + record: OperationRecord, + before: BillingOperationState | undefined + ) { + const state = record.state + for (const signal of paymentFrictionBetween(before, state)) { + onTelemetry?.({ + name: FRICTION_EVENT_NAME[signal.stage], + billing_op_id: state.id, + operation_type: state.kind, + presentation: state.presentation, + resumed: record.resumed, + ...('declineReason' in signal && signal.declineReason !== undefined + ? { decline_reason: signal.declineReason } + : {}) + }) + } + } + function dispatch(record: OperationRecord, event: BillingOperationEvent) { - const next = reduceBillingOperation(record.state, event) - if (next === record.state) return + const before = record.state + const next = reduceBillingOperation(before, event) + if (next === before) return record.state = next + emitFrictionTelemetry(record, before) if (isTerminal(next)) { stopTimer(record) pointers.settle(next) @@ -574,6 +604,7 @@ export function createBillingOperationLifecycle( presentation: input.presentation, resumed: input.resumed }) + emitFrictionTelemetry(record, undefined) startObserving(record, input, state) return record } diff --git a/packages/account-core/src/core/billing/paymentFriction.ts b/packages/account-core/src/core/billing/paymentFriction.ts new file mode 100644 index 00000000000..6eb0c5e78ab --- /dev/null +++ b/packages/account-core/src/core/billing/paymentFriction.ts @@ -0,0 +1,60 @@ +import type { + BillingDeclineReason, + BillingOperationState, + EmbeddedChallenge, + PendingBillingOperation +} from './operationState.js' + +export type PaymentFrictionSignal = + | { readonly stage: 'challenge_required' } + | { readonly stage: 'challenge_completed' } + | { + readonly stage: 'challenge_failed' + readonly declineReason?: BillingDeclineReason + } + +/** + * What one transition of a pending operation put in the customer's way: a + * challenge first required under a client secret, this tab's verdict on it, + * or a retryable decline with a reason the operation did not hold before. + * A decline the server reports for a challenge this tab already saw fail is + * the same failure, so it is not reported twice. + */ +export function paymentFrictionBetween( + before: BillingOperationState | undefined, + after: BillingOperationState +): readonly PaymentFrictionSignal[] { + if (after.phase !== 'pending') return [] + const earlier = before?.phase === 'pending' ? before : undefined + return [ + ...challengeSignals(earlier?.challenge, after.challenge), + ...declineSignals(earlier, after) + ] +} + +function challengeSignals( + before: EmbeddedChallenge | undefined, + after: EmbeddedChallenge | undefined +): readonly PaymentFrictionSignal[] { + if (after === undefined) return [] + if (after.clientSecret !== before?.clientSecret) { + return after.status === 'required' ? [{ stage: 'challenge_required' }] : [] + } + if (after.status === before.status) return [] + if (after.status === 'completed') return [{ stage: 'challenge_completed' }] + if (after.status === 'failed') return [{ stage: 'challenge_failed' }] + return [] +} + +function declineSignals( + before: PendingBillingOperation | undefined, + after: PendingBillingOperation +): readonly PaymentFrictionSignal[] { + const reason = after.declineReason + if (reason === undefined || reason === before?.declineReason) return [] + const echoesFailedChallenge = + after.presentation === 'embedded' && after.challenge?.status === 'failed' + return echoesFailedChallenge + ? [] + : [{ stage: 'challenge_failed', declineReason: reason }] +} diff --git a/src/platform/workspace/billing/sdk/billingSdkStore.ts b/src/platform/workspace/billing/sdk/billingSdkStore.ts index e25448663df..d1885df90c2 100644 --- a/src/platform/workspace/billing/sdk/billingSdkStore.ts +++ b/src/platform/workspace/billing/sdk/billingSdkStore.ts @@ -18,6 +18,7 @@ import type { SubscribeInput } from '@comfyorg/account-core/billing' import { + BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT, BILLING_OPERATION_TELEMETRY_EVENT, toBillingTelemetryEvent, validateActionUrl @@ -95,6 +96,14 @@ const PROGRESS_SUMMARY = { } as const satisfies Record> type ToastMessage = Parameters['add']>[0] +const FRICTION_EVENT_NAMES: ReadonlySet = new Set( + Object.values(BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT) +) + +function isFrictionEvent(event: BillingOperationTelemetryEvent): boolean { + return FRICTION_EVENT_NAMES.has(event.name) +} + async function loadChallengePort(): Promise { const publishableKey = resolveStripePublishableKey() const stripe = publishableKey @@ -242,7 +251,12 @@ export const useBillingSdkStore = defineStore('billingSdk', () => { // forwarded, since only the lifecycle holds the operation's id, category // and timing. A command no caller announced — a resubscribe, a checkout // that reports nothing — is reported at both ends by the lifecycle. + // Friction inside an operation has no other reporter, so it always goes. function reportTelemetry(event: BillingOperationTelemetryEvent) { + if (isFrictionEvent(event)) { + useTelemetry()?.trackBillingEvent(toBillingTelemetryEvent(event)) + return + } if (event.operation_type === 'topup' && !event.resumed) return const started = event.name === BILLING_OPERATION_TELEMETRY_EVENT.started if (started && event.resumed) resumedOperations.add(event.billing_op_id) From 67d5cf89205fe3f66b925a1ed89851420d731e17 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Fri, 2 Oct 2026 20:50:19 -0400 Subject: [PATCH 4/4] fix(billing): report a later decline after an echoed failed challenge --- .../core/billing/operationLifecycle.test.ts | 38 +++++++++++++++++++ .../src/core/billing/paymentFriction.ts | 8 ++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/packages/account-core/src/core/billing/operationLifecycle.test.ts b/packages/account-core/src/core/billing/operationLifecycle.test.ts index 12439defd66..63388563a05 100644 --- a/packages/account-core/src/core/billing/operationLifecycle.test.ts +++ b/packages/account-core/src/core/billing/operationLifecycle.test.ts @@ -1675,6 +1675,44 @@ describe('payment friction telemetry', () => { ]) }) + it('reports a later, different decline after the failed challenge was echoed', async () => { + const declined = (reason: BillingOpStatus['decline_reason']) => + httpOk( + opStatus({ + authentication_state: 'failed_retryable', + decline_reason: reason, + payment_intent_client_secret: 'pi_secret' + }) + ) + const { lifecycle, telemetry } = harness({ + embedded: true, + answers: [ + challengeRequired('pi_secret'), + declined('authentication_failed'), + declined('card_declined') + ] + }) + await lifecycle.begin( + 'topup', + issued({ operationId: 'op-1', clientSecret: 'pi_secret' }) + ) + await flush() + + lifecycle.reportChallengeStarted('op-1') + lifecycle.reportChallengeSettled('op-1', 'failed') + await vi.advanceTimersByTimeAsync(OPERATION_POLL_TIMING.parkedMs * 3) + + expect(frictionOf(telemetry)).toEqual([ + { name: 'billing.checkout.challenge_required', presentation: 'embedded' }, + { name: 'billing.checkout.challenge_failed', presentation: 'embedded' }, + { + name: 'billing.checkout.challenge_failed', + presentation: 'embedded', + decline_reason: 'card_declined' + } + ]) + }) + it('reports each retryable decline inside one hosted operation with its reason, once per decline', async () => { const declined = (reason: BillingOpStatus['decline_reason']) => httpOk( diff --git a/packages/account-core/src/core/billing/paymentFriction.ts b/packages/account-core/src/core/billing/paymentFriction.ts index 6eb0c5e78ab..c2812bf4b29 100644 --- a/packages/account-core/src/core/billing/paymentFriction.ts +++ b/packages/account-core/src/core/billing/paymentFriction.ts @@ -17,8 +17,8 @@ export type PaymentFrictionSignal = * What one transition of a pending operation put in the customer's way: a * challenge first required under a client secret, this tab's verdict on it, * or a retryable decline with a reason the operation did not hold before. - * A decline the server reports for a challenge this tab already saw fail is - * the same failure, so it is not reported twice. + * The first decline the server reports for a challenge this tab already saw + * fail is the same failure, so it is not reported twice. */ export function paymentFrictionBetween( before: BillingOperationState | undefined, @@ -53,7 +53,9 @@ function declineSignals( const reason = after.declineReason if (reason === undefined || reason === before?.declineReason) return [] const echoesFailedChallenge = - after.presentation === 'embedded' && after.challenge?.status === 'failed' + after.presentation === 'embedded' && + after.challenge?.status === 'failed' && + before?.declineReason === undefined return echoesFailedChallenge ? [] : [{ stage: 'challenge_failed', declineReason: reason }]