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/billing/set-default-source.ts
Views: 687
1
/*
2
Set default payment source for signed in customer.
3
*/
4
5
import setDefaultSource from "@cocalc/server/billing/set-default-source";
6
import getAccountId from "lib/account/get-account";
7
import getParams from "lib/api/get-params";
8
9
export default async function handle(req, res) {
10
try {
11
res.json(await set(req));
12
} catch (err) {
13
res.json({ error: `${err.message}` });
14
return;
15
}
16
}
17
18
async function set(req): Promise<{ success: true }> {
19
const account_id = await getAccountId(req);
20
if (account_id == null) {
21
throw Error("must be signed in to set stripe default card");
22
}
23
const { default_source } = getParams(req);
24
if (!default_source) {
25
throw Error("must specify the default source");
26
}
27
await setDefaultSource(account_id, default_source);
28
return { success: true };
29
}
30
31