Loading…
"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { useAuth } from "@/context/AuthContext"; import { useCart } from "@/context/CartContext"; import { loadStripe } from "@stripe/stripe-js"; import { Elements, PaymentElement, useStripe, useElements, } from "@stripe/react-stripe-js"; import { apiCompleteCheckout } from "@/lib/cartApi"; //Initializes Stripe with the publishable key const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY || ""); //Stripe payment form shown after the user clicks checkout function PaymentForm({ clientSecret, totalAmount, onSuccess, onCancel }) { const stripe = useStripe(); const elements = useElements(); const { token } = useAuth(); const [paying, setPaying] = useState(false); const [payError, setPayError] = useState(null); //Confirms the payment with Stripe, then tells the backend to complete the order async function handlePay(e) { e.preventDefault(); if (!stripe || !elements) return; setPaying(true); setPayError(null); const { error } = await stripe.confirmPayment({ elements, confirmParams: { return_url: window.location.origin + "/cart/confirmation" }, redirect: "if_required", }); if (error) { setPayError(error.message); setPaying(false); } else { const paymentIntentId = clientSecret.split("_secret_")[0]; try { await apiCompleteCheckout(token, paymentIntentId); } catch { setPayError("Order confirmation failed. Please contact support."); setPaying(false); return; } onSuccess(); } } return (
Total to pay: ${parseFloat(totalAmount).toFixed(2)}
{payError}
}Loading…
Thank you for your purchase. Your order has been placed successfully.
Please select a store from the navigation bar to view your cart.
{cartError}
} {items.length === 0 && !cartError && (Your cart is empty.
{item.prodName}
${parseFloat(item.unitPrice).toFixed(2)} each
${(parseFloat(item.unitPrice) * (localQuantities[item.cartItemId] ?? item.quantity)).toFixed(2)}