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
1 change: 0 additions & 1 deletion src/composables/__mocks__/useFeatureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const defaultFlags: FeatureFlags = {
billingSdkTopupRailEnabled: false,
billingSdkSubscriptionEnabled: false,
unifiedBillingSettingsEnabled: false,
autoTopUpEnabled: false,
billingSdkSubscriptionRailEnabled: false,
v1PaymentRecovery: false,
signupTurnstileMode: 'off',
Expand Down
3 changes: 1 addition & 2 deletions src/composables/useFeatureFlags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ describe('useFeatureFlags', () => {
{
flag: 'unifiedBillingSettingsEnabled' as const,
offCloudTrue: true
},
{ flag: 'autoTopUpEnabled' as const, offCloudTrue: false }
}
])('$flag', ({ flag, offCloudTrue }) => {
afterEach(() => {
vi.mocked(distributionTypes).isCloud = false
Expand Down
9 changes: 0 additions & 9 deletions src/composables/useFeatureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,6 @@ export function useFeatureFlags() {
remoteConfig.value.unified_billing_settings
)
},
get autoTopUpEnabled() {
if (!isCloud) return false

return resolveStrictBooleanFlag(
ServerFeatureFlag.AUTO_TOP_UP_ENABLED,
remoteConfig.value.auto_top_up_enabled
)
},
get v1PaymentRecovery() {
return resolveAuthGatedFlag(
ServerFeatureFlag.V1_PAYMENT_RECOVERY,
Expand Down Expand Up @@ -432,7 +424,6 @@ export function startFeatureFlagTelemetry() {
flags.billingSdkSubscriptionEnabled,
[ServerFeatureFlag.UNIFIED_BILLING_SETTINGS]:
flags.unifiedBillingSettingsEnabled,
[ServerFeatureFlag.AUTO_TOP_UP_ENABLED]: flags.autoTopUpEnabled,
[ServerFeatureFlag.V1_PAYMENT_RECOVERY]: flags.v1PaymentRecovery,
[ServerFeatureFlag.SIGNUP_TURNSTILE]: flags.signupTurnstileMode,
[ServerFeatureFlag.SUPPORTS_MODEL_TYPE_TAGS]: flags.supportsModelTypeTags,
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,10 @@
"verifyTitle": "Verify your payment",
"startOver": "Start over",
"viewPricing": "View pricing details",
"autoTopUp": {
"prompt": "Running low on credits? Turn on auto top-up.",
"setUp": "Set up"
},
"youPay": "Amount (USD)",
"youGet": "Credits",
"buyCredits": "Continue to payment",
Expand Down
127 changes: 127 additions & 0 deletions src/platform/billing/components/TopUpAutoTopUpCard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import type { AutoTopUpSetup } from '@comfyorg/ingest-types'
import { render, screen } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vitest'
import { computed } from 'vue'
import { createI18n } from 'vue-i18n'

import enMessages from '@/locales/en/main.json'
import { useTelemetry } from '@/platform/telemetry'
import {
autoTopUpCardState,
loadedAutoTopUp
} from '@/platform/billing/composables/__mocks__/useBillingAutoTopUpCard'
import {
autoTopUpEnabledFact,
canManageAutoTopUpFact
} from '@/platform/billing/composables/__mocks__/useBillingCapabilities'
import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'

import TopUpAutoTopUpCard from './TopUpAutoTopUpCard.vue'

vi.mock(import('@/platform/billing/composables/useBillingCapabilities'))
vi.mock(import('@/platform/billing/composables/useBillingAutoTopUpCard'))
vi.mock(import('@/platform/telemetry'))
vi.mock(import('@/platform/workspace/utils/platformLink'), () => ({
platformLink: (path: string, correlationId?: string) =>
`https://platform.test${path}?correlation_id=${correlationId}`
}))

const PROMPT = 'Running low on credits? Turn on auto top-up.'

type Served = {
enabled: boolean
canManage: boolean
setup: AutoTopUpSetup | null
card: ReturnType<typeof loadedAutoTopUp> | { kind: 'loading' | 'unavailable' }
}

const shown: Served = {
enabled: true,
canManage: true,
setup: { state: 'ready' },
card: loadedAutoTopUp('off')
}

function renderCard(served: Partial<Served> = {}) {
const { enabled, canManage, setup, card } = { ...shown, ...served }
const capabilities = useBillingCapabilities()
capabilities.autoTopUpEnabled = autoTopUpEnabledFact(enabled)
capabilities.canManageAutoTopUp = canManageAutoTopUpFact(canManage)
capabilities.autoTopUpSetup = computed(() => setup)
autoTopUpCardState.value = card
render(TopUpAutoTopUpCard, {
global: {
plugins: [
createI18n({
legacy: false,
locale: 'en',
messages: { en: enMessages }
})
]
}
})
}

describe('TopUpAutoTopUpCard', () => {
it('offers a manager with auto top-up off to set it up on the billing site', () => {
renderCard()

expect(screen.getByText(PROMPT)).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Set up' })).toHaveAttribute(
'href',
expect.stringContaining('https://platform.test/profile/billing')
)
})

it.for<{ name: string } & Partial<Served>>([
{ name: 'auto top-up is not served', enabled: false },
{ name: 'the viewer cannot manage it', canManage: false },
{ name: 'no setup is served', setup: null },
{
name: 'setup is unavailable',
setup: { state: 'unavailable', reason: 'workspace_blocked' }
},
{
name: 'setup is frozen by a past-due payment',
setup: { state: 'frozen', reason: 'payment_past_due' }
},
{ name: 'auto top-up is already on', card: loadedAutoTopUp('on') },
{ name: 'auto top-up has failed', card: loadedAutoTopUp('failed') },
{ name: 'the status is still loading', card: { kind: 'loading' } },
{ name: 'the status read failed', card: { kind: 'unavailable' } }
])('shows nothing when $name', ({ name: _, ...served }) => {
renderCard(served)

expect(screen.queryByText(PROMPT)).toBeNull()
})

it('stays shown while setup is frozen for another reason', () => {
renderCard({ setup: { state: 'frozen', reason: 'billing_paused' } })

expect(screen.getByText(PROMPT)).toBeInTheDocument()
})

it('reads the auto top-up status only when the served facts allow setup', () => {
renderCard({ canManage: false })
expect(useBillingAutoTopUpCard().ensureRead).not.toHaveBeenCalled()

renderCard()
expect(useBillingAutoTopUpCard().ensureRead).toHaveBeenCalledOnce()
})

it('reports the hand-off to the billing site', async () => {
renderCard()

await userEvent.click(screen.getByRole('link', { name: 'Set up' }))

expect(useTelemetry()?.trackBillingEvent).toHaveBeenCalledWith(
expect.objectContaining({
operation: 'platform_handoff',
target: 'auto_top_up',
action: 'turn_on'
})
)
})
})
63 changes: 63 additions & 0 deletions src/platform/billing/components/TopUpAutoTopUpCard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<template>
<section
v-if="visible"
class="flex items-center justify-between gap-4 rounded-2xl border border-border/60 p-4"
>
<p class="m-0 text-sm text-foreground">
{{ $t('credits.topUp.autoTopUp.prompt') }}
</p>
<Button
as="a"
:href
target="_blank"
rel="noopener"
variant="secondary"
size="md"
class="shrink-0 no-underline"
@click="track"
>
{{ $t('credits.topUp.autoTopUp.setUp') }}
<i class="icon-[lucide--external-link]" aria-hidden="true" />
</Button>
</section>
</template>

<script setup lang="ts">
import Button from '@/components/ui/button/Button.vue'
import { computed, watch } from 'vue'

import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
import { usePlatformHandoff } from '@/platform/workspace/composables/usePlatformHandoff'

const { autoTopUpEnabled, canManageAutoTopUp, autoTopUpSetup } =
useBillingCapabilities()
const { state, ensureRead } = useBillingAutoTopUpCard()

const canSetUp = computed(() => {
if (!autoTopUpEnabled.value) return false
if (!canManageAutoTopUp.value) return false
const setup = autoTopUpSetup.value
if (setup === null || setup.state === 'unavailable') return false
return !(setup.state === 'frozen' && setup.reason === 'payment_past_due')
})

watch(
canSetUp,
(allowed) => {
if (allowed) ensureRead()
},
{ immediate: true }
)

const visible = computed(() => {
if (!canSetUp.value) return false
const read = state.value
return read.kind === 'loaded' && read.settings.status === 'off'
})

const { href, track } = usePlatformHandoff('/profile/billing', {
target: 'auto_top_up',
action: 'turn_on'
})
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { createI18n } from 'vue-i18n'
import enMessages from '@/locales/en/main.json'

import { useToast } from '@/components/ui/toast/toastStore'
import {
autoTopUpCardState,
loadedAutoTopUp
} from '@/platform/billing/composables/__mocks__/useBillingAutoTopUpCard'
import {
autoTopUpEnabledFact,
canManageAutoTopUpFact
} from '@/platform/billing/composables/__mocks__/useBillingCapabilities'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
import { useBillingRouting } from '@/platform/billing/composables/useBillingRouting'
import { useSubscription } from '@/platform/billing/subscription/composables/useSubscription'
Expand All @@ -26,6 +35,8 @@ vi.mock(import('@/platform/billing/composables/useBillingContext'))

vi.mock(import('@/platform/billing/composables/useBillingCapabilities'))

vi.mock(import('@/platform/billing/composables/useBillingAutoTopUpCard'))

vi.mock(import('@/platform/distribution/types'), () => ({
isCloud: true,
isDesktop: false
Expand Down Expand Up @@ -68,7 +79,8 @@ const i18n = createI18n({
contactUs: 'Contact us',
viewPricing: 'View pricing',
insufficientWorkflowMessage: 'Insufficient credits',
buyCredits: 'Continue to payment'
buyCredits: 'Continue to payment',
autoTopUp: enMessages.credits.topUp.autoTopUp
}
}
}
Expand Down Expand Up @@ -237,6 +249,26 @@ describe('TopUpCreditsDialogContentLegacy', () => {
}
)

it('offers auto top-up set-up above Continue to payment when every served fact allows it', () => {
const capabilities = useBillingCapabilities()
capabilities.autoTopUpEnabled = autoTopUpEnabledFact(true)
capabilities.canManageAutoTopUp = canManageAutoTopUpFact(true)
capabilities.autoTopUpSetup = computed(() => ({ state: 'ready' as const }))
autoTopUpCardState.value = loadedAutoTopUp('off')

renderDialog()

const setUp = screen.getByRole('link', { name: 'Set up' })
expect(
screen.getByText('Running low on credits? Turn on auto top-up.')
).toBeInTheDocument()
expect(
setUp.compareDocumentPosition(
screen.getByRole('button', { name: 'Continue to payment' })
) & Node.DOCUMENT_POSITION_FOLLOWING
).toBeTruthy()
})

it('shows Plan & Credits after a successful Cloud purchase', async () => {
renderDialog()
await clickBuyCredits()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,18 @@
</p>

<div class="flex flex-col gap-8 p-8">
<Button
:disabled="!isValidAmount || loading"
:loading
variant="primary"
size="lg"
@click="handleBuy"
>
{{ $t('credits.topUp.buyCredits') }}
</Button>
<div class="flex flex-col gap-4">
<TopUpAutoTopUpCard />
<Button
:disabled="!isValidAmount || loading"
:loading
variant="primary"
size="lg"
@click="handleBuy"
>
{{ $t('credits.topUp.buyCredits') }}
</Button>
</div>
<div class="flex items-center justify-center gap-1">
<a
:href="pricingUrl"
Expand Down Expand Up @@ -174,6 +177,7 @@ import { creditsToUsd, usdToCredits } from '@/base/credits/comfyCredits'
import Button from '@/components/ui/button/Button.vue'
import DialogTitle from '@/components/ui/dialog/DialogTitle.vue'
import FormNumberField from '@/components/common/FormNumberField.vue'
import TopUpAutoTopUpCard from './TopUpAutoTopUpCard.vue'
import { useToast } from '@/components/ui/toast/toastStore'
import { useAuthActions } from '@/composables/auth/useAuthActions'
import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
Expand Down
Loading
Loading