Line data Source code
1 : import type {
2 : BillingClient,
3 : BillingPortalTarget,
4 : BillingTelemetryFailure
5 : } from '@comfyorg/account-core/billing'
6 :
7 : import type { TelemetryDispatcher } from '@comfyorg/telemetry/types'
8 : import { describeBillingFailure } from '@/platform/telemetry/utils/billingFailureCategory'
9 : import { registerRefreshOnReturn } from '@/platform/billing/refreshOnReturn'
10 :
11 2356 : const PORTAL_TAB_BLOCKED: BillingTelemetryFailure = {
12 : failure_category: 'redirect',
13 : error_code: 'payment_popup_blocked'
14 : }
15 :
16 : /** Only the latest portal trip waits for a return, so one return is reported once. */
17 2356 : let stopWatchingForReturn: (() => void) | undefined
18 :
19 : /** Reports one trip to the provider's portal, which opens in its own tab. */
20 0 : export function createBillingPortalReporter(
21 : telemetry: TelemetryDispatcher | null,
22 : target: BillingPortalTarget
23 : ) {
24 0 : const client = (billingClient?: BillingClient) =>
25 : billingClient ? { billing_client: billingClient } : {}
26 :
27 0 : function failed(
28 : failure: BillingTelemetryFailure,
29 : billingClient?: BillingClient
30 : ) {
31 0 : telemetry?.trackBillingEvent({
32 : operation: 'portal',
33 : stage: 'failed',
34 : outcome: 'failure',
35 : target,
36 : ...client(billingClient),
37 : ...failure
38 : })
39 : }
40 :
41 0 : return {
42 : /** Also reports the first return to this tab after the portal opened. */
43 : opened(billingClient?: BillingClient) {
44 0 : telemetry?.trackBillingEvent({
45 : operation: 'portal',
46 : stage: 'opened',
47 : outcome: 'pending',
48 : target,
49 : ...client(billingClient)
50 : })
51 0 : stopWatchingForReturn?.()
52 0 : const stopWatching = registerRefreshOnReturn(async () => {
53 0 : stopWatching()
54 0 : telemetry?.trackBillingEvent({
55 : operation: 'portal',
56 : stage: 'returned',
57 : outcome: 'pending',
58 : target,
59 : ...client(billingClient)
60 : })
61 : })
62 0 : stopWatchingForReturn = stopWatching
63 : },
64 : blocked(billingClient?: BillingClient) {
65 0 : failed(PORTAL_TAB_BLOCKED, billingClient)
66 : },
67 : failed(error: unknown, billingClient?: BillingClient) {
68 0 : failed(describeBillingFailure(error), billingClient)
69 : }
70 : }
71 : }
|