fix: add optional subscriptionId prop for checkout link (#17)

* feat: add optional subscriptionId prop for checkout link and upgrade functionality

* fix: move subscriptionId prop to the correct position in createCheckoutSession parameters
This commit is contained in:
Julian Sigmund
2025-09-01 19:53:55 +02:00
committed by GitHub
parent b6ecdfb15f
commit 2a7e92a99c
3 changed files with 10 additions and 1 deletions

View File

@@ -410,6 +410,7 @@ Props:
- `children`: React children (button content) - `children`: React children (button content)
- `embed`: (Optional) Whether to embed the checkout link. Defaults to `true`. - `embed`: (Optional) Whether to embed the checkout link. Defaults to `true`.
- `className`: (Optional) CSS class name - `className`: (Optional) CSS class name
- `subscriptionId`: (Optional) ID of a subscription to upgrade. It must be on a free pricing.
#### CustomerPortalLink #### CustomerPortalLink

View File

@@ -104,12 +104,14 @@ export class Polar<
email, email,
origin, origin,
successUrl, successUrl,
subscriptionId
}: { }: {
productIds: string[]; productIds: string[];
userId: string; userId: string;
email: string; email: string;
origin: string; origin: string;
successUrl: string; successUrl: string;
subscriptionId?: string;
} }
): Promise<Checkout> { ): Promise<Checkout> {
const dbCustomer = await ctx.runQuery( const dbCustomer = await ctx.runQuery(
@@ -141,6 +143,7 @@ export class Polar<
const checkout = await checkoutsCreate(this.polar, { const checkout = await checkoutsCreate(this.polar, {
allowDiscountCodes: true, allowDiscountCodes: true,
customerId, customerId,
subscriptionId,
embedOrigin: origin, embedOrigin: origin,
successUrl, successUrl,
...(productIds.length === 1 ...(productIds.length === 1
@@ -306,6 +309,7 @@ export class Polar<
productIds: v.array(v.string()), productIds: v.array(v.string()),
origin: v.string(), origin: v.string(),
successUrl: v.string(), successUrl: v.string(),
subscriptionId: v.optional(v.string())
}, },
returns: v.object({ returns: v.object({
url: v.string(), url: v.string(),
@@ -316,6 +320,7 @@ export class Polar<
productIds: args.productIds, productIds: args.productIds,
userId, userId,
email, email,
subscriptionId: args.subscriptionId,
origin: args.origin, origin: args.origin,
successUrl: args.successUrl, successUrl: args.successUrl,
}); });

View File

@@ -39,11 +39,13 @@ export const CheckoutLink = ({
productIds, productIds,
children, children,
className, className,
subscriptionId,
theme = "dark", theme = "dark",
embed = true, embed = true,
}: PropsWithChildren<{ }: PropsWithChildren<{
polarApi: Pick<PolarComponentApi, "generateCheckoutLink">; polarApi: Pick<PolarComponentApi, "generateCheckoutLink">;
productIds: string[]; productIds: string[];
subscriptionId?: string;
className?: string; className?: string;
theme?: "dark" | "light"; theme?: "dark" | "light";
embed?: boolean; embed?: boolean;
@@ -57,10 +59,11 @@ export const CheckoutLink = ({
} }
void generateCheckoutLink({ void generateCheckoutLink({
productIds, productIds,
subscriptionId,
origin: window.location.origin, origin: window.location.origin,
successUrl: window.location.href, successUrl: window.location.href,
}).then(({ url }) => setCheckoutLink(url)); }).then(({ url }) => setCheckoutLink(url));
}, [productIds]); }, [productIds, subscriptionId]);
return ( return (
<a <a