Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/polar-betterauth/src/plugins/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const CheckoutParams = z.object({
discountId: z.string().optional(),
redirect: z.coerce.boolean().optional(),
embedOrigin: z.string().url().optional(),
successQueryParams: z.record(z.string(), z.string()).optional(),
});

export type CheckoutParams = z.infer<typeof CheckoutParams>;
Expand Down Expand Up @@ -98,10 +99,19 @@ export const checkout =
externalCustomerId: session?.user.id,
products: productIds,
successUrl: checkoutOptions.successUrl
? new URL(
? (() => {
const url = new URL(
checkoutOptions.successUrl,
ctx.request?.url ?? ctx.context.baseURL,
).toString()
)

if (ctx.body.successQueryParams) {
for (const [key, value] of Object.entries(ctx.body.successQueryParams)) {
url.searchParams.set(key, value);
}
}
return url.toString();
})()
: undefined,
metadata: ctx.body.referenceId
? {
Expand Down
7 changes: 6 additions & 1 deletion packages/polar-betterauth/src/plugins/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface PortalConfig {
returnUrl?: string;
}

export const PortalParams = z.object({
redirect: z.coerce.boolean().optional(),
}).optional();

export const portal =
({ returnUrl }: PortalConfig = {}) =>
(polar: Polar) => {
Expand All @@ -19,6 +23,7 @@ export const portal =
{
method: "GET",
use: [sessionMiddleware],
query: PortalParams,
},
async (ctx) => {
if (!ctx.context.session?.user.id) {
Expand All @@ -35,7 +40,7 @@ export const portal =

return ctx.json({
url: customerSession.customerPortalUrl,
redirect: true,
redirect: ctx.query?.redirect ?? true,
});
} catch (e: unknown) {
if (e instanceof Error) {
Expand Down