import React, { useState, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { Card, Input, Button } from '../../ui'; import { PaymentElement, useStripe, useElements } from '@stripe/react-stripe-js'; import { AlertCircle, CheckCircle, CreditCard, Lock } from 'lucide-react'; interface PaymentFormProps { onPaymentSuccess: (paymentMethodId?: string) => void; onPaymentError: (error: string) => void; className?: string; bypassPayment?: boolean; onBypassToggle?: () => void; userName?: string; userEmail?: string; } const PaymentForm: React.FC = ({ onPaymentSuccess, onPaymentError, className = '', bypassPayment = false, onBypassToggle, userName = '', userEmail = '' }) => { const { t } = useTranslation(); const stripe = useStripe(); const elements = useElements(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [cardComplete, setCardComplete] = useState(false); const [billingDetails, setBillingDetails] = useState({ name: userName, email: userEmail, address: { line1: '', city: '', state: '', postal_code: '', country: 'ES', }, }); // For development mode - bypass payment option const handleBypassPayment = () => { if (onBypassToggle) { onBypassToggle(); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!stripe || !elements) { // Stripe.js has not loaded yet onPaymentError('Stripe.js no ha cargado correctamente'); return; } if (bypassPayment) { // In development mode, bypass payment processing onPaymentSuccess(); return; } setLoading(true); setError(null); try { // Submit the payment element to validate all inputs const { error: submitError } = await elements.submit(); if (submitError) { setError(submitError.message || 'Error al validar el formulario'); onPaymentError(submitError.message || 'Error al validar el formulario'); setLoading(false); return; } // Create payment method using the PaymentElement // This is the correct way to create a payment method with PaymentElement const { error: paymentError, paymentMethod } = await stripe.createPaymentMethod({ elements, params: { billing_details: { name: billingDetails.name, email: billingDetails.email, address: billingDetails.address, }, }, }); if (paymentError) { setError(paymentError.message || 'Error al procesar el pago'); onPaymentError(paymentError.message || 'Error al procesar el pago'); setLoading(false); return; } // Send the paymentMethod.id to your server to create a subscription console.log('Payment method created:', paymentMethod); // Pass the payment method ID to the parent component for server-side processing onPaymentSuccess(paymentMethod?.id); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Error desconocido al procesar el pago'; setError(errorMessage); onPaymentError(errorMessage); } finally { setLoading(false); } }; const handlePaymentElementChange = (event: any) => { setError(event.error?.message || null); setCardComplete(event.complete); }; return (

{t('auth:payment.payment_info', 'Información de Pago')}

{t('auth:payment.secure_payment', 'Tu información de pago está protegida con encriptación de extremo a extremo')}

{/* Development mode toggle */}
{t('auth:payment.dev_mode', 'Modo Desarrollo')}
{!bypassPayment && (
{/* Billing Details */}
setBillingDetails({...billingDetails, name: e.target.value})} required disabled={loading} /> setBillingDetails({...billingDetails, email: e.target.value})} required disabled={loading} />
setBillingDetails({...billingDetails, address: {...billingDetails.address, line1: e.target.value}})} required disabled={loading} /> setBillingDetails({...billingDetails, address: {...billingDetails.address, city: e.target.value}})} required disabled={loading} />
setBillingDetails({...billingDetails, address: {...billingDetails.address, state: e.target.value}})} required disabled={loading} /> setBillingDetails({...billingDetails, address: {...billingDetails.address, postal_code: e.target.value}})} required disabled={loading} /> setBillingDetails({...billingDetails, address: {...billingDetails.address, country: e.target.value}})} required disabled={loading} />
{/* Payment Element */}

{t('auth:payment.payment_info_secure', 'Tu información de pago está segura')}

{/* Error Message */} {error && (
{error}
)} {/* Submit Button */}
)} {bypassPayment && (

{t('auth:payment.payment_bypassed_title', 'Pago Bypassed')}

{t('auth:payment.payment_bypassed_description', 'El proceso de pago ha sido omitido en modo desarrollo. El registro continuará normalmente.')}

)}
); }; export default PaymentForm;