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.

update the link with new coupon code

Kiran Kf83ad9f8dcac
diff --git a/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts b/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts
index c6ec122cf9..8f55c57fad 100644
--- a/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts
+++ b/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts
@@ -105,6 +105,7 @@ export async function POST(req: Request) {
},
},
select: {
+ id: true,
key: true,
},
});
@@ -119,9 +120,9 @@ export async function POST(req: Request) {
for (const linksChunk of linksChunks) {
const results = await Promise.allSettled(
- linksChunk.map(({ key }) =>
+ linksChunk.map((link) =>
createStripePromotionCode({
- code: key,
+ link,
couponId,
stripeConnectId,
}),
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index 0b2021cbd6..5a8060c053 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -147,7 +147,6 @@ export async function createLink(link: CreateLinkProps) {
},
});
-
// TODO:
// Move to waitUntil
if (response.programId && response.partnerId && !program && !discount) {
@@ -254,7 +253,7 @@ export async function createLink(link: CreateLinkProps) {
shouldCreateCouponCode &&
stripeConnectId &&
createStripePromotionCode({
- code: response.key,
+ link: response,
couponId: discount?.couponId!,
stripeConnectId,
}),
diff --git a/apps/web/lib/stripe/create-stripe-promotion-code.ts b/apps/web/lib/stripe/create-stripe-promotion-code.ts
index beb6ef9e71..21e83776bb 100644
--- a/apps/web/lib/stripe/create-stripe-promotion-code.ts
+++ b/apps/web/lib/stripe/create-stripe-promotion-code.ts
@@ -1,16 +1,20 @@
+import { prisma } from "@dub/prisma";
import { stripeAppClient } from ".";
+import { LinkProps } from "../types";
const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { livemode: true }),
});
+const MAX_RETRIES = 2;
+
// Create a promotion code on Stripe for connected accounts
export async function createStripePromotionCode({
- code,
+ link,
couponId,
stripeConnectId,
}: {
- code: string;
+ link: Pick<LinkProps, "id" | "key">;
couponId: string | null;
stripeConnectId: string | null;
}) {
@@ -28,21 +32,58 @@ export async function createStripePromotionCode({
return;
}
- try {
- return await stripe.promotionCodes.create(
- {
- coupon: couponId,
- code: code.toUpperCase(),
- },
- {
- stripeAccount: stripeConnectId,
- },
- );
- } catch (error) {
- console.error(
- `Failed to create Stripe promotion code for ${stripeConnectId}: ${error}`,
- );
+ let lastError: Error | null = null;
+ let couponCode: string | undefined;
+
+ for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
+ try {
+ // Add DUB_ prefix after first retry
+ couponCode = attempt === 1 ? link.key : `DUB_${link.key}`;
+
+ const promotionCode = await stripe.promotionCodes.create(
+ {
+ coupon: couponId,
+ code: couponCode.toUpperCase(),
+ },
+ {
+ stripeAccount: stripeConnectId,
+ },
+ );
- throw new Error(error instanceof Error ? error.message : "Unknown error");
+ if (promotionCode) {
+ await prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ couponCode: promotionCode.code,
+ },
+ });
+ }
+
+ return promotionCode;
+ } catch (error) {
+ lastError = error instanceof Error ? error : new Error(String(error));
+
+ const isDuplicateError =
+ error instanceof Error &&
+ error.message.includes("An active promotion code with `code:") &&
+ error.message.includes("already exists");
+
+ if (isDuplicateError) {
+ if (attempt === MAX_RETRIES) {
+ throw lastError;
+ }
+
+ continue;
+ }
+
+ throw lastError;
+ }
}
+
+ throw (
+ lastError ||
+ new Error("Unknown error occurred while creating promotion code on Stripe.")
+ );
}