The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
split methods
Kiran Kc921170684d6
diff --git a/apps/web/app/(ee)/api/paypal/webhook/payout-status-changed.ts b/apps/web/app/(ee)/api/paypal/webhook/payout-status-changed.tsdeleted file mode 100644index 05a54a4f11..0000000000--- a/apps/web/app/(ee)/api/paypal/webhook/payout-status-changed.ts+++ /dev/null@@ -1,157 +0,0 @@-import { sendEmail } from "@dub/email";-import PartnerPayoutSent from "@dub/email/templates/partner-payout-sent";-import PartnerPaypalPayoutFailed from "@dub/email/templates/partner-paypal-payout-failed";-import { prisma } from "@dub/prisma";-import { log } from "@dub/utils";-import { z } from "zod";--const schema = z.object({- event_type: z.string(),- resource: z.object({- sender_batch_id: z.string(), // Dub invoice id- payout_item_id: z.string(),- payout_item_fee: z.object({- currency: z.string(),- value: z.string(),- }),- payout_item: z.object({- receiver: z.string(),- sender_item_id: z.string(), // Dub payout id- }),- errors: z- .object({- name: z.string(),- message: z.string(),- })- .nullable()- .optional(),- }),-});--const PAYPAL_TO_DUB_STATUS = {- "PAYMENT.PAYOUTS-ITEM.BLOCKED": "failed",- "PAYMENT.PAYOUTS-ITEM.CANCELED": "canceled",- "PAYMENT.PAYOUTS-ITEM.DENIED": "failed",- "PAYMENT.PAYOUTS-ITEM.FAILED": "failed",- "PAYMENT.PAYOUTS-ITEM.HELD": "processing",- "PAYMENT.PAYOUTS-ITEM.REFUNDED": "failed",- "PAYMENT.PAYOUTS-ITEM.RETURNED": "failed",-};--export async function payoutStatusChanged(event: any) {- const body = schema.parse(event);-- const invoiceId = body.resource.sender_batch_id;- const paypalEmail = body.resource.payout_item.receiver;- const payoutItemId = body.resource.payout_item_id;- const payoutId = body.resource.payout_item.sender_item_id;-- const payout = await prisma.payout.findUnique({- where: {- id: payoutId,- },- include: {- partner: true,- program: true,- },- });-- if (!payout) {- console.log(- `[PayPal] Payout not found for invoice ${invoiceId} and partner ${paypalEmail}`,- );- return;- }-- if (body.event_type === "PAYMENT.PAYOUTS-ITEM.SUCCEEDED") {- if (payout.status === "completed") {- console.log(- `[PayPal] Payout already completed for invoice ${invoiceId} and partner ${paypalEmail}`,- );- return;- }-- await Promise.all([- prisma.payout.update({- where: {- id: payout.id,- },- data: {- paypalTransferId: payoutItemId,- status: "completed",- paidAt: new Date(),- },- }),-- prisma.commission.updateMany({- where: {- payoutId: payout.id,- },- data: {- status: "paid",- },- }),-- payout.partner.email &&- sendEmail({- subject: "You've been paid!",- email: payout.partner.email,- react: PartnerPayoutSent({- email: payout.partner.email,- program: payout.program,- payout: {- id: payout.id,- amount: payout.amount,- startDate: payout.periodStart,- endDate: payout.periodEnd,- },- }),- variant: "notifications",- }),- ]);-- return;- }-- // For all other status, we need to update the payout status- const failureReason = body.resource.errors?.message;-- await Promise.all([- // Update the payout status- prisma.payout.update({- where: {- id: payout.id,- },- data: {- paypalTransferId: payoutItemId,- status: PAYPAL_TO_DUB_STATUS[body.event_type],- failureReason,- },- }),-- // Send an email to the partner about the payout status change- payout.partner.email &&- sendEmail({- subject: `Your recent partner payout from ${payout.program.name} failed`,- email: payout.partner.email,- react: PartnerPaypalPayoutFailed({- email: payout.partner.email,- program: payout.program,- payout: {- amount: payout.amount,- failureReason,- },- partner: {- paypalEmail: payout.partner.paypalEmail!,- },- }),- variant: "notifications",- }),-- // Send an alert to Slack- log({- message: `Paypal payout status changed to ${body.event_type} for invoice ${invoiceId} and partner ${paypalEmail}`,- type: "errors",- }),- ]);-}diff --git a/apps/web/app/(ee)/api/paypal/webhook/payouts-item-failed.ts b/apps/web/app/(ee)/api/paypal/webhook/payouts-item-failed.tsnew file mode 100644index 0000000000..c9c39eba9a--- /dev/null+++ b/apps/web/app/(ee)/api/paypal/webhook/payouts-item-failed.ts@@ -0,0 +1,81 @@+import { sendEmail } from "@dub/email";+import PartnerPaypalPayoutFailed from "@dub/email/templates/partner-paypal-payout-failed";+import { prisma } from "@dub/prisma";+import { log } from "@dub/utils";+import { payoutsItemSchema } from "./utils";++const PAYPAL_TO_DUB_STATUS = {+ "PAYMENT.PAYOUTS-ITEM.BLOCKED": "failed",+ "PAYMENT.PAYOUTS-ITEM.CANCELED": "canceled",+ "PAYMENT.PAYOUTS-ITEM.DENIED": "failed",+ "PAYMENT.PAYOUTS-ITEM.FAILED": "failed",+ "PAYMENT.PAYOUTS-ITEM.HELD": "processing",+ "PAYMENT.PAYOUTS-ITEM.REFUNDED": "failed",+ "PAYMENT.PAYOUTS-ITEM.RETURNED": "failed",+};++export async function payoutsItemFailed(event: any) {+ const body = payoutsItemSchema.parse(event);++ const invoiceId = body.resource.sender_batch_id;+ const paypalEmail = body.resource.payout_item.receiver;+ const payoutItemId = body.resource.payout_item_id;+ const payoutId = body.resource.payout_item.sender_item_id;++ const payout = await prisma.payout.findUnique({+ where: {+ id: payoutId,+ },+ include: {+ partner: true,+ program: true,+ },+ });++ if (!payout) {+ console.log(+ `[PayPal] Payout not found for invoice ${invoiceId} and partner ${paypalEmail}`,+ );+ return;+ }++ const failureReason = body.resource.errors?.message;++ await Promise.all([+ prisma.payout.update({+ where: {+ id: payout.id,+ },+ data: {+ paypalTransferId: payoutItemId,+ status: PAYPAL_TO_DUB_STATUS[body.event_type],+ failureReason,+ },+ }),++ payout.partner.email &&+ sendEmail({+ subject: `Your recent partner payout from ${payout.program.name} failed`,+ email: payout.partner.email,+ react: PartnerPaypalPayoutFailed({+ email: payout.partner.email,+ program: {+ name: payout.program.name,+ },+ payout: {+ amount: payout.amount,+ failureReason,+ },+ partner: {+ paypalEmail: payout.partner.paypalEmail!,+ },+ }),+ variant: "notifications",+ }),++ log({+ message: `Paypal payout status changed to ${body.event_type} for invoice ${invoiceId} and partner ${paypalEmail}`,+ type: "errors",+ }),+ ]);+}diff --git a/apps/web/app/(ee)/api/paypal/webhook/payouts-item-succeeded.ts b/apps/web/app/(ee)/api/paypal/webhook/payouts-item-succeeded.tsnew file mode 100644index 0000000000..db28c14820--- /dev/null+++ b/apps/web/app/(ee)/api/paypal/webhook/payouts-item-succeeded.ts@@ -0,0 +1,76 @@+import { sendEmail } from "@dub/email";+import PartnerPayoutSent from "@dub/email/templates/partner-payout-sent";+import { prisma } from "@dub/prisma";+import { payoutsItemSchema } from "./utils";++export async function payoutsItemSucceeded(event: any) {+ const body = payoutsItemSchema.parse(event);++ const invoiceId = body.resource.sender_batch_id;+ const paypalEmail = body.resource.payout_item.receiver;+ const payoutItemId = body.resource.payout_item_id;+ const payoutId = body.resource.payout_item.sender_item_id;++ const payout = await prisma.payout.findUnique({+ where: {+ id: payoutId,+ },+ include: {+ partner: true,+ program: true,+ },+ });++ if (!payout) {+ console.log(+ `[PayPal] Payout not found for invoice ${invoiceId} and partner ${paypalEmail}`,+ );+ return;+ }++ if (payout.status === "completed") {+ console.log(+ `[PayPal] Payout already completed for invoice ${invoiceId} and partner ${paypalEmail}`,+ );+ return;+ }++ await Promise.all([+ prisma.payout.update({+ where: {+ id: payout.id,+ },+ data: {+ paypalTransferId: payoutItemId,+ status: "completed",+ paidAt: new Date(),+ },+ }),++ prisma.commission.updateMany({+ where: {+ payoutId: payout.id,+ },+ data: {+ status: "paid",+ },+ }),++ payout.partner.email &&+ sendEmail({+ subject: "You've been paid!",+ email: payout.partner.email,+ react: PartnerPayoutSent({+ email: payout.partner.email,+ program: payout.program,+ payout: {+ id: payout.id,+ amount: payout.amount,+ startDate: payout.periodStart,+ endDate: payout.periodEnd,+ },+ }),+ variant: "notifications",+ }),+ ]);+}diff --git a/apps/web/app/(ee)/api/paypal/webhook/route.ts b/apps/web/app/(ee)/api/paypal/webhook/route.tsindex caeb5fb94e..3d7a0a25d7 100644--- a/apps/web/app/(ee)/api/paypal/webhook/route.ts+++ b/apps/web/app/(ee)/api/paypal/webhook/route.ts@@ -1,5 +1,6 @@import { log } from "@dub/utils";-import { payoutStatusChanged } from "./payout-status-changed";+import { payoutsItemFailed } from "./payouts-item-failed";+import { payoutsItemSucceeded } from "./payouts-item-succeeded";import { verifySignature } from "./verify-signature";const relevantEvents = new Set([@@ -41,6 +42,8 @@ export const POST = async (req: Request) => {switch (body.event_type) {case "PAYMENT.PAYOUTS-ITEM.SUCCEEDED":+ await payoutsItemSucceeded(body);+ break;case "PAYMENT.PAYOUTS-ITEM.BLOCKED":case "PAYMENT.PAYOUTS-ITEM.CANCELED":case "PAYMENT.PAYOUTS-ITEM.DENIED":@@ -49,7 +52,7 @@ export const POST = async (req: Request) => {case "PAYMENT.PAYOUTS-ITEM.REFUNDED":case "PAYMENT.PAYOUTS-ITEM.RETURNED":case "PAYMENT.PAYOUTS-ITEM.UNCLAIMED":- await payoutStatusChanged(body);+ await payoutsItemFailed(body);break;}} catch (error) {diff --git a/apps/web/app/(ee)/api/paypal/webhook/utils.ts b/apps/web/app/(ee)/api/paypal/webhook/utils.tsnew file mode 100644index 0000000000..a47d118237--- /dev/null+++ b/apps/web/app/(ee)/api/paypal/webhook/utils.ts@@ -0,0 +1,23 @@+import { z } from "zod";++export const payoutsItemSchema = z.object({+ event_type: z.string(),+ resource: z.object({+ sender_batch_id: z.string(), // Dub invoice id+ payout_item_id: z.string(),+ payout_item_fee: z.object({+ currency: z.string(),+ value: z.string(),+ }),+ payout_item: z.object({+ receiver: z.string(),+ sender_item_id: z.string(), // Dub payout id+ }),+ errors: z+ .object({+ name: z.string(),+ message: z.string(),+ })+ .nullish(),+ }),+});