Updated the Stripe webhook to work better
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {app} from "@/firebase";
|
import {app} from "@/firebase";
|
||||||
import {getFirestore, setDoc, doc} from "firebase/firestore";
|
import {getFirestore, setDoc, doc, getDocs, query, collection, where} from "firebase/firestore";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {Type} from "@/interfaces/user";
|
import {Type} from "@/interfaces/user";
|
||||||
@@ -14,7 +14,7 @@ import ShortUniqueId from "short-unique-id";
|
|||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const {email, expiryDate, key} = req.body as {email: string; expiryDate: Date; key: string};
|
const {email, expiryDate, key, checkout} = req.body as {email: string; expiryDate: Date; key: string; checkout: string};
|
||||||
if (!key || key !== process.env.STRIPE_KEY) {
|
if (!key || key !== process.env.STRIPE_KEY) {
|
||||||
res.status(403).json({ok: false});
|
res.status(403).json({ok: false});
|
||||||
return;
|
return;
|
||||||
@@ -23,8 +23,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const uid = new ShortUniqueId();
|
const uid = new ShortUniqueId();
|
||||||
const code = uid.randomUUID(6);
|
const code = uid.randomUUID(6);
|
||||||
|
|
||||||
|
const codeCheckerRef = await getDocs(query(collection(db, "codes"), where("checkout", "==", checkout)));
|
||||||
|
if (codeCheckerRef.docs.length !== 0) {
|
||||||
|
res.status(401).json({ok: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const codeRef = doc(db, "codes", code);
|
const codeRef = doc(db, "codes", code);
|
||||||
await setDoc(codeRef, {type: "student", code, expiryDate});
|
await setDoc(codeRef, {type: "student", code, expiryDate, checkout});
|
||||||
|
|
||||||
const transport = prepareMailer();
|
const transport = prepareMailer();
|
||||||
const mailOptions = prepareMailOptions(
|
const mailOptions = prepareMailOptions(
|
||||||
|
|||||||
Reference in New Issue
Block a user