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.

separation of concerns between paypal and stripe

Steven Tey906997490723
diff --git a/apps/web/app/api/cron/payouts/charge-succeeded/route.ts b/apps/web/app/api/cron/payouts/charge-succeeded/route.ts
index 2562bb82ce..8f36d44bb3 100644
--- a/apps/web/app/api/cron/payouts/charge-succeeded/route.ts
+++ b/apps/web/app/api/cron/payouts/charge-succeeded/route.ts
@@ -22,10 +22,14 @@ export async function POST(req: Request) {
id: invoiceId,
},
include: {
- payouts: {
- where: {
- status: {
- not: "completed",
+ _count: {
+ select: {
+ payouts: {
+ where: {
+ status: {
+ not: "completed",
+ },
+ },
},
},
},
@@ -34,17 +38,19 @@ export async function POST(req: Request) {
if (!invoice) {
console.log(`Invoice with id ${invoiceId} not found.`);
- return;
+ return new Response(`Invoice with id ${invoiceId} not found.`);
}
if (invoice.status === "completed") {
console.log("Invoice already completed, skipping...");
- return;
+ return new Response(`Invoice with id ${invoiceId} already completed.`);
}
- if (invoice.payouts.length === 0) {
+ if (invoice._count.payouts === 0) {
console.log("No payouts found with status not completed, skipping...");
- return;
+ return new Response(
+ `No payouts found with status not completed for invoice ${invoiceId}`,
+ );
}
const payouts = await prisma.payout.findMany({
@@ -73,7 +79,7 @@ export async function POST(req: Request) {
(payout) => payout.partner.paypalEmail,
);
- await Promise.all([
+ await Promise.allSettled([
sendStripePayouts({ ...body, payouts: stripePayouts }),
sendPaypalPayouts({ ...body, payouts: paypalPayouts }),
]);
diff --git a/apps/web/app/api/cron/payouts/charge-succeeded/send-paypal-payouts.ts b/apps/web/app/api/cron/payouts/charge-succeeded/send-paypal-payouts.ts
index ec918217ef..99b67f1911 100644
--- a/apps/web/app/api/cron/payouts/charge-succeeded/send-paypal-payouts.ts
+++ b/apps/web/app/api/cron/payouts/charge-succeeded/send-paypal-payouts.ts
@@ -1,9 +1,8 @@
import { paypalEnv } from "@/lib/paypal/env";
+import { prisma } from "@dub/prisma";
import { createPaypalToken } from "./create-paypal-token";
import { Payload, Payouts } from "./utils";
-export const dynamic = "force-dynamic";
-
export async function sendPaypalPayouts({
invoiceId,
payouts,
@@ -47,6 +46,18 @@ export async function sendPaypalPayouts({
if (!response.ok) {
console.error("Error creating PayPal batch payout", data);
+ console.log("Resetting payout status to pending");
+ await prisma.payout.updateMany({
+ where: {
+ id: {
+ in: payouts.map((payout) => payout.id),
+ },
+ },
+ data: {
+ status: "pending",
+ invoiceId: null,
+ },
+ });
throw new Error("Error creating PayPal batch payout");
}
diff --git a/apps/web/app/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts b/apps/web/app/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
index 01f438bd33..8539871182 100644
--- a/apps/web/app/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
+++ b/apps/web/app/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
@@ -4,8 +4,6 @@ import PartnerPayoutSent from "@dub/email/templates/partner-payout-sent";
import { prisma } from "@dub/prisma";
import { Payload, Payouts } from "./utils";
-export const dynamic = "force-dynamic";
-
export async function sendStripePayouts({
invoiceId,
chargeId,