diff --git a/src/pages/ConsultingBooking.tsx b/src/pages/ConsultingBooking.tsx index 5b54e1f..d50a8f3 100644 --- a/src/pages/ConsultingBooking.tsx +++ b/src/pages/ConsultingBooking.tsx @@ -2,7 +2,6 @@ import { useEffect, useState, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { supabase } from '@/integrations/supabase/client'; import { useAuth } from '@/hooks/useAuth'; -import { useCart } from '@/contexts/CartContext'; import { AppLayout } from '@/components/AppLayout'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; @@ -51,7 +50,6 @@ interface Profile { export default function ConsultingBooking() { const { user, loading: authLoading } = useAuth(); const navigate = useNavigate(); - const { addItem } = useCart(); const [settings, setSettings] = useState(null); const [workhours, setWorkhours] = useState([]); @@ -228,17 +226,46 @@ export default function ConsultingBooking() { const { error: slotsError } = await supabase.from('consulting_slots').insert(slotsToInsert); if (slotsError) throw slotsError; - // Add to cart for Pakasir checkout - addItem({ - id: `consulting-${order.id}`, - title: `Konsultasi 1-on-1 (${totalBlocks} blok)`, - price: totalPrice, - sale_price: null, - type: 'consulting', + // Generate Pakasir payment URL directly (no cart needed for consulting) + const pakasirOrderData = { + order_id: order.id, + amount: totalPrice, + customer_name: profile?.full_name || user.email, + customer_email: user.email, + items: [{ + id: order.id, + name: `Konsultasi 1-on-1 (${totalBlocks} blok)`, + price: totalPrice, + quantity: 1 + }], + success_url: `${window.location.origin}/orders/${order.id}`, + cancel_url: `${window.location.origin}/consulting`, + }; + + // Call your backend to create Pakasir payment + const paymentResponse = await fetch(`${import.meta.env.VITE_SUPABASE_URL}/functions/v1/create-pakasir-payment`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${import.meta.env.VITE_SUPABASE_ANON_KEY}`, + }, + body: JSON.stringify(pakasirOrderData), }); - toast({ title: 'Berhasil', description: 'Silakan lanjutkan ke pembayaran' }); - navigate('/checkout'); + if (!paymentResponse.ok) { + throw new Error('Failed to create payment'); + } + + const paymentData = await paymentResponse.json(); + + // Update order with payment URL + await supabase + .from('orders') + .update({ payment_url: paymentData.payment_url }) + .eq('id', order.id); + + // Redirect to payment + window.location.href = paymentData.payment_url; } catch (error: any) { toast({ title: 'Error', description: error.message, variant: 'destructive' }); } finally {