// @ts-nocheck import { serve } from "https://deno.land/std@0.168.0/http/server.ts" import { Stripe } from "https://esm.sh/stripe@12.0.0?target=deno" const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', } serve(async (req) => { // Handle CORS preflight requests if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }) } try { // 1. Check for Secret Key const STRIPE_SECRET_KEY = Deno.env.get('STRIPE_SECRET_KEY') if (!STRIPE_SECRET_KEY) { console.error('CRITICAL: STRIPE_SECRET_KEY is missing from Edge Function Secrets.') throw new Error('Server configuration error: Missing Stripe Key. Please set it in Supabase Secrets.') } const stripe = new Stripe(STRIPE_SECRET_KEY, { apiVersion: '2022-11-15', httpClient: Stripe.createFetchHttpClient(), }) // 2. Parse Request Body const { order_id, package_name, payment_type, customer_email } = await req.json() console.log(`Processing checkout: Order ${order_id}, Package: ${package_name}, Type: ${payment_type}`); // 3. Map to Stripe Price IDs // Ezeket a Price ID-kat a Stripe Dashboard-on hoztad létre a termékekhez. // Ha még nincsenek, hozd létre őket, vagy használd a lenti kódokat teszteléshez. let priceId = ''; const pkg = package_name?.trim(); const type = payment_type?.trim(); if (pkg === 'Landing Page') { if (type === 'deposit') { priceId = 'price_1SiIf725dc768V7U1zW8kBuN'; // Landing Page - Előleg } else if (type === 'final') { priceId = 'price_1SiIgw25dc768V7UrmEe2XRo'; // Landing Page - Fennmaradó } } else if (pkg === 'Pro Web') { if (type === 'deposit') { priceId = 'price_1SiIhq25dc768V7UlD8dx1I9'; // Pro Web - Előleg } else if (type === 'final') { priceId = 'price_1SiIif25dc768V7UqsfSi345'; // Pro Web - Fennmaradó } } else if (pkg === 'Maintenance' || pkg === 'Fenntartás') { priceId = 'price_1SiIm025dc768V7UDFMZHDox'; // Éves fenntartás } // 4. Validate Logic if (!priceId) { // Ha Enterprise csomag vagy ismeretlen kombináció érkezik, dobjunk hibát, // de a frontendnek ezt elvileg nem szabadna meghívnia Enterprise-ra. console.error(`No Price ID found for: ${pkg} - ${type}`); throw new Error(`Nem található árazás ehhez a kombinációhoz: ${pkg} - ${type === 'deposit' ? 'Előleg' : 'Végszámla'}. (Enterprise esetén nem kellene itt lennünk)`); } const origin = req.headers.get('origin') || 'https://motionweb.hu' // 5. Create Session const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: [ { price: priceId, quantity: 1, }, ], mode: 'payment', success_url: `${origin}/#/dashboard?payment_success=true&order_id=${order_id}`, cancel_url: `${origin}/#/dashboard?payment_cancelled=true&order_id=${order_id}`, customer_email: customer_email, metadata: { order_id: order_id, payment_type: payment_type, package_name: package_name }, }) return new Response( JSON.stringify({ url: session.url }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200, } ) } catch (error: any) { console.error('Stripe Edge Function Error:', error) return new Response( JSON.stringify({ error: error.message || 'Unknown error in checkout session creation' }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 400, } ) } })