2021-09-22 18:36:13 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
2021-09-22 19:52:38 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-28 21:27:06 +00:00
|
|
|
import { stringify } from "querystring";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-09 22:56:05 +00:00
|
|
|
import stripe, { StripeData } from "@calcom/stripe/server";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-09-28 21:27:06 +00:00
|
|
|
const { code, error, error_description } = req.query;
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2021-09-28 21:27:06 +00:00
|
|
|
if (error) {
|
|
|
|
const query = stringify({ error, error_description });
|
2022-03-23 22:00:30 +00:00
|
|
|
res.redirect("/apps/installed?" + query);
|
2021-09-28 21:27:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
if (!req.session?.user?.id) {
|
|
|
|
return res.status(401).json({ message: "You must be logged in to do this" });
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
const response = await stripe.oauth.token({
|
|
|
|
grant_type: "authorization_code",
|
2022-07-12 17:43:53 +00:00
|
|
|
code: code!.toString(),
|
2021-09-22 18:36:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2022-05-02 20:39:35 +00:00
|
|
|
userId: req.session.user.id,
|
|
|
|
appId: "stripe",
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
res.redirect("/apps/installed");
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|