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
25 changes: 16 additions & 9 deletions src/platform/billing/components/TopUpAmountFields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
:aria-label="$t('credits.topUp.youGet')"
input-class="text-lg font-medium"
:min="0"
:max="usdToCredits(maxAmount)"
:max="maxCredits"
:step="usdToCredits(getStepAmount(creditsToUsd(credits)))"
:format-options="{ maximumFractionDigits: 0 }"
@update:model-value="emit('credits', $event)"
Expand All @@ -85,14 +85,21 @@ import { cn } from '@comfyorg/tailwind-utils'
import { creditsToUsd, usdToCredits } from '@/base/credits/comfyCredits'
import FormNumberField from '@/components/common/FormNumberField.vue'

const { payAmount, credits, selectedPreset, maxAmount, planCreditCopy } =
defineProps<{
payAmount: number
credits: number
selectedPreset: number | null
maxAmount: number
planCreditCopy: string | null
}>()
const {
payAmount,
credits,
selectedPreset,
maxAmount,
maxCredits,
planCreditCopy
} = defineProps<{
payAmount: number
credits: number
selectedPreset: number | null
maxAmount: number
maxCredits: number
planCreditCopy: string | null
}>()

const emit = defineEmits<{
preset: [amount: number]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,93 @@ describe('TopUpCreditsDialogContentWorkspace', () => {
).not.toBeInTheDocument()
})

const SERVED_MINIMUM: Partial<BalanceInfo> = {
minTopupCents: 2_500,
minTopupCredits: 5_275
}
const SERVED_MAXIMUM: Partial<BalanceInfo> = {
maxTopupCents: 5_000,
maxTopupCredits: 10_550
}

it.for<{
name: string
balance: Partial<BalanceInfo>
preset: string
message: RegExp
}>([
{
name: 'below the served minimum',
balance: SERVED_MINIMUM,
preset: '$10',
message: /5,275 credits minimum/
},
{
name: 'above the served maximum',
balance: SERVED_MAXIMUM,
preset: '$100',
message: /10,550 credits maximum/
}
])(
'blocks an amount $name and names the bound',
async ({ balance, preset, message }) => {
renderWithBalance(balance)

await userEvent.click(screen.getByRole('button', { name: preset }))

expect(screen.getByRole('button', { name: 'Add credits' })).toBeDisabled()
expect(screen.getByText(message)).toBeVisible()
}
)

it.for<{ name: string; balance: Partial<BalanceInfo>; preset: string }>([
{ name: 'at the served minimum', balance: SERVED_MINIMUM, preset: '$25' },
{ name: 'above $5 when no minimum is served', balance: {}, preset: '$10' },
{
name: 'above $5 when the minimum has no served credits',
balance: { minTopupCents: 2_500 },
preset: '$10'
},
{ name: 'at the served maximum', balance: SERVED_MAXIMUM, preset: '$50' },
{
name: 'under $10,000 when no maximum is served',
balance: {},
preset: '$100'
},
{
name: 'under $10,000 when the maximum has no served credits',
balance: { maxTopupCents: 5_000 },
preset: '$100'
}
])('allows an amount $name', async ({ balance, preset }) => {
renderWithBalance(balance)

await userEvent.click(screen.getByRole('button', { name: preset }))

expect(screen.getByRole('button', { name: 'Add credits' })).toBeEnabled()
expect(screen.queryByText(/credits minimum/)).not.toBeInTheDocument()
})

it.for<{ name: string; balance: Partial<BalanceInfo>; cap: string }>([
{
name: 'the served maximum credits',
balance: SERVED_MAXIMUM,
cap: '10550'
},
{
name: '$10,000 in credits when no maximum is served',
balance: {},
cap: '2110000'
}
])('caps the credits input at $name', ({ balance, cap }) => {
renderWithBalance(balance)

expect(screen.getByRole('spinbutton', { name: 'Credits' })).toHaveAttribute(
'aria-valuemax',
cap
)
})

