diff --git a/apps/billing-web/README.md b/apps/billing-web/README.md index 198cf5fa0df..4b9fa1fbb7d 100644 --- a/apps/billing-web/README.md +++ b/apps/billing-web/README.md @@ -129,8 +129,12 @@ so the funnel compares per surface. The journey id is the entry link's `correlation_id`, or a fresh one for a link that carries none, and `payment_intent_source` on `entered` is the link's `source`. `preview_ready` and `preview_failed` report each quote once; a refused capability, plan or -quote names a bounded `denial_reason` or `error_code`. `entry_flow` is the -quote's, so it reads `unknown` on `entered`. No event carries a promo code, +quote names a bounded `denial_reason` or `error_code`. The payment form's +`payment_element_*` and `payment_submit_*` phases pass through as the form +reports them, `submitted` fires on each Pay that goes ahead, and +`operation_linked` carries the operation that Pay issued, never one the +checkout recovered. `entry_flow` is the quote's, so it reads `unknown` on +`entered`. No event carries a promo code, an email, a URL, a client secret or a provider id. ## Commands diff --git a/apps/billing-web/src/composables/useCheckoutJourney.ts b/apps/billing-web/src/composables/useCheckoutJourney.ts index 4e023536def..5eb26e12b2a 100644 --- a/apps/billing-web/src/composables/useCheckoutJourney.ts +++ b/apps/billing-web/src/composables/useCheckoutJourney.ts @@ -33,6 +33,8 @@ export function useCheckoutJourney( const enteredAt = new Date().toISOString() let entryFlow: CheckoutEntryFlow = 'unknown' let lastPreviewRevision: string | undefined + let awaitingOperation = false + let billingOpId: string | undefined function track(phase: CheckoutJourneyPhaseEvent) { billingWebTelemetry.trackCheckoutJourneyEvent({ @@ -42,6 +44,7 @@ export function useCheckoutJourney( ui_mode: uiMode, entry_flow: entryFlow, entry_source: entrySourceOf(arrival?.source), + ...(billingOpId !== undefined && { billing_op_id: billingOpId }), ...phase }) } @@ -68,6 +71,20 @@ export function useCheckoutJourney( track(phase) } + function submitted() { + awaitingOperation = true + billingOpId = undefined + track({ phase: 'submitted' }) + } + + /** Only the operation a press of Pay issued is linked, never one the checkout recovered. */ + function operationIssued(operationId: string) { + if (!awaitingOperation) return + awaitingOperation = false + billingOpId = operationId + track({ phase: 'operation_linked', billing_op_id: operationId }) + } + /** A quote answer, from the embedded checkout. */ function quoted(result: PreviewSubscribeResult) { if (result.status === 'ok' && result.value.allowed) { @@ -84,9 +101,10 @@ export function useCheckoutJourney( shown: SubscriptionPreview | undefined ) { if (event.type === 'quoted' && shown) return previewReady(shown) + if (event.type === 'paySubmitted') return submitted() const failed = previewFailureOfPageEvent(event) if (failed) track(failed) } - return { enter, track, quoted, observe } + return { enter, track, submitted, operationIssued, quoted, observe } } diff --git a/apps/billing-web/src/composables/useFullPageCheckout.ts b/apps/billing-web/src/composables/useFullPageCheckout.ts index e376d3aefb7..2ab537b5d29 100644 --- a/apps/billing-web/src/composables/useFullPageCheckout.ts +++ b/apps/billing-web/src/composables/useFullPageCheckout.ts @@ -456,6 +456,7 @@ export function useFullPageCheckout() { watch(checkout.operation, (operation) => { if (operation === undefined) return + journey.operationIssued(operation.id) const own = page.value.kind === 'capture' && page.value.attempt.kind === 'sent' dispatch({ @@ -537,6 +538,7 @@ export function useFullPageCheckout() { ) function onPaymentPhase(phase: StripePaymentPhase) { + journey.track(phase) if (phase.phase === 'payment_element_ready' && phase.element === 'payment') dispatch({ type: 'elementReady' }) else if (phase.phase === 'payment_element_failed') diff --git a/apps/billing-web/src/views/CheckoutView.journey.test.ts b/apps/billing-web/src/views/CheckoutView.journey.test.ts index 54a9bc4bf35..0664a5c86d4 100644 --- a/apps/billing-web/src/views/CheckoutView.journey.test.ts +++ b/apps/billing-web/src/views/CheckoutView.journey.test.ts @@ -1,5 +1,5 @@ import { datadogRum } from '@datadog/browser-rum' -import { render, waitFor } from '@testing-library/vue' +import { render, screen, waitFor } from '@testing-library/vue' import { defineComponent, h, ref } from 'vue' import { createMemoryHistory, createRouter } from 'vue-router' @@ -8,6 +8,7 @@ import type { SubscriptionPreview } from '@comfyorg/account-core/billing' import { BILLING_CLIENT_KEY } from '@comfyorg/account-ui/billing' +import type { StripePaymentPhase } from '@comfyorg/account-ui/billing/stripe' import { parseBillingEntry } from '@comfyorg/billing-contract' import { recordBillingEntry } from '@/entry/billingEntry' @@ -16,7 +17,12 @@ import type { FakeBillingClient, FakeBillingClientOptions } from '@/test/fakeBillingClient' -import { createFakeBillingClient, previewOf } from '@/test/fakeBillingClient' +import { + createFakeBillingClient, + pendingOperation, + previewOf, + succeededOperation +} from '@/test/fakeBillingClient' import { WORKSPACE_INVITES_KEY } from '@/session/workspaceInvites' import CheckoutView from '@/views/CheckoutView.vue' @@ -59,19 +65,29 @@ vi.mock(import('@/session/stripeChallengePort'), () => ({ }) })) +let reportPhase: (phase: StripePaymentPhase) => void = () => {} +let reportConfirm: (confirmationToken: string) => void = () => {} + /** The card form is covered in its package; here it takes the place of the provider's form. */ const PaymentFormStub = defineComponent({ name: 'CheckoutPaymentForm', props: { submitLabel: { type: String, default: '' }, + isLoading: { type: Boolean, default: false }, canSubmit: { type: Boolean, default: true } }, emits: ['confirm', 'phase'], - setup(props) { + setup(props, { emit }) { + reportPhase = (phase) => emit('phase', phase) + reportConfirm = (token) => emit('confirm', token) return () => h( 'button', - { type: 'submit', disabled: !props.canSubmit }, + { + type: 'submit', + disabled: !props.canSubmit, + 'aria-busy': props.isLoading || undefined + }, props.submitLabel ) } @@ -237,6 +253,137 @@ describe('the embedded checkout journey', () => { expect(journey()[1]).toMatchObject(last) }) + it.for<{ + name: string + phase: StripePaymentPhase + reported: Record + }>([ + { + name: 'the payment element mounting', + phase: { phase: 'payment_element_ready', element: 'payment' }, + reported: { phase: 'payment_element_ready', element: 'payment' } + }, + { + name: 'the address element mounting', + phase: { phase: 'payment_element_ready', element: 'address' }, + reported: { phase: 'payment_element_ready', element: 'address' } + }, + { + name: 'the payment element failing to load', + phase: { + phase: 'payment_element_failed', + element: 'payment', + element_phase: 'mount', + error_code: 'invalid_request_error' + }, + reported: { + phase: 'payment_element_failed', + element: 'payment', + element_phase: 'mount', + error_code: 'invalid_request_error' + } + }, + { + name: 'the form attempting to submit', + phase: { phase: 'payment_submit_attempted' }, + reported: { phase: 'payment_submit_attempted' } + }, + { + name: 'the form failing to validate', + phase: { + phase: 'payment_submit_failed', + submit_phase: 'validation', + error_code: 'incomplete_number' + }, + reported: { + phase: 'payment_submit_failed', + submit_phase: 'validation', + error_code: 'incomplete_number' + } + }, + { + name: 'the form failing to mint a token', + phase: { phase: 'payment_submit_failed', submit_phase: 'token_creation' }, + reported: { + phase: 'payment_submit_failed', + submit_phase: 'token_creation' + } + } + ])('reports $name as the cloud app does', async ({ phase, reported }) => { + await renderCheckout() + await screen.findByRole('button', { name: 'Pay and subscribe' }) + + reportPhase(phase) + + await waitFor(() => expect(journey()).toHaveLength(3)) + expect(journey()[2]).toMatchObject({ + name: `billing.checkout.${phase.phase}`, + ...reported, + ui_mode: 'embedded', + billing_surface: 'billing_web' + }) + }) + + it('reports the journey of a payment from the first look to the operation it issued', async () => { + const fake = await renderCheckout(CHECKOUT_PATH, { + subscribe: { + status: 'ok', + value: { phase: 'succeeded', operation: succeededOperation('op_9') } + } + }) + await screen.findByRole('button', { name: 'Pay and subscribe' }) + + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + reportPhase({ phase: 'payment_submit_attempted' }) + reportConfirm('ctoken_1') + + await waitFor(() => expect(fake.subscribe).toHaveBeenCalled()) + await waitFor(() => + expect(journeyNames()).toEqual([ + 'billing.checkout.entered', + 'billing.checkout.preview_ready', + 'billing.checkout.payment_element_ready', + 'billing.checkout.payment_submit_attempted', + 'billing.checkout.submitted', + 'billing.checkout.operation_linked' + ]) + ) + const [, , , , submitted, linked] = journey() + expect(submitted).not.toHaveProperty('billing_op_id') + expect(linked).toMatchObject({ billing_op_id: 'op_9' }) + }) + + it('reports no operation for a payment the server refused before issuing one', async () => { + const fake = await renderCheckout() + await screen.findByRole('button', { name: 'Pay and subscribe' }) + fake.subscribe.mockResolvedValueOnce({ status: 'error', code: 'CONFLICT' }) + + reportConfirm('ctoken_1') + + expect(await screen.findByRole('alert')).toBeInTheDocument() + expect(journeyNames()).toEqual([ + 'billing.checkout.entered', + 'billing.checkout.preview_ready', + 'billing.checkout.submitted' + ]) + }) + + it('reports no operation for one the checkout recovered rather than paid', async () => { + await renderCheckout(CHECKOUT_PATH, { + recover: { status: 'ok', value: pendingOperation('op_old') } + }) + + await waitFor(() => + expect( + screen.getByRole('button', { name: 'Pay and subscribe' }) + ).toHaveAttribute('aria-busy', 'true') + ) + expect(journeyNames()).toEqual([ + 'billing.checkout.entered', + 'billing.checkout.preview_ready' + ]) + }) + it('reports nothing for a quote a newer one overtook', async () => { let answerOvertaken: (result: PreviewSubscribeResult) => void = () => {} const fake = await renderCheckout(CHECKOUT_PATH, {}, (client) => diff --git a/apps/billing-web/src/views/CheckoutView.vue b/apps/billing-web/src/views/CheckoutView.vue index bc1ebce286b..07384fc669d 100644 --- a/apps/billing-web/src/views/CheckoutView.vue +++ b/apps/billing-web/src/views/CheckoutView.vue @@ -342,6 +342,7 @@ watch( () => checkout.operation.value, (operation) => { if (operation === undefined) return + journey.operationIssued(operation.id) if (operation.phase === 'failed') { submitFailure.value = declineDetail(operation.declineReason) checkout.reset() @@ -494,6 +495,7 @@ async function pay(choice: PaymentChoice) { const quoted = preview.value if (planSlug.value === undefined || !quoted || loading.value) return submitFailure.value = undefined + journey.submitted() const result = await checkout.subscribe( buildSubscribeRequest( { @@ -612,6 +614,7 @@ function leaveForHost() { @confirm-payment="pay({ confirmationToken: $event })" @apply-promotion-code="applyPromotionCode" @invalidate-quote="quoteIsCurrent = false" + @payment-phase="journey.track" @back="leaveForHost" /> { } }) -/** The provider form is covered in its package; here it only takes its place. */ +/** The provider form is covered in its package; here it reports phases and hands back a token. */ +const form = vi.hoisted(() => ({ + emit: (() => {}) as (event: string, ...payload: unknown[]) => void +})) + vi.mock(import('@comfyorg/account-ui/billing/stripe'), async () => { const { h } = await import('vue') return { @@ -83,11 +93,14 @@ vi.mock(import('@comfyorg/account-ui/billing/stripe'), async () => { setup( _props: object, { + emit, slots }: { + emit: (event: string, ...payload: unknown[]) => void slots: { submit?: (slotProps: Record) => VNode[] } } ) { + form.emit = emit return () => h('div', slots.submit?.({ disabled: false, loading: false })) } @@ -95,6 +108,10 @@ vi.mock(import('@comfyorg/account-ui/billing/stripe'), async () => { } }) +function reportPhase(phase: StripePaymentPhase) { + form.emit('phase', phase) +} + async function renderCheckout( options: FakeBillingClientOptions = {}, arrange: (fake: FakeBillingClient) => void = () => {}, @@ -277,4 +294,132 @@ describe('the full-page checkout journey', () => { expect(journeyNames()[0]).toBe('billing.checkout.entered') expect(journey()[1]).toMatchObject(last) }) + + it.for<{ + name: string + phase: StripePaymentPhase + reported: Record + }>([ + { + name: 'the payment element mounting', + phase: { phase: 'payment_element_ready', element: 'payment' }, + reported: { phase: 'payment_element_ready', element: 'payment' } + }, + { + name: 'the address element mounting', + phase: { phase: 'payment_element_ready', element: 'address' }, + reported: { phase: 'payment_element_ready', element: 'address' } + }, + { + name: 'the payment element failing to load', + phase: { + phase: 'payment_element_failed', + element: 'payment', + element_phase: 'mount', + error_code: 'invalid_request_error' + }, + reported: { + phase: 'payment_element_failed', + element: 'payment', + element_phase: 'mount', + error_code: 'invalid_request_error' + } + }, + { + name: 'the form attempting to submit', + phase: { phase: 'payment_submit_attempted' }, + reported: { phase: 'payment_submit_attempted' } + }, + { + name: 'the form failing to validate', + phase: { + phase: 'payment_submit_failed', + submit_phase: 'validation', + error_code: 'incomplete_number' + }, + reported: { + phase: 'payment_submit_failed', + submit_phase: 'validation', + error_code: 'incomplete_number' + } + }, + { + name: 'the form failing to mint a token', + phase: { phase: 'payment_submit_failed', submit_phase: 'token_creation' }, + reported: { + phase: 'payment_submit_failed', + submit_phase: 'token_creation' + } + } + ])('reports $name as the cloud app does', async ({ phase, reported }) => { + await renderCheckout() + await screen.findByText('Subscribe to Creator Plan · Acme Team') + + reportPhase(phase) + + await waitFor(() => expect(journey()).toHaveLength(3)) + expect(journey()[2]).toMatchObject({ + name: `billing.checkout.${phase.phase}`, + ...reported, + ui_mode: 'full_page', + billing_surface: 'billing_web' + }) + }) + + it('reports the journey of a payment from the first look to the operation it issued', async () => { + const fake = await renderCheckout({ + subscribe: { + status: 'ok', + value: { phase: 'succeeded', operation: succeededOperation('op_9') } + } + }) + await screen.findByText('Subscribe to Creator Plan · Acme Team') + + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + reportPhase({ phase: 'payment_submit_attempted' }) + form.emit('confirm', 'ctoken_1', 'card') + + await waitFor(() => expect(fake.subscribe).toHaveBeenCalled()) + await waitFor(() => + expect(journeyNames()).toEqual([ + 'billing.checkout.entered', + 'billing.checkout.preview_ready', + 'billing.checkout.payment_element_ready', + 'billing.checkout.payment_submit_attempted', + 'billing.checkout.submitted', + 'billing.checkout.operation_linked' + ]) + ) + const [, , , , submitted, linked] = journey() + expect(submitted).not.toHaveProperty('billing_op_id') + expect(linked).toMatchObject({ billing_op_id: 'op_9' }) + }) + + it('reports no operation for a payment the server refused before issuing one', async () => { + const fake = await renderCheckout() + await screen.findByText('Subscribe to Creator Plan · Acme Team') + fake.subscribe.mockResolvedValueOnce({ status: 'error', code: 'CONFLICT' }) + + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + form.emit('confirm', 'ctoken_1', 'card') + + await waitFor(() => expect(fake.subscribe).toHaveBeenCalled()) + await fake.subscribe.mock.results[0]?.value + expect(journeyNames()).toEqual([ + 'billing.checkout.entered', + 'billing.checkout.preview_ready', + 'billing.checkout.payment_element_ready', + 'billing.checkout.submitted' + ]) + }) + + it('reports no operation for one the checkout recovered rather than paid', async () => { + await renderCheckout({ + recover: { status: 'ok', value: pendingOperation('op_old') } + }) + + expect(await screen.findByTestId('checkout-waiting')).toBeInTheDocument() + expect(journeyNames()).not.toContain('billing.checkout.operation_linked') + expect(journeyNames()).not.toContain('billing.checkout.submitted') + }) })