| 1 |
import { |
| 2 |
useCallback, |
| 3 |
useEffect, useRef, |
| 4 |
useState, |
| 5 |
} from '@wordpress/element' |
| 6 |
import apiFetch from '@wordpress/api-fetch' |
| 7 |
import { __ } from '@wordpress/i18n' |
| 8 |
|
| 9 |
export default function CheckoutPage () { |
| 10 |
const checkoutData = window.VippsRecurringCheckout |
| 11 |
const { continueShoppingUrl } = window.VippsMobilePaySettings |
| 12 |
|
| 13 |
const [sessionStatus, setSessionStatus] = useState(null) |
| 14 |
const sessionPollHandler = useRef(null) |
| 15 |
|
| 16 |
const iframeRef = useRef(null) |
| 17 |
|
| 18 |
useEffect(() => { |
| 19 |
if (!checkoutData.redirect_url) { |
| 20 |
return |
| 21 |
} |
| 22 |
|
| 23 |
window.location.href = checkoutData.redirect_url |
| 24 |
}, [checkoutData]) |
| 25 |
|
| 26 |
const pollSessionStatus = useCallback(() => { |
| 27 |
apiFetch({ |
| 28 |
path: `/vipps-mobilepay-recurring/v1/checkout/session`, |
| 29 |
method: 'GET', |
| 30 |
}).then(response => { |
| 31 |
setSessionStatus(response) |
| 32 |
}) |
| 33 |
}, []) |
| 34 |
|
| 35 |
useEffect(() => { |
| 36 |
if (!checkoutData.session.token || !checkoutData.success) { |
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
sessionPollHandler.current = setInterval(pollSessionStatus, 20_000) |
| 41 |
|
| 42 |
return () => { |
| 43 |
clearInterval(sessionPollHandler.current) |
| 44 |
} |
| 45 |
}, [checkoutData]) |
| 46 |
|
| 47 |
useEffect(() => { |
| 48 |
if (!sessionStatus) { |
| 49 |
return |
| 50 |
} |
| 51 |
|
| 52 |
if (sessionStatus.status === 'EXPIRED') { |
| 53 |
clearInterval(sessionPollHandler.current) |
| 54 |
|
| 55 |
return |
| 56 |
} |
| 57 |
|
| 58 |
if (!sessionStatus.redirect_url) { |
| 59 |
return |
| 60 |
} |
| 61 |
|
| 62 |
window.location.href = sessionStatus.redirect_url |
| 63 |
}, [sessionStatus]) |
| 64 |
|
| 65 |
const listenToIframe = useCallback((e) => { |
| 66 |
const src = iframeRef.current.getAttribute('src') |
| 67 |
const origin = new URL(src).origin |
| 68 |
|
| 69 |
if (e.origin !== origin) return |
| 70 |
|
| 71 |
if (e.data.type === 'resize') { |
| 72 |
iframeRef.current.style.height = `${e.data.frameHeight}px` |
| 73 |
} |
| 74 |
|
| 75 |
if (e.data.type === 'payment_url') { |
| 76 |
window.location.href = e.data.paymentUrl |
| 77 |
} |
| 78 |
}, [iframeRef.current]) |
| 79 |
|
| 80 |
useEffect(() => { |
| 81 |
if (!iframeRef.current) { |
| 82 |
return |
| 83 |
} |
| 84 |
|
| 85 |
window.addEventListener('message', listenToIframe) |
| 86 |
|
| 87 |
return () => { |
| 88 |
window.removeEventListener('message', listenToIframe) |
| 89 |
} |
| 90 |
}, [iframeRef.current, checkoutData.session.token]) |
| 91 |
|
| 92 |
return <form id="vippsdata" className="woocommerce-checkout"> |
| 93 |
<div className={'vipps-recurring-checkout-page'}> |
| 94 |
{(!checkoutData.session.success && |
| 95 |
checkoutData.session.msg?.length > 0) ? <div |
| 96 |
className={'vipps-recurring-checkout-page__error'}> |
| 97 |
<p> |
| 98 |
{checkoutData.session.msg} |
| 99 |
</p> |
| 100 |
|
| 101 |
<p> |
| 102 |
<a href={continueShoppingUrl} |
| 103 |
className={'btn button vipps-recurring-checkout-page__error__action'}> |
| 104 |
{__('Continue shopping', |
| 105 |
'woo-vipps')} |
| 106 |
</a> |
| 107 |
</p> |
| 108 |
</div> : <div className={'vipps-recurring-checkout-page__loading'}> |
| 109 |
{(sessionStatus?.status !== 'EXPIRED') && <> |
| 110 |
{!checkoutData.session.token && <div |
| 111 |
className={'vipps-recurring-checkout-page__loading__spinner'}/>} |
| 112 |
|
| 113 |
{checkoutData.session.token && ( |
| 114 |
<iframe ref={iframeRef} |
| 115 |
src={`${(checkoutData.session.checkoutFrontendUrl || |
| 116 |
checkoutData.session.src)}?token=${checkoutData.session.token}&lang=${checkoutData.language}`} |
| 117 |
frameBorder="0" width={'100%'}></iframe> |
| 118 |
)} |
| 119 |
</>} |
| 120 |
|
| 121 |
{sessionStatus?.status === 'EXPIRED' && <div> |
| 122 |
{__('Checkout session expired. Please refresh to start a new session.', |
| 123 |
'woo-vipps')} |
| 124 |
</div>} |
| 125 |
</div>} |
| 126 |
</div> |
| 127 |
</form> |
| 128 |
} |
| 129 |
|