Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/purchases/cancel-subscription.ts
1454 views
1
/*
2
Cancel a subscription.
3
4
- now: if true, cancels license now and provides a refund. Otherwise, cancels at period end.
5
*/
6
7
import getAccountId from "lib/account/get-account";
8
import cancelSubscription from "@cocalc/server/purchases/cancel-subscription";
9
import getParams from "lib/api/get-params";
10
import { OkStatus } from "lib/api/status";
11
12
export default async function handle(req, res) {
13
try {
14
res.json(await get(req));
15
} catch (err) {
16
res.json({ error: `${err.message}` });
17
return;
18
}
19
}
20
21
async function get(req) {
22
const account_id = await getAccountId(req);
23
if (account_id == null) {
24
throw Error("must be signed in");
25
}
26
const { subscription_id, reason } = getParams(req);
27
await cancelSubscription({
28
account_id,
29
subscription_id,
30
reason,
31
});
32
return OkStatus;
33
}
34
35