The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
switch to charge.failed webhook instead
Steven Teyc6846f46bb0b
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.tsindex 96c03d5232..e874133b9b 100644--- a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts@@ -1,5 +1,6 @@import { prisma } from "@/lib/prisma";import Stripe from "stripe";+import { detectAndHandleFraudulentFailedCharge } from "./utils/detect-and-handle-fraudulent-failed-charge";import { processDomainRenewalFailure } from "./utils/process-domain-renewal-failure";import { processPayoutInvoiceFailure } from "./utils/process-payout-invoice-failure";@@ -9,7 +10,7 @@ export async function chargeFailed(event: Stripe.ChargeFailedEvent) {const { transfer_group: invoiceId, failure_message: failedReason } = charge;if (!invoiceId) {- return "No transfer group found, skipping...";+ return detectAndHandleFraudulentFailedCharge(event);}let invoice = await prisma.invoice.findUnique({diff --git a/apps/web/app/(ee)/api/stripe/webhook/route.ts b/apps/web/app/(ee)/api/stripe/webhook/route.tsindex 0960440afb..17c5e8d69d 100644--- a/apps/web/app/(ee)/api/stripe/webhook/route.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/route.ts@@ -10,7 +10,6 @@ import { customerSubscriptionCreated } from "./customer-subscription-created";import { customerSubscriptionDeleted } from "./customer-subscription-deleted";import { customerSubscriptionUpdated } from "./customer-subscription-updated";import { invoicePaymentFailed } from "./invoice-payment-failed";-import { paymentIntentPaymentFailed } from "./payment-intent-payment-failed";import { paymentIntentRequiresAction } from "./payment-intent-requires-action";import { transferReversed } from "./transfer-reversed";@@ -23,7 +22,6 @@ const relevantEvents = new Set(["customer.subscription.updated","customer.subscription.deleted","invoice.payment_failed",- "payment_intent.payment_failed","payment_intent.requires_action","transfer.reversed",]);@@ -78,9 +76,6 @@ export const POST = async (req: Request) => {case "invoice.payment_failed":response = await invoicePaymentFailed(event);break;- case "payment_intent.payment_failed":- response = await paymentIntentPaymentFailed(event);- break;case "payment_intent.requires_action":response = await paymentIntentRequiresAction(event);break;diff --git a/apps/web/app/(ee)/api/stripe/webhook/payment-intent-payment-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-failed-charge.tssimilarity index 77%rename from apps/web/app/(ee)/api/stripe/webhook/payment-intent-payment-failed.tsrename to apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-failed-charge.tsindex c87c19b19a..3bc77f63d2 100644--- a/apps/web/app/(ee)/api/stripe/webhook/payment-intent-payment-failed.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-failed-charge.ts@@ -12,14 +12,14 @@ const STRIPE_FRAUD_VALUE_LISTS = {CARD_FINGERPRINT: "rsl_1LeVdvAlJJEpqkPVvUZUm9eC",};-export async function paymentIntentPaymentFailed(- event: Stripe.PaymentIntentPaymentFailedEvent,+export async function detectAndHandleFraudulentFailedCharge(+ event: Stripe.ChargeFailedEvent,) {- const { customer, last_payment_error } = event.data.object;+ const { customer, outcome, payment_method_details } = event.data.object;// should never happen, but just in case- if (!customer || !last_payment_error) {- return `Invalid payment intent attributes: ${JSON.stringify({ customer, last_payment_error })}`;+ if (!customer || !outcome || !payment_method_details) {+ return `[detectAndHandleFraudulentFailedCharge]: Invalid charge attributes: ${JSON.stringify({ customer, outcome })}`;}const customerId = customer as string;@@ -31,20 +31,20 @@ export async function paymentIntentPaymentFailed(});if (workspace) {- return `Workspace with stripeId ${customer} already created, so this is most likely a recurring payment failure, skipping...`;+ return `[detectAndHandleFraudulentFailedCharge]: Workspace with stripeId ${customer} already created, so this is most likely a failed subscription renewal payment, skipping...`;}- const { decline_code, payment_method } = last_payment_error;+ const { risk_level } = outcome;- if (decline_code !== "fraudulent") {- return `Irrelevant decline code: ${decline_code}, skipping...`;+ if (risk_level !== "highest") {+ return `[detectAndHandleFraudulentFailedCharge]: Risk level "${risk_level}" is not highest, skipping...`;}const stripeCustomer = (await stripe.customers.retrieve(customerId,)) as Stripe.Customer;- const cardFingerprint = payment_method?.card?.fingerprint;+ const cardFingerprint = payment_method_details.card?.fingerprint;// add to Stripe Fraud Value Listsawait Promise.allSettled([@@ -105,7 +105,7 @@ export async function paymentIntentPaymentFailed(paidWorkspaces++;await log({type: "errors",- message: `[payment_intent.payment_failed]: Workspace ${workspace.slug} for fraudulent user ${user.email} is not a free plan, skipping...`,+ message: `[detectAndHandleFraudulentFailedCharge]: Workspace ${workspace.slug} for fraudulent user ${user.email} is not a free plan, skipping...`,});continue;}@@ -157,5 +157,5 @@ export async function paymentIntentPaymentFailed(}}- return `Processed payment_intent.payment_failed event for customer ${customerId} (${stripeCustomer.email}) and card fingerprint ${cardFingerprint})`;+ return `[detectAndHandleFraudulentFailedCharge]: Processed charge.failed event for customer ${customerId} (${stripeCustomer.email}) and card fingerprint ${cardFingerprint})`;}