The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import { disableWorkspaceLinks } from "@/lib/api/workspaces/disable-workspace-links";
import { updateConfig } from "@/lib/edge-config";
import { prisma } from "@/lib/prisma";
import { stripe } from "@/lib/stripe";
import { LEGAL_USER_ID, log } from "@dub/utils";
import Stripe from "stripe";
import { addToStripeFraudValueLists } from "./add-to-stripe-fraud-value-lists";
export async function detectAndHandleFraudulentFailedCharge(
event: Stripe.ChargeFailedEvent,
) {
const { customer, outcome, payment_method_details } = event.data.object;
// should never happen, but just in case
if (!customer || !outcome || !payment_method_details) {
return `[detectAndHandleFraudulentFailedCharge]: Invalid charge attributes: ${JSON.stringify({ customer, outcome })}`;
}
const customerId = customer as string;
const workspace = await prisma.project.findUnique({
where: {
stripeId: customerId,
},
});
if (workspace) {
return `[detectAndHandleFraudulentFailedCharge]: 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...`;
}
const stripeCustomer = (await stripe.customers.retrieve(
customerId,
)) as Stripe.Customer;
const cardFingerprint = payment_method_details.card?.fingerprint;
await addToStripeFraudValueLists({
customerId: customerId,
customerEmail: stripeCustomer.email,
cardFingerprint,
});
if (stripeCustomer.email) {
const user = await prisma.user.findUnique({
where: {
email: stripeCustomer.email,
},
include: {
projects: {
where: {
role: "owner",
},
select: {
project: true,
},
},
},
});
if (user) {
const workspaces = user.projects.map(({ project }) => project);
let paidWorkspaces = 0;
if (workspaces.length > 0) {
for (const workspace of workspaces) {
// this should never happen, but just in case
if (workspace.plan !== "free") {
paidWorkspaces++;
await log({
type: "errors",
message: `[detectAndHandleFraudulentFailedCharge]: Workspace ${workspace.slug} for fraudulent user ${user.email} is not a free plan, skipping...`,
});
continue;
}
const linksCount = await prisma.link.count({
where: {
projectId: workspace.id,
},
});
if (linksCount > 0) {
// transfer ownership to legal user
await prisma.projectUsers.update({
where: {
userId_projectId: {
projectId: workspace.id,
userId: user.id,
},
},
data: {
userId: LEGAL_USER_ID,
},
});
console.log(
`[detectAndHandleFraudulentFailedCharge]: Transferred ownership of workspace ${workspace.slug} to legal user ${LEGAL_USER_ID}`,
);
// disable workspace links
await disableWorkspaceLinks(workspace.id);
await prisma.project.update({
where: {
id: workspace.id,
},
data: {
disabledAt: new Date(),
},
});
} else {
// if workspace has no links, just delete it
await prisma.project.delete({
where: {
id: workspace.id,
},
});
console.log(
`[detectAndHandleFraudulentFailedCharge]: Deleted workspace ${workspace.slug} because it has no links`,
);
}
}
} else {
console.log(
`[detectAndHandleFraudulentFailedCharge]: User ${user.email} has no workspaces, skipping...`,
);
}
// delete and ban user if there are no paid workspaces
if (paidWorkspaces === 0) {
await Promise.allSettled([
prisma.user.delete({
where: {
id: user.id,
},
}),
updateConfig({
key: "emails",
value: user.email!,
}),
]);
}
await log({
message: `Banned user ${user.email} (workspace: ${workspaces.map(({ slug }) => slug).join(", ")}) for fraudulent Stripe charges: https://dashboard.stripe.com/customers/${customerId}`,
type: "alerts",
mention: true,
});
} else {
console.log(
`[detectAndHandleFraudulentFailedCharge]: User with email ${stripeCustomer.email} not found, skipping...`,
);
}
}
return `[detectAndHandleFraudulentFailedCharge]: Processed charge.failed event for customer ${customerId} (${stripeCustomer.email}) and card fingerprint "${cardFingerprint}".`;
}