Created a route for the Stripe webhook
This commit is contained in:
@@ -55,6 +55,7 @@
|
|||||||
"react-toastify": "^9.1.2",
|
"react-toastify": "^9.1.2",
|
||||||
"react-xarrows": "^2.0.2",
|
"react-xarrows": "^2.0.2",
|
||||||
"short-unique-id": "^5.0.2",
|
"short-unique-id": "^5.0.2",
|
||||||
|
"stripe": "^13.10.0",
|
||||||
"swr": "^2.1.3",
|
"swr": "^2.1.3",
|
||||||
"tailwind-scrollbar-hide": "^1.1.7",
|
"tailwind-scrollbar-hide": "^1.1.7",
|
||||||
"typescript": "4.9.5",
|
"typescript": "4.9.5",
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const codeData = codeDocs[0].data() as {code: string; type: Type; creator: string; expiryDate: Date | null};
|
const codeData = codeDocs[0].data() as {code: string; type: Type; creator?: string; expiryDate: Date | null};
|
||||||
|
|
||||||
createUserWithEmailAndPassword(auth, email, password)
|
createUserWithEmailAndPassword(auth, email, password)
|
||||||
.then(async (userCredentials) => {
|
.then(async (userCredentials) => {
|
||||||
@@ -57,7 +57,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
await setDoc(doc(db, "users", userId), user);
|
await setDoc(doc(db, "users", userId), user);
|
||||||
await setDoc(codeDocs[0].ref, {userId: userId}, {merge: true});
|
await setDoc(codeDocs[0].ref, {userId: userId}, {merge: true});
|
||||||
await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
|
if (codeData.creator) await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
|
||||||
|
|
||||||
req.session.user = {...user, id: userId};
|
req.session.user = {...user, id: userId};
|
||||||
await req.session.save();
|
await req.session.save();
|
||||||
|
|||||||
43
src/pages/api/stripe.ts
Normal file
43
src/pages/api/stripe.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
|
import {app} from "@/firebase";
|
||||||
|
import {getFirestore, setDoc, doc} from "firebase/firestore";
|
||||||
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
|
import {sessionOptions} from "@/lib/session";
|
||||||
|
import {Type} from "@/interfaces/user";
|
||||||
|
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||||
|
import {uuidv4} from "@firebase/util";
|
||||||
|
import {prepareMailer, prepareMailOptions} from "@/email";
|
||||||
|
import * as Stripe from "stripe";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
|
||||||
|
const db = getFirestore(app);
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const {email, expiryDate, key} = req.body as {email: string; expiryDate: Date; key: string};
|
||||||
|
if (!key || key !== process.env.STRIPE_KEY) {
|
||||||
|
res.status(403).json({ok: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uid = new ShortUniqueId();
|
||||||
|
const code = uid.randomUUID(6);
|
||||||
|
|
||||||
|
const codeRef = doc(db, "codes", code);
|
||||||
|
await setDoc(codeRef, {type: "student", code, expiryDate});
|
||||||
|
|
||||||
|
const transport = prepareMailer();
|
||||||
|
const mailOptions = prepareMailOptions(
|
||||||
|
{
|
||||||
|
type: "student",
|
||||||
|
code,
|
||||||
|
},
|
||||||
|
[email],
|
||||||
|
"EnCoach Registration",
|
||||||
|
"main",
|
||||||
|
);
|
||||||
|
|
||||||
|
await transport.sendMail(mailOptions);
|
||||||
|
|
||||||
|
res.status(200).json({ok: true});
|
||||||
|
}
|
||||||
27
yarn.lock
27
yarn.lock
@@ -1203,6 +1203,13 @@
|
|||||||
resolved "https://registry.npmjs.org/@types/node/-/node-18.13.0.tgz"
|
resolved "https://registry.npmjs.org/@types/node/-/node-18.13.0.tgz"
|
||||||
integrity sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==
|
integrity sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==
|
||||||
|
|
||||||
|
"@types/node@>=8.1.0":
|
||||||
|
version "20.8.5"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.5.tgz#13352ae1f80032171616910e8aba2e3e52e57d96"
|
||||||
|
integrity sha512-SPlobFgbidfIeOYlzXiEjSYeIJiOCthv+9tSQVpvk4PAdIIc+2SmjNVzWXk9t0Y7dl73Zdf+OgXKHX9XtkqUpw==
|
||||||
|
dependencies:
|
||||||
|
undici-types "~5.25.1"
|
||||||
|
|
||||||
"@types/node@^17.0.41":
|
"@types/node@^17.0.41":
|
||||||
version "17.0.45"
|
version "17.0.45"
|
||||||
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz"
|
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz"
|
||||||
@@ -4390,6 +4397,13 @@ pvutils@^1.1.3:
|
|||||||
resolved "https://registry.npmjs.org/pvutils/-/pvutils-1.1.3.tgz"
|
resolved "https://registry.npmjs.org/pvutils/-/pvutils-1.1.3.tgz"
|
||||||
integrity sha512-pMpnA0qRdFp32b1sJl1wOJNxZLQ2cbQx+k6tjNtZ8CpvVhNqEPRgivZ2WOUev2YMajecdH7ctUPDvEe87nariQ==
|
integrity sha512-pMpnA0qRdFp32b1sJl1wOJNxZLQ2cbQx+k6tjNtZ8CpvVhNqEPRgivZ2WOUev2YMajecdH7ctUPDvEe87nariQ==
|
||||||
|
|
||||||
|
qs@^6.11.0:
|
||||||
|
version "6.11.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.2.tgz#64bea51f12c1f5da1bc01496f48ffcff7c69d7d9"
|
||||||
|
integrity sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==
|
||||||
|
dependencies:
|
||||||
|
side-channel "^1.0.4"
|
||||||
|
|
||||||
queue-microtask@^1.2.2:
|
queue-microtask@^1.2.2:
|
||||||
version "1.2.3"
|
version "1.2.3"
|
||||||
resolved "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz"
|
resolved "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz"
|
||||||
@@ -4904,6 +4918,14 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1:
|
|||||||
resolved "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz"
|
resolved "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz"
|
||||||
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
|
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
|
||||||
|
|
||||||
|
stripe@^13.10.0:
|
||||||
|
version "13.10.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/stripe/-/stripe-13.10.0.tgz#054b8c036f84a5cb33787e214eac12ab799ebd42"
|
||||||
|
integrity sha512-8UGpNdM7oxjhqnNQB6/ouhT9reM80+A6a+NScg3rwzK2f0W/q8M+MdUivG1hL6wAdqF8DE8K2IbdzwwJCZ4WPw==
|
||||||
|
dependencies:
|
||||||
|
"@types/node" ">=8.1.0"
|
||||||
|
qs "^6.11.0"
|
||||||
|
|
||||||
strnum@^1.0.5:
|
strnum@^1.0.5:
|
||||||
version "1.0.5"
|
version "1.0.5"
|
||||||
resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db"
|
resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db"
|
||||||
@@ -5187,6 +5209,11 @@ underscore@~1.13.2:
|
|||||||
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.6.tgz#04786a1f589dc6c09f761fc5f45b89e935136441"
|
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.6.tgz#04786a1f589dc6c09f761fc5f45b89e935136441"
|
||||||
integrity sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==
|
integrity sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==
|
||||||
|
|
||||||
|
undici-types@~5.25.1:
|
||||||
|
version "5.25.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.25.3.tgz#e044115914c85f0bcbb229f346ab739f064998c3"
|
||||||
|
integrity sha512-Ga1jfYwRn7+cP9v8auvEXN1rX3sWqlayd4HP7OKk4mZWylEmu3KzXDUGrQUN6Ol7qo1gPvB2e5gX6udnyEPgdA==
|
||||||
|
|
||||||
update-browserslist-db@^1.0.10:
|
update-browserslist-db@^1.0.10:
|
||||||
version "1.0.11"
|
version "1.0.11"
|
||||||
resolved "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.11.tgz"
|
resolved "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.11.tgz"
|
||||||
|
|||||||
Reference in New Issue
Block a user