The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Handle `charge.dispute.created` webhook
Steven Teydedb2e822d65
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.tsindex e874133b9b..25824756b5 100644--- a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts@@ -1,6 +1,6 @@import { prisma } from "@/lib/prisma";import Stripe from "stripe";-import { detectAndHandleFraudulentFailedCharge } from "./utils/detect-and-handle-fraudulent-failed-charge";+import { detectAndHandleFraudulentCharge } from "./utils/detect-and-handle-fraudulent-charge";import { processDomainRenewalFailure } from "./utils/process-domain-renewal-failure";import { processPayoutInvoiceFailure } from "./utils/process-payout-invoice-failure";@@ -10,7 +10,7 @@ export async function chargeFailed(event: Stripe.ChargeFailedEvent) {const { transfer_group: invoiceId, failure_message: failedReason } = charge;if (!invoiceId) {- return detectAndHandleFraudulentFailedCharge(event);+ return detectAndHandleFraudulentCharge(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 17c5e8d69d..414b69ecbb 100644--- a/apps/web/app/(ee)/api/stripe/webhook/route.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/route.ts@@ -12,11 +12,13 @@ import { customerSubscriptionUpdated } from "./customer-subscription-updated";import { invoicePaymentFailed } from "./invoice-payment-failed";import { paymentIntentRequiresAction } from "./payment-intent-requires-action";import { transferReversed } from "./transfer-reversed";+import { detectAndHandleFraudulentCharge } from "./utils/detect-and-handle-fraudulent-charge";const relevantEvents = new Set(["charge.succeeded","charge.failed","charge.refunded",+ "charge.dispute.created","checkout.session.completed","customer.subscription.created","customer.subscription.updated",@@ -61,6 +63,9 @@ export const POST = async (req: Request) => {case "charge.refunded":response = await chargeRefunded(event);break;+ case "charge.dispute.created":+ response = await detectAndHandleFraudulentCharge(event);+ break;case "checkout.session.completed":response = await checkoutSessionCompleted(event);break;diff --git a/apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-failed-charge.ts b/apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-charge.tssimilarity index 72%rename from apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-failed-charge.tsrename to apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-charge.tsindex 91be086770..e13d683f64 100644--- a/apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-failed-charge.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/utils/detect-and-handle-fraudulent-charge.ts@@ -11,14 +11,25 @@ const STRIPE_FRAUD_VALUE_LISTS = {CARD_FINGERPRINT: "rsl_1LeVdvAlJJEpqkPVvUZUm9eC",};-export async function detectAndHandleFraudulentFailedCharge(- event: Stripe.ChargeFailedEvent,+export async function detectAndHandleFraudulentCharge(+ event: Stripe.ChargeFailedEvent | Stripe.ChargeDisputeCreatedEvent,) {- const { customer, outcome, payment_method_details } = event.data.object;+ let charge: Stripe.Charge;++ if (event.type === "charge.dispute.created") {+ const dispute = event.data.object;+ const chargeId =+ typeof dispute.charge === "string" ? dispute.charge : dispute.charge.id;+ charge = await stripe.charges.retrieve(chargeId);+ } else {+ charge = event.data.object;+ }++ const { customer, outcome, payment_method_details } = charge;// should never happen, but just in caseif (!customer || !outcome || !payment_method_details) {- return `[detectAndHandleFraudulentFailedCharge]: Invalid charge attributes: ${JSON.stringify({ customer, outcome })}`;+ return `[detectAndHandleFraudulentCharge]: Invalid charge attributes: ${JSON.stringify({ customer, outcome })}`;}const customerId = customer as string;@@ -30,13 +41,13 @@ export async function detectAndHandleFraudulentFailedCharge(});if (workspace) {- return `[detectAndHandleFraudulentFailedCharge]: Workspace with stripeId ${customer} already created, so this is most likely a failed subscription renewal payment, skipping...`;+ return `[detectAndHandleFraudulentCharge]: Workspace with stripeId ${customer} already created, so this is most likely a failed subscription renewal payment, skipping...`;}const { risk_level } = outcome;if (risk_level !== "highest") {- return `[detectAndHandleFraudulentFailedCharge]: Risk level "${risk_level}" is not highest, skipping...`;+ return `[detectAndHandleFraudulentCharge]: Risk level "${risk_level}" is not highest, skipping...`;}const stripeCustomer = (await stripe.customers.retrieve(@@ -104,7 +115,7 @@ export async function detectAndHandleFraudulentFailedCharge(paidWorkspaces++;await log({type: "errors",- message: `[detectAndHandleFraudulentFailedCharge]: Workspace ${workspace.slug} for fraudulent user ${user.email} is not a free plan, skipping...`,+ message: `[detectAndHandleFraudulentCharge]: Workspace ${workspace.slug} for fraudulent user ${user.email} is not a free plan, skipping...`,});continue;}@@ -127,7 +138,7 @@ export async function detectAndHandleFraudulentFailedCharge(},});console.log(- `[detectAndHandleFraudulentFailedCharge]: Transferred ownership of workspace ${workspace.slug} to legal user ${LEGAL_USER_ID}`,+ `[detectAndHandleFraudulentCharge]: Transferred ownership of workspace ${workspace.slug} to legal user ${LEGAL_USER_ID}`,);// disable workspace linksawait disableWorkspaceLinks(workspace.id);@@ -139,13 +150,13 @@ export async function detectAndHandleFraudulentFailedCharge(},});console.log(- `[detectAndHandleFraudulentFailedCharge]: Deleted workspace ${workspace.slug} because it has no links`,+ `[detectAndHandleFraudulentCharge]: Deleted workspace ${workspace.slug} because it has no links`,);}}} else {console.log(- `[detectAndHandleFraudulentFailedCharge]: User ${user.email} has no workspaces, skipping...`,+ `[detectAndHandleFraudulentCharge]: User ${user.email} has no workspaces, skipping...`,);}@@ -171,10 +182,10 @@ export async function detectAndHandleFraudulentFailedCharge(});} else {console.log(- `[detectAndHandleFraudulentFailedCharge]: User with email ${stripeCustomer.email} not found, skipping...`,+ `[detectAndHandleFraudulentCharge]: User with email ${stripeCustomer.email} not found, skipping...`,);}}- return `[detectAndHandleFraudulentFailedCharge]: Processed charge.failed event for customer ${customerId} (${stripeCustomer.email}) and card fingerprint "${cardFingerprint}".`;+ return `[detectAndHandleFraudulentCharge]: Processed ${event.type} event for customer ${customerId} (${stripeCustomer.email}) and card fingerprint "${cardFingerprint}".`;}