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/purchases/create-refund.ts
Views: 687
1
/*
2
Refund a transaction.
3
4
This is ONLY allowed for admins to refund some or all of a credit that was
5
created by any users. Users can't directly get refunds -- they must
6
go through support, in order to avoid abuse.
7
8
This API creates a refund object in stripe *and* creates a new transaction
9
(entry in our purchases table) for that refund, reducing the amount of the
10
customer's balance. It's possible it could reduce a balance below 0, in which
11
case the customer would have to add significant money before making a purchase.
12
13
We may create another different admin API call for canceling/refunding internal
14
transactions, if that turns out to be necessary.
15
16
- purchase_id - id of some purchase in the purchases table, so a positive integer
17
- reason - "duplicate", "fraudulent", "requested_by_customer" or "other" (same as in stripe)
18
- admount - positive floating point number in *dollars* (NOT cents like in stripe)
19
- notes - optional string; user DOES see this.
20
*/
21
22
import getAccountId from "lib/account/get-account";
23
import getParams from "lib/api/get-params";
24
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
25
import createRefund from "@cocalc/server/purchases/create-refund";
26
27
export default async function handle(req, res) {
28
try {
29
res.json(await get(req));
30
} catch (err) {
31
res.json({ error: `${err.message}` });
32
return;
33
}
34
}
35
36
async function get(req) {
37
const account_id = await getAccountId(req);
38
if (account_id == null) {
39
throw Error("must be signed in");
40
}
41
// This user MUST be an admin:
42
if (!(await userIsInGroup(account_id, "admin"))) {
43
throw Error("only admins can create refunds");
44
}
45
46
const { purchase_id, reason, notes } = getParams(req);
47
return {
48
id: await createRefund({ account_id, purchase_id, reason, notes }),
49
};
50
}
51
52