From 3078dc679421e684840facfbdbf21a3451908bbe Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Mon, 3 Mar 2025 11:47:45 -0500 Subject: [PATCH] redirect back after checkout --- example/convex/example.ts | 25 +++---------------------- src/client/index.ts | 5 +++++ src/react/index.tsx | 1 + 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/example/convex/example.ts b/example/convex/example.ts index 14a6678..cf05785 100644 --- a/example/convex/example.ts +++ b/example/convex/example.ts @@ -1,6 +1,6 @@ import { Polar } from "@convex-dev/polar"; import { api, components } from "./_generated/api"; -import { QueryCtx, mutation, query, action } from "./_generated/server"; +import { QueryCtx, mutation, query } from "./_generated/server"; import { v } from "convex/values"; import { Id } from "./_generated/dataModel"; @@ -43,27 +43,8 @@ export const { listAllProducts, } = polar.api(); -export const { generateCustomerPortalUrl } = polar.checkoutApi(); - -// Custom implementation of generateCheckoutLink that accepts a productKey -export const generateCheckoutLink = action({ - args: { - productIds: v.array(v.string()), - origin: v.string(), - }, - handler: async (ctx, args) => { - const user = await ctx.runQuery(api.example.getCurrentUser); - if (!user) throw new Error("No user found"); - - const session = await polar.createCheckoutSession(ctx, { - productIds: args.productIds, - userId: user._id, - email: user.email, - origin: args.origin, - }); - return { url: session.url }; - }, -}); +export const { generateCustomerPortalUrl, generateCheckoutLink } = + polar.checkoutApi(); // In a real app you'll set up authentication, we just use a // fake user for the example. diff --git a/src/client/index.ts b/src/client/index.ts index 235cfdd..c1a9174 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -95,11 +95,13 @@ export class Polar< userId, email, origin, + successUrl, }: { productIds: string[]; userId: string; email: string; origin: string; + successUrl: string; } ): Promise { const dbCustomer = await ctx.runQuery( @@ -128,6 +130,7 @@ export class Polar< allowDiscountCodes: true, customerId, embedOrigin: origin, + successUrl, ...(productIds.length === 1 ? { productId: productIds[0] } : { products: productIds }), @@ -275,6 +278,7 @@ export class Polar< args: { productIds: v.array(v.string()), origin: v.string(), + successUrl: v.string(), }, returns: v.object({ url: v.string(), @@ -286,6 +290,7 @@ export class Polar< userId, email, origin: args.origin, + successUrl: args.successUrl, }); return { url }; }, diff --git a/src/react/index.tsx b/src/react/index.tsx index f3b16af..103d8c9 100644 --- a/src/react/index.tsx +++ b/src/react/index.tsx @@ -59,6 +59,7 @@ export const CheckoutLink = ({ void generateCheckoutLink({ productIds, origin: window.location.origin, + successUrl: window.location.href, }).then(({ url }) => setCheckoutLink(url)); }, [productIds]);