Improve debug logging for checkout flow

Add emoji-prefixed logs to make debugging easier:
- 💳 Checkout button clicked
- ✓ Flag set/skipped actions
- 🚪 LogoutOnUnload handler
- 📡 API fetch
- 📦 API response
- 🔗 Stripe redirect
- 🗑️ Cart clearing

This makes it much easier to see the flow in the console.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Andymick
2026-07-18 06:59:59 +01:00
co-authored by Claude Haiku 4.5
parent 7434b68a79
commit eff9a72409
2 changed files with 9 additions and 6 deletions
+5 -3
View File
@@ -122,29 +122,31 @@ export default function CheckoutPageClient({ isLoggedIn, customer }: CheckoutPag
<p className="mt-2 text-sm text-muted">Logged in as {customer?.email}</p> <p className="mt-2 text-sm text-muted">Logged in as {customer?.email}</p>
<button <button
onClick={async () => { onClick={async () => {
console.log('💳 Checkout button clicked, cart items:', items.length);
setCheckingOut(true); setCheckingOut(true);
setCheckoutError(null); setCheckoutError(null);
try { try {
// Mark that we're checking out so LogoutOnUnload doesn't clear cart // Mark that we're checking out so LogoutOnUnload doesn't clear cart
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
sessionStorage.setItem('isCheckingOut', 'true'); sessionStorage.setItem('isCheckingOut', 'true');
console.log('Checkout button clicked: isCheckingOut flag set'); console.log('✓ Set isCheckingOut flag in sessionStorage');
} }
console.log('📡 Fetching /api/checkout with', items.length, 'items');
const res = await fetch('/api/checkout', { const res = await fetch('/api/checkout', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items, customerId: customer.id }), body: JSON.stringify({ items, customerId: customer.id }),
}); });
const data = await res.json(); const data = await res.json();
console.log('Checkout response:', { status: res.status, ok: res.ok, data }); console.log('📦 Checkout response:', { status: res.status, ok: res.ok, hasUrl: !!data.url });
if (!res.ok || !data.url) { if (!res.ok || !data.url) {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
sessionStorage.removeItem('isCheckingOut'); sessionStorage.removeItem('isCheckingOut');
} }
throw new Error(data.error ?? 'Something went wrong starting checkout.'); throw new Error(data.error ?? 'Something went wrong starting checkout.');
} }
console.log('Redirecting to Stripe:', data.url); console.log('🔗 Redirecting to Stripe');
window.location.href = data.url; window.location.href = data.url;
} catch (err) { } catch (err) {
const errorMsg = err instanceof Error ? err.message : 'Something went wrong.'; const errorMsg = err instanceof Error ? err.message : 'Something went wrong.';
+4 -3
View File
@@ -10,20 +10,21 @@ import { useCart } from '@/lib/cartStore';
*/ */
export default function LogoutOnUnload() { export default function LogoutOnUnload() {
const clearCart = useCart((s) => s.clear); const clearCart = useCart((s) => s.clear);
const cartItems = useCart((s) => s.items);
useEffect(() => { useEffect(() => {
const handleUnload = () => { const handleUnload = () => {
// Don't logout if we're in the middle of checkout (navigating to Stripe) // Don't logout if we're in the middle of checkout (navigating to Stripe)
// The checkout component sets this flag before making the API call // The checkout component sets this flag before making the API call
const isCheckingOut = typeof window !== 'undefined' && sessionStorage.getItem('isCheckingOut'); const isCheckingOut = typeof window !== 'undefined' && sessionStorage.getItem('isCheckingOut');
console.log('LogoutOnUnload handleUnload called, isCheckingOut flag:', isCheckingOut); console.log('🚪 LogoutOnUnload handleUnload - cart items:', cartItems.length, 'isCheckingOut:', isCheckingOut);
if (isCheckingOut) { if (isCheckingOut) {
console.log('Skipping logout because isCheckingOut is set'); console.log('Skipping logout - checkout in progress');
return; return;
} }
// Clear cart on logout // Clear cart on logout
console.log('Clearing cart and logging out'); console.log('🗑️ Clearing cart and logging out');
clearCart(); clearCart();
// Use sendBeacon to reliably send logout even when page is closing // Use sendBeacon to reliably send logout even when page is closing
// This doesn't wait for a response, which is fine for logout // This doesn't wait for a response, which is fine for logout