| 77 | } |
| 78 | |
| 79 | export async function cancelSubscription(subscriptionId: string) { |
| 80 | try { |
| 81 | return await polar.subscriptions.update({ |
| 82 | id: subscriptionId, |
| 83 | subscriptionUpdate: { |
| 84 | cancelAtPeriodEnd: true, |
| 85 | }, |
| 86 | }); |
| 87 | } catch (error) { |
| 88 | if (error instanceof Error) { |
| 89 | // Don't throw an error if the subscription is already canceled |
| 90 | if (error.name === 'AlreadyCanceledSubscription') { |
| 91 | return polar.subscriptions.get({ id: subscriptionId }); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | throw error; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export function reactivateSubscription(subscriptionId: string) { |
| 100 | return polar.subscriptions.update({ |