Files
MotionWebStudio/supabase/functions/create-checkout-session/index.ts

104 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-12-26 14:03:18 +01:00
// @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) => {
2025-12-29 16:07:33 +01:00
// Handle CORS preflight requests
2025-12-26 14:03:18 +01:00
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
try {
2025-12-29 16:07:33 +01:00
// 1. Check for Secret Key
2025-12-26 14:03:18 +01:00
const STRIPE_SECRET_KEY = Deno.env.get('STRIPE_SECRET_KEY')
if (!STRIPE_SECRET_KEY) {
2025-12-29 16:07:33 +01:00
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.')
2025-12-26 14:03:18 +01:00
}
const stripe = new Stripe(STRIPE_SECRET_KEY, {
apiVersion: '2022-11-15',
httpClient: Stripe.createFetchHttpClient(),
})
2025-12-29 16:07:33 +01:00
// 2. Parse Request Body
2025-12-26 14:03:18 +01:00
const { order_id, package_name, payment_type, customer_email } = await req.json()
2025-12-29 16:07:33 +01:00
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') {
2026-01-05 09:37:20 +01:00
priceId = 'price_1SiMK525dc768V7UDxZugw0Y'; // Éves fenntartás
2025-12-26 14:03:18 +01:00
}
2025-12-29 16:07:33 +01:00
// 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)`);
2025-12-26 14:03:18 +01:00
}
const origin = req.headers.get('origin') || 'https://motionweb.hu'
2025-12-29 16:07:33 +01:00
// 5. Create Session
2025-12-26 14:03:18 +01:00
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
2025-12-29 16:07:33 +01:00
price: priceId,
2025-12-26 14:03:18 +01:00
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,
2025-12-29 16:07:33 +01:00
payment_type: payment_type,
package_name: package_name
2025-12-26 14:03:18 +01:00
},
})
return new Response(
JSON.stringify({ url: session.url }),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
}
)
} catch (error: any) {
2025-12-29 16:07:33 +01:00
console.error('Stripe Edge Function Error:', error)
2025-12-26 14:03:18 +01:00
return new Response(
2025-12-29 16:07:33 +01:00
JSON.stringify({ error: error.message || 'Unknown error in checkout session creation' }),
2025-12-26 14:03:18 +01:00
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,
}
)
}
})