PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 6.1.5
Pay with Vipps and MobilePay for WooCommerce v6.1.5
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.10 6.1.9 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 5.4.3 5.4.2 All 187 releases
woo-vipps / recurring / src / pages / PaymentRedirectPage.jsx

PaymentRedirectPage.jsx in Pay with Vipps and MobilePay for WooCommerce 6.1.5, at recurring/src/pages/PaymentRedirectPage.jsx

81 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 useCallback,
3 useEffect,
4 useMemo,
5 useRef,
6 useState,
7 } from '@wordpress/element'
8 import apiFetch from '@wordpress/api-fetch'
9 import PaymentRedirect from '../components/PaymentRedirectPage/PaymentRedirect'
10 import PaymentCancelled
11 from '../components/PaymentRedirectPage/PaymentCancelled'
12 import PaymentError from '../components/PaymentRedirectPage/PaymentError'
13
14 export default function PaymentRedirectPage () {
15 const { logo, continueShoppingUrl } = window.VippsMobilePaySettings
16 const searchParams = new URLSearchParams(window.location.search)
17
18 const [response, setResponse] = useState(null)
19 const [error, setError] = useState(false)
20 const [errorCounter, setErrorCounter] = useState(0)
21 const intervalHandlerRef = useRef(null)
22
23 const checkStatus = useCallback(() => {
24 apiFetch({
25 path: `/vipps-mobilepay-recurring/v1/orders/status/${searchParams.get(
26 'order_id')}?key=${searchParams.get('key')}`, method: 'GET',
27 }).then(response => setResponse(response)).catch(() => {
28 setErrorCounter((value) => value += 1)
29 })
30 }, [])
31
32 useEffect(() => {
33 checkStatus();
34 intervalHandlerRef.current = setInterval(checkStatus, 10_000)
35
36 return () => clearInterval(intervalHandlerRef.current)
37 }, [])
38
39 useEffect(() => {
40 setError(errorCounter >= 4)
41 }, [errorCounter])
42
43 useEffect(() => {
44 if (error) {
45 clearInterval(intervalHandlerRef.current)
46
47 return
48 }
49
50 if (!response || response.status === 'PENDING' || cancelled) {
51 return
52 }
53
54 clearInterval(intervalHandlerRef.current)
55
56 if (response.redirect_url) {
57 window.location.href = response.redirect_url
58 }
59 }, [response, error])
60
61 const cancelled = useMemo(() => {
62 if (!response) {
63 return false
64 }
65
66 return ['EXPIRED', 'STOPPED'].includes(response.status)
67 }, [response])
68
69 return <>
70 {!error && <>
71 {!cancelled
72 ? <PaymentRedirect logo={logo}/>
73 : <PaymentCancelled logo={logo}
74 continueShoppingUrl={continueShoppingUrl}/>}
75 </>}
76
77 {error && <PaymentError logo={logo}
78 continueShoppingUrl={continueShoppingUrl}/>}
79 </>
80 }
81