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.

Add PayPal token creation and caching for payouts

Kiran K75e21c5b7586
diff --git a/apps/web/app/api/cron/payouts/charge-succeeded/create-paypal-token.ts b/apps/web/app/api/cron/payouts/charge-succeeded/create-paypal-token.ts
new file mode 100644
index 0000000000..bbe81054ca
--- /dev/null
+++ b/apps/web/app/api/cron/payouts/charge-succeeded/create-paypal-token.ts
@@ -0,0 +1,55 @@
+import { paypalEnv } from "@/lib/paypal/env";
+import { redis } from "@/lib/upstash/redis";
+import { waitUntil } from "@vercel/functions";
+
+interface PaypalTokenResponse {
+ access_token: string;
+ expires_in: number; // in seconds
+}
+
+const CACHE_KEY = "paypal:token";
+
+/**
+ * Creates and caches a PayPal access token for batch payouts authentication.
+ * First checks Redis cache for an existing valid token, and if not found,
+ * requests a new token from PayPal's OAuth2 endpoint.
+ * The token is cached in Redis with a 5-minute buffer before expiration.
+ */
+export async function createPaypalToken() {
+ const cachedToken = await redis.get(CACHE_KEY);
+
+ if (cachedToken) {
+ return cachedToken;
+ }
+
+ const response = await fetch(paypalEnv.PAYPAL_API_HOST + "/v1/oauth2/token", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded",
+ Authorization: `Basic ${paypalEnv.PAYPAL_CLIENT_ID}:${paypalEnv.PAYPAL_CLIENT_SECRET}`,
+ },
+ body: new URLSearchParams({
+ grant_type: "client_credentials",
+ }),
+ });
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ throw new Error("Failed to create PayPal token.", {
+ cause: data,
+ });
+ }
+
+ const token = data as PaypalTokenResponse;
+
+ console.log("Token created", token);
+
+ waitUntil(
+ redis.set(CACHE_KEY, token.access_token, {
+ ex: token.expires_in - 60 * 5, // 5 min buffer
+ }),
+ );
+
+ return token.access_token;
+}
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 57866727c3..8d8f2ef967 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,6 +1,7 @@
import { paypalEnv } from "@/lib/paypal/env";
import { prisma } from "@dub/prisma";
import { Partner, Payout, Program } from "@prisma/client";
+import { createPaypalToken } from "./create-paypal-token";
import { Payload } from "./utils";
export const dynamic = "force-dynamic";
@@ -52,7 +53,7 @@ export async function sendPaypalPayouts({
return;
}
- const token = "";
+ const paypalAccessToken = await createPaypalToken();
const body = {
sender_batch_header: {
@@ -75,7 +76,7 @@ export async function sendPaypalPayouts({
method: "POST",
headers: {
"Content-Type": "application/json",
- Authorization: `Bearer ${token}`,
+ Authorization: `Bearer ${paypalAccessToken}`,
},
body: JSON.stringify(body),
},