diff options
| author | Aiden Cline <[email protected]> | 2025-09-20 03:15:44 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-20 04:15:44 -0400 |
| commit | d63ce40af27194dd584e6499dbad5b17c9d7cab3 (patch) | |
| tree | 81558d45cda50ea20754a3157cc1a274929d0bed /packages | |
| parent | 5acdd7058793f09dea4281a696359dd9281c7a63 (diff) | |
| download | opencode-d63ce40af27194dd584e6499dbad5b17c9d7cab3.tar.gz opencode-d63ce40af27194dd584e6499dbad5b17c9d7cab3.zip | |
fix: no payment method (#2706)
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/console/app/src/routes/zen/handler.ts | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/packages/console/app/src/routes/zen/handler.ts b/packages/console/app/src/routes/zen/handler.ts index a60abeb70..09b5ca06a 100644 --- a/packages/console/app/src/routes/zen/handler.ts +++ b/packages/console/app/src/routes/zen/handler.ts @@ -88,6 +88,7 @@ export async function handler( const authInfo = await authenticate() const modelInfo = validateModel(body.model, authInfo) const providerInfo = selectProvider(modelInfo, authInfo) + if (authInfo && !providerInfo.allowAnonymous) validateBilling(authInfo) logger.metric({ provider: providerInfo.id }) // Request to model provider @@ -250,33 +251,43 @@ export async function handler( }) const isFree = FREE_WORKSPACES.includes(data.workspaceID) - if (!isFree) { - if (!data.paymentMethodID) throw new CreditsError("No payment method") - if (data.balance <= 0) throw new CreditsError("Insufficient balance") - if ( - data.monthlyLimit && - data.monthlyUsage && - data.timeMonthlyUsageUpdated && - data.monthlyUsage >= centsToMicroCents(data.monthlyLimit * 100) - ) { - const now = new Date() - const currentYear = now.getUTCFullYear() - const currentMonth = now.getUTCMonth() - const dateYear = data.timeMonthlyUsageUpdated.getUTCFullYear() - const dateMonth = data.timeMonthlyUsageUpdated.getUTCMonth() - if (currentYear === dateYear && currentMonth === dateMonth) - throw new MonthlyLimitError(`You have reached your monthly spending limit of $${data.monthlyLimit}.`) - } - } return { apiKeyId: data.apiKey, workspaceID: data.workspaceID, dataShare: data.dataShare, + billing: { + paymentMethodID: data.paymentMethodID, + balance: data.balance, + monthlyLimit: data.monthlyLimit, + monthlyUsage: data.monthlyUsage, + timeMonthlyUsageUpdated: data.timeMonthlyUsageUpdated, + }, isFree, } } + function validateBilling(authInfo: Awaited<ReturnType<typeof authenticate>>) { + if (!authInfo || authInfo.isFree) return + const billing = authInfo.billing + if (!billing.paymentMethodID) throw new CreditsError("No payment method") + if (billing.balance <= 0) throw new CreditsError("Insufficient balance") + if ( + billing.monthlyLimit && + billing.monthlyUsage && + billing.timeMonthlyUsageUpdated && + billing.monthlyUsage >= centsToMicroCents(billing.monthlyLimit * 100) + ) { + const now = new Date() + const currentYear = now.getUTCFullYear() + const currentMonth = now.getUTCMonth() + const dateYear = billing.timeMonthlyUsageUpdated.getUTCFullYear() + const dateMonth = billing.timeMonthlyUsageUpdated.getUTCMonth() + if (currentYear === dateYear && currentMonth === dateMonth) + throw new MonthlyLimitError(`You have reached your monthly spending limit of $${billing.monthlyLimit}.`) + } + } + function validateModel(reqModel: string, authInfo: Awaited<ReturnType<typeof authenticate>>) { const json = JSON.parse(Resource.ZEN_MODELS.value) |
