Line data Source code
1 : import type {
2 : AddCreditsPendingTopup,
3 : BillingAuthenticationState,
4 : HostedTopupCheckoutFailure
5 : } from '@comfyorg/account-core/billing'
6 : import { readBillingErrorCode } from '@comfyorg/account-core/billing'
7 :
8 : import type { ProcessingTopupPayment } from '@/platform/billing/processingTopupPayment'
9 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
10 :
11 : interface PendingTopupOperation {
12 : readonly status: string
13 : readonly actionUrl: string | null
14 : readonly authenticationState: BillingAuthenticationState | null
15 : }
16 :
17 : /** The open top-up the dialog shows up front, from either rail's operation and the processing checkout read. */
18 4 : export function addCreditsPendingTopup(
19 : operation: PendingTopupOperation | null | undefined,
20 : processing: ProcessingTopupPayment | null
21 : ): AddCreditsPendingTopup | undefined {
22 4 : if (
23 : operation?.authenticationState === 'failed_retryable' ||
24 : operation?.status === 'reconciliation_needed'
25 : ) {
26 0 : return { kind: 'not_verified' }
27 : }
28 4 : if (operation?.actionUrl) {
29 0 : return { kind: 'needs_verification', verifyUrl: operation.actionUrl }
30 : }
31 4 : if (!processing) return undefined
32 4 : return processing.paymentAmountCents === null
33 : ? { kind: 'processing' }
34 : : { kind: 'processing', amountCents: processing.paymentAmountCents }
35 : }
36 :
37 : /** A checkout the app's rails threw for, back in the SDK's failure shape. */
38 0 : export function topupCheckoutFailure(
39 : error: unknown
40 : ): HostedTopupCheckoutFailure {
41 0 : if (!(error instanceof WorkspaceApiError)) {
42 0 : return { status: 'error', code: 'REQUEST_FAILED' }
43 : }
44 0 : const serverCode =
45 : error.code === undefined
46 : ? undefined
47 : : readBillingErrorCode({ code: error.code, message: error.message })
48 0 : return {
49 : status: 'error',
50 : code: error.status === 403 ? 'ACCESS_DENIED' : 'REQUEST_FAILED',
51 : ...(error.status === undefined ? {} : { httpStatus: error.status }),
52 : ...(serverCode === undefined ? {} : { serverCode }),
53 : ...(error.retryAfterSeconds === undefined
54 : ? {}
55 : : { retryAfterSeconds: error.retryAfterSeconds })
56 : }
57 : }
|