diff --git a/package.json b/package.json index b7a72db..35c2ddd 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ }, "peerDependencies": { "@polar-sh/checkout": ">=0.1.10", - "@polar-sh/sdk": ">=0.32.11", + "@polar-sh/sdk": ">=0.35.4", "convex": "^1.29.0", "react": "^18 || ^19", "react-dom": "^18 || ^19" diff --git a/src/client/index.ts b/src/client/index.ts index ab82f7d..3021e0b 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -105,6 +105,8 @@ export class Polar< origin, successUrl, subscriptionId, + trialInterval, + trialIntervalCount, }: { productIds: string[]; userId: string; @@ -112,7 +114,9 @@ export class Polar< origin: string; successUrl: string; subscriptionId?: string; - }, + trialInterval?: "day" | "week" | "month" | "year" | null; + trialIntervalCount?: number | null; + } ): Promise { const dbCustomer = await ctx.runQuery( this.component.lib.getCustomerByUserId, @@ -146,6 +150,8 @@ export class Polar< subscriptionId, embedOrigin: origin, successUrl, + trialInterval, + trialIntervalCount, ...(productIds.length === 1 ? { products: productIds } : { products: productIds }), @@ -309,6 +315,16 @@ export class Polar< origin: v.string(), successUrl: v.string(), subscriptionId: v.optional(v.string()), + trialInterval: v.optional( + v.union( + v.literal("day"), + v.literal("week"), + v.literal("month"), + v.literal("year"), + v.null() + ) + ), + trialIntervalCount: v.optional(v.union(v.number(), v.null())), }, returns: v.object({ url: v.string(), @@ -322,6 +338,8 @@ export class Polar< subscriptionId: args.subscriptionId, origin: args.origin, successUrl: args.successUrl, + trialInterval: args.trialInterval, + trialIntervalCount: args.trialIntervalCount, }); return { url }; }, diff --git a/src/component/lib.ts b/src/component/lib.ts index 2dc7467..5c5d010 100644 --- a/src/component/lib.ts +++ b/src/component/lib.ts @@ -161,8 +161,11 @@ export const listUserSubscriptions = query({ .collect(), async (subscription) => { if ( - subscription.endedAt && - subscription.endedAt <= new Date().toISOString() + (subscription.endedAt && + subscription.endedAt <= new Date().toISOString()) || + (subscription.status === "trialing" && + subscription.trialEnd && + subscription.trialEnd <= new Date().toISOString()) ) { return; } diff --git a/src/component/schema.ts b/src/component/schema.ts index f5027fe..fd997a8 100644 --- a/src/component/schema.ts +++ b/src/component/schema.ts @@ -26,6 +26,16 @@ export default defineSchema( description: v.union(v.string(), v.null()), recurringInterval: v.optional(vRecurringInterval), isRecurring: v.boolean(), + trialInterval: v.optional( + v.union( + v.literal("day"), + v.literal("week"), + v.literal("month"), + v.literal("year"), + v.null() + ) + ), + trialIntervalCount: v.optional(v.union(v.number(), v.null())), isArchived: v.boolean(), organizationId: v.string(), metadata: v.optional(v.record(v.string(), v.any())), @@ -82,6 +92,8 @@ export default defineSchema( currentPeriodStart: v.string(), currentPeriodEnd: v.union(v.string(), v.null()), cancelAtPeriodEnd: v.boolean(), + trialStart: v.optional(v.union(v.string(), v.null())), + trialEnd: v.optional(v.union(v.string(), v.null())), startedAt: v.union(v.string(), v.null()), endedAt: v.union(v.string(), v.null()), productId: v.string(), diff --git a/src/component/util.ts b/src/component/util.ts index e64abb3..247fc26 100644 --- a/src/component/util.ts +++ b/src/component/util.ts @@ -36,6 +36,8 @@ export const convertToDatabaseSubscription = ( currency: subscription.currency, recurringInterval: subscription.recurringInterval, status: subscription.status, + trialStart: subscription.trialStart?.toISOString() ?? null, + trialEnd: subscription.trialEnd?.toISOString() ?? null, currentPeriodStart: subscription.currentPeriodStart.toISOString(), currentPeriodEnd: subscription.currentPeriodEnd?.toISOString() ?? null, cancelAtPeriodEnd: subscription.cancelAtPeriodEnd, @@ -60,6 +62,8 @@ export const convertToDatabaseProduct = ( createdAt: product.createdAt.toISOString(), modifiedAt: product.modifiedAt?.toISOString() ?? null, recurringInterval: product.recurringInterval, + trialInterval: product.trialInterval, + trialIntervalCount: product.trialIntervalCount, metadata: product.metadata, prices: product.prices.map((price) => ({ id: price.id, diff --git a/src/react/index.tsx b/src/react/index.tsx index c5c68bd..bedc62e 100644 --- a/src/react/index.tsx +++ b/src/react/index.tsx @@ -42,10 +42,14 @@ export const CheckoutLink = ({ subscriptionId, theme = "dark", embed = true, + trialInterval, + trialIntervalCount, }: PropsWithChildren<{ polarApi: Pick; productIds: string[]; subscriptionId?: string; + trialInterval?: "day" | "week" | "month" | "year" | null; + trialIntervalCount?: number | null; className?: string; theme?: "dark" | "light"; embed?: boolean; @@ -62,6 +66,8 @@ export const CheckoutLink = ({ subscriptionId, origin: window.location.origin, successUrl: window.location.href, + trialInterval, + trialIntervalCount, }).then(({ url }) => setCheckoutLink(url)); }, [productIds, subscriptionId, embed, generateCheckoutLink]);