Skip to content

Commit fee6b06

Browse files
committed
fix: handle error objects properly in classic checkout and hooks
Convert error objects to strings before displaying to users. result.error and event.error may be objects with .message property. - Extract error.message when available - Fall back to error if it's already a string - Use descriptive fallback messages - Apply to monei-cc-classic.js onChange, place_order, place_order_page - Apply to monei-card-input-hooks.js onChange and createToken
1 parent 13e7fa4 commit fee6b06

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

assets/js/helpers/monei-card-input-hooks.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ export const useMoneiCardInput = ( config ) => {
106106
},
107107
onChange( event ) {
108108
if ( event.isTouched && event.error ) {
109-
setError( event.error );
109+
const errorMessage =
110+
event.error.message ||
111+
( typeof event.error === 'string'
112+
? event.error
113+
: 'Validation error' );
114+
setError( errorMessage );
110115
setIsValid( false );
111116
if ( containerRef.current ) {
112117
containerRef.current.classList.add( 'is-invalid' );
@@ -162,7 +167,12 @@ export const useMoneiCardInput = ( config ) => {
162167
const result = await monei.createToken( cardInputRef.current );
163168

164169
if ( result.error ) {
165-
setError( result.error );
170+
const errorMessage =
171+
result.error.message ||
172+
( typeof result.error === 'string'
173+
? result.error
174+
: 'Token creation failed' );
175+
setError( errorMessage );
166176
return null;
167177
}
168178

assets/js/monei-cc-classic.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@
193193
onChange( event ) {
194194
// Handle real-time validation errors.
195195
if ( event.isTouched && event.error ) {
196-
wc_monei_form.print_errors( event.error );
196+
const errorMessage =
197+
event.error.message ||
198+
( typeof event.error === 'string'
199+
? event.error
200+
: 'Validation error' );
201+
wc_monei_form.print_errors( errorMessage );
197202
} else {
198203
wc_monei_form.clear_errors();
199204
}
@@ -233,7 +238,12 @@
233238
if ( result.error ) {
234239
// Error displayed via print_errors
235240
// Inform the user if there was an error.
236-
wc_monei_form.print_errors( result.error );
241+
const errorMessage =
242+
result.error.message ||
243+
( typeof result.error === 'string'
244+
? result.error
245+
: 'Payment error' );
246+
wc_monei_form.print_errors( errorMessage );
237247
} else {
238248
// Token created successfully
239249
// Create monei token and append it to DOM
@@ -267,7 +277,12 @@
267277
if ( result.error ) {
268278
// Error displayed via print_errors
269279
// Inform the user if there was an error.
270-
wc_monei_form.print_errors( result.error );
280+
const errorMessage =
281+
result.error.message ||
282+
( typeof result.error === 'string'
283+
? result.error
284+
: 'Payment error' );
285+
wc_monei_form.print_errors( errorMessage );
271286
} else {
272287
// Token created successfully
273288
// Create monei token and append it to DOM

src/Gateways/Blocks/MoneiAppleGoogleBlocksSupport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public function is_active() {
3737
return false;
3838
}
3939

40+
// Hide when neither Apple nor Google is available
41+
if ( ! $this->gateway->isAppleAvailable() && ! $this->gateway->isGoogleAvailable() ) {
42+
return false;
43+
}
44+
4045
return 'yes' === ( $this->get_setting( 'enabled' ) ?? 'no' );
4146
}
4247

src/Services/MoneiApplePayVerificationService.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,23 @@ public function apple_domain_register() {
4747
}
4848

4949
try {
50-
$domain = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field($_SERVER['HTTP_HOST']) : str_replace(array('https://', 'http://'), '', get_site_url()); // @codingStandardsIgnoreLine
50+
// Extract domain from HTTP_HOST or get_site_url()
51+
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
52+
// Parse HTTP_HOST to remove port portion and get host only
53+
$host_parts = explode( ':', $_SERVER['HTTP_HOST'] );
54+
$domain = sanitize_text_field( $host_parts[0] );
55+
} else {
56+
// Use wp_parse_url to extract host from get_site_url()
57+
$parsed_url = wp_parse_url( get_site_url() );
58+
$domain = isset( $parsed_url['host'] ) ? sanitize_text_field( $parsed_url['host'] ) : '';
59+
}
60+
61+
// Ensure domain is not empty before registering
62+
if ( empty( $domain ) ) {
63+
WC_Monei_Logger::log( 'Apple Pay domain registration failed: empty domain', 'error' );
64+
WC_Admin_Settings::add_error( __( 'Apple Pay domain registration failed: unable to determine domain.', 'monei' ) );
65+
return;
66+
}
5167

5268
$this->moneiPaymentServices->register_apple_domain( $domain );
5369

0 commit comments

Comments
 (0)