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. Commercial Alternative to JupyterHub.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/purchases/stripe/create-payment-intent.ts
Views: 791
1
import getAccountId from "lib/account/get-account";
2
import createPaymentIntent from "@cocalc/server/purchases/stripe/create-payment-intent";
3
import getParams from "lib/api/get-params";
4
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
5
import throttle from "@cocalc/util/api/throttle";
6
7
export default async function handle(req, res) {
8
try {
9
res.json(await get(req));
10
} catch (err) {
11
res.json({ error: `${err.message}` });
12
return;
13
}
14
}
15
16
async function get(req) {
17
const { user_account_id, lineItems, purpose, description, metadata } =
18
getParams(req);
19
if (user_account_id) {
20
// admin version
21
const admin_account_id = await getAccountId(req);
22
if (admin_account_id == null) {
23
throw Error("must be signed in");
24
}
25
throttle({
26
account_id: admin_account_id,
27
endpoint: "purchases/stripe/create-payment-intent",
28
});
29
if (!(await userIsInGroup(admin_account_id, "admin"))) {
30
throw Error("only admins can create a payment");
31
}
32
await createPaymentIntent({
33
account_id: user_account_id,
34
lineItems,
35
description,
36
purpose,
37
metadata: { ...metadata, admin_account_id },
38
});
39
} else {
40
const account_id = await getAccountId(req);
41
if (account_id == null) {
42
throw Error("must be signed in");
43
}
44
throttle({
45
account_id,
46
endpoint: "purchases/stripe/create-payment-intent",
47
});
48
await createPaymentIntent({
49
account_id,
50
description,
51
lineItems,
52
purpose,
53
metadata,
54
});
55
}
56
return { success: true };
57
}
58
59