Line data Source code
1 : import type { HostedTopupCheckout } from '@comfyorg/ingest-types'
2 : import { onScopeDispose, shallowRef, toValue, watch } from 'vue'
3 : import type { MaybeRefOrGetter } from 'vue'
4 :
5 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
6 : import { reportError } from '@comfyorg/telemetry/reportError'
7 : import { billingApi } from '@/platform/billing/api/billingApi'
8 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
9 : import { useWorkspaceIdentity } from '@/platform/workspace/ports/workspaceIdentity'
10 : import type { ProcessingTopupPayment } from '@/platform/billing/processingTopupPayment'
11 : import { processingTopupPayment } from '@/platform/billing/processingTopupPayment'
12 : import { registerRefreshOnReturn } from '@/platform/billing/refreshOnReturn'
13 :
14 : interface ProcessingTopupRead {
15 : checkouts: HostedTopupCheckout[]
16 : sessionFailures: unknown[]
17 : }
18 :
19 7 : async function readProcessingTopupCheckouts(
20 : lastKnown: ReadonlyMap<string, HostedTopupCheckout>
21 : ): Promise<
22 : { ok: true; read: ProcessingTopupRead } | { ok: false; cause: unknown }
23 : > {
24 7 : try {
25 7 : const { checkouts: listed } = await billingApi.listTopupCheckouts()
26 7 : const reads = await Promise.allSettled(
27 : listed.map(({ session_id }) => billingApi.getTopupCheckout(session_id))
28 : )
29 7 : const checkouts: HostedTopupCheckout[] = []
30 7 : const sessionFailures: unknown[] = []
31 7 : reads.forEach((read, index) => {
32 0 : if (read.status === 'fulfilled') {
33 0 : checkouts.push(read.value)
34 0 : return
35 : }
36 0 : sessionFailures.push(read.reason)
37 0 : const known = lastKnown.get(listed[index].session_id)
38 0 : if (known) checkouts.push(known)
39 : })
40 7 : return { ok: true, read: { checkouts, sessionFailures } }
41 : } catch (cause) {
42 0 : return { ok: false, cause }
43 : }
44 : }
45 :
46 0 : function isBillingOwnerRequired(cause: unknown) {
47 0 : return cause instanceof WorkspaceApiError && cause.status === 403
48 : }
49 :
50 9 : export function useProcessingTopupPayment(
51 : shown: MaybeRefOrGetter<boolean> = true
52 : ) {
53 9 : const { flags } = useFeatureFlags()
54 9 : const workspaceStore = useWorkspaceIdentity()
55 9 : const workspaceScope = () =>
56 : `${workspaceStore.workspaceTransitionGeneration}:${workspaceStore.activeWorkspaceId}`
57 9 : const payment = shallowRef<ProcessingTopupPayment | null>(null)
58 9 : let knownCheckouts = new Map<string, HostedTopupCheckout>()
59 9 : let generation = 0
60 9 : let disposed = false
61 9 : let failureReported = false
62 :
63 0 : function reportCheckoutReadFailure(cause: unknown) {
64 0 : if (failureReported) return
65 0 : failureReported = true
66 0 : reportError(cause, {
67 : errorType: 'failure_reading_topup_checkout',
68 : surface: 'billing'
69 : })
70 : }
71 :
72 2 : function clear() {
73 2 : payment.value = null
74 2 : knownCheckouts = new Map()
75 : }
76 :
77 25 : function reading() {
78 25 : return !disposed && toValue(shown) && flags.unifiedBillingSettingsEnabled
79 : }
80 :
81 9 : async function load() {
82 9 : const current = ++generation
83 9 : if (!reading()) {
84 2 : clear()
85 2 : return
86 : }
87 :
88 7 : const outcome = await readProcessingTopupCheckouts(knownCheckouts)
89 9 : if (current !== generation || !reading()) return
90 7 : if (!outcome.ok) {
91 0 : if (isBillingOwnerRequired(outcome.cause)) {
92 0 : clear()
93 0 : return
94 : }
95 0 : reportCheckoutReadFailure(outcome.cause)
96 0 : return
97 : }
98 :
99 7 : const { checkouts, sessionFailures } = outcome.read
100 7 : for (const cause of sessionFailures) reportCheckoutReadFailure(cause)
101 7 : knownCheckouts = new Map(
102 : checkouts.map((known) => [known.session_id, known])
103 : )
104 7 : const servedPayment = processingTopupPayment(checkouts)
105 9 : payment.value =
106 : servedPayment !== null && sessionFailures.length > 0
107 : ? { paymentAmountCents: null }
108 : : servedPayment
109 : }
110 :
111 9 : watch(workspaceScope, () => {
112 0 : clear()
113 0 : void load()
114 : })
115 9 : watch(reading, () => void load(), { immediate: true })
116 9 : const stopRefreshOnReturn = registerRefreshOnReturn(load)
117 9 : onScopeDispose(() => {
118 4 : disposed = true
119 4 : stopRefreshOnReturn()
120 : })
121 :
122 9 : return { payment }
123 : }
|