Line data Source code
1 : import {
2 : deriveLocalCreditBanner,
3 : isMemberTopUpDenial
4 : } from '@comfyorg/account-core/billing'
5 : import type {
6 : BillingBannerAction,
7 : BillingBannerCopy,
8 : BillingBannerView
9 : } from '@comfyorg/account-ui/billing'
10 : import { computed, ref, toValue, watch } from 'vue'
11 : import type { MaybeRefOrGetter } from 'vue'
12 : import { useI18n } from 'vue-i18n'
13 :
14 : import {
15 : centsToCredits,
16 : formatCreditsFromCents
17 : } from '@/base/credits/comfyCredits'
18 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
19 : import { useAutoTopUpBannerKind } from '@/platform/billing/composables/useAutoTopUpBannerKind'
20 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
21 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
22 : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
23 : import { useBillingUI } from '@/platform/billing/composables/useBillingUI'
24 : import { useLongDate } from '@/platform/billing/composables/useLongDate'
25 : import {
26 : isTopUpDeniedPastDue,
27 : usePaymentRecoveryDialog
28 : } from '@/platform/billing/composables/usePaymentRecoveryDialog'
29 : import { usePlatformBillingLink } from '@/platform/billing/composables/usePlatformHandoff'
30 : import { isCloud } from '@/platform/distribution/types'
31 :
32 : type CreditAction = 'addCredits' | 'updatePayment' | null
33 :
34 5 : const bs = 'workspacePanel.billingStatus'
35 :
36 : /** The Billing tab's credit or auto top-up banner, its copy and its handlers. */
37 5 : export function useCreditBanner(suppressed: MaybeRefOrGetter<boolean>) {
38 5 : const billing = useBillingContext()
39 5 : const { flags } = useFeatureFlags()
40 5 : const { workspaceType } = useBillingUI()
41 5 : const {
42 : canTopUp,
43 : topUpDenial,
44 : hasResolvedCapabilities,
45 : canManageBilling,
46 : runDeniedPaymentPastDue
47 : } = useBillingCapabilities()
48 5 : const { showTopUpCreditsDialog } = useBillingDialogs()
49 5 : const { updatePayment, isUpdatingPayment } = usePaymentRecoveryDialog()
50 5 : const { t, locale } = useI18n()
51 5 : const { longDate } = useLongDate()
52 :
53 5 : const outOfCreditsDismissed = ref(false)
54 5 : const lowCreditsDismissed = ref(false)
55 :
56 5 : const { subscription, balance, isTeamPlan } = billing
57 :
58 5 : const autoTopUp = useAutoTopUpBannerKind()
59 5 : const { href, open } = usePlatformBillingLink('/profile/billing', {
60 : target: 'auto_top_up',
61 : action: 'update_payment'
62 : })
63 :
64 5 : const kind = computed(() => {
65 5 : if (toValue(suppressed)) return null
66 : if (isCloud && autoTopUp.value) return autoTopUp.value
67 5 : return deriveLocalCreditBanner({
68 : autoTopUp: autoTopUp.value,
69 : unifiedBillingSettingsEnabled: flags.unifiedBillingSettingsEnabled,
70 : isCloud,
71 : isActive: subscription.value?.isActive === true,
72 : hasFunds: subscription.value?.hasFunds ?? null,
73 : lowBalance: balance.value?.lowBalance,
74 : outOfCreditsDismissed: outOfCreditsDismissed.value,
75 : lowCreditsDismissed: lowCreditsDismissed.value
76 : })
77 : })
78 :
79 5 : watch(
80 : () => subscription.value?.hasFunds === false,
81 : (exhausted) => {
82 0 : if (!exhausted) outOfCreditsDismissed.value = false
83 : }
84 : )
85 :
86 5 : watch(
87 : () => balance.value?.lowBalance === true,
88 : (low) => {
89 0 : if (!low) lowCreditsDismissed.value = false
90 : }
91 : )
92 :
93 5 : const availableCents = computed(() => {
94 5 : const current = balance.value
95 5 : if (!current) return null
96 5 : return current.effectiveBalanceMicros ?? current.amountMicros
97 : })
98 :
99 5 : watch(availableCents, (cents, previous) => {
100 0 : if (previous != null && cents !== previous)
101 0 : lowCreditsDismissed.value = false
102 : })
103 :
104 5 : const audience = computed(() => (isTeamPlan.value ? 'team' : 'personal'))
105 :
106 5 : const refillDate = computed(() => longDate(billing.nextCreditRefillAt.value))
107 :
108 0 : function datedBody(key: string, noDateKey: string): string {
109 0 : return refillDate.value ? t(key, { date: refillDate.value }) : t(noDateKey)
110 : }
111 :
112 0 : function pastDueBodyKey(): string {
113 0 : if (audience.value === 'personal') {
114 0 : return `${bs}.outOfCredits.pastDuePersonalBody`
115 : }
116 0 : return canManageBilling.value
117 : ? `${bs}.outOfCredits.pastDueBody`
118 : : `${bs}.outOfCredits.pastDueMemberBody`
119 : }
120 :
121 0 : function creditView(
122 : kindKey: 'outOfCredits' | 'lowCredits',
123 : body: string,
124 : action: CreditAction
125 : ): BillingBannerView {
126 0 : return {
127 : kind: 'credit',
128 : title: t(`${bs}.${kindKey}.title`),
129 : body,
130 : action,
131 : dismissKey: kindKey
132 : }
133 : }
134 :
135 0 : function refusedOutOfCreditsBody(): string {
136 0 : const denial = topUpDenial.value
137 0 : if (isMemberTopUpDenial(denial)) {
138 0 : return datedBody(
139 : `${bs}.outOfCredits.memberBody`,
140 : `${bs}.outOfCredits.memberBodyNoDate`
141 : )
142 : }
143 0 : if (denial === 'topup_purchases_paused') {
144 0 : return audience.value === 'personal'
145 : ? datedBody(
146 : `${bs}.outOfCredits.pausedPersonalBody`,
147 : `${bs}.outOfCredits.pausedPersonalBodyNoDate`
148 : )
149 : : datedBody(
150 : `${bs}.outOfCredits.pausedBody`,
151 : `${bs}.outOfCredits.pausedBodyNoDate`
152 : )
153 : }
154 0 : return t(
155 : audience.value === 'personal'
156 : ? `${bs}.outOfCredits.refusedPersonalBody`
157 : : `${bs}.outOfCredits.refusedBody`
158 : )
159 : }
160 :
161 0 : function outOfCreditsView(): BillingBannerView | null {
162 0 : if (
163 : runDeniedPaymentPastDue.value ||
164 : isTopUpDeniedPastDue(topUpDenial.value)
165 : ) {
166 0 : return creditView(
167 : 'outOfCredits',
168 : t(pastDueBodyKey()),
169 : canManageBilling.value ? 'updatePayment' : null
170 : )
171 : }
172 0 : if (canTopUp.value) {
173 0 : const personal = audience.value === 'personal'
174 0 : return creditView(
175 : 'outOfCredits',
176 : datedBody(
177 : personal
178 : ? `${bs}.outOfCredits.personalBody`
179 : : `${bs}.outOfCredits.body`,
180 : personal
181 : ? `${bs}.outOfCredits.personalBodyNoDate`
182 : : `${bs}.outOfCredits.bodyNoDate`
183 : ),
184 : 'addCredits'
185 : )
186 : }
187 0 : if (!hasResolvedCapabilities.value) return null
188 0 : return creditView('outOfCredits', refusedOutOfCreditsBody(), null)
189 : }
190 :
191 0 : function creditsLeft(): string {
192 0 : const cents = availableCents.value
193 0 : if (cents == null) return ''
194 0 : return formatCreditsFromCents({
195 : cents,
196 : locale: locale.value,
197 : numberOptions: { minimumFractionDigits: 0, maximumFractionDigits: 0 }
198 : })
199 : }
200 :
201 0 : function lowCreditsBodyKey(): string {
202 0 : if (topUpDenial.value === 'topup_purchases_paused')
203 0 : return `${bs}.lowCredits.pausedBody`
204 0 : if (!isCloud) return `${bs}.lowCredits.localBody`
205 : if (workspaceType.value !== 'team') return `${bs}.lowCredits.personalBody`
206 : return canManageBilling.value
207 : ? `${bs}.lowCredits.ownerBody`
208 : : `${bs}.lowCredits.memberBody`
209 : }
210 :
211 0 : function lowCreditsView(): BillingBannerView {
212 0 : return creditView(
213 : 'lowCredits',
214 : t(
215 : lowCreditsBodyKey(),
216 : { credits: creditsLeft() },
217 : centsToCredits(availableCents.value ?? 0)
218 : ),
219 : canTopUp.value ? 'addCredits' : null
220 : )
221 : }
222 :
223 5 : const view = computed<BillingBannerView | null>(() => {
224 5 : const shown = kind.value
225 5 : if (shown === 'autoTopUpFailed' || shown === 'autoTopUpOff') {
226 0 : return {
227 : kind: 'autoTopUp',
228 : title: t(`${bs}.${shown}.title`),
229 : body: t(`${bs}.${shown}.body`),
230 : action: 'updatePayment',
231 : href: href.value ?? null
232 : }
233 : }
234 5 : if (shown === 'outOfCredits') return outOfCreditsView()
235 5 : if (shown === 'lowCredits') return lowCreditsView()
236 5 : return null
237 : })
238 :
239 5 : const copy = computed<Pick<BillingBannerCopy, 'dismiss' | 'actions'>>(() => ({
240 : dismiss: t(`${bs}.outOfCredits.dismiss`),
241 : actions: {
242 : addCredits: t(`${bs}.outOfCredits.addCredits`),
243 : updatePayment: t(`${bs}.updatePayment`),
244 : verifyPayment: t(`${bs}.planCheckout.verifyPayment.action`),
245 : contactSupport: t(`${bs}.parked.contactSupport`),
246 : resumePlan: t(`${bs}.ending.reactivate`),
247 : choosePlan: t('workspacePanel.billingTab.plan.actions.choosePlan'),
248 : contactSales: t(`${bs}.ending.contactSales`)
249 : }
250 : }))
251 :
252 0 : function dismiss() {
253 0 : const visible = kind.value
254 0 : if (visible === 'outOfCredits') {
255 0 : outOfCreditsDismissed.value = true
256 0 : if (balance.value?.lowBalance === true) lowCreditsDismissed.value = true
257 0 : } else if (visible === 'lowCredits') {
258 0 : lowCreditsDismissed.value = true
259 : }
260 : }
261 :
262 0 : function act(action: BillingBannerAction) {
263 0 : if (view.value?.kind === 'autoTopUp') open()
264 0 : else if (action === 'addCredits') void showTopUpCreditsDialog()
265 0 : else if (action === 'updatePayment') void updatePayment()
266 : }
267 :
268 5 : return { view, copy, isUpdatingPayment, dismiss, act }
269 : }
|