stripe full

This commit is contained in:
2025-12-29 16:07:33 +01:00
parent 235e1d9b52
commit d04d9d9887
4 changed files with 137 additions and 37 deletions

View File

@@ -8,14 +8,17 @@ const corsHeaders = {
}
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) {
throw new Error('STRIPE_SECRET_KEY not set in environment variables')
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, {
@@ -23,40 +26,50 @@ serve(async (req) => {
httpClient: Stripe.createFetchHttpClient(),
})
// 2. Parse Request Body
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;
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
}
if (priceAmount === 0) {
throw new Error('Invalid package or payment type')
// 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)`);
}
// Origin meghatározása a visszairányításhoz
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_data: {
currency: 'huf',
product_data: {
name: `${package_name} - ${payment_type === 'deposit' ? 'Előleg' : 'Fennmaradó összeg'}`,
},
unit_amount: priceAmount * 100, // Fillérben kell megadni
},
price: priceId,
quantity: 1,
},
],
@@ -66,7 +79,8 @@ serve(async (req) => {
customer_email: customer_email,
metadata: {
order_id: order_id,
payment_type: payment_type
payment_type: payment_type,
package_name: package_name
},
})
@@ -78,9 +92,9 @@ serve(async (req) => {
}
)
} catch (error: any) {
console.error(error)
console.error('Stripe Edge Function Error:', error)
return new Response(
JSON.stringify({ error: error.message }),
JSON.stringify({ error: error.message || 'Unknown error in checkout session creation' }),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,