The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Charge failure handling
Kiran Kb9efb8ffe519
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.tsindex 31363f5eaa..71aa6f2a52 100644--- a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts@@ -1,10 +1,26 @@+import { PAYOUT_FAILURE_FEE_CENTS } from "@/lib/partners/constants";+import { stripe } from "@/lib/stripe";+import { sendEmail } from "@dub/email";+import PartnerPayoutFailed from "@dub/email/templates/partner-payout-failed";import { prisma } from "@dub/prisma";+import { log } from "@dub/utils";+import { waitUntil } from "@vercel/functions";import Stripe from "stripe";export async function chargeFailed(event: Stripe.Event) {const charge = event.data.object as Stripe.Charge;- const { transfer_group: invoiceId, failure_message: failedReason } = charge;+ const {+ amount,+ transfer_group: invoiceId,+ failure_message: failedReason,+ } = charge;++ log({+ message: `Partner payout failed for invoice ${invoiceId}.`,+ type: "errors",+ mention: true,+ });if (!invoiceId) {console.log("No transfer group found, skipping...");@@ -12,7 +28,7 @@ export async function chargeFailed(event: Stripe.Event) {}// Mark the invoice as failed- await prisma.invoice.update({+ const invoice = await prisma.invoice.update({where: {id: invoiceId,},@@ -34,6 +50,91 @@ export async function chargeFailed(event: Stripe.Event) {},});- // TODO:- // Send an email to the program owner about the failure+ const workspace = await prisma.project.findUniqueOrThrow({+ where: {+ id: invoice.workspaceId,+ },+ include: {+ users: {+ select: {+ user: {+ select: {+ email: true,+ },+ },+ },+ },+ programs: {+ select: {+ name: true,+ },+ },+ },+ });++ if (!workspace.stripeId) {+ console.log("Workspace does not have a Stripe ID, skipping...");+ return;+ }++ const paymentMethods = await stripe.paymentMethods.list({+ customer: workspace.stripeId,+ type: "card",+ });++ if (paymentMethods.data.length === 0) {+ console.log("No valid payment methods found for workspace, skipping...");+ return;+ }++ const paymentMethod = paymentMethods.data[0];++ // Charge the failure fee to the workspace+ await stripe.paymentIntents.create({+ amount: PAYOUT_FAILURE_FEE_CENTS,+ customer: workspace.stripeId,+ payment_method_types: ["card"],+ payment_method: paymentMethod.id,+ currency: "usd",+ confirmation_method: "automatic",+ confirm: true,+ statement_descriptor: "Dub Partners",+ description: `Dub Partners payout failure fee for invoice ${invoice.id}`,+ });++ waitUntil(+ (async () => {+ // Send email to the workspace users about the failed payout+ const emailData = workspace.users.map((user) => ({+ email: user.user.email!,+ workspace: {+ slug: workspace.slug,+ },+ program: {+ name: workspace.programs[0].name,+ },+ payout: {+ amount,+ failureFee: PAYOUT_FAILURE_FEE_CENTS,+ cardLast4: paymentMethod.card?.last4 || "",+ },+ }));++ if (emailData.length === 0) {+ console.log("No users found to send email, skipping...");+ return;+ }++ await Promise.all(+ emailData.map((data) => {+ sendEmail({+ subject: "Partner payout failed",+ email: data.email,+ react: PartnerPayoutFailed(data),+ variant: "notifications",+ });+ }),+ );+ })(),+ );}diff --git a/apps/web/lib/partners/constants.ts b/apps/web/lib/partners/constants.tsindex 36aea2c043..81c6671198 100644--- a/apps/web/lib/partners/constants.ts+++ b/apps/web/lib/partners/constants.ts@@ -17,4 +17,6 @@ export const PAYOUT_FEES = {},};-export const DUB_MIN_PAYOUT_AMOUNT_CENTS = 10000;+export const DUB_MIN_PAYOUT_AMOUNT_CENTS = 10000; // 100 USD++export const PAYOUT_FAILURE_FEE_CENTS = 1000; // 10 USDdiff --git a/packages/email/src/templates/partner-payout-failed.tsx b/packages/email/src/templates/partner-payout-failed.tsxindex 7a8fa807f1..c8bd5d1aa7 100644--- a/packages/email/src/templates/partner-payout-failed.tsx+++ b/packages/email/src/templates/partner-payout-failed.tsx@@ -22,8 +22,8 @@ export default function PartnerPayoutFailed({name: "Acme",},payout = {- amount: 224.12,- failureFee: 10,+ amount: 530000,+ failureFee: 1000,cardLast4: "01234",},email = "[email protected]",@@ -35,13 +35,13 @@ export default function PartnerPayoutFailed({name: string;};payout: {- amount: number;- failureFee: number;+ amount: number; // in cents+ failureFee: number; // in centscardLast4: string;};email: string;}) {- const payoutAmount = currencyFormatter(payout.amount, {+ const payoutAmount = currencyFormatter(payout.amount / 100, {minimumFractionDigits: 2,maximumFractionDigits: 2,});@@ -78,7 +78,7 @@ export default function PartnerPayoutFailed({<Text className="text-sm leading-6 text-neutral-600">We've charged a{" "}<span className="font-semibold text-neutral-800">- ${payout.failureFee} payment failure fee+ ${payout.failureFee / 100} payment failure fee</span>{" "}to your card ending in {payout.cardLast4}.</Text>