I'm trying to grasp how to setup a PayPal method within React properly.
I've got three cards with each a different payment amount and cannot figure out how to pass props from my Class to the PayPal Function.
All i need is the Cost and Description to change the purchase_items[] array within the PayPal Function right?
FYI: I followed the tutorial from PayPal mainly and can make a payment, just not with the Different Costs and Descriptions i want.
PayPal.js
import React, {useRef, useEffect} from 'react';
export default function PayPal(){
const [paid, setPaid] = React.useState(false);
const paypal = useRef()
useEffect(()=> {
window.paypal.Buttons({
createOrder: (data, actions, err) => {
return actions.order.create({
intent: "CAPTURE",
purchase_units: [
{
description: "Dummy",
amount: {
currency_code: "AUD",
value: 15.49
}
}
]
})
},
onApprove: async(data, actions) => {
const order = await actions.order.capture()
setPaid(true);
console.log(order);
},
onError: (err) => {
console.error(err);
}
}).render(paypal.current)
}, [])
return(
<div className="Processing">
{paid ?(
// If the payment is made
<div>Payment successful!</div>
):(
// If any error occurs
<div>Error Occurred in processing payment! Please try again.</div>
)}
</div>
);
}
Purchases.js
import React, {Component} from 'react';
import PayPal from '../../Components/PayPal.js';
import {Card, CardActions} from '@material-ui/core';
class Purchases extends Component(){
constructor(props){
super(props);
this.state ={
cost: 5.00,
checkout: false,
desc: "Test"
};
}
setCheckout = (bool) =>{
this.setState({checkout: bool});
};
handlePayment = (price, info) =>{
this.setState(state => ({cost: price}, {desc: info}));
};
render(){
return (
<div className="Purchase">
{this.state.checkout ?(
<PayPal cost={this.state.cost} desc={this.state.desc}/>
) : (
<div>
<Card>
Payment 1 = $1 AUD
<CardActions
onClick={() =>{
setCheckout(true);
this.handlePayment(1.00, "Purchase Plan 1");
}}
/>
</Card>
<Card>
Payment 2 = $2 AUD
<CardActions
onClick={() =>{
setCheckout(true);
this.handlePayment(2.00, "Purchase Plan 2");
}}
/>
</Card>
<Card>
Payment 3 = $3 AUD
<CardActions
onClick={() =>{
setCheckout(true);
this.handlePayment(3.00, "Purchase Plan 3");
}}
/>
</Card>
</div>
)}
</div>
);
}
}
export default Purchases;