Skip to content

Commit 0488427

Browse files
committed
fix: add null checks and fallbacks to all classic payment methods
- PayPal: Add fallback from monei-paypal-form to wc-monei_paypal-form - Apple/Google Pay: Add null check before appending hidden input - Credit Card: Add null check before appending hidden input - All methods now log errors when form container not found - Prevents exceptions in order-pay flow when form IDs differ - Consistent error handling across all payment methods
1 parent bd25b6b commit 0488427

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

assets/js/monei-apple-google-classic.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,15 @@
244244
hiddenInput.setAttribute( 'id', id );
245245
hiddenInput.setAttribute( 'value', token );
246246
wc_monei_form.$paymentForm = document.getElementById( form );
247-
wc_monei_form.$paymentForm.appendChild( hiddenInput );
247+
248+
// Only append if we found a target element
249+
if ( wc_monei_form.$paymentForm ) {
250+
wc_monei_form.$paymentForm.appendChild( hiddenInput );
251+
} else {
252+
console.error(
253+
`Apple/Google Pay form container "${ form }" not found. Cannot append payment token.`
254+
);
255+
}
248256
},
249257
/**
250258
* If Apple can make payments then we need to show the apple logo and title instead of Google

assets/js/monei-cc-classic.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,15 @@
346346
hiddenInput.setAttribute( 'id', id );
347347
hiddenInput.setAttribute( 'value', token );
348348
wc_monei_form.$paymentForm = document.getElementById( form );
349-
wc_monei_form.$paymentForm.appendChild( hiddenInput );
349+
350+
// Only append if we found a target element
351+
if ( wc_monei_form.$paymentForm ) {
352+
wc_monei_form.$paymentForm.appendChild( hiddenInput );
353+
} else {
354+
console.error(
355+
`Credit Card form container "${ form }" not found. Cannot append payment token.`
356+
);
357+
}
350358
},
351359
render_card_brands_in_label() {
352360
if ( ! wc_monei_params.cardBrands ) {

assets/js/monei-paypal-classic.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,20 @@
227227
hiddenInput.setAttribute( 'name', id );
228228
hiddenInput.setAttribute( 'id', id );
229229
hiddenInput.setAttribute( 'value', token );
230+
231+
// Try primary form ID first, then fall back to order-pay form ID
230232
wc_paypal_form.$paymentForm =
231-
document.getElementById( 'monei-paypal-form' );
232-
wc_paypal_form.$paymentForm.appendChild( hiddenInput );
233+
document.getElementById( 'monei-paypal-form' ) ||
234+
document.getElementById( 'wc-monei_paypal-form' );
235+
236+
// Only append if we found a target element
237+
if ( wc_paypal_form.$paymentForm ) {
238+
wc_paypal_form.$paymentForm.appendChild( hiddenInput );
239+
} else {
240+
console.error(
241+
'PayPal form container not found. Cannot append payment token.'
242+
);
243+
}
233244
},
234245
};
235246

0 commit comments

Comments
 (0)