Line data Source code
1 : import { delay } from 'es-toolkit'
2 : import { computed } from 'vue'
3 :
4 : import type { BillingClient } from '@comfyorg/account-core/billing'
5 : import type { CreateTopupResponse } from '@comfyorg/billing-core'
6 : import type { TopupAutoTopUpSettings } from '@comfyorg/ingest-types'
7 : import type { PaymentIntentSource } from '@comfyorg/telemetry/types'
8 :
9 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
10 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
11 : import { billingApi } from '@/platform/billing/api/billingApi'
12 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
13 : import { useBillingSdkStore } from '@/platform/billing/sdk/billingSdkStore'
14 : import type { HostedCheckoutSession } from '@/platform/billing/sdk/topupOperationView'
15 : import { useBillingOperationStore } from '@/platform/billing/stores/billingOperationStore'
16 :
17 2356 : const RETRYABLE_CHECKOUT_REFUSALS: ReadonlyArray<readonly [number, string]> = [
18 : [409, 'TOPUP_CHECKOUT_BUSY'],
19 : [409, 'TOPUP_CHECKOUT_NOT_PAYABLE'],
20 : [503, 'TOPUP_CHECKOUT_UNAVAILABLE']
21 : ]
22 :
23 2356 : const CHECKOUT_RETRY_DELAYS_MS = [1000, 2000]
24 :
25 0 : function isRetryableCheckoutRefusal(error: unknown): boolean {
26 0 : return (
27 : error instanceof WorkspaceApiError &&
28 : RETRYABLE_CHECKOUT_REFUSALS.some(
29 : ([status, code]) => error.status === status && error.code === code
30 : )
31 : )
32 : }
33 :
34 : /**
35 : * The top-up quote, checkout session and operation the dialog reads, from one rail.
36 : * The rail is fixed when the dialog opens: a flag flip mid-operation must not
37 : * issue on one rail and observe on the other, or move a live operation
38 : * between two observers.
39 : */
40 4 : export function useTopupOperation() {
41 4 : const { flags } = useFeatureFlags()
42 4 : const sdkStore = flags.billingSdkRailEnabled ? useBillingSdkStore() : null
43 4 : const operationStore = useBillingOperationStore()
44 4 : const billingContext = useBillingContext()
45 4 : const billingClient: BillingClient = sdkStore ? 'sdk' : 'legacy'
46 :
47 4 : const isAddingCredits = computed(() =>
48 : sdkStore ? sdkStore.isAddingCredits : operationStore.isAddingCredits
49 : )
50 4 : const topupOperation = computed(() =>
51 : sdkStore
52 : ? sdkStore.topupActionOperation
53 : : operationStore.topupActionOperation
54 : )
55 :
56 0 : function topup(amountCents: number): Promise<CreateTopupResponse | void> {
57 0 : if (sdkStore) return sdkStore.createTopup(amountCents)
58 0 : return billingContext.topup(amountCents)
59 : }
60 :
61 0 : async function createHostedCheckout(
62 : amountCents: number,
63 : returnUrl: string,
64 : signal?: AbortSignal,
65 : autoTopUp?: TopupAutoTopUpSettings
66 : ): Promise<HostedCheckoutSession> {
67 0 : for (const retryDelayMs of CHECKOUT_RETRY_DELAYS_MS) {
68 0 : try {
69 0 : return await mintHostedCheckout(
70 : amountCents,
71 : returnUrl,
72 : signal,
73 : autoTopUp
74 : )
75 : } catch (error) {
76 0 : if (!isRetryableCheckoutRefusal(error)) throw error
77 0 : await delay(retryDelayMs, { signal })
78 : }
79 : }
80 0 : return mintHostedCheckout(amountCents, returnUrl, signal, autoTopUp)
81 : }
82 :
83 0 : async function mintHostedCheckout(
84 : amountCents: number,
85 : returnUrl: string,
86 : signal: AbortSignal | undefined,
87 : autoTopUp: TopupAutoTopUpSettings | undefined
88 : ): Promise<HostedCheckoutSession> {
89 0 : if (sdkStore) {
90 0 : return sdkStore.createHostedTopupCheckout(
91 : amountCents,
92 : returnUrl,
93 : signal,
94 : autoTopUp
95 : )
96 : }
97 0 : const { checkout_url, session_id } = await billingApi.createTopupCheckout(
98 : amountCents,
99 : returnUrl,
100 : signal,
101 : autoTopUp
102 : )
103 0 : return session_id === undefined
104 : ? { url: checkout_url }
105 : : { url: checkout_url, sessionId: session_id }
106 : }
107 :
108 5 : async function quoteTopup(
109 : amountCents: number,
110 : signal: AbortSignal
111 : ): Promise<number | null> {
112 5 : if (sdkStore) return sdkStore.quoteTopup(amountCents, signal)
113 5 : try {
114 5 : return (await billingApi.quoteTopup(amountCents, signal)).credits
115 : } catch {
116 0 : return null
117 : }
118 : }
119 :
120 0 : function dismissOperation(operationId: string): void {
121 0 : if (sdkStore) sdkStore.dismissOperation(operationId)
122 0 : else operationStore.dismissOperation(operationId)
123 : }
124 :
125 0 : async function adoptPendingOperation(
126 : operationId: string,
127 : metadata: {
128 : attemptStartedAt: number
129 : paymentIntentSource?: PaymentIntentSource
130 : }
131 : ): Promise<void> {
132 0 : if (sdkStore) return
133 0 : await operationStore.startOperation(operationId, 'topup', metadata)
134 : }
135 :
136 4 : return {
137 : billingClient,
138 : isAddingCredits,
139 : topupOperation,
140 : topup,
141 : createHostedCheckout,
142 : dismissOperation,
143 : adoptPendingOperation,
144 : quoteTopup
145 : }
146 : }
|