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/get-payment-method.ts
Views: 791
1
import getAccountId from "lib/account/get-account";
2
import { getPaymentMethod } from "@cocalc/server/purchases/stripe/get-payment-methods";
3
import throttle from "@cocalc/util/api/throttle";
4
import getParams from "lib/api/get-params";
5
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
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 account_id = await getAccountId(req);
18
if (account_id == null) {
19
throw Error("must be signed in");
20
}
21
throttle({ account_id, endpoint: "purchases/stripe/get-payment-method" });
22
23
const { user_account_id, id } = getParams(req);
24
if (user_account_id) {
25
// This user MUST be an admin:
26
if (!(await userIsInGroup(account_id, "admin"))) {
27
throw Error("only admins can get other user's payment methods");
28
}
29
return await getPaymentMethod({
30
account_id: user_account_id,
31
id,
32
});
33
}
34
35
return await getPaymentMethod({
36
account_id,
37
id,
38
});
39
}
40
41