/** * Shopify Checkout Redirect * * Redirects to Shopify checkout with session_id attached as a line item property. * After payment, Shopify fires an orders/paid webhook → backend generates the PDF. * * SETUP: * 1. Create products in Shopify with variant IDs * 2. Set the variant IDs below * 3. Set your Shopify store domain */ const SHOPIFY_CONFIG = { storeDomain: 'avvaleconsulting.myshopify.com', variantIds: { 'business-plan': '57824589709696', // AI Business Plan Generator — £0 (testing) → £29 'pitch-deck': '57824615760256', // AI Pitch Deck Generator — £0 (testing) → £39 'marketing-plan': '57824628113792', // AI Marketing Plan Generator — £0 (testing) → £49 }, // Set to true to enable Shopify checkout flow enabled: true, }; /** * Redirect to Shopify checkout with session_id attached. * Falls back to direct generation if Shopify is not configured. */ async function redirectToShopifyCheckout(sessionId, toolType) { if (!SHOPIFY_CONFIG.enabled) { console.log('[Shopify] Not configured — using direct generation'); return false; } const variantId = SHOPIFY_CONFIG.variantIds[toolType]; if (!variantId || variantId === 'REPLACE_WITH_VARIANT_ID') { console.warn(`[Shopify] No variant ID for ${toolType} — using direct generation`); return false; } // Build tool-specific return path for Shopify's "Continue shopping" link const toolNames = { 'business-plan': 'business-plan-generator', 'pitch-deck': 'pitch-deck-generator', 'marketing-plan': 'marketing-plan-generator', }; const returnPath = `/tools/${toolNames[toolType] || toolType}?session_id=${sessionId}`; // Build Shopify checkout URL with session_id as line item property const checkoutUrl = `https://${SHOPIFY_CONFIG.storeDomain}/cart/${variantId}:1` + `?attributes[session_id]=${encodeURIComponent(sessionId)}` + `¬e=${encodeURIComponent('AI Tool: ' + toolType + ' | Session: ' + sessionId)}` + `&return_to=${encodeURIComponent(returnPath)}`; // Store session_id in localStorage — cleared only after download is clicked localStorage.setItem('avvale_session_id', sessionId); localStorage.setItem('avvale_tool_type', toolType); window.location.href = checkoutUrl; return true; } /** * Check if returning from Shopify checkout. * Checks URL query params first, then localStorage. * Does NOT clear localStorage — that happens when user clicks download. */ function checkPostCheckoutReturn() { // 1. Check URL query param (most reliable — survives browser navigation) const urlParams = new URLSearchParams(window.location.search); const urlSessionId = urlParams.get('session_id'); if (urlSessionId) { // Clean URL without reloading const cleanUrl = window.location.pathname; window.history.replaceState({}, '', cleanUrl); return urlSessionId; } // 2. Check localStorage (fallback — user pressed back from Shopify) const storedSessionId = localStorage.getItem('avvale_session_id'); if (storedSessionId) { return storedSessionId; } return null; } /** * Clear session from localStorage. Call after download is clicked. */ function clearSessionStorage() { localStorage.removeItem('avvale_session_id'); localStorage.removeItem('avvale_tool_type'); }