Enhancement: Changing shopper feedback on payment response failure (#45961)

* Changing shopper feedback on payment response issues

* Add changefile(s) from automation for the following project(s): woocommerce

* Disambiguated error log

* Prevent unexpected error response from outputting technical data to the user. Instead display an helpful message to prevent retries.

* Fix PHP linting.

* Changed error message and added translators comment

* Safeguarding against wc_checkout_params.i18n_checkout_error definition

* Added null check
This commit is contained in:
Paulo Arromba 2024-04-05 15:21:47 +01:00 committed by GitHub
parent 0c1e4efc89
commit afe19f2f08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 8 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: enhancement
Comment: Changed shopper feedback on payment response failure

View File

@ -501,13 +501,13 @@ jQuery( function( $ ) {
var maybe_valid_json = raw_response.match( /{"result.*}/ );
if ( null === maybe_valid_json ) {
console.log( 'Unable to fix malformed JSON' );
console.log( 'Unable to fix malformed JSON #1' );
} else if ( wc_checkout_form.is_valid_json( maybe_valid_json[0] ) ) {
console.log( 'Fixed malformed JSON. Original:' );
console.log( raw_response );
raw_response = maybe_valid_json[0];
} else {
console.log( 'Unable to fix malformed JSON' );
console.log( 'Unable to fix malformed JSON #2' );
}
}
@ -560,10 +560,21 @@ jQuery( function( $ ) {
// Detach the unload handler that prevents a reload / redirect
wc_checkout_form.detachUnloadEventsOnSubmit();
// This is just a technical error fallback. i18_checkout_error is expected to be always defined and localized.
var errorMessage = errorThrown;
if (
typeof wc_checkout_params === 'object' &&
wc_checkout_params !== null &&
wc_checkout_params.hasOwnProperty( 'i18n_checkout_error' ) &&
typeof wc_checkout_params.i18n_checkout_error === 'string' &&
wc_checkout_params.i18n_checkout_error.trim() !== ''
) {
errorMessage = wc_checkout_params.i18n_checkout_error;
}
wc_checkout_form.submit_error(
'<div class="woocommerce-error">' +
( errorThrown || wc_checkout_params.i18n_checkout_error ) +
'</div>'
'<div class="woocommerce-error">' + errorMessage + '</div>'
);
}
});

View File

@ -1050,11 +1050,11 @@ class WC_Checkout {
return;
}
// Store Order ID in session so it can be re-used after payment failure.
// Store Order ID in session, so it can be re-used after payment failure.
WC()->session->set( 'order_awaiting_payment', $order_id );
// We save the session early because if the payment gateway hangs
// the request will never finish, thus the session data will neved be saved,
// the request will never finish, thus the session data will never be saved,
// and this can lead to duplicate orders if the user submits the order again.
WC()->session->save_data();
@ -1073,6 +1073,7 @@ class WC_Checkout {
exit;
}
// Using wp_send_json will gracefully handle any problem encoding data.
wp_send_json( $result );
}
}
@ -1287,6 +1288,7 @@ class WC_Checkout {
* since it could be empty see:
* https://github.com/woocommerce/woocommerce/issues/24631
*/
if ( apply_filters( 'woocommerce_cart_needs_payment', $order->needs_payment(), WC()->cart ) ) {
$this->process_order_payment( $order_id, $posted_data['payment_method'] );
} else {

View File

@ -541,7 +541,9 @@ class WC_Frontend_Scripts {
'checkout_url' => WC_AJAX::get_endpoint( 'checkout' ),
'is_checkout' => is_checkout() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0,
'debug_mode' => Constants::is_true( 'WP_DEBUG' ),
'i18n_checkout_error' => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ),
/* translators: %s: Order history URL on My Account section */
'i18n_checkout_error' => sprintf( esc_attr__( 'There was an error processing your order. Please check for any charges in your payment method and review your <a href="%s">order history</a> before placing the order again.', 'woocommerce' ), esc_url( wc_get_account_endpoint_url( 'orders' ) ) ),
);
break;
case 'wc-address-i18n':