Skip to content

Commit f411c6e

Browse files
steven-teyzomarsPeerRich
authored
feat: Use dub.customer.list to issue dual-sided incentives (#18452)
* [FEAT] Use dub.customer to issue dual-sided incentives * update dub implementation * uncommit yarn.lock * Update yarn.lock * account for self-hosting * remove comments --------- Co-authored-by: Omar López <zomars@me.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
1 parent 245196f commit f411c6e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

apps/api/v1/pages/api/teams/_post.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NextApiRequest } from "next";
22

33
import { getStripeCustomerIdFromUserId } from "@calcom/app-store/stripepayment/lib/customer";
44
import stripe from "@calcom/app-store/stripepayment/lib/server";
5+
import { getDubCustomer } from "@calcom/features/auth/lib/dub";
56
import { IS_PRODUCTION } from "@calcom/lib/constants";
67
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
78
import { HttpError } from "@calcom/lib/http-error";
@@ -192,11 +193,26 @@ const generateTeamCheckoutSession = async ({
192193
pendingPaymentTeamId: number;
193194
ownerId: number;
194195
}) => {
195-
const customer = await getStripeCustomerIdFromUserId(ownerId);
196+
const [customer, dubCustomer] = await Promise.all([
197+
getStripeCustomerIdFromUserId(ownerId),
198+
getDubCustomer(ownerId.toString()),
199+
]);
200+
196201
const session = await stripe.checkout.sessions.create({
197202
customer,
198203
mode: "subscription",
199-
allow_promotion_codes: true,
204+
...(dubCustomer?.discount?.couponId
205+
? {
206+
discounts: [
207+
{
208+
coupon:
209+
process.env.NODE_ENV !== "production" && dubCustomer.discount.couponTestId
210+
? dubCustomer.discount.couponTestId
211+
: dubCustomer.discount.couponId,
212+
},
213+
],
214+
}
215+
: { allow_promotion_codes: true }),
200216
success_url: `${WEBAPP_URL}/api/teams/api/create?session_id={CHECKOUT_SESSION_ID}`,
201217
cancel_url: `${WEBAPP_URL}/settings/my-account/profile`,
202218
line_items: [

packages/features/auth/lib/dub.ts

+13
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ import { Dub } from "dub";
33
export const dub = new Dub({
44
token: process.env.DUB_API_KEY,
55
});
6+
7+
export const getDubCustomer = async (userId: string) => {
8+
if (!process.env.DUB_API_KEY) {
9+
return null;
10+
}
11+
12+
const customer = await dub.customers.list({
13+
externalId: userId,
14+
includeExpandedFields: true,
15+
});
16+
17+
return customer.length > 0 ? customer[0] : null;
18+
};

0 commit comments

Comments
 (0)