CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/purchases/setup-automatic-billing.ts
Views: 687
1
/*
2
Creates a stripe checkout session that sets up automatic billing via a stripe usage-based subscription.
3
4
This is mainly used to *increase* the user's balance periodically so that their subscriptions will
5
get automatically paid. Also, if they are allowed to let their balance go below 0, this periodically
6
tops it back up to 0.
7
*/
8
9
import getAccountId from "lib/account/get-account";
10
import { createStripeUsageBasedSubscription } from "@cocalc/server/purchases/stripe-usage-based-subscription";
11
import getParams from "lib/api/get-params";
12
13
export default async function handle(req, res) {
14
try {
15
res.json(await get(req));
16
} catch (err) {
17
res.json({ error: `${err.message}` });
18
return;
19
}
20
}
21
22
async function get(req) {
23
const account_id = await getAccountId(req);
24
if (account_id == null) {
25
throw Error("must be signed in");
26
}
27
const { success_url, cancel_url } = getParams(req);
28
return await createStripeUsageBasedSubscription({
29
account_id,
30
success_url,
31
cancel_url,
32
});
33
}
34
35