From 801f0f773e82abf731f20ab3ad968f461b542eb2 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 26 Oct 2024 03:59:15 -0300 Subject: [PATCH] Nick: fix auto charge failing when payment is through Link --- apps/api/src/services/billing/auto_charge.ts | 13 +++++++++++-- apps/api/src/services/billing/stripe.ts | 11 ++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/apps/api/src/services/billing/auto_charge.ts b/apps/api/src/services/billing/auto_charge.ts index 9669972a..5f318321 100644 --- a/apps/api/src/services/billing/auto_charge.ts +++ b/apps/api/src/services/billing/auto_charge.ts @@ -113,15 +113,24 @@ export async function autoCharge( // Reset ACUC cache to reflect the new credit balance const cacheKeyACUC = `acuc_${chunk.api_key}`; await deleteKey(cacheKeyACUC); - if (process.env.SLACK_ADMIN_WEBHOOK_URL ) { + + if (process.env.SLACK_ADMIN_WEBHOOK_URL) { + const webhookCooldownKey = `webhook_cooldown_${chunk.team_id}`; + const isInCooldown = await getValue(webhookCooldownKey); + + if (!isInCooldown) { sendSlackWebhook( - `Auto-recharge successful: Team ${chunk.team_id}. ${AUTO_RECHARGE_CREDITS} credits added. Payment status: ${paymentStatus.return_status}. User was notified via email.`, + `Auto-recharge: Team ${chunk.team_id}. ${AUTO_RECHARGE_CREDITS} credits added. Payment status: ${paymentStatus.return_status}.`, false, process.env.SLACK_ADMIN_WEBHOOK_URL ).catch((error) => { Logger.debug(`Error sending slack notification: ${error}`); }); + + // Set cooldown for 1 hour + await setValue(webhookCooldownKey, 'true', 60 * 60); } + } return { success: true, message: "Auto-recharge successful", diff --git a/apps/api/src/services/billing/stripe.ts b/apps/api/src/services/billing/stripe.ts index f1400804..e459d3e3 100644 --- a/apps/api/src/services/billing/stripe.ts +++ b/apps/api/src/services/billing/stripe.ts @@ -7,7 +7,7 @@ async function getCustomerDefaultPaymentMethod(customerId: string) { const paymentMethods = await stripe.customers.listPaymentMethods(customerId, { limit: 3, }); - return paymentMethods.data[0]?.id; + return paymentMethods.data[0] ?? null; } type ReturnStatus = "succeeded" | "requires_action" | "failed"; @@ -16,13 +16,18 @@ export async function createPaymentIntent( customer_id: string ): Promise<{ return_status: ReturnStatus; charge_id: string }> { try { + const defaultPaymentMethod = await getCustomerDefaultPaymentMethod(customer_id); + if (!defaultPaymentMethod) { + Logger.error(`No default payment method found for customer: ${customer_id}`); + return { return_status: "failed", charge_id: "" }; + } const paymentIntent = await stripe.paymentIntents.create({ amount: 1100, currency: "usd", customer: customer_id, description: "Firecrawl: Auto re-charge of 1000 credits", - payment_method_types: ["card"], - payment_method: await getCustomerDefaultPaymentMethod(customer_id), + payment_method_types: [defaultPaymentMethod?.type ?? "card"], + payment_method: defaultPaymentMethod?.id, off_session: true, confirm: true, });