-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
85 lines (77 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React, { useEffect } from 'react';
import PropTypes from "prop-types";
const Paypal = (props) => {
const createOrderPaypal = (data, actions) => {
return actions.order.create({
purchase_units: [
{
amount: {
currency_code: props.currency
? props.currency
: props.currency
? props.currency
: "USD",
value: props.amount.toString()
}
}
],
application_context: {
shipping_preference: props.shippingPreference
}
});
}
const onApprovePaypal = (data, actions) => {
return actions.order.capture().then((details) => {
return onSuccess(details, data);
}).catch((err) => {
return onError(err);
})
}
const onErrorPaypal = (err) => {
console.log(err);
}
useEffect(() => {
if (typeof window !== `undefined`) {
window.paypal
.Buttons({
...props,
createOrder: (props.createOrder ? props.createOrder : createOrderPaypal),
onApprove: (props.onApprove ? props.onApprove : onApprovePaypal),
onError: (props.onError ? props.onError : onErrorPaypal),
})
.render('#paypal-button');
}
}, []);
return (
<div id="smart-button-container">
<div>
<div id="paypal-button"></div>
</div>
</div>
);
};
Paypal.propTypes = {
amount: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
currency: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
shippingPreference: PropTypes.string,
onSuccess: PropTypes.func,
onError: PropTypes.func,
createOrder: PropTypes.func,
createSubscription: PropTypes.func,
onApprove: PropTypes.func,
style: PropTypes.object,
onInit: PropTypes.func,
onClick: PropTypes.func
};
Paypal.defaultProps = {
style: {},
currency: 'USD',
shippingPreference: "GET_FROM_FILE",
};
export default Paypal;