The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Enhance Stripe webhook handling: update balanceAvailable to associate payouts with transfers and modify payoutPaid to mark payouts as completed. Add stripePayoutId field to Payout model in Prisma schema.
Kiran Kbde5b6ba4cd9
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.tsindex 07f10f83cc..b09052ecea 100644--- a/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts@@ -114,14 +114,15 @@ export async function balanceAvailable(event: Stripe.Event) {let withdrawalFee = 0;+ const transfers = await stripe.transfers.list({+ destination: stripeAccount,+ limit: 100,+ });+// Decide if we need to charge a withdrawal fee for the partnerif (partner.minWithdrawalAmount < MIN_WITHDRAWAL_AMOUNT_CENTS) {withdrawalFee = BELOW_MIN_WITHDRAWAL_FEE_CENTS;- const transfers = await stripe.transfers.list({- destination: stripeAccount,- });-if (transfers.data.length === 0) {console.error(`No transfers found for partner ${partner.email} (${stripeAccount}). Skipping...`,@@ -191,6 +192,20 @@ export async function balanceAvailable(event: Stripe.Event) {},);+ // Find the Dub payouts associated with this Stripe payout+ await prisma.payout.updateMany({+ where: {+ status: "sent",+ stripePayoutId: null,+ stripeTransferId: {+ in: transfers.data.map(({ id }) => id),+ },+ },+ data: {+ stripePayoutId: payout.id,+ },+ });+console.log(`Stripe payout created for partner ${partner.email} (${stripeAccount}): ${payout.id} (${currencyFormatter(payout.amount / 100, { maximumFractionDigits: 2, currency })})`,);diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.tsindex b7eb7864e9..8d60ac370c 100644--- a/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts@@ -4,8 +4,6 @@ import Stripe from "stripe";export async function payoutPaid(event: Stripe.Event) {const stripeAccount = event.account;- console.log("payoutPaid", event);-if (!stripeAccount) {console.error("Stripe account not found. Skipping...");return;@@ -24,6 +22,15 @@ export async function payoutPaid(event: Stripe.Event) {return;}- // TODO:- // Update the associated payouts on Dub to completed+ const stripePayout = event.data.object as Stripe.Payout;++ await prisma.payout.updateMany({+ where: {+ status: "sent",+ stripePayoutId: stripePayout.id,+ },+ data: {+ status: "completed",+ },+ });}diff --git a/packages/prisma/schema/payout.prisma b/packages/prisma/schema/payout.prismaindex f93e24904a..b603c606a6 100644--- a/packages/prisma/schema/payout.prisma+++ b/packages/prisma/schema/payout.prisma@@ -21,6 +21,7 @@ model Payout {periodEnd DateTime?paypalTransferId String? @uniquestripeTransferId String? @unique+ stripePayoutId String?failureReason String? @db.TextcreatedAt DateTime @default(now())updatedAt DateTime @updatedAt