"use client"; import { useEffect, Suspense, useState } from "react"; import { useSearchParams, useRouter } from "next/navigation"; import Link from "next/link"; import { createOrder } from "@/actions/checkout"; type OrderItem = { product_id: string; product_name: string; quantity: number; price: number; fulfillment: string; }; type StoredOrder = { id: string; customer_name: string; customer_email: string; subtotal: number; status: string; stop_city: string; stop_state: string; stop_date: string; stop_time: string | null; stop_location: string; items: OrderItem[]; }; function shortId(id: string) { return id.slice(0, 8).toUpperCase(); } function formatStopDate(dateStr: string): string { try { return new Date(dateStr + "T00:00:00").toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric", year: "numeric", }); } catch { return dateStr; } } function SuccessContent() { const searchParams = useSearchParams(); const router = useRouter(); const sessionId = searchParams.get("session_id"); const orderIdParam = searchParams.get("order_id"); const [order, setOrder] = useState(null); const [creating, setCreating] = useState(!!sessionId); const [error, setError] = useState(null); useEffect(() => { if (orderIdParam && !sessionId) { // Direct access with order_id — load from sessionStorage const stored = sessionStorage.getItem(`order_${orderIdParam}`); if (stored) { try { setOrder(JSON.parse(stored) as StoredOrder); } catch { // ignore } } } }, [orderIdParam, sessionId]); // Stripe redirected back — create order from pending checkout data useEffect(() => { if (!sessionId) return; const pendingStr = sessionStorage.getItem("pending_checkout"); if (!pendingStr) { setError("No pending order found. Please start checkout again."); setCreating(false); return; } let pending: { customerName: string; customerEmail: string; customerPhone: string; stopId: string | null; items: Array<{ id: string; name: string; price: number; quantity: number; fulfillment: "pickup" | "ship" }>; cartBrandId?: string; shippingAddress?: { state: string; postal_code: string; city?: string }; idempotencyKey: string; }; try { pending = JSON.parse(pendingStr); } catch { setError("Failed to read checkout data. Please start again."); setCreating(false); return; } createOrder( pending.idempotencyKey, pending.customerName, pending.customerEmail, pending.customerPhone, pending.stopId, pending.items, pending.cartBrandId, pending.shippingAddress ) .then((result) => { if (!result.success) { setError(result.error ?? "Failed to create order"); setCreating(false); return; } sessionStorage.setItem(`order_${result.order.id}`, JSON.stringify(result.order)); sessionStorage.removeItem("pending_checkout"); sessionStorage.removeItem("cart"); setOrder(result.order); setCreating(false); }) .catch(() => { setError("Failed to create order. Please contact support."); setCreating(false); }); }, [sessionId]); if (!order || !orderIdParam) { return (
{creating ? ( <>

Confirming your payment and creating order...

) : error ? ( <>

{error}

← Back to Storefront ) : ( <>

Order not found

← Back to Storefront )}
); } const pickupItems = order.items.filter((i) => i.fulfillment === "pickup"); const shipItems = order.items.filter((i) => i.fulfillment === "ship"); const hasPickup = pickupItems.length > 0; const hasShip = shipItems.length > 0; const isMixed = hasPickup && hasShip; const stopLabel = [order.stop_city, order.stop_state].filter(Boolean).join(", "); return (
{/* Header */}

{hasPickup ? "Your pickup order has been placed!" : "Your order has been placed!"}

Order #{shortId(order.id)} · {order.customer_name}

{/* Pickup details */} {hasPickup && (

Pickup Details

{stopLabel && (

{stopLabel}

)} {order.stop_date && (

{formatStopDate(order.stop_date)} {order.stop_time ? ` · ${order.stop_time}` : ""}

)} {order.stop_location && (

{order.stop_location}

)} {isMixed && (

See pickup items in the list below.

)} {!isMixed && (

Bring your order number or name when you pick up.

)}
)} {/* Shipping details */} {hasShip && (

Shipping

Tracking information will be sent when your order ships.

)} {/* Order items */}

Order Summary

{isMixed && hasPickup && (

Pickup Items

    {pickupItems.map((item, i) => (
  • {item.quantity > 1 && ( {item.quantity}× )} {item.product_name} ${(item.price * item.quantity).toFixed(2)}
  • ))}
)} {isMixed && hasShip && (

Shipping Items

    {shipItems.map((item, i) => (
  • {item.quantity > 1 && ( {item.quantity}× )} {item.product_name} ${(item.price * item.quantity).toFixed(2)}
  • ))}
)} {!isMixed && (
    {order.items.map((item, i) => (
  • {item.quantity > 1 && ( {item.quantity}× )} {item.product_name} ${(item.price * item.quantity).toFixed(2)}
  • ))}
)}
Total ${order.subtotal.toFixed(2)}
{/* Back link */}
Back to Storefront
); } export default function CheckoutSuccessPage() { return (
Loading...
}> ); }