51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
import type {NextApiRequest, NextApiResponse} from "next";
|
|
import client from "@/lib/mongodb";
|
|
import {withIronSessionApiRoute} from "iron-session/next";
|
|
import {sessionOptions} from "@/lib/session";
|
|
import {Group} from "@/interfaces/user";
|
|
import {Payment} from "@/interfaces/paypal";
|
|
import {v4} from "uuid";
|
|
import ShortUniqueId from "short-unique-id";
|
|
import axios from "axios";
|
|
import {IntentionResult, PaymentIntention} from "@/interfaces/paymob";
|
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (!req.session.user) {
|
|
res.status(401).json({ok: false});
|
|
return;
|
|
}
|
|
|
|
if (req.method === "GET") await get(req, res);
|
|
if (req.method === "POST") await post(req, res);
|
|
}
|
|
|
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
|
const snapshot = await db.collection("payments").find().toArray();
|
|
res.status(200).json(snapshot);
|
|
}
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
const intention = req.body as PaymentIntention;
|
|
|
|
const response = await axios.post<IntentionResult>(
|
|
"https://oman.paymob.com/v1/intention/",
|
|
{
|
|
...intention,
|
|
payment_methods: [parseInt(process.env.PAYMOB_INTEGRATION_ID || "0")],
|
|
items: [],
|
|
extras: {...intention.extras, userID: req.session.user!.id},
|
|
} as PaymentIntention,
|
|
{headers: {Authorization: `Token ${process.env.PAYMOB_SECRET_KEY}`}},
|
|
);
|
|
const intentionResult = response.data;
|
|
|
|
res.status(200).json({
|
|
iframeURL: `https://oman.paymob.com/unifiedcheckout/?publicKey=${process.env.PAYMOB_PUBLIC_KEY}&clientSecret=${intentionResult.client_secret}`,
|
|
});
|
|
}
|