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.

Refactor Stripe client initialization to use VERCEL_ENV for livemode in create-coupon, create-promotion-code, and delete-coupon functions

Kiran K33dc5f27b609
diff --git a/apps/web/lib/stripe/create-coupon.ts b/apps/web/lib/stripe/create-coupon.ts
index 749b72a5ce..f383c0f3ba 100644
--- a/apps/web/lib/stripe/create-coupon.ts
+++ b/apps/web/lib/stripe/create-coupon.ts
@@ -1,6 +1,10 @@
import { Discount } from "@prisma/client";
import { stripeAppClient } from ".";
+const stripe = stripeAppClient({
+ ...(process.env.VERCEL_ENV && { livemode: true }),
+});
+
// Create a coupon on Stripe for connected accounts
export async function createStripeCoupon({
coupon,
@@ -16,25 +20,23 @@ export async function createStripeCoupon({
return;
}
- const stripe = stripeAppClient({
- livemode: process.env.NODE_ENV === "production",
- });
-
- const { type, amount, maxDuration } = coupon;
-
const duration =
- maxDuration === null ? "forever" : maxDuration === 1 ? "once" : "repeating";
+ coupon.maxDuration === null
+ ? "forever"
+ : coupon.maxDuration === 1
+ ? "once"
+ : "repeating";
return await stripe.coupons.create(
{
currency: "usd",
duration,
...(duration === "repeating" && {
- duration_in_months: maxDuration!,
+ duration_in_months: coupon.maxDuration!,
}),
- ...(type === "percentage"
- ? { percent_off: amount }
- : { amount_off: amount }),
+ ...(coupon.type === "percentage"
+ ? { percent_off: coupon.amount }
+ : { amount_off: coupon.amount }),
},
{
stripeAccount: stripeConnectId,
diff --git a/apps/web/lib/stripe/create-promotion-code.ts b/apps/web/lib/stripe/create-promotion-code.ts
index 2d58694f27..93c034316c 100644
--- a/apps/web/lib/stripe/create-promotion-code.ts
+++ b/apps/web/lib/stripe/create-promotion-code.ts
@@ -1,5 +1,9 @@
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({
couponId,
@@ -17,10 +21,6 @@ export async function createStripePromotionCode({
return;
}
- const stripe = stripeAppClient({
- livemode: process.env.NODE_ENV === "production",
- });
-
try {
return await stripe.promotionCodes.create(
{
diff --git a/apps/web/lib/stripe/delete-coupon.ts b/apps/web/lib/stripe/delete-coupon.ts
index 1c8b877065..dc5496db53 100644
--- a/apps/web/lib/stripe/delete-coupon.ts
+++ b/apps/web/lib/stripe/delete-coupon.ts
@@ -1,5 +1,9 @@
import { stripeAppClient } from ".";
+const stripe = stripeAppClient({
+ ...(process.env.VERCEL_ENV && { livemode: true }),
+});
+
// Delete a coupon on Stripe for connected accounts
export async function deleteStripeCoupon({
couponId,
@@ -15,10 +19,6 @@ export async function deleteStripeCoupon({
return;
}
- const stripe = stripeAppClient({
- livemode: process.env.NODE_ENV === "production",
- });
-
try {
return await stripe.coupons.del(couponId, {
stripeAccount: stripeConnectId,