Add amount to payment validation button

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2022-03-14 18:59:11 +04:00
parent 414db4d14d
commit f2ba39aeba
2 changed files with 7 additions and 2 deletions
+5 -2
View File
@@ -9,10 +9,11 @@ import {Button, Container, LinearProgress, Paper} from '@mui/material'
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3001' const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3001'
export default function CheckoutForm({setPaymentIsReady, setError, isLoading, setIsLoading}) { export default function CheckoutForm({prices, validMontant, setPaymentIsReady, setError, isLoading, setIsLoading}) {
const stripe = useStripe() const stripe = useStripe()
const elements = useElements() const elements = useElements()
const [isPaymentLoading, setIsPaymentLoading] = useState(false) const [isPaymentLoading, setIsPaymentLoading] = useState(false)
const amount = prices.find(({id}) => id === validMontant).unit_amount
const handleSubmit = async event => { const handleSubmit = async event => {
event.preventDefault() event.preventDefault()
@@ -50,7 +51,7 @@ export default function CheckoutForm({setPaymentIsReady, setError, isLoading, se
<PaymentElement id='payment-element' onReady={handleReady} /> <PaymentElement id='payment-element' onReady={handleReady} />
{!isLoading && ( {!isLoading && (
<Button fullWidth sx={{marginTop: 2}} variant='outlined' disabled={isPaymentLoading || !stripe || !elements} id='submit' onClick={handleSubmit}> <Button fullWidth sx={{marginTop: 2}} variant='outlined' disabled={isPaymentLoading || !stripe || !elements} id='submit' onClick={handleSubmit}>
Valider le paiement Valider le paiement de&nbsp;<strong>{amount / 100} euros</strong>
</Button> </Button>
)} )}
{isPaymentLoading && ( {isPaymentLoading && (
@@ -62,6 +63,8 @@ export default function CheckoutForm({setPaymentIsReady, setError, isLoading, se
} }
CheckoutForm.propTypes = { CheckoutForm.propTypes = {
validMontant: PropTypes.string.isRequired,
prices: PropTypes.array.isRequired,
setPaymentIsReady: PropTypes.func.isRequired, setPaymentIsReady: PropTypes.func.isRequired,
setError: PropTypes.func.isRequired, setError: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired, isLoading: PropTypes.bool.isRequired,
+2
View File
@@ -93,6 +93,8 @@ export default function PaymentMethod({isLoading, paymentMethod, setPaymentMetho
{clientSecret && validMontant && paymentMethod === 0 && ( {clientSecret && validMontant && paymentMethod === 0 && (
<Elements options={options} stripe={stripePromise}> <Elements options={options} stripe={stripePromise}>
<CheckoutForm <CheckoutForm
prices={prices}
validMontant={validMontant}
setPaymentIsReady={setPaymentIsReady} setPaymentIsReady={setPaymentIsReady}
setError={setError} setError={setError}
isLoading={isLoading} isLoading={isLoading}