Path: blob/master/src/packages/next/pages/api/v2/purchases/stripe/create-payment-intent.ts
1456 views
import getAccountId from "lib/account/get-account";1import createPaymentIntent from "@cocalc/server/purchases/stripe/create-payment-intent";2import getParams from "lib/api/get-params";3import userIsInGroup from "@cocalc/server/accounts/is-in-group";4import throttle from "@cocalc/util/api/throttle";56export default async function handle(req, res) {7try {8res.json(await get(req));9} catch (err) {10res.json({ error: `${err.message}` });11return;12}13}1415async function get(req) {16const { user_account_id, lineItems, purpose, description, metadata } =17getParams(req);18if (user_account_id) {19// admin version20const admin_account_id = await getAccountId(req);21if (admin_account_id == null) {22throw Error("must be signed in");23}24throttle({25account_id: admin_account_id,26endpoint: "purchases/stripe/create-payment-intent",27});28if (!(await userIsInGroup(admin_account_id, "admin"))) {29throw Error("only admins can create a payment");30}31await createPaymentIntent({32account_id: user_account_id,33lineItems,34description,35purpose,36metadata: { ...metadata, admin_account_id },37});38} else {39const account_id = await getAccountId(req);40if (account_id == null) {41throw Error("must be signed in");42}43throttle({44account_id,45endpoint: "purchases/stripe/create-payment-intent",46});47await createPaymentIntent({48account_id,49description,50lineItems,51purpose,52metadata,53});54}55return { success: true };56}575859