Path: blob/master/src/packages/next/pages/api/v2/purchases/stripe/cancel-payment-intent.ts
1456 views
/*1An admin can cancel anybody's payment intent, whereas a user can only cancel their own.2*/34import getAccountId from "lib/account/get-account";5import {6cancelPaymentIntent,7getPaymentIntentAccountId,8} from "@cocalc/server/purchases/stripe/create-payment-intent";9import getParams from "lib/api/get-params";10import userIsInGroup from "@cocalc/server/accounts/is-in-group";11import throttle from "@cocalc/util/api/throttle";1213export default async function handle(req, res) {14try {15res.json(await get(req));16} catch (err) {17res.json({ error: `${err.message}` });18return;19}20}2122async function get(req) {23const account_id = await getAccountId(req);24if (account_id == null) {25throw Error("must be signed in");26}27throttle({28account_id,29endpoint: "purchases/stripe/cancel-payment-intent",30});31const { id, reason } = getParams(req);32const owner_id = await getPaymentIntentAccountId(id);33if (owner_id != account_id) {34if (!(await userIsInGroup(account_id, "admin"))) {35throw Error("only admins can cancel other user's payment intents");36}37}38await cancelPaymentIntent({ id, reason });39return { success: true };40}414243