MCPcopy Create free account
hub / github.com/basir/node-react-ecommerce / PaypalButton

Function PaypalButton

frontend/src/components/PaypalButton.js:4–51  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

2import ReactDOM from 'react-dom';
3import axios from 'axios';
4function PaypalButton(props) {
5 const [sdkReady, setSdkReady] = useState(false);
6
7 const addPaypalSdk = async () => {
8 const result = await axios.get("/api/config/paypal");
9 const clientID = result.data;
10 const script = document.createElement('script');
11 script.type = 'text/javascript';
12 script.src = 'https://www.paypal.com/sdk/js?client-id=' + clientID;
13 script.async = true;
14 script.onload = () => {
15 setSdkReady(true);
16 }
17 document.body.appendChild(script);
18 }
19
20 const createOrder = (data, actions) => actions.order.create({
21 purchase_units: [
22 {
23 amount: {
24 currency_code: 'USD',
25 value: props.amount
26 }
27 }
28 ]
29 });
30
31 const onApprove = (data, actions) => actions.order
32 .capture()
33 .then(details => props.onSuccess(data, details))
34 .catch(err => console.log(err));
35
36 useEffect(() => {
37 if (!window.paypal) {
38 addPaypalSdk();
39 }
40 return () => {
41 //
42 };
43 }, []);
44
45 if (!sdkReady) {
46 return <div>Loading...</div>
47 }
48
49 const Button = window.paypal.Buttons.driver('react', { React, ReactDOM });
50
51 return <Button {...props} createOrder={(data, actions) => createOrder(data, actions)}
52 onApprove={(data, actions) => onApprove(data, actions)} />
53}
54

Callers

nothing calls this directly

Calls 2

addPaypalSdkFunction · 0.85
createOrderFunction · 0.70

Tested by

no test coverage detected