Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser_tests/tests/dialogs/enterpriseTierCloud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
4 changes: 2 additions & 2 deletions browser_tests/tests/workspaceSwitcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') &&
Expand All @@ -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 ({
Expand Down
7 changes: 7 additions & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down
48 changes: 36 additions & 12 deletions src/platform/account/components/CurrentUserPopoverWorkspace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- User Info Section -->
<div class="mb-4 flex flex-col items-center px-0 py-3">
<UserAvatar
:label="$t('auth.login.userAvatar')"

Check failure on line 7 in src/platform/account/components/CurrentUserPopoverWorkspace.vue

View workflow job for this annotation

GitHub Actions / fallow / fallow

High complexity (high)

Function '<template>' exceeds both complexity thresholds: • Severity: high • Cyclomatic: 31 (threshold: 20) • Cognitive: 37 (threshold: 15) • Lines: 480 Consider splitting this function into smaller, focused functions.
class="mb-1"
:photo-url="userPhotoUrl"
icon-class="size-6"
Expand Down Expand Up @@ -94,27 +94,26 @@
>
<i class="icon-[lucide--circle-help]" />
</Button>
<Button
v-if="canTopUp"
<AddCreditsButton
:availability="addCredits"
:reason-id="refusalReasonId"
variant="secondary"
size="sm"
data-testid="add-credits-button"
:data-dd-action-name="BILLING_ACTION_NAME.addCreditsPopover"
@click="handleTopUp"
>
{{ $t('subscription.addCredits') }}
</Button>
</AddCreditsButton>
<Button
v-else-if="paymentRecoveryAvailable"
v-if="paymentRecoveryAvailable"
variant="secondary"
size="sm"
data-testid="add-credits-button"
:data-dd-action-name="BILLING_ACTION_NAME.addCreditsPopover"
@click="handleTopUp"
:loading="isUpdatingPayment"
@click="updatePayment"
>
{{ $t('subscription.addCredits') }}
{{ $t('workspacePanel.billingStatus.updatePayment') }}
</Button>
<!-- Subscribe/Resubscribe (only when not subscribed or cancelled) -->
<SubscribeButton
v-if="showSubscribeAction && isPersonalWorkspace"
:fluid="false"
Expand All @@ -140,6 +139,13 @@
</Button>
</div>

<AddCreditsRefusalReason
v-if="!accountActionsOnly"
:availability="addCredits"
:reason-id="refusalReasonId"
class="m-0 px-4 pb-2 text-xs text-muted-foreground"
/>

<p
v-if="processingPayment"
role="status"
Expand Down Expand Up @@ -250,7 +256,7 @@
import { PopoverArrow, PopoverRoot, PopoverTrigger } from 'reka-ui'
import { BILLING_ACTION_NAME } from '@/platform/telemetry/billingActionNames'
import { storeToRefs } from 'pinia'
import { computed, onMounted } from 'vue'
import { computed, onMounted, useId } from 'vue'
import { useI18n } from 'vue-i18n'

import { formatCreditsFromCents } from '@/base/credits/comfyCredits'
Expand All @@ -275,6 +281,10 @@
import { useTelemetry } from '@comfyorg/telemetry'
import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
import { usePaymentRecoveryDialog } from '@/platform/billing/composables/usePaymentRecoveryDialog'
import { addCreditsAvailability } from '@/platform/billing/addCreditsAvailability'
import AddCreditsButton from '@/platform/billing/components/AddCreditsButton.vue'
import AddCreditsRefusalReason from '@/platform/billing/components/AddCreditsRefusalReason.vue'
import { useAccountUI } from '@/platform/account/composables/useAccountUI'
import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog'
Expand All @@ -295,8 +305,22 @@
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: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@
>
<BillingCreditsCard
:state="creditsCard"
:can-top-up="capabilities.canTopUp.value"
:add-credits="
addCreditsAvailability({
resolved: capabilities.hasResolvedCapabilities.value,
canTopUp: capabilities.canTopUp.value,
denial: capabilities.topUpDenial.value
})
"
:add-credits-variant="addCreditsVariant"
:plan-credit-note="planCreditNote"
@retry="retryBalance"
Expand Down Expand Up @@ -176,6 +182,7 @@ import { usePlanCheckoutBanner } from '@/platform/billing/composables/usePlanChe
import BillingAutoTopUpCard from '@/platform/billing/components/billingTab/BillingAutoTopUpCard.vue'
import BillingCreditsCard from '@/platform/billing/components/billingTab/BillingCreditsCard.vue'
import BillingPlanRow from '@/platform/billing/components/billingTab/BillingPlanRow.vue'
import { addCreditsAvailability } from '@/platform/billing/addCreditsAvailability'
import { addCreditsEmphasis } from '@/platform/billing/components/billingTab/creditsCard'
import {
deriveBillingTabLoad,
Expand Down
73 changes: 73 additions & 0 deletions src/platform/billing/addCreditsAvailability.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { BillingTopUpDenial } from '@comfyorg/billing-core'
import { describe, expect, it } from 'vitest'

import { addCreditsAvailability } from '@/platform/billing/addCreditsAvailability'

const refused = { resolved: true, canTopUp: false }

describe('addCreditsAvailability', () => {
it.for<{
case: string
facts: {
resolved: boolean
canTopUp: boolean
denial: BillingTopUpDenial | undefined
}
expected: ReturnType<typeof addCreditsAvailability>
}>([
{
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)
})
})
41 changes: 41 additions & 0 deletions src/platform/billing/addCreditsAvailability.ts
Original file line number Diff line number Diff line change
@@ -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<BillingTopUpDenial> = new Set([
'not_workspace_owner',
'not_a_member'
])

const REFUSAL_REASON_KEYS: Partial<Record<BillingTopUpDenial, string>> = {
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 }
}
Loading
Loading