|
30 | 30 | return select( 'wc/store/cart' ).getCartTotals(); |
31 | 31 | }, [] ); |
32 | 32 |
|
33 | | - useEffect( () => { |
34 | | - // Don't modify the Place Order button if using redirect flow |
35 | | - if ( isRedirectFlow ) { |
36 | | - return; |
37 | | - } |
38 | | - |
39 | | - const placeOrderButton = document.querySelector( |
40 | | - '.wc-block-components-checkout-place-order-button' |
41 | | - ); |
42 | | - if ( activePaymentMethod === 'monei_paypal' ) { |
43 | | - if ( placeOrderButton ) { |
44 | | - //on hover over the button the text should not change color to white |
45 | | - placeOrderButton.style.color = 'black'; |
46 | | - placeOrderButton.style.backgroundColor = '#ccc'; |
47 | | - placeOrderButton.disabled = true; |
48 | | - } |
49 | | - } |
50 | | - return () => { |
51 | | - if ( paypalInstanceRef.current ) { |
52 | | - paypalInstanceRef.current.close(); |
53 | | - paypalInstanceRef.current = null; |
54 | | - } |
55 | | - if ( placeOrderButton ) { |
56 | | - placeOrderButton.style.color = ''; |
57 | | - placeOrderButton.style.backgroundColor = ''; |
58 | | - placeOrderButton.disabled = false; |
59 | | - } |
60 | | - }; |
61 | | - }, [ activePaymentMethod, isRedirectFlow ] ); |
62 | | - |
63 | 33 | /** |
64 | | - * Initialize MONEI PayPal component and handle token creation. |
| 34 | + * Create or re-render PayPal instance with specified amount |
| 35 | + * @param {number} amount - Payment amount in cents |
65 | 36 | */ |
66 | | - const initMoneiPayPal = useCallback( () => { |
67 | | - // eslint-disable-next-line no-undef |
68 | | - if ( typeof monei === 'undefined' || ! monei.PayPal ) { |
69 | | - console.error( 'MONEI SDK is not available' ); |
70 | | - return; |
71 | | - } |
72 | | - |
73 | | - const currentTotal = cartTotals?.total_price |
74 | | - ? parseInt( cartTotals.total_price ) |
75 | | - : Math.round( paypalData.total * 100 ); |
76 | | - |
77 | | - lastAmountRef.current = currentTotal; |
78 | | - |
| 37 | + const createOrRenderPayPal = useCallback( ( amount ) => { |
79 | 38 | // Clean up existing instance |
80 | 39 | if ( paypalInstanceRef.current?.close ) { |
81 | 40 | try { |
|
103 | 62 | accountId: paypalData.accountId, |
104 | 63 | sessionId: paypalData.sessionId, |
105 | 64 | language: paypalData.language, |
106 | | - amount: currentTotal, |
| 65 | + amount, |
107 | 66 | currency: paypalData.currency, |
108 | 67 | style: paypalData.paypalStyle || {}, |
109 | 68 | onSubmit( result ) { |
110 | 69 | if ( result.token ) { |
111 | | - setError( '' ); // Clear any previous errors |
| 70 | + setError( '' ); |
112 | 71 | requestTokenRef.current = result.token; |
113 | 72 | const placeOrderButton = document.querySelector( |
114 | 73 | '.wc-block-components-checkout-place-order-button' |
|
141 | 100 | setTimeout( () => { |
142 | 101 | setIsLoading( false ); |
143 | 102 | }, 1000 ); |
144 | | - }, [ cartTotals ] ); |
| 103 | + }, [] ); |
| 104 | + |
| 105 | + useEffect( () => { |
| 106 | + // Don't modify the Place Order button if using redirect flow |
| 107 | + if ( isRedirectFlow ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const placeOrderButton = document.querySelector( |
| 112 | + '.wc-block-components-checkout-place-order-button' |
| 113 | + ); |
| 114 | + if ( activePaymentMethod === 'monei_paypal' ) { |
| 115 | + if ( placeOrderButton ) { |
| 116 | + //on hover over the button the text should not change color to white |
| 117 | + placeOrderButton.style.color = 'black'; |
| 118 | + placeOrderButton.style.backgroundColor = '#ccc'; |
| 119 | + placeOrderButton.disabled = true; |
| 120 | + } |
| 121 | + } |
| 122 | + return () => { |
| 123 | + if ( paypalInstanceRef.current ) { |
| 124 | + paypalInstanceRef.current.close(); |
| 125 | + paypalInstanceRef.current = null; |
| 126 | + } |
| 127 | + if ( placeOrderButton ) { |
| 128 | + placeOrderButton.style.color = ''; |
| 129 | + placeOrderButton.style.backgroundColor = ''; |
| 130 | + placeOrderButton.disabled = false; |
| 131 | + } |
| 132 | + }; |
| 133 | + }, [ activePaymentMethod, isRedirectFlow ] ); |
| 134 | + |
| 135 | + /** |
| 136 | + * Initialize MONEI PayPal component and handle token creation. |
| 137 | + */ |
| 138 | + const initMoneiPayPal = useCallback( () => { |
| 139 | + // eslint-disable-next-line no-undef |
| 140 | + if ( typeof monei === 'undefined' || ! monei.PayPal ) { |
| 141 | + console.error( 'MONEI SDK is not available' ); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + const currentTotal = cartTotals?.total_price |
| 146 | + ? parseInt( cartTotals.total_price ) |
| 147 | + : Math.round( paypalData.total * 100 ); |
| 148 | + |
| 149 | + lastAmountRef.current = currentTotal; |
| 150 | + createOrRenderPayPal( currentTotal ); |
| 151 | + }, [ cartTotals, createOrRenderPayPal ] ); |
145 | 152 |
|
146 | 153 | /** |
147 | 154 | * Update the amount in the existing PayPal instance |
|
159 | 166 | lastAmountRef.current = currentTotal; |
160 | 167 |
|
161 | 168 | if ( paypalInstanceRef.current ) { |
162 | | - // Clean up existing instance |
163 | | - if ( paypalInstanceRef.current?.close ) { |
164 | | - try { |
165 | | - paypalInstanceRef.current.close(); |
166 | | - } catch ( e ) { |
167 | | - // Silent fail |
168 | | - } |
169 | | - } |
170 | | - |
171 | | - const paypalContainer = |
172 | | - document.getElementById( 'paypal-container' ); |
173 | | - if ( ! paypalContainer ) { |
174 | | - return; |
175 | | - } |
176 | | - |
177 | | - // Show skeleton loading state |
178 | | - setIsLoading( true ); |
179 | | - |
180 | | - // Clear container |
181 | | - paypalContainer.innerHTML = ''; |
182 | | - |
183 | | - // eslint-disable-next-line no-undef |
184 | | - const paypalInstance = monei.PayPal( { |
185 | | - accountId: paypalData.accountId, |
186 | | - sessionId: paypalData.sessionId, |
187 | | - language: paypalData.language, |
188 | | - amount: currentTotal, |
189 | | - currency: paypalData.currency, |
190 | | - style: paypalData.paypalStyle || {}, |
191 | | - onSubmit( result ) { |
192 | | - if ( result.token ) { |
193 | | - setError( '' ); |
194 | | - requestTokenRef.current = result.token; |
195 | | - const placeOrderButton = document.querySelector( |
196 | | - '.wc-block-components-checkout-place-order-button' |
197 | | - ); |
198 | | - if ( placeOrderButton ) { |
199 | | - placeOrderButton.style.color = ''; |
200 | | - placeOrderButton.style.backgroundColor = ''; |
201 | | - placeOrderButton.disabled = false; |
202 | | - placeOrderButton.click(); |
203 | | - } else { |
204 | | - console.error( |
205 | | - 'Place Order button not found.' |
206 | | - ); |
207 | | - } |
208 | | - } |
209 | | - }, |
210 | | - onError( error ) { |
211 | | - const errorMessage = |
212 | | - error.message || |
213 | | - `${ error.status || 'Error' } - ${ |
214 | | - error.statusMessage || 'Payment failed' |
215 | | - }`; |
216 | | - setError( errorMessage ); |
217 | | - console.error( 'PayPal error:', error ); |
218 | | - }, |
219 | | - } ); |
220 | | - |
221 | | - paypalInstance.render( paypalContainer ); |
222 | | - paypalInstanceRef.current = paypalInstance; |
223 | | - |
224 | | - // Remove skeleton loading state after rendering |
225 | | - setTimeout( () => { |
226 | | - setIsLoading( false ); |
227 | | - }, 1000 ); |
| 169 | + createOrRenderPayPal( currentTotal ); |
228 | 170 | } |
229 | | - }, [ cartTotals ] ); |
| 171 | + }, [ cartTotals, createOrRenderPayPal ] ); |
230 | 172 |
|
231 | 173 | // Initialize on mount |
232 | 174 | useEffect( () => { |
|
0 commit comments