|
<!DOCTYPE html> |
|
|
|
<head> |
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
|
<meta name="viewport" content="width=device-width, initial-scale=1"> |
|
<meta charset="UTF-8"> |
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.0.0/umd/react.production.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.0.0/umd/react-dom.production.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.38/browser.js"></script> |
|
</head> |
|
|
|
<body> |
|
<div id="container"></div> |
|
|
|
<script id="checkoutjs" src="https://www.paypalobjects.com/api/checkout.min.js" async></script> |
|
|
|
<script type="text/babel"> |
|
const { React, ReactDOM } = window; |
|
|
|
class LazyPayPalButton extends React.Component { |
|
|
|
constructor() { |
|
super(); |
|
this.state = {}; |
|
} |
|
|
|
getPayPalScript(checkoutjsElement) { |
|
return new Promise(resolve => { |
|
return window.paypal |
|
? resolve(window.paypal) |
|
: document.querySelector(checkoutjsElement) |
|
.addEventListener('load', () => resolve(window.paypal)); |
|
}); |
|
} |
|
|
|
getPayPalButton(checkoutjsElement) { |
|
return this.getPayPalScript(checkoutjsElement).then(paypal => { |
|
return paypal.Button.driver('react', { React, ReactDOM }); |
|
}); |
|
} |
|
|
|
componentDidMount() { |
|
this.getPayPalButton(this.props.checkoutjsElement).then(PayPalButton => { |
|
this.setState({ PayPalButton }); |
|
}); |
|
} |
|
|
|
render() { |
|
const { checkoutjsElement, loading, ...buttonProps } = this.props; |
|
const { PayPalButton } = this.state; |
|
|
|
return PayPalButton |
|
? <PayPalButton { ...buttonProps } /> |
|
: loading |
|
} |
|
} |
|
|
|
const loading = ( |
|
<p>Loading...</p> |
|
); |
|
|
|
const style = { |
|
label: 'paypal', |
|
size: 'medium', |
|
tagline: false |
|
}; |
|
|
|
const payment = () => { |
|
// ... |
|
}; |
|
|
|
const onAuthorize = () => { |
|
// ... |
|
}; |
|
|
|
ReactDOM.render( |
|
<LazyPayPalButton |
|
checkoutjsElement='#checkoutjs' |
|
loading={ loading } |
|
style={ style } |
|
payment={ payment } |
|
onAuthorize={ onAuthorize } |
|
/>, document.querySelector('#container')); |
|
</script> |
|
</body> |
|
|