diff --git a/components/soutyen/stripe-payment.js b/components/soutyen/stripe-payment.js new file mode 100644 index 0000000..c4c51af --- /dev/null +++ b/components/soutyen/stripe-payment.js @@ -0,0 +1,117 @@ +import PropTypes from 'prop-types' +import axios from 'axios' +import Box from '@mui/material/Box' +import Container from '@mui/material/Container' +import Typography from '@mui/material/Typography' +import Stack from '@mui/material/Stack' +import Button from '@mui/material/Button' +import LinearProgress from '@mui/material/LinearProgress' + +import {validateEmail} from '../../lib/utils/emails' + +import Email from './email' +import Montant from './montant' + +export default function StripePayment({isLoading, selectedMontant, setSelectedMontant, validMontant, setValidMontant, prices, paymentIntent, setClientSecret, setPaymentIntent, setIsLoading, setPaymentIsReady, setClientEmail, clientEmail, error, setError}) { + const cancelPayment = async () => { + setPaymentIsReady(false) + setClientEmail('') + setIsLoading(false) + setClientSecret(null) + + try { + await axios.post('/stripe/cancel-payment', { + paymentIntent + }) + setPaymentIntent(null) + } catch (error_) { + console.log(error_.message) + } + } + + const handleCancel = async () => { + setValidMontant(null) + setSelectedMontant(null) + await cancelPayment() + } + + const handleClick = () => { + if (!clientEmail) { + setError('Adresse e-mail obligatoire.') + return + } + + if (!validateEmail(clientEmail)) { + setError('Adresse e-mail invalide.') + return + } + + setIsLoading(true) + setValidMontant(selectedMontant) + } + + return ( + + + <> + + + * Obligatoire + + + {validMontant && ( + + )} + + + {isLoading && ( + + + + )} + + + ) +} + +StripePayment.defaultProps = { + selectedMontant: null, + validMontant: null, + paymentIntent: null, + clientEmail: null, + error: null +} + +StripePayment.propTypes = { + isLoading: PropTypes.bool.isRequired, + selectedMontant: PropTypes.string, + setSelectedMontant: PropTypes.func.isRequired, + validMontant: PropTypes.string, + setValidMontant: PropTypes.func.isRequired, + prices: PropTypes.array.isRequired, + paymentIntent: PropTypes.string, + setClientSecret: PropTypes.func.isRequired, + setPaymentIntent: PropTypes.func.isRequired, + setIsLoading: PropTypes.func.isRequired, + setPaymentIsReady: PropTypes.func.isRequired, + setClientEmail: PropTypes.func.isRequired, + clientEmail: PropTypes.string, + error: PropTypes.string, + setError: PropTypes.func.isRequired +}