first commit
This commit is contained in:
58
calcom/packages/app-store/stripepayment/api/callback.ts
Normal file
58
calcom/packages/app-store/stripepayment/api/callback.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { stringify } from "querystring";
|
||||
|
||||
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
||||
import createOAuthAppCredential from "../../_utils/oauth/createOAuthAppCredential";
|
||||
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
|
||||
import type { StripeData } from "../lib/server";
|
||||
import stripe from "../lib/server";
|
||||
|
||||
function getReturnToValueFromQueryState(req: NextApiRequest) {
|
||||
let returnTo = "";
|
||||
try {
|
||||
returnTo = JSON.parse(`${req.query.state}`).returnTo;
|
||||
} catch (error) {
|
||||
console.info("No 'returnTo' in req.query.state");
|
||||
}
|
||||
return returnTo;
|
||||
}
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { code, error, error_description } = req.query;
|
||||
const state = decodeOAuthState(req);
|
||||
|
||||
if (error) {
|
||||
// User cancels flow
|
||||
if (error === "access_denied") {
|
||||
state?.onErrorReturnTo ? res.redirect(state.onErrorReturnTo) : res.redirect("/apps/installed/payment");
|
||||
}
|
||||
const query = stringify({ error, error_description });
|
||||
res.redirect(`/apps/installed?${query}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.session?.user?.id) {
|
||||
return res.status(401).json({ message: "You must be logged in to do this" });
|
||||
}
|
||||
|
||||
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 createOAuthAppCredential(
|
||||
{ appId: "stripe", type: "stripe_payment" },
|
||||
data as unknown as Prisma.InputJsonObject,
|
||||
req
|
||||
);
|
||||
|
||||
const returnTo = getReturnToValueFromQueryState(req);
|
||||
res.redirect(returnTo || getInstalledAppPath({ variant: "payment", slug: "stripe" }));
|
||||
}
|
||||
Reference in New Issue
Block a user