Line data Source code
1 : import type { CustomerActionHold } from '@comfyorg/account-core/billing'
2 : import { isBlockedOnCustomerPhase } from '@comfyorg/account-core/billing'
3 :
4 : import type {
5 : BillingAuthenticationState,
6 : BillingOperationPhase
7 : } from '@comfyorg/billing-core'
8 : import type { ToastId } from '@/types/toastId'
9 :
10 : // Re-exported so both rails read one definition: a second copy that drifted
11 : // would reintroduce the give-up bug this predicate exists to prevent.
12 : export { isBlockedOnCustomerPhase }
13 :
14 : type OperationStatus =
15 : | 'pending'
16 : | 'succeeded'
17 : | 'failed'
18 : | 'timeout'
19 : | 'reconciliation_needed'
20 :
21 : interface CustomerAttentionOperation {
22 : readonly status: OperationStatus
23 : readonly actionUrl: string | null
24 : readonly authenticationState: BillingAuthenticationState | null
25 : readonly phase?: BillingOperationPhase | null
26 : }
27 :
28 : /** The poller's rule for which operation a dialog must show instead of its first step. */
29 0 : export function needsCustomerAttention(
30 : operation: CustomerAttentionOperation
31 : ): boolean {
32 0 : return (
33 : operation.status === 'reconciliation_needed' ||
34 : (operation.status === 'pending' &&
35 : (operation.actionUrl !== null ||
36 : isBlockedOnCustomerPhase(operation.phase) ||
37 : operation.authenticationState === 'requires_action' ||
38 : operation.authenticationState === 'failed_retryable'))
39 : )
40 : }
41 :
42 : export type ProgressToastKind = 'processing' | 'action'
43 :
44 : export interface ProgressToast {
45 : id: ToastId
46 : kind: ProgressToastKind
47 : }
48 :
49 : interface ProgressToastOperation {
50 : readonly actionUrl?: string | null
51 : readonly paymentMethodUrl?: string | null
52 : readonly phase?: BillingOperationPhase | null
53 : }
54 :
55 : /**
56 : * A checkout parked on a card with no verification link to offer is a state the customer
57 : * leaves by subscribing again, not a payment in flight, so no surface may
58 : * describe it as one. A served link turns it back into a verification ask.
59 : */
60 0 : export function isParkedCheckout(operation: ProgressToastOperation): boolean {
61 0 : return (
62 : operation.phase === 'awaiting_payment_method' &&
63 : (!operation.actionUrl || operation.actionUrl === operation.paymentMethodUrl)
64 : )
65 : }
66 :
67 0 : export function progressToastKind(
68 : operation: ProgressToastOperation
69 : ): ProgressToastKind | undefined {
70 0 : if (operation.actionUrl) return 'action'
71 0 : if (isParkedCheckout(operation)) return undefined
72 0 : return 'processing'
73 : }
74 :
75 : interface LegacyActionOperation {
76 : readonly actionUrl: string | null
77 : readonly authenticationState: BillingAuthenticationState | null
78 : readonly isAuthenticating: boolean
79 : }
80 :
81 : /**
82 : * This store's surface offers the hosted link beside the embedded challenge,
83 : * and its challenge exists only while it holds the client secret. Rows that
84 : * intentionally differ from the SDK's `pendingOperationActionHold`:
85 : * - embedded with only an `action_url`: acts here, where the link is shown
86 : * beside the challenge; the SDK's embedded surface never opens it.
87 : * - embedded checkout off with a retryable failure: waits here, where
88 : * `authentication_state` is never read; the SDK's hosted surface shows the
89 : * decline.
90 : */
91 0 : export function legacyOperationActionHold(
92 : operation: LegacyActionOperation,
93 : holdsClientSecret: boolean
94 : ): CustomerActionHold {
95 0 : return {
96 : authenticationState: operation.authenticationState,
97 : offersHostedPage: operation.actionUrl !== null,
98 : ...(holdsClientSecret && {
99 : challenge: operation.isAuthenticating ? 'in_progress' : 'required'
100 : })
101 : }
102 : }
|