Files
pawol.nu/components/soutyen/payment-method.js
T

173 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {useState} from 'react'
import PropTypes from 'prop-types'
import Tabs from '@mui/material/Tabs'
import Tab from '@mui/material/Tab'
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import {Paypal, Liberapay} from '@icons-pack/react-simple-icons'
import {Elements} from '@stripe/react-stripe-js'
import {loadStripe} from '@stripe/stripe-js'
import {Grid} from '@mui/material'
import {appearance} from '../../lib/utils/stripe-style'
import CheckoutForm from './checkout-form'
import StripeDialog from './stripe-dialog'
const stripePromise = loadStripe(
process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY
)
const PAYPAL_ID = process.env.NEXT_PUBLIC_PAYPAL_DONATE_ID
const LIBERAPAY_DONATE = process.env.NEXT_PUBLIC_LIBERAPAY_DONATE
function TabPanel(props) {
const {children, value, index, ...other} = props
return (
<div
role='tabpanel'
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{p: 3}}>
{children}
</Box>
)}
</div>
)
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.number.isRequired,
value: PropTypes.number.isRequired,
}
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
}
}
export default function PaymentMethod({isLoading, paymentMethod, setPaymentMethod, selectedMontant, setSelectedMontant, validMontant, setValidMontant, prices, paymentIntent, setClientSecret, setPaymentIntent, setIsLoading, setClientEmail, clientEmail, error, setError, clientSecret}) {
const [open, setOpen] = useState(false)
const options = {
clientSecret,
appearance,
}
const handleChange = (event, newValue) => {
setPaymentMethod(newValue)
}
const handleClickOpen = () => {
setOpen(true)
}
const handleClose = () => {
setOpen(false)
}
return (
<Box sx={{width: '100%'}}>
<Box sx={{borderBottom: 1, borderColor: 'divider'}}>
<Tabs scrollButtons allowScrollButtonsMobile value={paymentMethod} aria-label='basic tabs example' variant='fullWidth' onChange={handleChange}>
<Tab wrapped label='Adhésion' {...a11yProps(0)} />
<Tab wrapped label='PayPal / Liberapay' {...a11yProps(1)} />
<Tab wrapped label='Carte bancaire' {...a11yProps(2)} />
</Tabs>
</Box>
<TabPanel value={paymentMethod} index={0}>
<Button size='large' variant='outlined' onClick={() => window.open('https://asso.oki.re/public/members/new.php', '_blank')}>
Cliquez ici pour adhérer à lassociation OKi
</Button>
</TabPanel>
<TabPanel value={paymentMethod} index={1}>
<Grid container rowSpacing={1}>
<Grid item md={6} xs={12}>
<Button variant='outlined' size='large' endIcon={<Liberapay />} onClick={() => window.open(`https://liberapay.com/${LIBERAPAY_DONATE}/donate`, '_blank')}>
Faire un don via Liberapay
</Button>
</Grid>
<Grid item md={6} xs={12}>
<Button variant='outlined' size='large' endIcon={<Paypal />} onClick={() => window.open(`https://www.paypal.com/donate/?hosted_button_id=${PAYPAL_ID}`, '_blank')}>
Faire un don via PayPal
</Button>
</Grid>
</Grid>
</TabPanel>
<TabPanel value={paymentMethod} index={2}>
<Button size='large' variant='outlined' onClick={handleClickOpen}>
Faire un don par carte bancaire
</Button>
<StripeDialog
open={open}
handleClose={handleClose}
selectedMontant={selectedMontant}
setSelectedMontant={setSelectedMontant}
validMontant={validMontant}
setValidMontant={setValidMontant}
prices={prices}
paymentIntent={paymentIntent}
setClientSecret={setClientSecret}
setPaymentIntent={setPaymentIntent}
setIsLoading={setIsLoading}
setClientEmail={setClientEmail}
clientEmail={clientEmail}
error={error}
setError={setError}
isLoading={isLoading}
>
{clientSecret && validMontant && paymentMethod === 2 && (
<Elements options={options} stripe={stripePromise}>
<CheckoutForm
prices={prices}
validMontant={validMontant}
setError={setError}
isLoading={isLoading}
setIsLoading={setIsLoading}
/>
</Elements>
)}
</StripeDialog>
</TabPanel>
</Box>
)
}
PaymentMethod.defaultProps = {
selectedMontant: null,
validMontant: null,
paymentIntent: null,
clientEmail: null,
error: null,
clientSecret: null
}
PaymentMethod.propTypes = {
isLoading: PropTypes.bool.isRequired,
paymentMethod: PropTypes.number.isRequired,
setPaymentMethod: PropTypes.func.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,
setClientEmail: PropTypes.func.isRequired,
clientEmail: PropTypes.string,
error: PropTypes.string,
setError: PropTypes.func.isRequired,
clientSecret: PropTypes.string
}