Handle payment errors in blocks

This commit is contained in:
Alex P 2023-05-22 10:38:31 +03:00
parent fe08c2d4f4
commit 2df970a74a
No known key found for this signature in database
GPG key ID: 54487A734A204D71
2 changed files with 21 additions and 3 deletions

View file

@ -17,7 +17,7 @@ const PayPalComponent = ({
activePaymentMethod,
shippingData,
}) => {
const {onPaymentSetup} = eventRegistration;
const {onPaymentSetup, onCheckoutAfterProcessingWithError} = eventRegistration;
const {responseTypes} = emitResponse;
const [paypalOrder, setPaypalOrder] = useState(null);
@ -205,6 +205,23 @@ const PayPalComponent = ({
};
}, [onPaymentSetup, paypalOrder, activePaymentMethod]);
useEffect(() => {
const unsubscribe = onCheckoutAfterProcessingWithError(({ processingResponse }) => {
if (onClose) {
onClose();
}
if (processingResponse?.paymentDetails?.errorMessage) {
return {
type: emitResponse.responseTypes.ERROR,
message: processingResponse.paymentDetails.errorMessage,
messageContext: config.scriptData.continuation ? emitResponse.noticeContexts.PAYMENTS : emitResponse.noticeContexts.EXPRESS_PAYMENTS,
};
}
return true;
});
return unsubscribe;
}, [onCheckoutAfterProcessingWithError, onClose]);
if (config.scriptData.continuation) {
return (
<div dangerouslySetInnerHTML={{__html: config.scriptData.continuation.cancel.html}}>

View file

@ -81,8 +81,9 @@ trait ProcessPaymentTrait {
wc_add_notice( $error->getMessage(), 'error' );
return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
'errorMessage' => $error->getMessage(),
);
}