mirror of
https://github.com/LukeHagar/polar.git
synced 2025-12-06 04:20:58 +00:00
redirect back after checkout
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -95,11 +95,13 @@ export class Polar<
|
||||
userId,
|
||||
email,
|
||||
origin,
|
||||
successUrl,
|
||||
}: {
|
||||
productIds: string[];
|
||||
userId: string;
|
||||
email: string;
|
||||
origin: string;
|
||||
successUrl: string;
|
||||
}
|
||||
): Promise<Checkout> {
|
||||
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 };
|
||||
},
|
||||
|
||||
@@ -59,6 +59,7 @@ export const CheckoutLink = ({
|
||||
void generateCheckoutLink({
|
||||
productIds,
|
||||
origin: window.location.origin,
|
||||
successUrl: window.location.href,
|
||||
}).then(({ url }) => setCheckoutLink(url));
|
||||
}, [productIds]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user