From 161aece3a1e35936b83a43bb20cb23d3d4af477c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20L=C3=B3pez?= Date: Thu, 23 Jan 2025 14:30:36 -0700 Subject: [PATCH] fix: dual-sided incentives on webapp (#18846) --- packages/features/ee/teams/lib/payments.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/features/ee/teams/lib/payments.ts b/packages/features/ee/teams/lib/payments.ts index 2b0a238984dadb..b104c423cb6d38 100644 --- a/packages/features/ee/teams/lib/payments.ts +++ b/packages/features/ee/teams/lib/payments.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import { getStripeCustomerIdFromUserId } from "@calcom/app-store/stripepayment/lib/customer"; import stripe from "@calcom/app-store/stripepayment/lib/server"; +import { getDubCustomer } from "@calcom/features/auth/lib/dub"; import { IS_PRODUCTION, MINIMUM_NUMBER_OF_ORG_SEATS, @@ -53,11 +54,25 @@ export const generateTeamCheckoutSession = async ({ teamSlug: string; userId: number; }) => { - const customer = await getStripeCustomerIdFromUserId(userId); + const [customer, dubCustomer] = await Promise.all([ + getStripeCustomerIdFromUserId(userId), + getDubCustomer(userId.toString()), + ]); const session = await stripe.checkout.sessions.create({ customer, mode: "subscription", - allow_promotion_codes: true, + ...(dubCustomer?.discount?.couponId + ? { + discounts: [ + { + coupon: + process.env.NODE_ENV !== "production" && dubCustomer.discount.couponTestId + ? dubCustomer.discount.couponTestId + : dubCustomer.discount.couponId, + }, + ], + } + : { allow_promotion_codes: true }), success_url: `${WEBAPP_URL}/api/teams/create?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${WEBAPP_URL}/settings/my-account/profile`, line_items: [