Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions app/checkout/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use client'
import React from "react";
import Navbar from '@/components/shop/Navbar';
import Footer from '@/components/shop/Footer';

import ShippingForm from "@/app/checkout/shippingForm";
import Image from "next/image";
import {PaymentOptions} from "@/app/checkout/paymentOptions";
import Link from "next/link";
import {Elements} from '@stripe/react-stripe-js';
import Cart from "./cart"
import {loadStripe} from '@stripe/stripe-js';

export default async function CheckoutPage() {
const stripePromise = loadStripe('STRIPE_API_KEY'); // replace this with the stripe API Key once we get one

export default async function CheckoutPage() {
return (
<div className="min-h-screen">
<main className="bg-white py-16 px-16">
Expand All @@ -20,7 +19,9 @@ export default async function CheckoutPage() {
<ShippingForm/>
</div>
<div className="p-8 border border-gray-200 bg-gray-100 rounded-lg m-8">
<PaymentOptions/>
<Elements stripe = {stripePromise}>
<PaymentOptions/>
</Elements>
</div>
</div>
<div className='flex-1'>
Expand Down
43 changes: 40 additions & 3 deletions app/checkout/paymentOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,54 @@
import Image from 'next/image';
import PayPalLogo from '@/public/img/PayPal.svg'
import React from "react";
import {useState} from 'react';
import { useStripe, useElements, CardElement } from '@stripe/react-stripe-js';


export function PaymentOptions() {
const stripe = useStripe();
const elements = useElements();
const [paymentProcessing, setPaymentProcessing] = useState(false);

// note that the backend route has been created using express, please let Umar know if there are any problems with this
async function handleSubmit(e: any) { // simple event handler function for submitting the form
e.preventDefault();
setPaymentProcessing(true);
const cardElement = elements.getElement(CardElement); // just ignore this error
const response = await fetch('/create-payment-intent', {
method: "POST",
headers: {"Content-Type" : "application/json"},
body: JSON.stringify({amount: 1000})
});

const {clientSecret} = await response.json();
const paymentResult = await stripe.confirmCardPayment(clientSecret, { // just ignore this error, this is to do with the fact that the variable is imported
payment_method: {card: cardElement} // ignore this error for the same reasons given above
});
setPaymentProcessing(false);
if (paymentResult.error) {
console.error(paymentResult.error);
} else {
console.log('Payment successful!');
};
};

return(
<>
<>
<p className="font-bold text-xl pb-2">Payment Options</p>
<button className="w-full bg-white border border-gray-200 h-10 rounded-md px-4 flex flex-row justify-center items-center">
<Image src={PayPalLogo} alt="PayPal" width={90} height={20} className="contain"/>
</button>
<button className="w-full bg-green-600 hover:bg-green-500 text-white font-bold h-10 rounded-lg py-2 px-4 mt-4">
Buy Now
</button>
<br />
<form onSubmit={handleSubmit}>
<CardElement />
<button type="submit" disabled={!stripe || paymentProcessing}>
{paymentProcessing ? "Processing..." : "Pay Now"}
</button>
</form>
</>
)
}
);
};
26 changes: 26 additions & 0 deletions app/checkout/stripeRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// custom API to create a payment intent for Stripe
// not too sure if this will work until we have access to the stripe key
// please test if you have that
const express = require('express');
const bodyParser = require('body-parser');
const stripe = require('stripe')('STRIPE_API_KEY'); // paste the api key here later
const app = express();

// post route to stripe
app.post('/create-payment-intent', async (req: { body: { amount: any; }; }, res: { send: (arg0: { clientSecret: any; }) => void; }) => {
const {amount} = req.body; // get the payment amount from the request's body

try {
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: 'cad',
automatic_payment_methods: {enabled: true}
});

res.send({clientSecret: paymentIntent.client_secret});
} catch (err) {
console.log(err);
}
});

app.listen(69, () => console.log('Server route started successfully')); // 69 = funny lol
Loading