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.

Create disable-promotion-code.ts

Kiran K94a12102259f
diff --git a/apps/web/lib/stripe/disable-promotion-code.ts b/apps/web/lib/stripe/disable-promotion-code.ts
new file mode 100644
index 0000000000..beb6ef9e71
--- /dev/null
+++ b/apps/web/lib/stripe/disable-promotion-code.ts
@@ -0,0 +1,48 @@
+import { stripeAppClient } from ".";
+
+const stripe = stripeAppClient({
+ ...(process.env.VERCEL_ENV && { livemode: true }),
+});
+
+// Create a promotion code on Stripe for connected accounts
+export async function createStripePromotionCode({
+ code,
+ couponId,
+ stripeConnectId,
+}: {
+ code: string;
+ couponId: string | null;
+ stripeConnectId: string | null;
+}) {
+ if (!couponId) {
+ console.error(
+ "couponId not found for the discount. Stripe promotion code creation skipped.",
+ );
+ return;
+ }
+
+ if (!stripeConnectId) {
+ console.error(
+ "stripeConnectId not found for the workspace. Stripe promotion code creation skipped.",
+ );
+ 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}`,
+ );
+
+ throw new Error(error instanceof Error ? error.message : "Unknown error");
+ }
+}