Line data Source code
1 : <template>
2 4 : <div class="flex w-64 flex-col" data-testid="credits-flyout">
3 : <BillingCreditsBreakdown
4 : :state="breakdown"
5 : :copy="breakdownCopy"
6 : :locale
7 : class="px-2"
8 : >
9 : <template #topUpNote>
10 : <p
11 : v-if="processingPayment"
12 : role="status"
13 : class="m-0 text-xs text-muted-foreground"
14 : data-testid="topup-payment-processing-line"
15 : >
16 : {{ $t('profileMenu.paymentProcessing') }}
17 : </p>
18 : <p
19 : v-if="refusalReason"
20 : :id="refusalReasonId"
21 : class="m-0 text-xs text-muted-foreground"
22 : >
23 : {{ refusalReason }}
24 : </p>
25 : <p
26 : v-if="planCreditNote"
27 : class="m-0 text-xs text-muted-foreground"
28 : data-testid="credits-flyout-plan-note"
29 : >
30 : {{ planCreditNote }}
31 : </p>
32 : </template>
33 : </BillingCreditsBreakdown>
34 : <div class="-mx-2 my-1 border-t border-border" role="separator" />
35 : <div class="flex flex-col gap-2">
36 : <Tooltip
37 : v-if="addCredits.kind !== 'hidden'"
38 : :disabled="!paymentPastDue && !purchasesStopped"
39 : >
40 : <TooltipTrigger as-child>
41 : <span class="flex flex-col" data-testid="add-credits-trigger">
42 : <Button
43 : variant="secondary"
44 : size="md"
45 : data-testid="add-credits-button"
46 : :disabled="addCredits.kind === 'disabled'"
47 : :aria-describedby="refusalReason ? refusalReasonId : undefined"
48 : :data-dd-action-name="BILLING_ACTION_NAME.addCreditsPopover"
49 : @click="handleTopUp"
50 : >
51 : {{ $t('subscription.addCredits') }}
52 : </Button>
53 : </span>
54 : </TooltipTrigger>
55 : <TooltipContent side="left">{{ refusalReason }}</TooltipContent>
56 : </Tooltip>
57 : <Button
58 : v-if="paymentRecoveryAvailable"
59 : variant="secondary"
60 : size="md"
61 : :loading="isUpdatingPayment"
62 : data-testid="update-payment-button"
63 : @click="updatePayment"
64 : >
65 : {{ $t('workspacePanel.billingStatus.updatePayment') }}
66 : </Button>
67 : <Button
68 : variant="muted-textonly"
69 : size="md"
70 : data-testid="billing-settings-button"
71 : @click="emit('openBilling')"
72 : >
73 : {{ $t('profileMenu.billingSettings') }}
74 : </Button>
75 : </div>
76 : </div>
77 : </template>
78 :
79 : <script setup lang="ts">
80 : import {
81 : addCreditsAvailability,
82 : deriveCreditsFlyout
83 : } from '@comfyorg/account-core/billing'
84 : import { BillingCreditsBreakdown } from '@comfyorg/account-ui/billing'
85 : import Button from '@comfyorg/design-system/button/Button'
86 : import Tooltip from '@comfyorg/design-system/tooltip/Tooltip'
87 : import TooltipContent from '@comfyorg/design-system/tooltip/TooltipContent'
88 : import TooltipTrigger from '@comfyorg/design-system/tooltip/TooltipTrigger'
89 : import { useTelemetry } from '@comfyorg/telemetry'
90 : import { computed, useId } from 'vue'
91 : import { useI18n } from 'vue-i18n'
92 :
93 : import { useBillingPlanRow } from '@/platform/billing/components/billingTab/useBillingPlanRow'
94 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
95 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
96 : import { useCreditsBreakdownCopy } from '@/platform/billing/composables/useCreditsCardCopy'
97 : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
98 : import { usePaymentRecoveryDialog } from '@/platform/billing/composables/usePaymentRecoveryDialog'
99 : import { useProcessingTopupPayment } from '@/platform/billing/composables/useProcessingTopupPayment'
100 : import { useTopUpPlanCreditCopy } from '@/platform/billing/composables/useTopUpPlanCreditCopy'
101 : import { BILLING_ACTION_NAME } from '@/platform/telemetry/billingActionNames'
102 : import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource'
103 :
104 4 : const emit = defineEmits<{
105 : openBilling: []
106 : close: []
107 : }>()
108 :
109 4 : const { t, locale } = useI18n()
110 4 : const {
111 : balance,
112 : isLoading,
113 : billingStatus,
114 : periodCreditGrant,
115 : nextCreditRefillAt
116 : } = useBillingContext()
117 4 : const { state: planRow } = useBillingPlanRow()
118 4 : const breakdownCopy = useCreditsBreakdownCopy()
119 4 : const planCreditNote = useTopUpPlanCreditCopy()
120 4 : const breakdown = computed(() =>
121 : deriveCreditsFlyout({
122 : loading: isLoading.value,
123 : balance: balance.value,
124 : planKind: planRow.value.kind,
125 : billingStatus: billingStatus.value,
126 : periodCreditGrant: periodCreditGrant.value,
127 : nextCreditRefillAt: nextCreditRefillAt.value,
128 : endDate: planRow.value.kind === 'ending' ? planRow.value.endsAt : null
129 : })
130 : )
131 4 : const {
132 : canTopUp,
133 : topUpDenial,
134 : hasResolvedCapabilities,
135 : paymentRecoveryAvailable
136 : } = useBillingCapabilities()
137 4 : const { updatePayment, isUpdatingPayment } = usePaymentRecoveryDialog()
138 4 : const { showTopUpCreditsDialog } = useBillingDialogs()
139 4 : const { payment: processingPayment } = useProcessingTopupPayment(() => true)
140 :
141 4 : const addCredits = computed(() =>
142 : addCreditsAvailability({
143 : resolved: hasResolvedCapabilities.value,
144 : canTopUp: canTopUp.value,
145 : denial: topUpDenial.value
146 : })
147 : )
148 4 : const paymentPastDue = computed(
149 : () =>
150 : addCredits.value.kind === 'disabled' &&
151 : topUpDenial.value === 'payment_past_due'
152 : )
153 4 : const refusalReasonId = useId()
154 4 : const purchasesStopped = computed(
155 : () =>
156 : addCredits.value.kind === 'disabled' &&
157 : topUpDenial.value === 'topup_purchases_paused'
158 : )
159 4 : const refusalReason = computed(() => {
160 4 : if (addCredits.value.kind !== 'disabled') return null
161 4 : return purchasesStopped.value
162 : ? t('profileMenu.purchasesStopped')
163 : : t(addCredits.value.reasonKey)
164 : })
165 :
166 4 : function handleTopUp() {
167 4 : useTelemetry()?.trackAddApiCreditButtonClicked({ source: 'avatar_menu' })
168 4 : showTopUpCreditsDialog({
169 : source: paymentIntentSourceForAddCreditsClick('avatar_menu')
170 : })
171 4 : emit('close')
172 : }
173 : </script>
|