|
| 1 | +import type { LoaderFunctionArgs } from '@remix-run/node' |
| 2 | +import { useLoaderData } from '@remix-run/react' |
| 3 | +import { |
| 4 | + Elements, |
| 5 | + PaymentElement, |
| 6 | + useElements, |
| 7 | + useStripe |
| 8 | +} from '@stripe/react-stripe-js' |
| 9 | +import { loadStripe } from '@stripe/stripe-js' |
| 10 | +import { useEffect, useState } from 'react' |
| 11 | +import { createPaymentIntent } from '../lib/stripe.server' |
| 12 | +import { getSession } from '../session' |
| 13 | + |
| 14 | +const stripePromise = loadStripe('pk_test_B4Mlg9z1svOsuVjovpcLaK0d00lWym58fF') |
| 15 | + |
| 16 | +export default function CheckoutPage() { |
| 17 | + const [clientSecret, setClientSecret] = useState('') |
| 18 | + |
| 19 | + const paymentIntent: any = useLoaderData() |
| 20 | + const options = { |
| 21 | + clientSecret: paymentIntent.client_secret |
| 22 | + } |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (paymentIntent && paymentIntent.client_secret) { |
| 26 | + setClientSecret(paymentIntent.client_secret) |
| 27 | + } |
| 28 | + }, [paymentIntent]) |
| 29 | + |
| 30 | + return ( |
| 31 | + <Elements stripe={stripePromise} options={options}> |
| 32 | + <CheckoutForm clientSecret={clientSecret} /> |
| 33 | + </Elements> |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +export async function loader({ request }: LoaderFunctionArgs) { |
| 38 | + const session = await getSession(request.headers.get('Cookie')) |
| 39 | + const amount = session.get('amount') |
| 40 | + return await createPaymentIntent(amount) |
| 41 | +} |
| 42 | + |
| 43 | +type CheckoutFormProps = { |
| 44 | + clientSecret: string |
| 45 | +} |
| 46 | + |
| 47 | +function CheckoutForm({ clientSecret }: CheckoutFormProps) { |
| 48 | + console.log('clientSecret', clientSecret) |
| 49 | + // Client secret might still need to be passed down as props in case of using the CardElement instead of PaymentElement (which allows for full customization) |
| 50 | + /* |
| 51 | + stripe.confirmCardPayment.(clientSecret, { |
| 52 | + payment_method: {card: cardElement} |
| 53 | + })` |
| 54 | + */ |
| 55 | + // Leaving it as frontend's choice |
| 56 | + |
| 57 | + const stripe = useStripe() |
| 58 | + const elements = useElements() |
| 59 | + |
| 60 | + const handleSubmit = async (event: React.FormEvent) => { |
| 61 | + event.preventDefault() |
| 62 | + if (!stripe || !elements) return |
| 63 | + |
| 64 | + const { error } = await stripe.confirmPayment({ |
| 65 | + elements, |
| 66 | + confirmParams: { |
| 67 | + return_url: 'http://localhost:3000/success' // TODO Success Page |
| 68 | + } |
| 69 | + }) |
| 70 | + if (error) { |
| 71 | + console.error(error.message) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <form onSubmit={handleSubmit}> |
| 77 | + <PaymentElement /> |
| 78 | + <button |
| 79 | + type="submit" |
| 80 | + style={{ |
| 81 | + marginTop: '20px', |
| 82 | + background: '#5469d4', |
| 83 | + color: '#ffffff', |
| 84 | + border: 'none', |
| 85 | + borderRadius: '4px', |
| 86 | + fontSize: '16px', |
| 87 | + cursor: 'pointer', |
| 88 | + padding: '10px 20px' |
| 89 | + }} |
| 90 | + disabled={!stripe || !elements} |
| 91 | + > |
| 92 | + Pay |
| 93 | + </button> |
| 94 | + </form> |
| 95 | + ) |
| 96 | +} |
0 commit comments