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 disable-promotion-code.ts

Kiran Kbefd2af35682
diff --git a/apps/web/lib/stripe/disable-promotion-code.ts b/apps/web/lib/stripe/disable-promotion-code.ts
index beb6ef9e71..0d0ce4e66a 100644
--- a/apps/web/lib/stripe/disable-promotion-code.ts
+++ b/apps/web/lib/stripe/disable-promotion-code.ts
@@ -4,35 +4,40 @@ const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { livemode: true }),
});
-// Create a promotion code on Stripe for connected accounts
-export async function createStripePromotionCode({
+// Disable a promotion code on Stripe for connected accounts
+export async function disableStripePromotionCode({
code,
- couponId,
stripeConnectId,
}: {
code: string;
- couponId: string | null;
stripeConnectId: string | null;
}) {
- if (!couponId) {
+ if (!stripeConnectId) {
console.error(
- "couponId not found for the discount. Stripe promotion code creation skipped.",
+ "stripeConnectId not found for the workspace. Stripe promotion code update skipped.",
);
return;
}
- if (!stripeConnectId) {
+ const promotionCodes = await stripe.promotionCodes.list({
+ code,
+ limit: 1,
+ });
+
+ if (promotionCodes.data.length === 0) {
console.error(
- "stripeConnectId not found for the workspace. Stripe promotion code creation skipped.",
+ `Promotion code ${code} not found in the connected account ${stripeConnectId}. Stripe promotion code update skipped.`,
);
return;
}
try {
- return await stripe.promotionCodes.create(
+ const promotionCode = promotionCodes.data[0];
+
+ return await stripe.promotionCodes.update(
+ promotionCode.id,
{
- coupon: couponId,
- code: code.toUpperCase(),
+ active: false,
},
{
stripeAccount: stripeConnectId,
@@ -40,7 +45,7 @@ export async function createStripePromotionCode({
);
} catch (error) {
console.error(
- `Failed to create Stripe promotion code for ${stripeConnectId}: ${error}`,
+ `Failed to disable Stripe promotion code ${code} for ${stripeConnectId}: ${error}`,
);
throw new Error(error instanceof Error ? error.message : "Unknown error");