Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/purchases/create-refund.ts
Views: 687
/*1Refund a transaction.23This is ONLY allowed for admins to refund some or all of a credit that was4created by any users. Users can't directly get refunds -- they must5go through support, in order to avoid abuse.67This API creates a refund object in stripe *and* creates a new transaction8(entry in our purchases table) for that refund, reducing the amount of the9customer's balance. It's possible it could reduce a balance below 0, in which10case the customer would have to add significant money before making a purchase.1112We may create another different admin API call for canceling/refunding internal13transactions, if that turns out to be necessary.1415- purchase_id - id of some purchase in the purchases table, so a positive integer16- reason - "duplicate", "fraudulent", "requested_by_customer" or "other" (same as in stripe)17- admount - positive floating point number in *dollars* (NOT cents like in stripe)18- notes - optional string; user DOES see this.19*/2021import getAccountId from "lib/account/get-account";22import getParams from "lib/api/get-params";23import userIsInGroup from "@cocalc/server/accounts/is-in-group";24import createRefund from "@cocalc/server/purchases/create-refund";2526export default async function handle(req, res) {27try {28res.json(await get(req));29} catch (err) {30res.json({ error: `${err.message}` });31return;32}33}3435async function get(req) {36const account_id = await getAccountId(req);37if (account_id == null) {38throw Error("must be signed in");39}40// This user MUST be an admin:41if (!(await userIsInGroup(account_id, "admin"))) {42throw Error("only admins can create refunds");43}4445const { purchase_id, reason, notes } = getParams(req);46return {47id: await createRefund({ account_id, purchase_id, reason, notes }),48};49}505152