From d63ce40af27194dd584e6499dbad5b17c9d7cab3 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sat, 20 Sep 2025 03:15:44 -0500 Subject: [PATCH] fix: no payment method (#2706) --- .../console/app/src/routes/zen/handler.ts | 47 ++++++++++++------- 1 file 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 a60abeb7..09b5ca06 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>) { + 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>) { const json = JSON.parse(Resource.ZEN_MODELS.value)