Line data Source code
1 : /**
2 : * Projections from the SDK's operation state and top-up result onto the two
3 : * shapes the top-up dialog already reads: the poller's operation record and
4 : * the `CreateTopupResponse` the legacy call returned, plus the terminal the
5 : * SDK observed where those shapes cannot hold it.
6 : */
7 : import type {
8 : BillingDeclineReason,
9 : BillingOperationState,
10 : BillingOperationTerminal,
11 : BillingTelemetryFailure,
12 : HostedTopupCheckoutFailure,
13 : HostedTopupCheckoutResult,
14 : TopupFailure,
15 : TopupResult
16 : } from '@comfyorg/account-core/billing'
17 : import {
18 : declineDetailKey,
19 : unwrapServerCode
20 : } from '@comfyorg/account-core/billing'
21 :
22 : import { t } from '@/i18n'
23 : import type {
24 : BillingAuthenticationState,
25 : BillingOperationPhase,
26 : CreateTopupResponse
27 : } from '@comfyorg/billing-core'
28 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
29 :
30 : export interface TopupOperationView {
31 : readonly opId: string
32 : readonly status: 'pending' | 'reconciliation_needed'
33 : readonly actionUrl: string | null
34 : readonly phase: BillingOperationPhase | null
35 : readonly authenticationState: BillingAuthenticationState | null
36 : readonly authenticationDeadline?: string
37 : readonly isAuthenticating: boolean
38 : readonly canRetryAuthentication: boolean
39 : readonly errorMessage: string | null
40 : }
41 :
42 0 : export function declineDetail(reason: BillingDeclineReason): string {
43 0 : return t(`billingOperation.${declineDetailKey(reason)}`)
44 : }
45 :
46 0 : export function projectTopupOperation(
47 : state: BillingOperationState
48 : ): TopupOperationView | undefined {
49 0 : if (state.kind !== 'topup') return undefined
50 0 : if (state.phase === 'reconciliation_needed') {
51 0 : return {
52 : opId: state.id,
53 : status: 'reconciliation_needed',
54 : actionUrl: null,
55 : phase: null,
56 : authenticationState: 'reconciliation_needed',
57 : isAuthenticating: false,
58 : canRetryAuthentication: false,
59 : errorMessage: null
60 : }
61 : }
62 0 : if (state.phase !== 'pending') return undefined
63 :
64 0 : const authenticationState = state.authenticationState ?? null
65 0 : return {
66 : opId: state.id,
67 : status: 'pending',
68 : actionUrl: state.actionUrl ?? null,
69 : phase: state.serverPhase ?? null,
70 : authenticationState,
71 : authenticationDeadline: state.authenticationDeadline,
72 : isAuthenticating: state.challenge?.status === 'in_progress',
73 : canRetryAuthentication: state.challenge?.status === 'required',
74 : errorMessage:
75 : authenticationState === 'failed_retryable'
76 : ? declineDetail(state.declineReason ?? 'authentication_failed')
77 : : null
78 : }
79 : }
80 :
81 : /**
82 : * A purchase the SDK settled without crediting, as the response the dialog
83 : * handles, carrying the terminal its status cannot: the decline reason, or
84 : * that this tab stopped watching, or that support must reconcile it.
85 : */
86 2356 : export class UncreditedTopupResponse implements CreateTopupResponse {
87 : readonly topup_id = ''
88 :
89 : constructor(
90 0 : readonly billing_op_id: string,
91 0 : readonly status: 'failed' | 'pending',
92 0 : readonly amount_cents: number,
93 0 : readonly terminal: BillingOperationTerminal
94 : ) {}
95 : }
96 :
97 0 : function topupFailureError(
98 : failure: TopupFailure | HostedTopupCheckoutFailure
99 : ): WorkspaceApiError {
100 0 : if (failure.code === 'OPERATION_ALREADY_PENDING') {
101 0 : return new WorkspaceApiError(
102 : t('billingOperation.topupAlreadyPendingDetail'),
103 : undefined,
104 : failure.code,
105 : 'stale_operation'
106 : )
107 : }
108 0 : const serverCode = 'serverCode' in failure ? failure.serverCode : undefined
109 0 : const serverMessage =
110 : 'serverMessage' in failure ? failure.serverMessage : undefined
111 0 : return new WorkspaceApiError(
112 : serverMessage ?? t('credits.topUp.unknownError'),
113 : 'httpStatus' in failure ? failure.httpStatus : undefined,
114 : serverCode === undefined ? failure.code : unwrapServerCode(serverCode),
115 : topupFailureCategory(failure),
116 : undefined,
117 : 'retryAfterSeconds' in failure ? failure.retryAfterSeconds : undefined
118 : )
119 : }
120 :
121 0 : function topupFailureCategory(
122 : failure: TopupFailure | HostedTopupCheckoutFailure
123 : ): BillingTelemetryFailure['failure_category'] {
124 0 : switch (failure.code) {
125 : case 'REQUEST_FAILED':
126 0 : return failure.httpStatus === undefined ? 'network' : 'api_rejected'
127 : case 'SUPERSEDED':
128 0 : return 'stale_operation'
129 : case 'INVALID_AMOUNT':
130 : case 'INVALID_RETURN_URL':
131 0 : return 'validation'
132 : default:
133 0 : return 'api_rejected'
134 : }
135 : }
136 :
137 : /**
138 : * A settled result as the response the dialog handles today. `unsettled` is
139 : * the pending response: the server may still settle it, and the operation
140 : * stays visible through `projectTopupOperation`. `topup_id` is not surfaced
141 : * by the SDK and nothing reads it.
142 : */
143 0 : export function projectTopupResult(
144 : result: TopupResult,
145 : amountCents: number
146 : ): CreateTopupResponse {
147 0 : switch (result.status) {
148 : case 'ok':
149 0 : return {
150 : billing_op_id: result.operation.id,
151 : topup_id: '',
152 : status: 'completed',
153 : amount_cents: amountCents
154 : }
155 : case 'declined':
156 0 : return new UncreditedTopupResponse(
157 : result.operation.id,
158 : 'failed',
159 : amountCents,
160 : {
161 : stage: 'failed',
162 : outcome: 'failure',
163 : failure_category: 'provider_decline',
164 : decline_reason: result.operation.declineReason
165 : }
166 : )
167 : case 'unsettled':
168 0 : return new UncreditedTopupResponse(
169 : result.operation.id,
170 : 'pending',
171 : amountCents,
172 : result.operation.phase === 'timed_out'
173 : ? {
174 : stage: 'timeout',
175 : outcome: 'failure',
176 : failure_category: 'poll_timeout'
177 : }
178 : : {
179 : stage: 'failed',
180 : outcome: 'failure',
181 : failure_category: 'reconciliation_needed'
182 : }
183 : )
184 : case 'error':
185 0 : throw topupFailureError(result)
186 : }
187 : }
188 :
189 : export type HostedCheckoutSession = Pick<
190 : Extract<HostedTopupCheckoutResult, { status: 'ok' }>,
191 : 'url' | 'sessionId'
192 : >
193 :
194 : /** The hosted Checkout session, or the failure as the error the dialog handles. */
195 0 : export function projectHostedTopupCheckoutResult(
196 : result: HostedTopupCheckoutResult
197 : ): HostedCheckoutSession {
198 0 : if (result.status !== 'ok') throw topupFailureError(result)
199 0 : return result.sessionId === undefined
200 : ? { url: result.url }
201 : : { url: result.url, sessionId: result.sessionId }
202 : }
|