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.

retry individual payout

Kiran K0f45f48701e0
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 6a3775b5c6..8eefdc1487 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
@@ -17,11 +17,15 @@ const PAYPAL_TO_DUB_STATUS = {
export async function payoutsItemFailed(event: any) {
const body = payoutsItemSchema.parse(event);
- const invoiceId = body.resource.sender_batch_id;
+ let 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;
+ if (invoiceId.includes("-")) {
+ invoiceId = invoiceId.split("-")[0];
+ }
+
const payout = await prisma.payout.findUnique({
where: {
id: payoutId,
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.ts
index db28c14820..dbdd5fb1ab 100644
--- a/apps/web/app/(ee)/api/paypal/webhook/payouts-item-succeeded.ts
+++ b/apps/web/app/(ee)/api/paypal/webhook/payouts-item-succeeded.ts
@@ -6,11 +6,15 @@ import { payoutsItemSchema } from "./utils";
export async function payoutsItemSucceeded(event: any) {
const body = payoutsItemSchema.parse(event);
- const invoiceId = body.resource.sender_batch_id;
+ let 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;
+ if (invoiceId.includes("-")) {
+ invoiceId = invoiceId.split("-")[0];
+ }
+
const payout = await prisma.payout.findUnique({
where: {
id: payoutId,
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/payouts/payout-stats-and-settings.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/payouts/payout-stats-and-settings.tsx
index c8a8749b55..52b9414c5b 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/payouts/payout-stats-and-settings.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/payouts/payout-stats-and-settings.tsx
@@ -1,5 +1,6 @@
"use client";
+import { retryFailedPaypalPayoutsAction } from "@/lib/actions/partners/retry-failed-paypal-payouts";
import usePartnerPayoutsCount from "@/lib/swr/use-partner-payouts-count";
import usePartnerProfile from "@/lib/swr/use-partner-profile";
import { PayoutsCount } from "@/lib/types";
@@ -9,6 +10,7 @@ import { AlertCircleFill } from "@/ui/shared/icons";
import { PayoutStatus } from "@dub/prisma/client";
import { Tooltip } from "@dub/ui";
import NumberFlow from "@number-flow/react";
+import { useAction } from "next-safe-action/hooks";
export function PayoutStatsAndSettings() {
const { partner } = usePartnerProfile();
@@ -16,6 +18,9 @@ export function PayoutStatsAndSettings() {
groupBy: "status",
});
+ const { executeAsync: retryFailedPaypalPayouts, isPending: isRetrying } =
+ useAction(retryFailedPaypalPayoutsAction);
+
return (
<div className="grid grid-cols-1 divide-neutral-200 rounded-lg border border-neutral-200 max-sm:divide-y sm:grid-cols-2 sm:divide-x">
<div className="flex flex-col gap-1.5 p-4">
@@ -26,6 +31,8 @@ export function PayoutStatsAndSettings() {
{partner && !partner.payoutsEnabledAt && (
<ConnectPayoutButton className="h-8 w-fit px-3" />
)}
+
+ <button onClick={() => retryFailedPaypalPayouts()}>Retry</button>
</div>
<div className="flex items-end justify-between gap-5">
<div className="mt-2 flex items-center gap-2">
diff --git a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
index 109e9f83b7..f0fae1f0f6 100644
--- a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
+++ b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
@@ -3,12 +3,20 @@
import { createPayPalBatchPayout } from "@/lib/paypal/create-batch-payout";
import { ratelimit } from "@/lib/upstash";
import { prisma } from "@dub/prisma";
+import { nanoid } from "@dub/utils";
+import { z } from "zod";
import { authPartnerActionClient } from "../safe-action";
-// Retry failed PayPal payouts for a partner
-export const retryFailedPaypalPayoutsAction = authPartnerActionClient.action(
- async ({ ctx }) => {
+const retryFailedPaypalPayoutSchema = z.object({
+ payoutId: z.string().min(1, "Payout ID is required"),
+});
+
+// Retry a failed PayPal payout for a partner
+export const retryFailedPaypalPayoutsAction = authPartnerActionClient
+ .schema(retryFailedPaypalPayoutSchema)
+ .action(async ({ ctx, parsedInput }) => {
const { partner } = ctx;
+ const { payoutId } = parsedInput;
if (!partner.payoutsEnabledAt) {
throw new Error(
@@ -30,14 +38,15 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient.action(
);
}
- const failedPayouts = await prisma.payout.findMany({
+ const payout = await prisma.payout.findUniqueOrThrow({
where: {
- partnerId: partner.id,
- status: "failed",
+ id: payoutId,
},
select: {
id: true,
invoiceId: true,
+ partnerId: true,
+ status: true,
amount: true,
program: {
select: {
@@ -47,63 +56,29 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient.action(
},
});
- if (failedPayouts.length === 0) {
- throw new Error("No failed payouts found.");
+ if (payout.partnerId !== partner.id) {
+ throw new Error("You are not authorized to retry this payout.");
}
- const payoutsByInvoiceId = Object.entries(
- failedPayouts.reduce<
- Record<
- string,
- {
- id: string;
- amount: number;
- program: { name: string };
- partner: { paypalEmail: string };
- }[]
- >
- >((acc, payout) => {
- if (payout.invoiceId) {
- acc[payout.invoiceId] = acc[payout.invoiceId] || [];
- acc[payout.invoiceId].push({
- id: payout.id,
- amount: payout.amount,
- program: payout.program,
- partner: { paypalEmail: partner.paypalEmail! },
- });
- }
-
- return acc;
- }, {}),
- ).map(([invoiceId, payouts]) => ({
- invoiceId,
- payouts,
- }));
-
- const results = await Promise.allSettled(
- payoutsByInvoiceId.map(({ invoiceId, payouts }) =>
- createPayPalBatchPayout({
- payouts,
- invoiceId,
- }),
- ),
- );
+ if (payout.status !== "failed") {
+ throw new Error("This payout cannot be retried.");
+ }
- await Promise.all(
- results.map((res, idx) => {
- const { invoiceId } = payoutsByInvoiceId[idx];
+ if (!payout.invoiceId) {
+ throw new Error("This payout has no invoice ID.");
+ }
- if (res.status === "fulfilled") {
- console.log(
- `[PayPal] Retry of payout invoice ${invoiceId} succeeded`,
- );
- } else {
- console.error(
- `[PayPal] Retry of payout invoice ${invoiceId} failed:`,
- res.status,
- );
- }
- }),
- );
- },
-);
+ await createPayPalBatchPayout({
+ invoiceId: `${payout.invoiceId}-${nanoid(10)}`,
+ payouts: [
+ {
+ id: payout.id,
+ amount: payout.amount,
+ program: payout.program,
+ partner: {
+ paypalEmail: partner.paypalEmail,
+ },
+ },
+ ],
+ });
+ });