import React, { useState } from 'react'; import { useOrg } from '@components/Contexts/OrgContext'; import { SiStripe } from '@icons-pack/react-simple-icons' import { useLHSession } from '@components/Contexts/LHSessionContext'; import { getPaymentConfigs, createPaymentConfig, updatePaymentConfig } from '@services/payments/payments'; import FormLayout, { ButtonBlack, Input, Textarea, FormField, FormLabelAndMessage, Flex } from '@components/StyledElements/Form/Form'; import { Check, Edit } from 'lucide-react'; import toast from 'react-hot-toast'; import useSWR, { mutate } from 'swr'; import Modal from '@components/StyledElements/Modal/Modal'; const PaymentsConfigurationPage: React.FC = () => { const org = useOrg() as any; const session = useLHSession() as any; const access_token = session?.data?.tokens?.access_token; const { data: paymentConfigs, error, isLoading } = useSWR( () => (org && access_token ? [`/payments/${org.id}/config`, access_token] : null), ([url, token]) => getPaymentConfigs(org.id, token) ); const stripeConfig = paymentConfigs?.find((config: any) => config.provider === 'stripe'); const [isModalOpen, setIsModalOpen] = useState(false); const enableStripe = async () => { try { const newConfig = { provider: 'stripe', enabled: true }; const config = await createPaymentConfig(org.id, newConfig, access_token); toast.success('Stripe enabled successfully'); mutate([`/payments/${org.id}/config`, access_token]); } catch (error) { console.error('Error enabling Stripe:', error); toast.error('Failed to enable Stripe'); } }; const editConfig = async () => { setIsModalOpen(true); }; if (isLoading) { return
Loading...
; } if (error) { return
Error loading payment configuration
; } return (

Payments Configuration

Manage your organization payments configuration

{stripeConfig ? (
Stripe is enabled
Edit Configuration
) : ( Enable Stripe )}
{stripeConfig && ( setIsModalOpen(false)} /> )}
); }; interface EditStripeConfigModalProps { orgId: number; configId: string; accessToken: string; isOpen: boolean; onClose: () => void; } const EditStripeConfigModal: React.FC = ({ orgId, configId, accessToken, isOpen, onClose }) => { const [stripeKey, setStripeKey] = useState(''); const [stripeSecretKey, setStripeSecretKey] = useState(''); const [stripeWebhookSecret, setStripeWebhookSecret] = useState(''); const handleSubmit = async () => { try { const stripe_config = { stripe_key: stripeKey, stripe_secret_key: stripeSecretKey, stripe_webhook_secret: stripeWebhookSecret, }; const updatedConfig = { provider_config: stripe_config, }; await updatePaymentConfig(orgId, configId, updatedConfig, accessToken); toast.success('Configuration updated successfully'); mutate([`/payments/${orgId}/config`, accessToken]); onClose(); } catch (error) { console.error('Error updating config:', error); toast.error('Failed to update configuration'); } }; return ( setStripeKey(e.target.value)} /> setStripeSecretKey(e.target.value)} /> setStripeWebhookSecret(e.target.value)} /> Save } /> ); }; export default PaymentsConfigurationPage;