"use client"; import { useEffect, useState, useRef } from "react"; import Link from "next/link"; import { useCart } from "@/components/cart/CartContext"; import type { MedusaOrder } from "@/lib/medusa"; function formatPrice(amount: number, currencyCode: string) { return new Intl.NumberFormat("en-US", { style: "currency", currency: currencyCode, minimumFractionDigits: 2, }).format(amount); } interface DownloadItem { product_title: string; variant_title: string; sku: string; download_url: string; } interface UpcomingItem { product_title: string; variant_title: string; sku: string; release_date: string; } export default function CheckoutReturnPage() { const { cart, resetCart } = useCart(); const [order, setOrder] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const [downloads, setDownloads] = useState([]); const [upcoming, setUpcoming] = useState([]); const attempted = useRef(false); useEffect(() => { if (attempted.current) return; if (!cart?.id) { // Cart context still loading — wait return; } attempted.current = true; async function completeOrder() { try { const res = await fetch("/api/checkout/complete", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ cartId: cart!.id }), }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error ?? "Failed to complete order"); } const result = await res.json(); if (result.type === "order" && result.order) { setOrder(result.order); resetCart(); // Fetch download links for digital items try { const dlRes = await fetch( `/api/checkout/downloads?orderId=${result.order.id}`, ); if (dlRes.ok) { const dlData = await dlRes.json(); if (dlData.downloads?.length) setDownloads(dlData.downloads); if (dlData.upcoming?.length) setUpcoming(dlData.upcoming); } } catch { // Download links are non-critical — don't block the page } } else { throw new Error("Payment was not completed. Please try again."); } } catch (err) { setError(err instanceof Error ? err.message : "Something went wrong"); } finally { setLoading(false); } } completeOrder(); }, [cart?.id, resetCart]); if (loading) { return (

Completing your order…

); } if (error) { return (

Something went wrong

{error}

Try Again Go Home
); } if (!order) return null; const currencyCode = order.currency_code ?? "eur"; return (

Thank you!

Your order has been confirmed.

Order number #{order.display_id}
{order.email && (
Confirmation sent to {order.email}
)}
Subtotal {formatPrice(order.subtotal, currencyCode)}
{order.shipping_total > 0 && (
Shipping {formatPrice(order.shipping_total, currencyCode)}
)} {order.tax_total > 0 && (
Tax {formatPrice(order.tax_total, currencyCode)}
)}
Total {formatPrice(order.total, currencyCode)}
{/* Download links for digital purchases */} {downloads.length > 0 && (

Your Downloads

{downloads.map((dl) => (

{dl.product_title}

{dl.variant_title}

Download
))}

Download links expire after 5 minutes. A copy has been sent to your email.

)} {/* Pre-order items not yet available */} {upcoming.length > 0 && (

Upcoming Releases

{upcoming.map((item) => (

{item.product_title}

{item.variant_title}

Available {new Date(item.release_date).toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric", })}
))}

You'll receive download links by email when these releases become available.

)}
Continue Shopping
); }