+
+
activeWorkspace.value ? billingState.getSummary(activeWorkspace.value) : null
)
const { permissions, canReactivatePlan, canOpenPricingSurface } = useAccountUI()
-const { canTopUp, canSubscribeSelfServe, paymentRecoveryAvailable } =
- useBillingCapabilities()
+const {
+ canTopUp,
+ topUpDenial,
+ hasResolvedCapabilities,
+ canSubscribeSelfServe,
+ paymentRecoveryAvailable
+} = useBillingCapabilities()
+const { updatePayment, isUpdatingPayment } = usePaymentRecoveryDialog()
+const addCredits = computed(() =>
+ addCreditsAvailability({
+ resolved: hasResolvedCapabilities.value,
+ canTopUp: canTopUp.value,
+ denial: topUpDenial.value
+ })
+)
+const refusalReasonId = useId()
const emit = defineEmits<{
close: []
diff --git a/src/platform/account/components/dialogs/settings/BillingTab.vue b/src/platform/account/components/dialogs/settings/BillingTab.vue
index 5422f1a3eee..8665690e0fa 100644
--- a/src/platform/account/components/dialogs/settings/BillingTab.vue
+++ b/src/platform/account/components/dialogs/settings/BillingTab.vue
@@ -71,7 +71,13 @@
>
{
+ it.for<{
+ case: string
+ facts: {
+ resolved: boolean
+ canTopUp: boolean
+ denial: BillingTopUpDenial | undefined
+ }
+ expected: ReturnType
+ }>([
+ {
+ case: 'an allowed top-up',
+ facts: { resolved: true, canTopUp: true, denial: undefined },
+ expected: { kind: 'enabled' }
+ },
+ {
+ case: 'capabilities not yet served',
+ facts: { resolved: false, canTopUp: false, denial: undefined },
+ expected: { kind: 'hidden' }
+ },
+ {
+ case: 'a member who is not the owner',
+ facts: { ...refused, denial: 'not_workspace_owner' },
+ expected: { kind: 'hidden' }
+ },
+ {
+ case: 'a caller outside the workspace',
+ facts: { ...refused, denial: 'not_a_member' },
+ expected: { kind: 'hidden' }
+ },
+ {
+ case: 'paused top-up purchases',
+ facts: { ...refused, denial: 'topup_purchases_paused' },
+ expected: {
+ kind: 'disabled',
+ reasonKey: 'credits.addCreditsRefusal.topupPurchasesPaused'
+ }
+ },
+ {
+ case: 'a past-due payment',
+ facts: { ...refused, denial: 'payment_past_due' },
+ expected: {
+ kind: 'disabled',
+ reasonKey: 'credits.addCreditsRefusal.paymentPastDue'
+ }
+ },
+ {
+ case: 'another served refusal',
+ facts: { ...refused, denial: 'creator_only_provisioning' },
+ expected: {
+ kind: 'disabled',
+ reasonKey: 'credits.addCreditsRefusal.unavailable'
+ }
+ },
+ {
+ case: 'a refusal with an unrecognized or missing reason',
+ facts: { ...refused, denial: undefined },
+ expected: {
+ kind: 'disabled',
+ reasonKey: 'credits.addCreditsRefusal.unavailable'
+ }
+ }
+ ])('renders $case as $expected.kind', ({ facts, expected }) => {
+ expect(addCreditsAvailability(facts)).toEqual(expected)
+ })
+})
diff --git a/src/platform/billing/addCreditsAvailability.ts b/src/platform/billing/addCreditsAvailability.ts
new file mode 100644
index 00000000000..07009dc205b
--- /dev/null
+++ b/src/platform/billing/addCreditsAvailability.ts
@@ -0,0 +1,41 @@
+import type { BillingTopUpDenial } from '@comfyorg/billing-core'
+
+export type AddCreditsAvailability =
+ | { kind: 'enabled' }
+ | { kind: 'hidden' }
+ | { kind: 'disabled'; reasonKey: string }
+
+const MEMBER_TOP_UP_DENIALS: ReadonlySet = new Set([
+ 'not_workspace_owner',
+ 'not_a_member'
+])
+
+const REFUSAL_REASON_KEYS: Partial> = {
+ topup_purchases_paused: 'credits.addCreditsRefusal.topupPurchasesPaused',
+ payment_past_due: 'credits.addCreditsRefusal.paymentPastDue'
+}
+
+const UNAVAILABLE_REASON_KEY = 'credits.addCreditsRefusal.unavailable'
+
+/** Whether the served `can_top_up` refusal names someone who is not the workspace owner. */
+export function isMemberTopUpDenial(
+ denial: BillingTopUpDenial | undefined
+): boolean {
+ return denial !== undefined && MEMBER_TOP_UP_DENIALS.has(denial)
+}
+
+/** How Add credits renders for the served `can_top_up` and its denial reason. */
+export function addCreditsAvailability(facts: {
+ resolved: boolean
+ canTopUp: boolean
+ denial: BillingTopUpDenial | undefined
+}): AddCreditsAvailability {
+ if (facts.canTopUp) return { kind: 'enabled' }
+ if (!facts.resolved || isMemberTopUpDenial(facts.denial)) {
+ return { kind: 'hidden' }
+ }
+ const reasonKey =
+ (facts.denial && REFUSAL_REASON_KEYS[facts.denial]) ??
+ UNAVAILABLE_REASON_KEY
+ return { kind: 'disabled', reasonKey }
+}
diff --git a/src/platform/billing/components/AddCreditsButton.vue b/src/platform/billing/components/AddCreditsButton.vue
new file mode 100644
index 00000000000..859b43c38cc
--- /dev/null
+++ b/src/platform/billing/components/AddCreditsButton.vue
@@ -0,0 +1,20 @@
+
+
+
+
+
diff --git a/src/platform/billing/components/AddCreditsRefusalReason.vue b/src/platform/billing/components/AddCreditsRefusalReason.vue
new file mode 100644
index 00000000000..972aade6364
--- /dev/null
+++ b/src/platform/billing/components/AddCreditsRefusalReason.vue
@@ -0,0 +1,14 @@
+
+
+ {{ $t(availability.reasonKey) }}
+
+
+
+
diff --git a/src/platform/billing/components/billingTab/BillingCreditsCard.test.ts b/src/platform/billing/components/billingTab/BillingCreditsCard.test.ts
index 57cedce6f72..929ef61f5ca 100644
--- a/src/platform/billing/components/billingTab/BillingCreditsCard.test.ts
+++ b/src/platform/billing/components/billingTab/BillingCreditsCard.test.ts
@@ -7,6 +7,7 @@ import type { BalanceInfo } from '@/platform/billing/composables/types'
import enMessages from '@/locales/en/main.json'
import { deriveCreditsCard } from '@/platform/billing/components/billingTab/creditsCard'
import type { CreditsCardFacts } from '@/platform/billing/components/billingTab/creditsCard'
+import type { AddCreditsAvailability } from '@/platform/billing/addCreditsAvailability'
import BillingCreditsCard from './BillingCreditsCard.vue'
@@ -45,13 +46,15 @@ const CARD_NOTES = [
"Couldn't load your balance."
]
+const ENABLED: AddCreditsAvailability = { kind: 'enabled' }
+
function renderCard(
facts: CreditsCardFacts,
- canTopUp = true,
+ addCredits: AddCreditsAvailability = ENABLED,
planCreditNote: string | null = null
) {
return render(BillingCreditsCard, {
- props: { state: deriveCreditsCard(facts), canTopUp, planCreditNote },
+ props: { state: deriveCreditsCard(facts), addCredits, planCreditNote },
global: {
plugins: [
createI18n({
@@ -68,7 +71,7 @@ describe('BillingCreditsCard', () => {
it.for<{
case: string
facts: Partial
- canTopUp?: boolean
+ availability?: AddCreditsAvailability
planCreditNote?: string
available: string
plan: string | null
@@ -166,9 +169,9 @@ describe('BillingCreditsCard', () => {
notes: [SUBSCRIBE_NOTE]
},
{
- case: 'a server that refuses top-up',
+ case: 'a server that refuses top-up to a member',
facts: {},
- canTopUp: false,
+ availability: { kind: 'hidden' },
available: '1,477',
plan: '844 of 1,000',
topUp: '633',
@@ -204,7 +207,7 @@ describe('BillingCreditsCard', () => {
])('renders $case', (row) => {
renderCard(
{ ...activeFacts, ...row.facts },
- row.canTopUp ?? true,
+ row.availability ?? ENABLED,
row.planCreditNote ?? null
)
@@ -226,6 +229,50 @@ describe('BillingCreditsCard', () => {
)
})
+ it.for<{
+ case: string
+ facts: Partial
+ reasonKey: string
+ reason: string
+ }>([
+ {
+ case: 'paused top-up purchases on a plan',
+ facts: {},
+ reasonKey: 'credits.addCreditsRefusal.topupPurchasesPaused',
+ reason:
+ "Buying credits isn't available right now. Try again later, or message support."
+ },
+ {
+ case: 'a past-due payment on a plan',
+ facts: {},
+ reasonKey: 'credits.addCreditsRefusal.paymentPastDue',
+ reason: 'Update your payment method to add credits.'
+ },
+ {
+ case: 'another refusal without a plan',
+ facts: {
+ planKind: 'noPlan',
+ balance: balanceOf({ effective: 200, plan: 0, topUp: 200 })
+ },
+ reasonKey: 'credits.addCreditsRefusal.unavailable',
+ reason: "Adding credits isn't available for this workspace right now."
+ }
+ ])(
+ 'shows Add credits disabled with the reason for $case',
+ async ({ facts, reasonKey, reason }) => {
+ const { emitted } = renderCard(
+ { ...activeFacts, ...facts },
+ { kind: 'disabled', reasonKey }
+ )
+
+ const addCredits = screen.getByRole('button', { name: 'Add credits' })
+ expect(addCredits).toBeDisabled()
+ expect(addCredits).toHaveAccessibleDescription(reason)
+ await userEvent.click(addCredits)
+ expect(emitted('addCredits')).toBeUndefined()
+ }
+ )
+
it.for<{
case: string
facts: Partial
diff --git a/src/platform/billing/components/billingTab/BillingCreditsCard.vue b/src/platform/billing/components/billingTab/BillingCreditsCard.vue
index 1fc0e0da021..b1d0c7b6b15 100644
--- a/src/platform/billing/components/billingTab/BillingCreditsCard.vue
+++ b/src/platform/billing/components/billingTab/BillingCreditsCard.vue
@@ -70,15 +70,25 @@
{{ planCreditNote }}
-
+
+ {{ $t('workspacePanel.billingTab.credits.addCredits') }}
+
+
+
@@ -108,15 +118,25 @@
{{ $t('workspacePanel.billingTab.credits.inUse') }}
-
+
+ {{ $t('workspacePanel.billingTab.credits.addCredits') }}
+
+
+
@@ -125,7 +145,7 @@