diff --git a/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php b/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php index d515ba16d..39df60f40 100644 --- a/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php @@ -329,7 +329,20 @@ class OrderEndpoint { ); throw $error; } + $order = $this->order_factory->from_paypal_response( $json ); + + $purchase_units_payments_captures_status = $order->purchase_units()[0]->payments()->captures()[0]->status() ?? ''; + if ( $purchase_units_payments_captures_status && 'DECLINED' === $purchase_units_payments_captures_status ) { + throw new RuntimeException( + sprintf( + // translators: %s is the current status. + __( 'Purchase units payments captures status %s', 'woocommerce-paypal-payments' ), + $purchase_units_payments_captures_status + ) + ); + } + return $order; } diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php b/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php index 8f8006b29..dc8b72de7 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php @@ -10,6 +10,7 @@ declare( strict_types=1 ); namespace WooCommerce\PayPalCommerce\WcGateway\Gateway; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; +use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; /** * Trait ProcessPaymentTrait @@ -71,7 +72,15 @@ trait ProcessPaymentTrait { } $this->session_handler->destroy_session_data(); + } catch ( RuntimeException $error ) { + $this->session_handler->destroy_session_data(); + wc_add_notice( + __( 'Payment provider declined the payment, please use a different payment method.', 'woocommerce-paypal-payments' ), + 'error' + ); + return null; } + wc_add_notice( $this->order_processor->last_error(), 'error' diff --git a/modules/ppcp-wc-gateway/src/Processor/class-orderprocessor.php b/modules/ppcp-wc-gateway/src/Processor/class-orderprocessor.php index 70231592f..14cec7266 100644 --- a/modules/ppcp-wc-gateway/src/Processor/class-orderprocessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/class-orderprocessor.php @@ -168,14 +168,10 @@ class OrderProcessor { __( 'Awaiting payment.', 'woocommerce-paypal-payments' ) ); if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'CAPTURE' ) { - - $purchase_units_payments_captures_status = $order->purchase_units()[0]->payments()->captures()[0]->status() ?? ''; - if ( $purchase_units_payments_captures_status && 'DECLINED' !== $purchase_units_payments_captures_status ) { - $wc_order->update_status( - 'processing', - __( 'Payment received.', 'woocommerce-paypal-payments' ) - ); - } + $wc_order->update_status( + 'processing', + __( 'Payment received.', 'woocommerce-paypal-payments' ) + ); } if ( $this->capture_authorized_downloads( $order ) && $this->authorized_payments_processor->process( $wc_order ) ) {