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