diff --git a/browser_tests/tests/dialogs/enterpriseTierCloud.spec.ts b/browser_tests/tests/dialogs/enterpriseTierCloud.spec.ts index a28fc121ade..46e3620af20 100644 --- a/browser_tests/tests/dialogs/enterpriseTierCloud.spec.ts +++ b/browser_tests/tests/dialogs/enterpriseTierCloud.spec.ts @@ -262,7 +262,7 @@ test.describe('Enterprise workspace billing', { tag: '@cloud' }, () => { await expect(content.getByTestId('subscription-state-card')).toHaveCount(0) await expect( content.getByRole('button', { name: 'Add credits' }) - ).toHaveCount(0) + ).toBeDisabled() await expect( content.getByText( "You can't run workflows or add new members. Contact sales to restore access." diff --git a/browser_tests/tests/workspaceSwitcher.spec.ts b/browser_tests/tests/workspaceSwitcher.spec.ts index 243074ccb17..724b812550e 100644 --- a/browser_tests/tests/workspaceSwitcher.spec.ts +++ b/browser_tests/tests/workspaceSwitcher.spec.ts @@ -145,7 +145,7 @@ test.describe('Workspace switcher', { tag: '@cloud' }, () => { await comfyPage.toast.closeToasts() await page.getByRole('button', { name: 'Current user' }).click() - await expect(page.getByTestId('add-credits-button')).toBeVisible() + await expect(page.getByTestId('add-credits-button')).toBeEnabled() const longWorkspaceCapabilities = page.waitForResponse( (response) => response.url().includes('/api/billing/capabilities') && @@ -161,7 +161,7 @@ test.describe('Workspace switcher', { tag: '@cloud' }, () => { await comfyPage.waitForAppReady() await page.getByRole('button', { name: 'Current user' }).click() - await expect(page.getByTestId('add-credits-button')).toHaveCount(0) + await expect(page.getByTestId('add-credits-button')).toBeDisabled() }) test('isolates all open workflow tabs between workspaces', async ({ diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 24bcfdbeba7..6976270c2b5 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -2983,6 +2983,11 @@ }, "credits": { "activity": "Activity", + "addCreditsRefusal": { + "topupPurchasesPaused": "Buying credits isn't available right now. Try again later, or message support.", + "paymentPastDue": "Update your payment method to add credits.", + "unavailable": "Adding credits isn't available for this workspace right now." + }, "workspaceCredits": "Workspace credits", "insufficient": { "memberTitle": "This workspace is out of credits", @@ -3774,6 +3779,8 @@ "pastDuePersonalBody": "You've used all your credits. Update your payment method to add more.", "memberBody": "Your team has used all its credits. Ask your workspace owner to add more credits or wait until credits refill on {date}.", "memberBodyNoDate": "Your team has used all its credits. Ask your workspace owner to add more credits.", + "refusedBody": "Your team has used all its credits.", + "refusedPersonalBody": "You've used all your credits.", "addCredits": "Add credits", "dismiss": "Dismiss" }, diff --git a/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts b/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts index e780a540340..4fa24650592 100644 --- a/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts +++ b/src/platform/account/components/CurrentUserPopoverWorkspace.test.ts @@ -3,6 +3,7 @@ import type { HostedTopupCheckout } from '@comfyorg/ingest-types' import { billingApi } from '@/platform/billing/api/billingApi' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs' +import { usePaymentRecoveryDialog } from '@/platform/billing/composables/usePaymentRecoveryDialog' import { useWorkspaceDialogs } from '@/platform/account/composables/useWorkspaceDialogs' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { getActivePinia } from 'pinia' @@ -31,7 +32,7 @@ import enMessages from '@/locales/en/main.json' import { useSubscriptionDialog } from '@/platform/billing/subscription/composables/useSubscriptionDialog' import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog' import { useAccountUI } from '@/platform/account/composables/useAccountUI' -import type { BillingStatus } from '@comfyorg/billing-core' +import type { BillingStatus, BillingTopUpDenial } from '@comfyorg/billing-core' import { receiveServerFact } from '@/platform/serverFacts/serverFact' import CurrentUserPopoverWorkspace from '@/platform/account/components/CurrentUserPopoverWorkspace.vue' @@ -87,6 +88,7 @@ vi.mock(import('@/platform/distribution/types'), () => ({ })) vi.mock(import('@/platform/billing/composables/useBillingDialogs')) +vi.mock(import('@/platform/billing/composables/usePaymentRecoveryDialog')) vi.mock(import('@/platform/account/composables/useWorkspaceDialogs')) vi.mock(import('@comfyorg/telemetry')) @@ -161,6 +163,14 @@ describe('CurrentUserPopoverWorkspace', () => { state.isCancelled = false state.planSlug = 'pro-monthly' useBillingCapabilities().canTopUp = computed(() => false) + useBillingCapabilities().topUpDenial = computed(() => 'not_workspace_owner') + vi.mocked(usePaymentRecoveryDialog).mockReturnValue({ + showPaymentRecoveryDialog: vi.fn(), + updatePayment: vi.fn(), + isUpdatingPayment: computed(() => false), + paymentRecoveryLock: computed(() => null), + refreshCapabilities: vi.fn(async () => undefined) + }) state.canManageSubscription = false state.canManageSubscriptionLifecycle = false @@ -514,19 +524,60 @@ describe('CurrentUserPopoverWorkspace', () => { expect(screen.queryByTestId('add-credits-button')).not.toBeInTheDocument() }) - it('keeps Add credits for a past-due owner so it can open Update payment', async () => { + it.for<{ denial: BillingTopUpDenial | undefined; reason: string }>([ + { + denial: 'topup_purchases_paused', + reason: + "Buying credits isn't available right now. Try again later, or message support." + }, + { + denial: 'payment_past_due', + reason: 'Update your payment method to add credits.' + }, + { + denial: 'tier_not_self_serve', + reason: "Adding credits isn't available for this workspace right now." + }, + { + denial: undefined, + reason: "Adding credits isn't available for this workspace right now." + } + ])( + 'shows Add credits disabled with the reason for a $denial refusal', + ({ denial, reason }) => { + useBillingCapabilities().topUpDenial = computed(() => denial) + + renderComponent('team') + + const addCredits = screen.getByTestId('add-credits-button') + expect(addCredits).toBeDisabled() + expect(addCredits).toHaveAccessibleDescription(reason) + } + ) + + it.for(['not_workspace_owner', 'not_a_member'] as const)( + 'hides Add credits for a %s refusal', + (denial) => { + useBillingCapabilities().topUpDenial = computed(() => denial) + + renderComponent('team') + + expect(screen.queryByTestId('add-credits-button')).not.toBeInTheDocument() + } + ) + + it('keeps a past-due owner a way to update payment', async () => { const user = userEvent.setup() - useBillingCapabilities().canTopUp = computed(() => false) + useBillingCapabilities().topUpDenial = computed(() => 'payment_past_due') useBillingCapabilities().paymentRecoveryAvailable = paymentRecoveryAvailableFact(true) renderComponent('team') - await user.click(screen.getByTestId('add-credits-button')) + await user.click(screen.getByRole('button', { name: 'Update payment' })) - expect( - useBillingDialogs().showTopUpCreditsDialog - ).toHaveBeenCalledExactlyOnceWith({ source: 'avatar_menu_plans' }) + expect(usePaymentRecoveryDialog().updatePayment).toHaveBeenCalledOnce() + expect(useBillingDialogs().showTopUpCreditsDialog).not.toHaveBeenCalled() }) it('keeps Resubscribe hidden on Local for a cancelled plan', () => { diff --git a/src/platform/account/components/CurrentUserPopoverWorkspace.vue b/src/platform/account/components/CurrentUserPopoverWorkspace.vue index 6695b9d717d..e2f01f27706 100644 --- a/src/platform/account/components/CurrentUserPopoverWorkspace.vue +++ b/src/platform/account/components/CurrentUserPopoverWorkspace.vue @@ -94,8 +94,9 @@ > - + - + +

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 @@ + + + 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 @@