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-methods.ts
Views: 791
1
import getAccountId from "lib/account/get-account";
2
import getPaymentMethods 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-methods" });
22
23
const { user_account_id, ending_before, starting_after, limit } =
24
getParams(req);
25
if (user_account_id) {
26
// This user MUST be an admin:
27
if (!(await userIsInGroup(account_id, "admin"))) {
28
throw Error("only admins can get other user's payment methods");
29
}
30
return await getPaymentMethods({
31
account_id: user_account_id,
32
ending_before,
33
starting_after,
34
limit,
35
});
36
}
37
38
return await getPaymentMethods({
39
account_id,
40
ending_before,
41
starting_after,
42
limit,
43
});
44
}
45
46