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.

Cancel existing PayPal payout after retrying

Steven Tey9c5fb217e8c2
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 199d016037..139fcb85bc 100644
--- a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
+++ b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
@@ -1,5 +1,6 @@
"use server";
+import { cancelPaypalPayout } from "@/lib/paypal/cancel-paypal-payout";
import { createPayPalBatchPayout } from "@/lib/paypal/create-batch-payout";
import { ratelimit } from "@/lib/upstash";
import { prisma } from "@dub/prisma";
@@ -48,6 +49,7 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient
partnerId: true,
status: true,
amount: true,
+ paypalTransferId: true,
program: {
select: {
name: true,
@@ -68,6 +70,10 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient
throw new Error("This payout has no invoice ID.");
}
+ if (!payout.paypalTransferId) {
+ throw new Error("This payout has no existing PayPal transfer ID.");
+ }
+
try {
await createPayPalBatchPayout({
invoiceId: `${payout.invoiceId}-${nanoid(10)}`,
@@ -82,6 +88,8 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient
},
],
});
+
+ await cancelPaypalPayout(payout.paypalTransferId);
} catch (error) {
throw new Error(
"Failed to retry payout. Please try again or contact support.",
diff --git a/apps/web/lib/paypal/cancel-paypal-payout.ts b/apps/web/lib/paypal/cancel-paypal-payout.ts
new file mode 100644
index 0000000000..bd55438dd9
--- /dev/null
+++ b/apps/web/lib/paypal/cancel-paypal-payout.ts
@@ -0,0 +1,18 @@
+import { createPaypalToken } from "@/lib/paypal/create-paypal-token";
+import { paypalEnv } from "@/lib/paypal/env";
+
+export const cancelPaypalPayout = async (paypalTransferId: string) => {
+ const paypalAccessToken = await createPaypalToken();
+
+ const response = await fetch(
+ `${paypalEnv.PAYPAL_API_HOST}/v1/payments/payouts-item/${paypalTransferId}/cancel`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${paypalAccessToken}`,
+ },
+ },
+ ).then((r) => r.json());
+
+ return response;
+};