mirror of
https://github.com/LukeHagar/polar.git
synced 2025-12-06 12:47:46 +00:00
* wip * add static example app * wip * wip * use relative path for jsx import * wip * working with single upgrade * add premium plus upgrade cta * make the ctas pricing page style * update price tiles * wip * add current plan state * wip * working checkouts * add free tier tile * fix cta button arrows * fix customer model * add dummy manage subscription button * improve subscription manage button * fix current plan tile state * revoke existing subscriptions when a new subscription is created * support upgrade, downgrade, cancel * add downgrade buttons * wip * implement upgrade/cancel functionality * add downgrade confirmation * add upgrade confirmation * add billing settings panel * wip * wip * split out pricing panel section * fix manage button * wip * working * wip * differentiate premium plus * fix price for current subscription * add prices * add interval toggle * wip * set default interval for change plan screen * simplify * add yearly pricing * fix upgrade/downgrade * prompt on delete * wip - docs * wip * wip - docs * wip * wip * wip * wip * wip * update version
104 lines
2.8 KiB
TypeScript
104 lines
2.8 KiB
TypeScript
import { Polar } from "@polar-sh/sdk";
|
|
import { internalAction, internalMutation } from "./_generated/server";
|
|
import { internal } from "./_generated/api";
|
|
|
|
const accessToken = process.env.POLAR_ORGANIZATION_TOKEN;
|
|
|
|
const polar = new Polar({
|
|
accessToken,
|
|
server: "sandbox",
|
|
});
|
|
|
|
export const PREMIUM_PLAN_NAME = "Premium Plan";
|
|
export const PREMIUM_PLUS_PLAN_NAME = "Premium Plus Plan";
|
|
|
|
export const insertFakeUser = internalMutation({
|
|
handler: async (ctx) => {
|
|
const existingUser = await ctx.db.query("users").first();
|
|
if (existingUser) {
|
|
console.log("User already exists");
|
|
return;
|
|
}
|
|
await ctx.db.insert("users", { email: "user@example.com" });
|
|
},
|
|
});
|
|
|
|
const seed = internalAction({
|
|
handler: async (ctx) => {
|
|
// Insert a fake user for test purposes since this example doesn't have
|
|
// working authentication.
|
|
await ctx.runMutation(internal.seed.insertFakeUser);
|
|
|
|
async function hasItems(asyncIterable: AsyncIterable<any>) {
|
|
for await (const {
|
|
result: { items },
|
|
} of asyncIterable) {
|
|
return items.length > 0;
|
|
}
|
|
}
|
|
const result = await polar.products.list({
|
|
isArchived: false,
|
|
limit: 1,
|
|
});
|
|
const hasProducts = await hasItems(result);
|
|
|
|
// Return early if the Polar organization already has products, ensures
|
|
// this doesn't run more than once.
|
|
if (hasProducts) {
|
|
console.log("Products already exist");
|
|
return;
|
|
}
|
|
|
|
// Create example products. In a real app you would likely create your
|
|
// products in the Polar dashboard and reference them by id in your application.
|
|
await Promise.all([
|
|
polar.products.create({
|
|
name: PREMIUM_PLAN_NAME,
|
|
description: "All the things for one low monthly price.",
|
|
recurringInterval: "month",
|
|
prices: [
|
|
{
|
|
amountType: "fixed",
|
|
priceAmount: 1000,
|
|
},
|
|
],
|
|
}),
|
|
polar.products.create({
|
|
name: PREMIUM_PLAN_NAME,
|
|
description: "All the things for one low annual price.",
|
|
recurringInterval: "year",
|
|
prices: [
|
|
{
|
|
amountType: "fixed",
|
|
priceAmount: 10000,
|
|
},
|
|
],
|
|
}),
|
|
polar.products.create({
|
|
name: PREMIUM_PLUS_PLAN_NAME,
|
|
description: "All the things for one low monthly price.",
|
|
recurringInterval: "month",
|
|
prices: [
|
|
{
|
|
amountType: "fixed",
|
|
priceAmount: 2000,
|
|
},
|
|
],
|
|
}),
|
|
polar.products.create({
|
|
name: PREMIUM_PLUS_PLAN_NAME,
|
|
description: "All the things for one low annual price.",
|
|
recurringInterval: "year",
|
|
prices: [
|
|
{
|
|
amountType: "fixed",
|
|
priceAmount: 20000,
|
|
},
|
|
],
|
|
}),
|
|
]);
|
|
},
|
|
});
|
|
|
|
export default seed;
|