| 1 |
import React, { useState, useEffect, useRef } from 'react'; |
| 2 |
import { Helmet } from 'react-helmet'; |
| 3 |
import { clearUpCopyAndPay, generateOptions, useFetch } from './Helpers'; |
| 4 |
|
| 5 |
const ExpressComponent = React.memo((props) => { |
| 6 |
const [checkoutId, setCheckoutId] = useState(null); |
| 7 |
const shippingMethod = useRef(null); |
| 8 |
const containerRef = useRef(null); |
| 9 |
|
| 10 |
useEffect(() => { |
| 11 |
useFetch('hyperpay_prepare_checkout') |
| 12 |
.get('') |
| 13 |
.then((res) => { |
| 14 |
const id = res.data; |
| 15 |
window.wpwlOptions = generateOptions(props, shippingMethod, id); |
| 16 |
setCheckoutId(id); |
| 17 |
}); |
| 18 |
|
| 19 |
return () => clearUpCopyAndPay(); |
| 20 |
}, []); |
| 21 |
|
| 22 |
useEffect(() => { |
| 23 |
if (!checkoutId || !containerRef.current) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
containerRef.current.innerHTML = ''; |
| 28 |
|
| 29 |
const form = document.createElement('form'); |
| 30 |
form.className = 'paymentWidgets'; |
| 31 |
form.setAttribute('data-brands', props.setting.brands); |
| 32 |
|
| 33 |
containerRef.current.appendChild(form); |
| 34 |
}, [checkoutId]); |
| 35 |
|
| 36 |
return ( |
| 37 |
<div> |
| 38 |
{checkoutId && ( |
| 39 |
<> |
| 40 |
<div ref={containerRef}></div> |
| 41 |
|
| 42 |
<Helmet> |
| 43 |
<script src={`${props.setting.script_url}${checkoutId}`}></script> |
| 44 |
</Helmet> |
| 45 |
</> |
| 46 |
)} |
| 47 |
</div> |
| 48 |
); |
| 49 |
}, () => true); |
| 50 |
|
| 51 |
export default ExpressComponent; |