describe('without a default payment method', () => {
const checkoutUrl = 'https://checkout.stripe.com/c/pay/cs_test'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
:pay-amount
:credits="creditsModel"
:selected-preset
:max-amount="MAX_AMOUNT"
:max-amount="maxTopup.usd"
:max-credits="maxTopup.credits"
:plan-credit-copy
@preset="handlePresetClick"
@pay-amount="handlePayAmountChange"
Expand Down Expand Up @@ -171,7 +172,7 @@
<i class="icon-[lucide--coins]" />
{{
$t('credits.topUp.minRequired', {
credits: formatNumber(usdToCredits(MIN_AMOUNT))
credits: formatNumber(minTopup.credits)
})
}}
</p>
Expand All @@ -182,7 +183,7 @@
<i class="icon-[lucide--coins]" />
{{
$t('credits.topUp.maxAllowed', {
credits: formatNumber(usdToCredits(MAX_AMOUNT))
credits: formatNumber(maxTopup.credits)
})
}}
<span>{{ $t('credits.topUp.needMore') }}</span>
Expand Down Expand Up @@ -596,8 +597,35 @@ const creditsModel = computed({
}
})

function servedBoundOr(
fallbackUsd: number,
cents: number | undefined,
credits: number | undefined
) {
return cents === undefined || credits === undefined
? { usd: fallbackUsd, credits: usdToCredits(fallbackUsd) }
: { usd: cents / 100, credits }
}

const minTopup = computed(() =>
servedBoundOr(
MIN_AMOUNT,
balance.value?.minTopupCents,
balance.value?.minTopupCredits
)
)
const maxTopup = computed(() =>
servedBoundOr(
MAX_AMOUNT,
balance.value?.maxTopupCents,
balance.value?.maxTopupCredits
)
)

const isValidAmount = computed(
() => payAmount.value >= MIN_AMOUNT && payAmount.value <= MAX_AMOUNT
() =>
payAmount.value >= minTopup.value.usd &&
payAmount.value <= maxTopup.value.usd
)

const negativeBalanceCredits = computed(() => {
Expand All @@ -608,8 +636,8 @@ const negativeBalanceCredits = computed(() => {
return centsToCredits(-cents)
})

const isBelowMin = computed(() => payAmount.value < MIN_AMOUNT)
const showCeilingWarning = computed(() => payAmount.value >= MAX_AMOUNT)
const isBelowMin = computed(() => payAmount.value < minTopup.value.usd)
const showCeilingWarning = computed(() => payAmount.value >= maxTopup.value.usd)

const displayTotal = computed(() =>
n(payAmount.value, {
Expand Down
8 changes: 8 additions & 0 deletions src/platform/billing/composables/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ export interface BalanceInfo {
cloudCreditBalanceMicros?: number
/** Server-suggested top-up that covers a negative balance; null otherwise. */
suggestedTopupAmountCents?: number | null
/** Served `min_topup_cents`. Absent when the balance response omits it. */
minTopupCents?: number
/** Served `min_topup_credits`. Absent when the balance response omits it. */
minTopupCredits?: number
/** Served `max_topup_cents`. Absent when the balance response omits it. */
maxTopupCents?: number
/** Served `max_topup_credits`. Absent when the balance response omits it. */
maxTopupCredits?: number
/** Served `low_balance`. Absent when the balance response omits it. */
lowBalance?: boolean
}
Expand Down
4 changes: 4 additions & 0 deletions src/platform/billing/composables/useWorkspaceBilling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export function useWorkspaceBilling(): WorkspaceBilling {
prepaidBalanceMicros: data.prepaid_balance_micros ?? 0,
cloudCreditBalanceMicros: data.cloud_credit_balance_micros ?? 0,
suggestedTopupAmountCents: data.suggested_topup_amount_cents,
minTopupCents: data.min_topup_cents,
minTopupCredits: data.min_topup_credits,
maxTopupCents: data.max_topup_cents,
maxTopupCredits: data.max_topup_credits,
lowBalance: data.low_balance
}
})
Expand Down
Loading