Started implementing the Paymob integration
This commit is contained in:
101
src/pages/api/paymob.ts
Normal file
101
src/pages/api/paymob.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, collection, getDocs, setDoc, doc} from "firebase/firestore";
|
||||
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";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
interface BillingData {
|
||||
apartment: string;
|
||||
email: string;
|
||||
floor: string;
|
||||
first_name: string;
|
||||
street: string;
|
||||
building: string;
|
||||
phone_number: string;
|
||||
shipping_method: string;
|
||||
postal_code: string;
|
||||
city: string;
|
||||
country: string;
|
||||
last_name: string;
|
||||
state: string;
|
||||
}
|
||||
|
||||
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 getDocs(collection(db, "payments"));
|
||||
|
||||
res.status(200).json(
|
||||
snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const body = req.body as Payment;
|
||||
|
||||
const shortUID = new ShortUniqueId();
|
||||
await setDoc(doc(db, "payments", shortUID.randomUUID(8)), body);
|
||||
res.status(200).json({ok: true});
|
||||
}
|
||||
|
||||
const authenticatePaymob = async () => {
|
||||
const response = await axios.post<{token: string}>(
|
||||
"https://oman.paymob.com/api/auth/tokens",
|
||||
{
|
||||
api_key: process.env.PAYMOB_API_KEY,
|
||||
},
|
||||
{headers: {Authorization: `Bearer ${process.env.PAYMOB_SECRET_KEY}`}},
|
||||
);
|
||||
|
||||
return response.data.token;
|
||||
};
|
||||
|
||||
const createOrder = async (token: string) => {
|
||||
const response = await axios.post<{id: number}>(
|
||||
"https://oman.paymob.com/api/ecommerce/orders",
|
||||
{auth_token: token, delivery_needed: "false", currency: "OMR", amount_cents: "100", items: []},
|
||||
{headers: {Authorization: `Bearer ${token}`}},
|
||||
);
|
||||
|
||||
return response.data.id;
|
||||
};
|
||||
|
||||
const createTransactionIFrame = async (token: string, orderID: number, billingData: BillingData) => {
|
||||
const response = await axios.post<{token: string}>(
|
||||
"https://oman.paymob.com/api/acceptance/payment_keys",
|
||||
{
|
||||
auth_token: token,
|
||||
amount_cents: "100",
|
||||
order_id: orderID,
|
||||
currency: "OMR",
|
||||
expiration: 3600,
|
||||
integration_id: 1540,
|
||||
lock_order_when_paid: "true",
|
||||
billing_data: billingData,
|
||||
},
|
||||
{headers: {Authorization: `Bearer ${token}`}},
|
||||
);
|
||||
|
||||
return response.data.token;
|
||||
};
|
||||
Reference in New Issue
Block a user