Line data Source code
1 : import {
2 : BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT,
3 : BILLING_OPERATION_TELEMETRY_EVENT
4 : } from '#/telemetry'
5 : import type { BillingOperationTelemetryEvent } from '#/core/billing/operationLifecycle'
6 : import type { BillingTelemetryEvent } from './billingTelemetryEvent'
7 : import type { PaymentIntentSource } from './vocabulary'
8 :
9 : /**
10 : * The lifecycle's event onto the poller's `billing.operation.*` stages, so
11 : * both rails count in one funnel; only this rail sets `presentation` and
12 : * `resumed`. Friction inside the operation maps onto `billing.checkout.*`.
13 : */
14 0 : export function toBillingTelemetryEvent(
15 : event: BillingOperationTelemetryEvent,
16 : paymentIntentSource?: PaymentIntentSource
17 : ): BillingTelemetryEvent {
18 0 : const friction = {
19 : operation: 'checkout',
20 : outcome: 'pending',
21 : billing_client: 'sdk',
22 : operation_type: event.operation_type,
23 : billing_op_id: event.billing_op_id,
24 : presentation: event.presentation,
25 : resumed: event.resumed
26 : } as const
27 0 : if (
28 : event.name === BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.redirectStarted ||
29 : event.name === BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.returned
30 : ) {
31 0 : return {
32 : ...friction,
33 : stage:
34 : event.name === BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.returned
35 : ? 'returned'
36 : : 'redirect_started',
37 : destination: event.destination,
38 : step: event.step,
39 : navigation: event.navigation,
40 : ...(event.method_kind === undefined
41 : ? {}
42 : : { method_kind: event.method_kind })
43 : }
44 : }
45 0 : const shared = {
46 : operation: 'operation',
47 : billing_client: 'sdk',
48 : operation_type: event.operation_type,
49 : billing_op_id: event.billing_op_id,
50 : presentation: event.presentation,
51 : resumed: event.resumed,
52 : ...(paymentIntentSource === undefined
53 : ? {}
54 : : { payment_intent_source: paymentIntentSource }),
55 : ...('duration_ms' in event && event.duration_ms !== undefined
56 : ? { duration_ms: event.duration_ms }
57 : : {})
58 : } as const
59 :
60 0 : switch (event.name) {
61 : case BILLING_OPERATION_TELEMETRY_EVENT.started:
62 0 : return { ...shared, stage: 'started', outcome: 'pending' }
63 : case BILLING_OPERATION_TELEMETRY_EVENT.succeeded:
64 0 : return { ...shared, stage: 'succeeded', outcome: 'success' }
65 : case BILLING_OPERATION_TELEMETRY_EVENT.timeout:
66 0 : return {
67 : ...shared,
68 : stage: 'timeout',
69 : outcome: 'failure',
70 : failure_category: 'poll_timeout'
71 : }
72 : case BILLING_OPERATION_TELEMETRY_EVENT.failed:
73 0 : return {
74 : ...shared,
75 : stage: 'failed',
76 : outcome: 'failure',
77 : failure_category: event.failure_category ?? 'provider_decline',
78 : decline_reason: event.decline_reason
79 : }
80 : case BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeRequired:
81 0 : return { ...friction, stage: 'challenge_required' }
82 : case BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeCompleted:
83 0 : return { ...friction, stage: 'challenge_completed' }
84 : case BILLING_CHECKOUT_FRICTION_TELEMETRY_EVENT.challengeFailed:
85 0 : return {
86 : ...friction,
87 : stage: 'challenge_failed',
88 : ...(event.decline_reason === undefined
89 : ? {}
90 : : { decline_reason: event.decline_reason })
91 : }
92 : }
93 : }
|