diff --git a/donutshop_ecommerce/public/check.png b/donutshop_ecommerce/public/check.png new file mode 100644 index 0000000..9d5a0be Binary files /dev/null and b/donutshop_ecommerce/public/check.png differ diff --git a/donutshop_ecommerce/public/empty.png b/donutshop_ecommerce/public/empty.png new file mode 100644 index 0000000..6bc9907 Binary files /dev/null and b/donutshop_ecommerce/public/empty.png differ diff --git a/donutshop_ecommerce/src/app/api/checkout/route.js b/donutshop_ecommerce/src/app/api/checkout/route.js index a0c35eb..14c515e 100644 --- a/donutshop_ecommerce/src/app/api/checkout/route.js +++ b/donutshop_ecommerce/src/app/api/checkout/route.js @@ -56,9 +56,12 @@ export async function POST(req){ line_items: stripeLineItems, mode: 'payment', customer_email: userEmail, - success_url: process.env.NEXTAUTH_URL + 'cart?success=1', + success_url: process.env.NEXTAUTH_URL + 'orders/' + orderDoc._id.toString() + '?clear-cart=1', cancel_url: process.env.NEXTAUTH_URL + 'cart?canceled=1', metadata: {orderId: orderDoc._id.toString()}, + payment_intent_data: { + metadata:{orderId:orderDoc._id.toString()}, + }, shipping_options: [ { shipping_rate_data: { diff --git a/donutshop_ecommerce/src/app/api/webhook/route.js b/donutshop_ecommerce/src/app/api/webhook/route.js index bf029e7..a989db9 100644 --- a/donutshop_ecommerce/src/app/api/webhook/route.js +++ b/donutshop_ecommerce/src/app/api/webhook/route.js @@ -6,8 +6,20 @@ export async function POST(req){ let event; try { - + const reqBuffer = await req.text(); + const signSecret = process.env.STRIPE_SIGN_SECRET + event = stripe.webhooks.constructEvent(reqBuffer, sig, signSecret); } catch (error) { - + return Response.json(error, {status: 400}); } + + if(event.type === 'checkout.session.completed'){ + const orderId = event?.data?.object?.metadata?.orderId; + const isPaid = event?.data?.object?.payment_status === 'paid'; + if (isPaid) { + await Order.updateOne({_id:orderId}, {paid:true}); + } + } + + return Response.json('ok', {status: 200}) } \ No newline at end of file diff --git a/donutshop_ecommerce/src/app/cart/page.js b/donutshop_ecommerce/src/app/cart/page.js index 2851255..3bfc654 100644 --- a/donutshop_ecommerce/src/app/cart/page.js +++ b/donutshop_ecommerce/src/app/cart/page.js @@ -5,12 +5,24 @@ import Image from 'next/image'; import { IoMdCloseCircleOutline } from "react-icons/io"; import AddressInfo from '../../components/layout/AddressInfo'; import useProfile from '../../components/UseProfile'; +import toast from 'react-hot-toast'; +import Link from 'next/link'; const CartPage = () => { const {cartProducts, removeCartProduct} = useContext(CartContext); const [address, setAddress] = useState({}) const {data:profileCheckoutData} = useProfile() + + + useEffect(() => { + if(typeof window !== 'undefined'){ + if(window.location.href.includes('canceled=1')){ + toast.error('Fail to make a payment!') + } + } + }, []) + useEffect(() => { if(profileCheckoutData?.city){ const {phoneNumber, streetAddress, city, stateProvince, zipCode, country} = profileCheckoutData; @@ -44,7 +56,21 @@ const CartPage = () => { console.log({cartProducts}) - + if(cartProducts?.length === 0){ + return( +
+

My cart

+
+ empty-cart +
+

Empty Menu

+

Look like you have not made your choice yet...

+ + + +
+ ) + } return (
@@ -53,7 +79,7 @@ const CartPage = () => {
{cartProducts?.length === 0 && ( -
Your cart is empty
+

Your cart is empty

)} {cartProducts?.length > 0 && cartProducts.map((product, index) => (
diff --git a/donutshop_ecommerce/src/app/orders/[id]/page.js b/donutshop_ecommerce/src/app/orders/[id]/page.js new file mode 100644 index 0000000..4cb459b --- /dev/null +++ b/donutshop_ecommerce/src/app/orders/[id]/page.js @@ -0,0 +1,32 @@ +"use client" +import Image from 'next/image' +import React, { useContext, useEffect } from 'react' +import { CartContext } from '../../../components/AppContext' + +const OrdersPage = () => { + const {clearCart} = useContext(CartContext); + + useEffect(() => { + if(typeof window !== 'undefined'){ + if(window.location.href.includes('clear-cart=1')){ + clearCart(); + } + } + }, []) + + + return ( +
+
+
+ checked +

Thank you

+

Your order is confirmed

+

We will be sending you an email confirmation to your email shortly

+
+
+
+ ) +} + +export default OrdersPage \ No newline at end of file