From 2e49a44a4af393d88a216d2d64d4bb14285aef0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Sat, 19 Feb 2022 22:22:57 +0400 Subject: [PATCH] Create /soutyen page --- pages/soutyen.js | 253 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 pages/soutyen.js diff --git a/pages/soutyen.js b/pages/soutyen.js new file mode 100644 index 0000000..cc45f1e --- /dev/null +++ b/pages/soutyen.js @@ -0,0 +1,253 @@ +import {useEffect, useState, forwardRef, useRef} from 'react' +import {useRouter} from 'next/router' +import PropTypes from 'prop-types' +import {loadStripe} from '@stripe/stripe-js' +import {Elements} from '@stripe/react-stripe-js' +import {Box, Container, Typography, Snackbar, LinearProgress, Paper} from '@mui/material' +import MuiAlert from '@mui/material/Alert' +import axios from 'axios' + +import HeadLayout from '../components/head-layout' +import CheckoutForm from '../components/soutyen/checkout-form' +import Presantasyon from '../components/soutyen/presantasyon' +import Don from '../components/soutyen/don' +import PaymentMethod from '../components/soutyen/payment-method' +import Footer from '../components/footer' + +import {appearance} from '../lib/utils/stripe-style' + +const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3001' + +const stripePromise = loadStripe( + process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY +) + +const Alert = forwardRef(function Alert(props, ref) { + return +}) + +export default function Soutyen({prices, paymentStatus}) { + const router = useRouter() + const scrollEvent = useRef(null) + const [clientSecret, setClientSecret] = useState(null) + const [paymentIntent, setPaymentIntent] = useState(null) + const [isPaypal, setIsPaypal] = useState(true) + const [selectedMontant, setSelectedMontant] = useState(null) + const [validMontant, setValidMontant] = useState(null) + const [success, setSuccess] = useState(null) + const [error, setError] = useState(null) + const [open, setOpen] = useState(false) + const [payementIsReady, setPaymentIsReady] = useState(false) + const [isLoading, setIsLoading] = useState(false) + const [clientEmail, setClientEmail] = useState('') + + const scrollToBottom = () => { + scrollEvent.current?.scrollIntoView({behavior: 'smooth', block: 'end', inline: 'nearest'}) + } + + const options = { + clientSecret, + appearance, + } + + useEffect(() => { + const getClientSecret = async () => { + try { + const response = await axios.post('/stripe/valid-payment', { + id: validMontant, + email: clientEmail + }) + setClientSecret(response.data.clientSecret) + setPaymentIntent(response.data.paymentIntent) + } catch (error_) { + setError(error_.message) + } + } + + if (validMontant) { + getClientSecret() + } + }, [selectedMontant, clientEmail, validMontant]) + + const handleClose = (event, reason) => { + if (reason === 'clickaway') { + return + } + + setSuccess(null) + setError(null) + setOpen(false) + } + + useEffect(() => { + if (isPaypal) { + setSelectedMontant(null) + setValidMontant(null) + } + }, [isPaypal, setSelectedMontant, setValidMontant]) + + useEffect(() => { + if (paymentStatus) { + switch (paymentStatus) { + case 'succeeded': + setSuccess('Paiement réussi. Merci pour votre soutien 🥰') + break + case 'processing': + setSuccess('Votre paiement en cours de traitement.') + break + case 'requires_payment_method': + setError('Votre paiement n’a pas abouti, veuillez réessayer.') + break + default: + setError('Une erreur s’est produite.') + break + } + } else { + setSuccess(null) + setError(null) + } + }, [paymentStatus, router]) + + useEffect(() => { + if (success) { + setOpen(true) + } else { + setOpen(false) + } + }, [success]) + + useEffect(() => { + if (error) { + setOpen(true) + } else { + setOpen(false) + } + }, [error]) + + useEffect(() => { + router.push('/soutyen', undefined, {shallow: true}) + }, []) // eslint-disable-line react-hooks/exhaustive-deps + + useEffect(() => {}, [router.query.payment_intent]) + + useEffect(() => { + if (!isPaypal || validMontant) { + scrollToBottom() + } + }, [isPaypal, validMontant]) + + useEffect(() => { + if (payementIsReady) { + scrollToBottom() + } + }, [payementIsReady]) + + return ( + + + + + + Soutenir Organisation KA Internationale ! + + + + + Faire un don + + + + + + {isLoading && ( + + + + )} + + {clientSecret && validMontant && !isPaypal && ( + + + + )} + + + +