cal.pub0.org/packages/app-store/stripepayment/api/callback.ts

38 lines
1.1 KiB
TypeScript

import { Prisma } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { stringify } from "querystring";
import prisma from "@calcom/prisma";
import stripe, { StripeData } from "@calcom/stripe/server";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { code, error, error_description } = req.query;
if (error) {
const query = stringify({ error, error_description });
res.redirect("/apps/installed?" + query);
return;
}
const response = await stripe.oauth.token({
grant_type: "authorization_code",
code: code.toString(),
});
const data: StripeData = { ...response, default_currency: "" };
if (response["stripe_user_id"]) {
const account = await stripe.accounts.retrieve(response["stripe_user_id"]);
data["default_currency"] = account.default_currency;
}
await prisma.credential.create({
data: {
type: "stripe_payment",
key: data as unknown as Prisma.InputJsonObject,
userId: req.session?.user.id,
},
});
res.redirect("/apps/installed");
}