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

172 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-06-21 19:45:39 +04:00
import {useState} from 'react'
2022-02-19 22:21:55 +04:00
import PropTypes from 'prop-types'
2022-03-06 10:11:02 +04:00
import Tabs from '@mui/material/Tabs'
import Tab from '@mui/material/Tab'
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
2022-02-19 22:21:55 +04:00
2022-05-05 14:01:36 +04:00
import {Paypal, Liberapay} from '@icons-pack/react-simple-icons'
2022-03-06 10:11:02 +04:00
import {Elements} from '@stripe/react-stripe-js'
import {loadStripe} from '@stripe/stripe-js'
2022-02-19 22:21:55 +04:00
2022-05-05 14:01:36 +04:00
import {Grid} from '@mui/material'
2022-03-06 10:11:02 +04:00
import {appearance} from '../../lib/utils/stripe-style'
2022-02-19 22:21:55 +04:00
2022-03-06 10:11:02 +04:00
import CheckoutForm from './checkout-form'
2022-06-21 19:45:39 +04:00
import StripeDialog from './stripe-dialog'
2022-02-19 22:21:55 +04:00
const STRIPE_PUBLIC_KEY = process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY
2022-03-06 10:11:02 +04:00
const PAYPAL_ID = process.env.NEXT_PUBLIC_PAYPAL_DONATE_ID
2022-05-05 14:01:36 +04:00
const LIBERAPAY_DONATE = process.env.NEXT_PUBLIC_LIBERAPAY_DONATE
2022-02-19 22:21:55 +04:00
2022-03-06 10:11:02 +04:00
function TabPanel(props) {
const {children, value, index, ...other} = props
2022-02-19 22:21:55 +04:00
2022-03-06 10:11:02 +04:00
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>
)
}
2022-02-19 22:21:55 +04:00
2022-03-06 10:11:02 +04:00
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.number.isRequired,
value: PropTypes.number.isRequired,
}
2022-02-19 22:21:55 +04:00
2022-03-06 10:11:02 +04:00
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
2022-02-19 22:21:55 +04:00
}
2022-03-06 10:11:02 +04:00
}
2022-02-19 22:21:55 +04:00
export default function PaymentMethod({isLoading, paymentMethod, setPaymentMethod, selectedMontant, setSelectedMontant, validMontant, setValidMontant, paymentIntent, setClientSecret, setPaymentIntent, setIsLoading, setClientEmail, clientEmail, error, setError, clientSecret}) {
2022-06-21 19:45:39 +04:00
const [open, setOpen] = useState(false)
2022-03-06 10:11:02 +04:00
const options = {
clientSecret,
appearance,
}
2022-02-19 22:21:55 +04:00
2022-03-06 10:11:02 +04:00
const handleChange = (event, newValue) => {
setPaymentMethod(newValue)
}
2022-02-19 22:21:55 +04:00
2022-06-21 19:45:39 +04:00
const handleClickOpen = () => {
setOpen(true)
}
const handleClose = () => {
2023-06-25 15:43:30 +04:00
if (selectedMontant) {
setSelectedMontant(null)
setValidMontant(null)
}
2022-06-21 19:45:39 +04:00
setOpen(false)
}
2022-02-19 22:21:55 +04:00
return (
2022-03-06 10:11:02 +04:00
<Box sx={{width: '100%'}}>
<Box sx={{borderBottom: 1, borderColor: 'divider'}}>
<Tabs scrollButtons allowScrollButtonsMobile value={paymentMethod} aria-label='basic tabs example' variant='fullWidth' onChange={handleChange}>
2023-06-29 06:32:42 +04:00
<Tab wrapped label='Liberapay / PayPal' {...a11yProps(0)} />
2023-06-25 15:44:04 +04:00
<Tab wrapped label='Adhésion' {...a11yProps(1)} />
<Tab wrapped label='Carte bancaire' {...a11yProps(2)} />
2022-03-06 10:11:02 +04:00
</Tabs>
</Box>
<TabPanel value={paymentMethod} index={0}>
<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>
2023-06-25 15:44:04 +04:00
<TabPanel value={paymentMethod} index={1}>
<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={2}>
2022-06-21 19:45:39 +04:00
<Button size='large' variant='outlined' onClick={handleClickOpen}>
Faire un don par carte bancaire
</Button>
<StripeDialog
open={open}
handleClose={handleClose}
2022-03-06 10:11:02 +04:00
selectedMontant={selectedMontant}
setSelectedMontant={setSelectedMontant}
validMontant={validMontant}
setValidMontant={setValidMontant}
paymentIntent={paymentIntent}
setClientSecret={setClientSecret}
setPaymentIntent={setPaymentIntent}
setIsLoading={setIsLoading}
setClientEmail={setClientEmail}
clientEmail={clientEmail}
error={error}
setError={setError}
isLoading={isLoading}
2022-06-21 19:45:39 +04:00
>
{clientSecret && validMontant && paymentMethod === 2 && (
<Elements options={options} stripe={loadStripe(STRIPE_PUBLIC_KEY)}>
2022-06-21 19:45:39 +04:00
<CheckoutForm
validMontant={validMontant}
setError={setError}
isLoading={isLoading}
setIsLoading={setIsLoading}
/>
</Elements>
)}
</StripeDialog>
2022-03-06 10:11:02 +04:00
</TabPanel>
</Box>
2022-02-19 22:21:55 +04:00
)
}
2022-03-06 10:11:02 +04:00
PaymentMethod.defaultProps = {
selectedMontant: null,
validMontant: null,
paymentIntent: null,
clientEmail: null,
error: null,
clientSecret: null
2022-02-19 22:21:55 +04:00
}
PaymentMethod.propTypes = {
2022-03-06 10:11:02 +04:00
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,
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
2022-02-19 22:21:55 +04:00
}