Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import React, { useState } from 'react'; import { PaymentElement, useStripe, useElements, } from '@stripe/react-stripe-js'; import { Button } from '@/Components/ui/button'; import { Alert, AlertDescription } from '@/Components/ui/alert'; import { Loader2 } from 'lucide-react'; interface PaymentFormProps { returnUrl: string; onSuccess?: () => void; submitText?: string; } export default function PaymentForm({ returnUrl, onSuccess, submitText = 'Pay now' }: PaymentFormProps) { const stripe = useStripe(); const elements = useElements(); const [error, setError] = useState<string | null>(null); const [processing, setProcessing] = useState(false); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (!stripe || !elements) { return; } setError(null); setProcessing(true); const { error: submitError } = await stripe.confirmPayment({ elements, confirmParams: { return_url: returnUrl, }, redirect: onSuccess ? 'if_required' : 'always', }); if (submitError) { setError(submitError.message || 'An error occurred'); setProcessing(false); } else if (onSuccess) { onSuccess(); } }; return ( <form onSubmit={handleSubmit} className="space-y-6"> <PaymentElement options={{ layout: 'tabs', }} /> {error && ( <Alert variant="destructive"> <AlertDescription>{error}</AlertDescription> </Alert> )} <Button type="submit" disabled={!stripe || processing} className="w-full" > {processing ? ( <> <Loader2 className="mr-2 h-4 w-4 animate-spin" /> Processing... </> ) : ( submitText )} </Button> </form> ); } |