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
31 changes: 24 additions & 7 deletions src/platform/workspace/composables/useTopupOperation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ describe('useTopupOperation', () => {
status: 409,
code: 'TOPUP_CHECKOUT_NOT_PAYABLE'
}
const unavailable: Refusal = {
status: 503,
code: 'TOPUP_CHECKOUT_UNAVAILABLE'
}
const returnUrl = 'https://cloud.comfy.org/topup-return'

const rails = [
Expand Down Expand Up @@ -253,27 +257,40 @@ describe('useTopupOperation', () => {
return outcome
}

it.for(rails)(
'mints a fresh checkout after a retryable refusal on the $rail rail',
async ({ flagOn, mint }) => {
it.for(
rails.flatMap((rail) => [
{
...rail,
name: 'a busy then not-payable checkout',
outcomes: [busy, notPayable, 'https://stripe/new']
},
{
...rail,
name: 'an unavailable checkout',
outcomes: [unavailable, 'https://stripe/new']
}
])
)(
'mints a fresh checkout after $name on the $rail rail',
async ({ flagOn, mint, outcomes }) => {
flagState.billingSdkTopupEnabled = flagOn
const mintCheckout = mint([busy, notPayable, 'https://stripe/new'])
const mintCheckout = mint(outcomes)

const outcome = await settle(
useTopupOperation().createHostedCheckout(1000, returnUrl)
)

expect(outcome).toEqual({ url: 'https://stripe/new' })
expect(mintCheckout).toHaveBeenCalledTimes(3)
expect(mintCheckout).toHaveBeenCalledTimes(outcomes.length)
}
)

it.for(
rails.flatMap((rail) => [
{
...rail,
name: 'a refusal outside the retryable set',
outcomes: [{ status: 503, code: 'TOPUP_CHECKOUT_UNAVAILABLE' }],
name: 'a 503 without the unavailable code',
outcomes: [{ status: 503, code: 'UPSTREAM_UNAVAILABLE' }],
attempts: 1
},
{
Expand Down
14 changes: 8 additions & 6 deletions src/platform/workspace/composables/useTopupOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import {
import { useBillingSdkStore } from '@/platform/workspace/billing/sdk/billingSdkStore'
import { useBillingOperationStore } from '@/platform/workspace/stores/billingOperationStore'

const RETRYABLE_CHECKOUT_CODES: ReadonlySet<string | undefined> = new Set([
'TOPUP_CHECKOUT_BUSY',
'TOPUP_CHECKOUT_NOT_PAYABLE'
])
const RETRYABLE_CHECKOUT_REFUSALS: ReadonlyArray<readonly [number, string]> = [
[409, 'TOPUP_CHECKOUT_BUSY'],
[409, 'TOPUP_CHECKOUT_NOT_PAYABLE'],
[503, 'TOPUP_CHECKOUT_UNAVAILABLE']
]

const CHECKOUT_RETRY_DELAYS_MS = [1000, 2000]

function isRetryableCheckoutRefusal(error: unknown): boolean {
return (
error instanceof WorkspaceApiError &&
error.status === 409 &&
RETRYABLE_CHECKOUT_CODES.has(error.code)
RETRYABLE_CHECKOUT_REFUSALS.some(
([status, code]) => error.status === status && error.code === code
)
)
}

Expand Down
Loading