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.

Set referral discount via Dub Customer object

Steven Tey7ad643e8a73a
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index e1a0b05063..95ae990915 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -1,4 +1,5 @@
import { withWorkspace } from "@/lib/auth";
+import { getDubCustomer } from "@/lib/dub";
import { stripe } from "@/lib/stripe";
import { APP_DOMAIN } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -46,6 +47,10 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
});
return NextResponse.json({ url });
} else {
+ const customer = await getDubCustomer(session.user.id);
+ const isReferral =
+ customer?.link?.programId === "prog_d8pl69xXCv4AoHNT281pHQdo";
+
// For both new users and users with canceled subscriptions
const stripeSession = await stripe.checkout.sessions.create({
...(workspace.stripeId
@@ -64,7 +69,18 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
success_url: `${APP_DOMAIN}/${workspace.slug}?${onboarding ? "onboarded" : "upgraded"}=true&plan=${plan}&period=${period}`,
cancel_url: baseUrl,
line_items: [{ price: prices.data[0].id, quantity: 1 }],
- allow_promotion_codes: true,
+ ...(isReferral
+ ? {
+ discounts: [
+ {
+ coupon:
+ process.env.NODE_ENV === "production"
+ ? "pEVpzGQE"
+ : "k8v8KtqG",
+ },
+ ],
+ }
+ : { allow_promotion_codes: true }),
automatic_tax: {
enabled: true,
},
diff --git a/apps/web/lib/dub.ts b/apps/web/lib/dub.ts
index 7f32903592..6c6fa1ca2f 100644
--- a/apps/web/lib/dub.ts
+++ b/apps/web/lib/dub.ts
@@ -1,3 +1,13 @@
import { Dub } from "dub";
export const dub = new Dub();
+
+export const getDubCustomer = async (userId: string) => {
+ try {
+ return await dub.customers.get({
+ id: `ext_${userId}`,
+ });
+ } catch (error) {
+ return null;
+ }
+};
diff --git a/apps/web/package.json b/apps/web/package.json
index 230033112f..00eeafbc0c 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -64,7 +64,7 @@
"concurrently": "^8.0.1",
"csv-parse": "^5.5.6",
"date-fns": "^3.6.0",
- "dub": "^0.46.1",
+ "dub": "^0.46.8",
"framer-motion": "^10.16.16",
"fuse.js": "^6.6.2",
"geist": "^1.3.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b055203519..1dcbc975c4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -184,8 +184,8 @@ importers:
specifier: ^3.6.0
version: 3.6.0
dub:
- specifier: ^0.46.1
+ specifier: ^0.46.8
+ version: 0.46.8([email protected])
framer-motion:
specifier: ^10.16.16
@@ -12560,15 +12560,11 @@ packages:
zod: 3.23.8
dev: false
- resolution: {integrity: sha512-3VRmxY3oAl4w3PCpwIO6R+SQCumlIJKZR5RCF9lPhj5IeUAGVTZsms3bMwKmO+uwQoWZ79ymZHnMOmmZvUaEsg==}
+ resolution: {integrity: sha512-48r051aekxXnRvHtxwaJeKeUYi0MgX3jiEE9BbCdGkAaNOBU6+hfl2bJPYMK+MO+qBRAc0uYOeBvIxcKKgrBCQ==}
peerDependencies:
- react: ^18 || ^19
- react-dom: ^18 || ^19
zod: '>= 3'
dependencies:
- react: 18.2.0
- react-dom: 18.2.0([email protected])
zod: 3.22.4
dev: false