Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { prisma } from "@dub/prisma";
import { log } from "@dub/utils";
import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
import { z } from "zod";
import { logAndRespond } from "../../utils";
import { sendPaypalPayouts } from "./send-paypal-payouts";
import { sendStripePayouts } from "./send-stripe-payouts";

Expand All @@ -17,9 +19,9 @@ const stripeChargeMetadataSchema = z.object({
});

// POST /api/cron/payouts/charge-succeeded
// This route is used to process the charge-succeeded event from Stripe
// we're intentionally offloading this to a cron job to avoid blocking the main thread
// so that we can return a 200 to Stripe immediately
// This route is used to process the charge-succeeded event from Stripe.
// We're intentionally offloading this to a cron job so we can return a 200 to Stripe immediately.
// We'll also be calling this route recursively to process payouts in batches of 100.
export async function POST(req: Request) {
try {
const rawBody = await req.text();
Expand All @@ -36,9 +38,7 @@ export async function POST(req: Request) {
select: {
payouts: {
where: {
status: {
not: "completed",
},
status: "processing",
},
},
},
Expand All @@ -47,14 +47,12 @@ export async function POST(req: Request) {
});

if (!invoice) {
console.log(`Invoice with id ${invoiceId} not found.`);
return new Response(`Invoice with id ${invoiceId} not found.`);
return logAndRespond(`Invoice ${invoiceId} not found.`);
}

if (invoice._count.payouts === 0) {
console.log("No payouts found with status not completed, skipping...");
return new Response(
`No payouts found with status not completed for invoice ${invoiceId}`,
return logAndRespond(
`No payouts found with status 'processing' for invoice ${invoiceId}, skipping...`,
);
}

Expand Down Expand Up @@ -88,7 +86,36 @@ export async function POST(req: Request) {
}),
]);

return new Response(`Invoice ${invoiceId} processed.`);
if (invoice._count.payouts > 100) {
console.log(
"More than 100 payouts found for invoice, scheduling next batch...",
);
const qstashResponse = await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/payouts/charge-succeeded`,
body: {
invoiceId: invoiceId,
},
});
if (qstashResponse.messageId) {
console.log(
`Message sent to Qstash with id ${qstashResponse.messageId}`,
);
} else {
// should never happen but just in case
await log({
message: `Error sending message to Qstash to schedule next batch of payouts for invoice ${invoiceId}: ${JSON.stringify(qstashResponse)}`,
type: "errors",
});
}

return logAndRespond(
`Completed processing current batch of payouts for invoice ${invoiceId}. Next batch scheduled.`,
);
}

return logAndRespond(
`Completed processing all payouts for invoice ${invoiceId}.`,
);
} catch (error) {
await log({
message: `Error sending payouts for invoice: ${error.message}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export async function sendPaypalPayouts({ invoiceId }: { invoiceId: string }) {
const payouts = await prisma.payout.findMany({
where: {
invoiceId,
status: {
not: "completed",
},
status: "processing",
partner: {
payoutsEnabledAt: {
not: null,
Expand All @@ -33,6 +31,7 @@ export async function sendPaypalPayouts({ invoiceId }: { invoiceId: string }) {
},
},
},
take: 100,
});

if (payouts.length === 0) {
Expand All @@ -47,6 +46,18 @@ export async function sendPaypalPayouts({ invoiceId }: { invoiceId: string }) {

console.log("PayPal batch payout created", batchPayout);

// update the payouts to "sent" status
const updatedPayouts = await prisma.payout.updateMany({
where: {
id: { in: payouts.map((p) => p.id) },
},
data: {
status: "sent",
paidAt: new Date(),
},
});
console.log(`Updated ${updatedPayouts.count} payouts to "sent" status`);

const batchEmails = await sendBatchEmail(
payouts
.filter((payout) => payout.partner.email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function sendStripePayouts({
},
},
include: commonInclude,
take: 100,
});

if (currentInvoicePayouts.length === 0) {
Expand Down
8 changes: 4 additions & 4 deletions apps/web/app/(ee)/api/paypal/webhook/payouts-item-failed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const PAYPAL_TO_DUB_STATUS = {
"PAYMENT.PAYOUTS-ITEM.CANCELED": "canceled",
"PAYMENT.PAYOUTS-ITEM.DENIED": "failed",
"PAYMENT.PAYOUTS-ITEM.FAILED": "failed",
"PAYMENT.PAYOUTS-ITEM.HELD": "processing",
"PAYMENT.PAYOUTS-ITEM.HELD": "processed",
"PAYMENT.PAYOUTS-ITEM.REFUNDED": "failed",
"PAYMENT.PAYOUTS-ITEM.RETURNED": "failed",
"PAYMENT.PAYOUTS-ITEM.UNCLAIMED": "processing",
"PAYMENT.PAYOUTS-ITEM.UNCLAIMED": "processed",
};

export async function payoutsItemFailed(event: any) {
Expand Down Expand Up @@ -58,9 +58,9 @@ export async function payoutsItemFailed(event: any) {
},
});

if (payoutStatus === "processing") {
if (payoutStatus === "processed") {
await log({
message: `Paypal payout is stuck in processing for invoice ${invoiceId} and partner ${paypalEmail}. PayPal webhook status: ${body.event_type}.${
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function payoutsItemSucceeded(event: any) {
data: {
paypalTransferId: payoutItemId,
status: "completed",
paidAt: new Date(),
paidAt: payout.paidAt ?? new Date(), // preserve the paidAt if it already exists
},
}),

Expand Down