From c10ffa7f4258193c080ff40012eb788270cf5d3f Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Fri, 13 Oct 2023 15:22:18 +0100 Subject: [PATCH] Made it so it is possible to buy a package --- package.json | 5 +- src/app/api/success/route.ts | 35 ++++ src/app/join/page.tsx | 140 ++++++++++++++ src/app/layout.tsx | 32 ++- src/app/success/page.tsx | 140 ++++++++++++++ yarn.lock | 364 ++++++++++++++++++++++++++++++++++- 6 files changed, 692 insertions(+), 24 deletions(-) create mode 100644 src/app/api/success/route.ts create mode 100644 src/app/join/page.tsx create mode 100644 src/app/success/page.tsx diff --git a/package.json b/package.json index 103e164..76ac192 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,13 @@ "dependencies": { "@stripe/react-stripe-js": "^2.3.1", "@stripe/stripe-js": "^2.1.7", + "axios": "^1.5.1", + "moment": "^2.29.4", "next": "13.5.4", "react": "^18", "react-dom": "^18", - "react-icons": "^4.11.0" + "react-icons": "^4.11.0", + "sharp": "^0.32.6" }, "devDependencies": { "@types/node": "^20", diff --git a/src/app/api/success/route.ts b/src/app/api/success/route.ts new file mode 100644 index 0000000..1ed5b4d --- /dev/null +++ b/src/app/api/success/route.ts @@ -0,0 +1,35 @@ +import axios from "axios"; +import moment from "moment"; + +export async function POST(request: Request) { + const {id, product} = (await request.json()) as {id: string; product: string}; + + if (!id || !product) { + return Response.json({error: "Product and/or id missing from the body."}, {status: 400}); + } + + try { + const idChecker = await axios.get(`https://api.stripe.com/v1/checkout/sessions/${id}`, { + auth: {username: process.env.STRIPE_SECRET || "", password: ""}, + }); + + if (idChecker.data["payment_status"] !== "paid") { + return Response.json({error: "The payment has not yet finished!"}, {status: 401}); + } + + const productChecker = await axios.get(`https://api.stripe.com/v1/products/${product}`, { + auth: {username: process.env.STRIPE_SECRET || "", password: ""}, + }); + + const email = idChecker.data["customer_details"]["email"]; + + const days = productChecker.data["metadata"]["expiry_days"]; + const expiryDate = moment(new Date()).add(days, "days").toDate(); + + await axios.post("https://encoach.com/api/stripe", {expiryDate, email, key: process.env.STRIPE_SECRET, checkout: id}); + + return Response.json({error: null, ok: true}); + } catch (e) { + return Response.json({error: "Something went wrong or one of the values is invalid!"}, {status: 500}); + } +} diff --git a/src/app/join/page.tsx b/src/app/join/page.tsx new file mode 100644 index 0000000..af3ffe1 --- /dev/null +++ b/src/app/join/page.tsx @@ -0,0 +1,140 @@ +"use client"; + +/* eslint-disable @next/next/no-img-element */ +import Image from "next/image"; +import Link from "next/link"; +import { + BsBookFill, + BsCardChecklist, + BsClipboardFill, + BsClock, + BsClockFill, + BsFacebook, + BsFillBookFill, + BsInstagram, + BsSearch, + BsShieldFillCheck, + BsTwitter, +} from "react-icons/bs"; +import {BiLogoFacebook} from "react-icons/bi"; +import {PaymentElement} from "@stripe/react-stripe-js"; +import {Elements} from "@stripe/react-stripe-js"; +import {loadStripe} from "@stripe/stripe-js"; + +const stripePromise = loadStripe("pk_test_51NzD5xFI67mXFum2XDMXiLu89SbCAMY5O0RnKjlU6XqyfboRVvFHI3f5OJHaxsrjjB7WqDYqN7Y3eF8mq3sF354F00l30L5GuJ"); + +export default function Home() { + return ( + +