improve(payments): handling free products and subscriptions

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-04-01 21:27:11 +02:00
parent 1e99c1843a
commit a6762b90ca
4 changed files with 106 additions and 49 deletions

View File

@@ -84,14 +84,25 @@ export async function createCheckout({
});
}
export function cancelSubscription(subscriptionId: string) {
return polar.subscriptions.update({
id: subscriptionId,
subscriptionUpdate: {
cancelAtPeriodEnd: true,
revoke: null,
},
});
export async function cancelSubscription(subscriptionId: string) {
try {
return await polar.subscriptions.update({
id: subscriptionId,
subscriptionUpdate: {
cancelAtPeriodEnd: true,
revoke: null,
},
});
} catch (error) {
if (error instanceof Error) {
// Don't throw an error if the subscription is already canceled
if (error.name === 'AlreadyCanceledSubscription') {
return polar.subscriptions.get({ id: subscriptionId });
}
}
throw error;
}
}
export function reactivateSubscription(subscriptionId: string) {

View File

@@ -16,3 +16,8 @@ export const PRICING: IPrice[] = [
// { price: 650, events: 20_000_000 },
// { price: 900, events: 30_000_000 },
];
export const FREE_PRODUCT_IDS = [
'a18b4bee-d3db-4404-be6f-fba2f042d9ed', // Prod
'036efa2a-b3b4-4c75-b24a-9cac6bb8893b', // Sandbox
];