Skip to content

Commit 7549144

Browse files
committed
feat: Autofill customer email from better auth session or url
1 parent 838f048 commit 7549144

File tree

2 files changed

+8903
-11250
lines changed

2 files changed

+8903
-11250
lines changed

packages/polar-betterauth/src/endpoints/checkout.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const checkout = (options: PolarOptions) =>
1010
method: "GET",
1111
query: z.object({
1212
products: z.union([z.array(z.string()), z.string()]),
13+
customerEmail: z.string().email().optional(),
1314
}),
1415
},
1516
async (ctx) => {
@@ -30,6 +31,7 @@ export const checkout = (options: PolarOptions) =>
3031

3132
try {
3233
const checkout = await options.client.checkouts.create({
34+
customerEmail: ctx.query.customerEmail ?? session?.user.email,
3335
customerExternalId: session?.user.id,
3436
products: Array.isArray(products) ? products : [products],
3537
successUrl: options.checkout.successUrl
@@ -60,8 +62,12 @@ export const checkoutWithSlug = (options: PolarOptions) =>
6062
params: z.object({
6163
slug: z.string(),
6264
}),
65+
query: z.object({
66+
customerEmail: z.string().email().optional(),
67+
}),
6368
},
6469
async (ctx) => {
70+
6571
if (!options.checkout?.enabled) {
6672
throw new APIError("BAD_REQUEST", {
6773
message: "Checkout is not enabled",
@@ -92,6 +98,7 @@ export const checkoutWithSlug = (options: PolarOptions) =>
9298

9399
try {
94100
const checkout = await options.client.checkouts.create({
101+
customerEmail: ctx.query.customerEmail ?? session?.user.email,
95102
customerExternalId: session?.user.id,
96103
products: [productId],
97104
successUrl: options.checkout.successUrl

0 commit comments

Comments
 (0)