Skip to content

Commit

Permalink
feat: Use dub.customer.list to issue dual-sided incentives (#18452)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
3 people authored Jan 17, 2025
1 parent 245196f commit f411c6e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 18 additions & 2 deletions apps/api/v1/pages/api/teams/_post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextApiRequest } from "next";

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 } from "@calcom/lib/constants";
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
import { HttpError } from "@calcom/lib/http-error";
Expand Down Expand Up @@ -192,11 +193,26 @@ const generateTeamCheckoutSession = async ({
pendingPaymentTeamId: number;
ownerId: number;
}) => {
const customer = await getStripeCustomerIdFromUserId(ownerId);
const [customer, dubCustomer] = await Promise.all([
getStripeCustomerIdFromUserId(ownerId),
getDubCustomer(ownerId.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/api/create?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${WEBAPP_URL}/settings/my-account/profile`,
line_items: [
Expand Down
13 changes: 13 additions & 0 deletions packages/features/auth/lib/dub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@ import { Dub } from "dub";
export const dub = new Dub({
token: process.env.DUB_API_KEY,
});

export const getDubCustomer = async (userId: string) => {
if (!process.env.DUB_API_KEY) {
return null;
}

const customer = await dub.customers.list({
externalId: userId,
includeExpandedFields: true,
});

return customer.length > 0 ? customer[0] : null;
};

0 comments on commit f411c6e

Please sign in to comment.