From 2df970a74aa5a41bdf04ee7e7e4715bbab30df90 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 22 May 2023 10:38:31 +0300 Subject: [PATCH] Handle payment errors in blocks --- .../resources/js/checkout-block.js | 19 ++++++++++++++++++- .../src/Gateway/ProcessPaymentTrait.php | 5 +++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index c6013d255..a3fa34bce 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -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 (
diff --git a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php index e983333bc..1772b2838 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php @@ -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(), ); }