Added integration for paypal payments
This commit is contained in:
23
src/hooks/usePaypalPayments.tsx
Normal file
23
src/hooks/usePaypalPayments.tsx
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import {PaypalPayment} from "@/interfaces/paypal";
|
||||||
|
import axios from "axios";
|
||||||
|
import {useEffect, useState} from "react";
|
||||||
|
|
||||||
|
export default function usePaypalPayments() {
|
||||||
|
const [payments, setPayments] = useState<PaypalPayment[]>([]);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [isError, setIsError] = useState(false);
|
||||||
|
|
||||||
|
const getData = () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
axios
|
||||||
|
.get<PaypalPayment[]>("/api/payments/paypal")
|
||||||
|
.then((response) => {
|
||||||
|
return setPayments(response.data);
|
||||||
|
})
|
||||||
|
.finally(() => setIsLoading(false));
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(getData, []);
|
||||||
|
|
||||||
|
return {payments, isLoading, isError, reload: getData};
|
||||||
|
}
|
||||||
@@ -35,3 +35,16 @@ export interface Payment {
|
|||||||
corporateTransfer?: string;
|
corporateTransfer?: string;
|
||||||
commissionTransfer?: string;
|
commissionTransfer?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export interface PaypalPayment {
|
||||||
|
orderId: string;
|
||||||
|
userId: string;
|
||||||
|
status: string;
|
||||||
|
createdAt: Date;
|
||||||
|
value: number;
|
||||||
|
currency: string;
|
||||||
|
subscriptionDuration: number;
|
||||||
|
subscriptionDurationUnit: DurationUnit;
|
||||||
|
subscriptionExpirationDate: Date;
|
||||||
|
}
|
||||||
30
src/pages/api/payments/paypal.ts
Normal file
30
src/pages/api/payments/paypal.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
import { app } from "@/firebase";
|
||||||
|
import {
|
||||||
|
getFirestore,
|
||||||
|
getDocs,
|
||||||
|
collection,
|
||||||
|
} from "firebase/firestore";
|
||||||
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
|
import { sessionOptions } from "@/lib/session";
|
||||||
|
|
||||||
|
const db = getFirestore(app);
|
||||||
|
|
||||||
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const payments = await getDocs(collection(db, "paypalpayments"));
|
||||||
|
|
||||||
|
const data = payments.docs.map((doc) => doc.data());
|
||||||
|
res.status(200).json(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -53,6 +53,25 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
{merge: true},
|
{merge: true},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await setDoc(
|
||||||
|
doc(db, 'paypalpayments', v4()),
|
||||||
|
{
|
||||||
|
orderId: id,
|
||||||
|
userId: req.session.user.id,
|
||||||
|
status: request.data.status,
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
value: request.data.purchase_units[0].payments.captures[0].amount.value,
|
||||||
|
currency: request.data.purchase_units[0].payments.captures[0].amount.currency_code,
|
||||||
|
subscriptionDuration: duration,
|
||||||
|
subscriptionDurationUnit: duration_unit,
|
||||||
|
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch(err) {
|
||||||
|
console.error('Failed to insert paypal payment!', err);
|
||||||
|
}
|
||||||
|
|
||||||
if (user.type === "corporate") {
|
if (user.type === "corporate") {
|
||||||
const snapshot = await getDocs(collection(db, "groups"));
|
const snapshot = await getDocs(collection(db, "groups"));
|
||||||
const groups: Group[] = (
|
const groups: Group[] = (
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user