Files
MotionWebStudio/supabase/functions/create-checkout-session/index.ts
2025-12-26 14:03:18 +01:00

90 lines
2.7 KiB
TypeScript

// @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) => {
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
try {
const STRIPE_SECRET_KEY = Deno.env.get('STRIPE_SECRET_KEY')
if (!STRIPE_SECRET_KEY) {
throw new Error('STRIPE_SECRET_KEY not set in environment variables')
}
const stripe = new Stripe(STRIPE_SECRET_KEY, {
apiVersion: '2022-11-15',
httpClient: Stripe.createFetchHttpClient(),
})
const { order_id, package_name, payment_type, customer_email } = await req.json()
// Árazási logika (HUF-ban)
let priceAmount = 0
// Landing Page: 190.000 Ft (Előleg: 40.000, Vég: 150.000)
if (package_name === 'Landing Page') {
if (payment_type === 'deposit') priceAmount = 40000;
else if (payment_type === 'final') priceAmount = 150000;
}
// Pro Web: 350.000 Ft (Előleg: 80.000, Vég: 270.000)
else if (package_name === 'Pro Web') {
if (payment_type === 'deposit') priceAmount = 80000;
else if (payment_type === 'final') priceAmount = 270000;
}
if (priceAmount === 0) {
throw new Error('Invalid package or payment type')
}
// Origin meghatározása a visszairányításhoz
const origin = req.headers.get('origin') || 'https://motionweb.hu'
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'huf',
product_data: {
name: `${package_name} - ${payment_type === 'deposit' ? 'Előleg' : 'Fennmaradó összeg'}`,
},
unit_amount: priceAmount * 100, // Fillérben kell megadni
},
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
},
})
return new Response(
JSON.stringify({ url: session.url }),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
}
)
} catch (error: any) {
console.error(error)
return new Response(
JSON.stringify({ error: error.message }),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,
}
)
}
})