Skip to content
Merged
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
8 changes: 6 additions & 2 deletions apps/billing-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion apps/billing-web/src/composables/useCheckoutJourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
})
}
Expand All @@ -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) {
Expand All @@ -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 }
}
2 changes: 2 additions & 0 deletions apps/billing-web/src/composables/useFullPageCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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')
Expand Down
155 changes: 151 additions & 4 deletions apps/billing-web/src/views/CheckoutView.journey.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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'
Expand All @@ -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'

Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -237,6 +253,137 @@ describe('the embedded checkout journey', () => {
expect(journey()[1]).toMatchObject(last)
})

it.for<{
name: string
phase: StripePaymentPhase
reported: Record<string, unknown>
}>([
{
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) =>
Expand Down
3 changes: 3 additions & 0 deletions apps/billing-web/src/views/CheckoutView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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"
/>
<CheckoutTransitionConfirm
Expand Down
Loading
Loading