Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor payout status handling in PayPal webhook (#3173)

Kiran K324c2c8b9bc0
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.ts
index dd6a39b186..95b224af4f 100644
--- a/apps/web/app/(ee)/api/paypal/webhook/payouts-item-failed.ts
+++ b/apps/web/app/(ee)/api/paypal/webhook/payouts-item-failed.ts
@@ -1,18 +1,14 @@
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": "processed",
"PAYMENT.PAYOUTS-ITEM.REFUNDED": "failed",
"PAYMENT.PAYOUTS-ITEM.RETURNED": "failed",
- "PAYMENT.PAYOUTS-ITEM.UNCLAIMED": "processed",
};
export async function payoutsItemFailed(event: any) {
@@ -44,7 +40,8 @@ export async function payoutsItemFailed(event: any) {
return;
}
- const payoutStatus = PAYPAL_TO_DUB_STATUS[body.event_type];
+ const payoutStatus: "failed" | undefined =
+ PAYPAL_TO_DUB_STATUS[body.event_type];
const failureReason = body.resource.errors?.message;
await prisma.payout.update({
@@ -53,46 +50,42 @@ export async function payoutsItemFailed(event: any) {
},
data: {
paypalTransferId: payoutItemId,
- status: payoutStatus,
+ status: "failed",
failureReason,
},
});
- if (payoutStatus === "processed") {
- await log({
- message: `Paypal payout is stuck in processed for invoice ${invoiceId} and partner ${paypalEmail}. PayPal webhook status: ${body.event_type}.${
- failureReason ? ` Failure reason: ${failureReason}` : ""
- }`,
- type: "errors",
- });
- return; // we only send emails for failed payouts
+ if (payoutStatus !== "failed") {
+ // we only send emails for failed payouts
+ console.log(
+ `Paypal payout status changed to ${body.event_type} for invoice ${invoiceId} and partner ${paypalEmail}. This is not a failure event, skipping email send...`,
+ );
+ return;
}
- await Promise.all([
- payout.partner.email
- ? sendEmail({
- subject: `Your recent partner payout from ${payout.program.name} failed`,
- to: 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",
- })
- : Promise.resolve(),
+ if (!payout.partner.email) {
+ console.log(
+ `Paypal payout partner email not found for invoice ${invoiceId} and partner ${paypalEmail}. Skipping email send...`,
+ );
+ return;
+ }
- log({
- message: `Paypal payout status changed to ${body.event_type} for invoice ${invoiceId} and partner ${paypalEmail}`,
- type: "errors",
+ await sendEmail({
+ subject: `Your recent partner payout from ${payout.program.name} failed`,
+ to: 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",
+ });
}