Skip to content

Commit

Permalink
Fix ECE exception when payment fails (#10048)
Browse files Browse the repository at this point in the history
Co-authored-by: Alfredo Sumaran <[email protected]>
  • Loading branch information
cesarcosta99 and asumaran authored Jan 3, 2025
1 parent 75b102d commit 6846918
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 28 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-9990-payment-failed-exception
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix JS exception when ECE payment fails.
3 changes: 1 addition & 2 deletions client/express-checkout/blocks/hooks/use-express-checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export const useExpressCheckout = ( {
window.location = redirectUrl;
};

const abortPayment = ( onConfirmEvent, message ) => {
onConfirmEvent.paymentFailed( { reason: 'fail' } );
const abortPayment = ( message ) => {
setExpressPaymentError( message );
onAbortPaymentHandler();
};
Expand Down
6 changes: 2 additions & 4 deletions client/express-checkout/event-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ export const onConfirmHandler = async (
) => {
const { error: submitError } = await elements.submit();
if ( submitError ) {
return abortPayment( event, submitError.message );
return abortPayment( submitError.message );
}

const { paymentMethod, error } = await stripe.createPaymentMethod( {
elements,
} );

if ( error ) {
return abortPayment( event, error.message );
return abortPayment( error.message );
}

try {
Expand All @@ -104,7 +104,6 @@ export const onConfirmHandler = async (

if ( orderResponse.result !== 'success' ) {
return abortPayment(
event,
getErrorMessageFromNotice( orderResponse.messages )
);
}
Expand All @@ -121,7 +120,6 @@ export const onConfirmHandler = async (
}
} catch ( e ) {
return abortPayment(
event,
e.message ??
__(
'There was a problem processing the order.',
Expand Down
6 changes: 2 additions & 4 deletions client/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ jQuery( ( $ ) => {
/**
* Abort the payment and display error messages.
*
* @param {PaymentResponse} payment Payment response instance.
* @param {string} message Error message to display.
* @param {string} message Error message to display.
*/
abortPayment: ( payment, message ) => {
payment.paymentFailed( { reason: 'fail' } );
abortPayment: ( message ) => {
onAbortPaymentHandler();

$( '.woocommerce-error' ).remove();
Expand Down
10 changes: 1 addition & 9 deletions client/express-checkout/test/event-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,7 @@ describe( 'Express checkout event handlers', () => {
);

expect( elements.submit ).toHaveBeenCalled();
expect( abortPayment ).toHaveBeenCalledWith(
event,
'Submit error'
);
expect( abortPayment ).toHaveBeenCalledWith( 'Submit error' );
expect( completePayment ).not.toHaveBeenCalled();
} );

Expand All @@ -310,7 +307,6 @@ describe( 'Express checkout event handlers', () => {
elements,
} );
expect( abortPayment ).toHaveBeenCalledWith(
event,
'Payment method error'
);
expect( completePayment ).not.toHaveBeenCalled();
Expand Down Expand Up @@ -340,7 +336,6 @@ describe( 'Express checkout event handlers', () => {
expectedOrderData
);
expect( abortPayment ).toHaveBeenCalledWith(
event,
'Order creation error'
);
expect( completePayment ).not.toHaveBeenCalled();
Expand Down Expand Up @@ -432,7 +427,6 @@ describe( 'Express checkout event handlers', () => {
'https://example.com/redirect'
);
expect( abortPayment ).toHaveBeenCalledWith(
event,
'Intent confirmation error'
);
expect( completePayment ).not.toHaveBeenCalled();
Expand Down Expand Up @@ -467,7 +461,6 @@ describe( 'Express checkout event handlers', () => {
expectedOrderData
);
expect( abortPayment ).toHaveBeenCalledWith(
event,
'Order creation error'
);
expect( completePayment ).not.toHaveBeenCalled();
Expand Down Expand Up @@ -562,7 +555,6 @@ describe( 'Express checkout event handlers', () => {
'https://example.com/redirect'
);
expect( abortPayment ).toHaveBeenCalledWith(
event,
'Intent confirmation error'
);
expect( completePayment ).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export const useExpressCheckout = ( {
window.location = redirectUrl;
};

const abortPayment = ( onConfirmEvent, message ) => {
onConfirmEvent.paymentFailed( { reason: 'fail' } );
const abortPayment = ( message ) => {
setExpressPaymentError( message );
onAbortPaymentHandler();
};
Expand Down
6 changes: 2 additions & 4 deletions client/tokenized-express-checkout/event-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ export const onConfirmHandler = async (
) => {
const { error: submitError } = await elements.submit();
if ( submitError ) {
return abortPayment( event, submitError.message );
return abortPayment( submitError.message );
}

const { paymentMethod, error } = await stripe.createPaymentMethod( {
elements,
} );

if ( error ) {
return abortPayment( event, error.message );
return abortPayment( error.message );
}

try {
Expand All @@ -133,7 +133,6 @@ export const onConfirmHandler = async (

if ( orderResponse.payment_result.payment_status !== 'success' ) {
return abortPayment(
event,
getErrorMessageFromNotice(
orderResponse.message ??
orderResponse.payment_result?.payment_details.find(
Expand Down Expand Up @@ -163,7 +162,6 @@ export const onConfirmHandler = async (
}

return abortPayment(
event,
getErrorMessageFromNotice(
e.message ||
e.payment_result?.payment_details.find(
Expand Down
4 changes: 1 addition & 3 deletions client/tokenized-express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ jQuery( ( $ ) => {
/**
* Abort the payment and display error messages.
*
* @param {PaymentResponse} payment Payment response instance.
* @param {string} message Error message to display.
*/
abortPayment: ( payment, message ) => {
payment.paymentFailed( { reason: 'fail' } );
abortPayment: ( message ) => {
onAbortPaymentHandler();

$( '.woocommerce-error' ).remove();
Expand Down

0 comments on commit 6846918

Please sign in to comment.