import React, { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { SettingsLayout } from './components/SettingsLayout'; import { SettingsCard } from './components/SettingsCard'; import { ToggleField } from './components/ToggleField'; import { GenericGatewayForm } from '@/components/settings/GenericGatewayForm'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { CreditCard, Banknote, Settings, RefreshCw, ExternalLink, Loader2, AlertTriangle } from 'lucide-react'; import { toast } from 'sonner'; interface GatewayField { id: string; type: string; title: string; description: string; default: string | boolean; value: string | boolean; // Current saved value placeholder?: string; required: boolean; options?: Record; custom_attributes?: Record; } interface PaymentGateway { id: string; title: string; description: string; enabled: boolean; type: 'manual' | 'provider' | 'other'; icon: string; method_title: string; method_description: string; supports: string[]; requirements: { met: boolean; missing: string[]; }; settings: { basic: Record; api: Record; advanced: Record; }; has_fields: boolean; webhook_url: string | null; has_custom_ui: boolean; wc_settings_url: string; } export default function PaymentsPage() { const queryClient = useQueryClient(); const [selectedGateway, setSelectedGateway] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); const [togglingGateway, setTogglingGateway] = useState(null); // Fetch all payment gateways const { data: gateways = [], isLoading, refetch } = useQuery({ queryKey: ['payment-gateways'], queryFn: () => api.get('/payments/gateways'), refetchOnWindowFocus: true, staleTime: 5 * 60 * 1000, // 5 minutes }); // Toggle gateway mutation const toggleMutation = useMutation({ mutationFn: ({ id, enabled }: { id: string; enabled: boolean }) => { setTogglingGateway(id); return api.post(`/payments/gateways/${id}/toggle`, { enabled }); }, onSuccess: async () => { // Wait for refetch to complete before showing toast await queryClient.invalidateQueries({ queryKey: ['payment-gateways'] }); toast.success('Gateway updated successfully'); setTogglingGateway(null); }, onError: () => { toast.error('Failed to update gateway'); setTogglingGateway(null); }, }); // Save gateway settings mutation const saveMutation = useMutation({ mutationFn: ({ id, settings }: { id: string; settings: Record }) => api.post(`/payments/gateways/${id}`, settings), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['payment-gateways'] }); toast.success('Settings saved successfully'); setIsModalOpen(false); setSelectedGateway(null); }, onError: () => { toast.error('Failed to save settings'); }, }); const handleToggle = (id: string, enabled: boolean) => { toggleMutation.mutate({ id, enabled }); }; const handleManageGateway = (gateway: PaymentGateway) => { setSelectedGateway(gateway); setIsModalOpen(true); }; const handleSaveGateway = async (settings: Record) => { if (!selectedGateway) return; await saveMutation.mutateAsync({ id: selectedGateway.id, settings }); }; // Categorize gateways const manualGateways = gateways.filter((g: PaymentGateway) => g.type === 'manual'); // Combine provider and other into single "3rd party" category const thirdPartyGateways = gateways.filter((g: PaymentGateway) => g.type === 'provider' || g.type === 'other'); if (isLoading) { return (
); } return ( <> refetch()} disabled={isLoading} > Refresh } > {/* Manual Payment Methods - First priority */} {manualGateways.length === 0 ? (

No manual payment methods available

) : (
{manualGateways.map((gateway: PaymentGateway) => (

{gateway.method_title || gateway.title}

{gateway.description && (

{gateway.description}

)}
{gateway.enabled && ( )} handleToggle(gateway.id, checked)} disabled={togglingGateway === gateway.id} />
))}
)}
{/* Online Payment Methods - Flat list */} {thirdPartyGateways.length > 0 && (
{thirdPartyGateways.map((gateway: PaymentGateway) => (

{gateway.method_title || gateway.title}

{gateway.method_description && (

{gateway.method_description}

)} {!gateway.requirements.met && ( Requirements not met: {gateway.requirements.missing.join(', ')} )}
{gateway.enabled && ( )} handleToggle(gateway.id, checked)} disabled={!gateway.requirements.met || togglingGateway === gateway.id} />
))}
)}
{/* Gateway Settings Modal */} {selectedGateway && ( {selectedGateway.title} Settings
setIsModalOpen(false)} hideFooter />
{/* Footer outside scrollable area */}
)} ); }