From fa0508d57244f4e1906dcd6d60ee76302d30fa17 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Mon, 12 Jun 2023 08:46:45 +0200 Subject: [PATCH] Move improved error to ApiException --- .../src/Endpoint/OrderEndpoint.php | 36 --------------- .../src/Exception/PayPalApiException.php | 44 +++++++++++++++++-- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index 05e8ae7db..72163f35c 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -416,7 +416,6 @@ class OrderEndpoint { if ( false !== strpos( $response['body'], ErrorResponse::ORDER_ALREADY_AUTHORIZED ) ) { return $this->order( $order->id() ); } - $json = $this->add_improved_error_message( $json ); $error = new PayPalApiException( $json, $status_code @@ -635,39 +634,4 @@ class OrderEndpoint { return $json; } - - /** - * Adds an improved error message to the response if the error detail is known. - * - * @param stdClass $json The response. - * @return stdClass - */ - public function add_improved_error_message( stdClass $json ): stdClass { - if ( ! isset( $json->details ) ) { - return $json; - } - $improved_keys_messages = array( - 'PAYMENT_DENIED' => __( 'PayPal rejected the payment. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ), - 'TRANSACTION_REFUSED' => __( 'The transaction has been refused by the payment processor. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ), - 'DUPLICATE_INVOICE_ID' => __( 'The transaction has been refused because the Invoice ID already exists. Please create a new order or reach out to the store owner.', 'woocommerce-paypal-payments' ), - 'PAYER_CANNOT_PAY' => __( 'There was a problem processing this transaction. Please reach out to the store owner.', 'woocommerce-paypal-payments' ), - 'PAYEE_ACCOUNT_RESTRICTED' => __( 'There was a problem processing this transaction. Please reach out to the store owner.', 'woocommerce-paypal-payments' ), - ); - $improved_errors = array_filter( - array_keys( $improved_keys_messages ), - function ( $key ) use ( $json ): bool { - foreach ( $json->details as $detail ) { - if ( isset( $detail->issue ) && $detail->issue === $key ) { - return true; - } - } - return false; - } - ); - if ( $improved_errors ) { - $improved_errors = array_values( $improved_errors ); - $json->message = $improved_keys_messages[ $improved_errors[0] ]; - } - return $json; - } } diff --git a/modules/ppcp-api-client/src/Exception/PayPalApiException.php b/modules/ppcp-api-client/src/Exception/PayPalApiException.php index 36e6c1e57..4dfe3494d 100644 --- a/modules/ppcp-api-client/src/Exception/PayPalApiException.php +++ b/modules/ppcp-api-client/src/Exception/PayPalApiException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Exception; +use stdClass; + /** * Class PayPalApiException */ @@ -17,7 +19,7 @@ class PayPalApiException extends RuntimeException { /** * The JSON response object of PayPal. * - * @var \stdClass + * @var stdClass */ private $response; @@ -31,10 +33,10 @@ class PayPalApiException extends RuntimeException { /** * PayPalApiException constructor. * - * @param \stdClass|null $response The JSON object. + * @param stdClass|null $response The JSON object. * @param int $status_code The HTTP status code. */ - public function __construct( \stdClass $response = null, int $status_code = 0 ) { + public function __construct( stdClass $response = null, int $status_code = 0 ) { if ( is_null( $response ) ) { $response = new \stdClass(); } @@ -57,6 +59,7 @@ class PayPalApiException extends RuntimeException { if ( ! isset( $response->links ) || ! is_array( $response->links ) ) { $response->links = array(); } + $response = $this->add_improved_error_message( $response ); /** * The JSON response object. @@ -141,4 +144,39 @@ class PayPalApiException extends RuntimeException { return $details; } + + /** + * Adds an improved error message to the response if the error detail is known. + * + * @param stdClass $json The response. + * @return stdClass + */ + public function add_improved_error_message( stdClass $json ): stdClass { + if ( ! isset( $json->details ) ) { + return $json; + } + $improved_keys_messages = array( + 'PAYMENT_DENIED' => __( 'PayPal rejected the payment. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ), + 'TRANSACTION_REFUSED' => __( 'The transaction has been refused by the payment processor. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ), + 'DUPLICATE_INVOICE_ID' => __( 'The transaction has been refused because the Invoice ID already exists. Please create a new order or reach out to the store owner.', 'woocommerce-paypal-payments' ), + 'PAYER_CANNOT_PAY' => __( 'There was a problem processing this transaction. Please reach out to the store owner.', 'woocommerce-paypal-payments' ), + 'PAYEE_ACCOUNT_RESTRICTED' => __( 'There was a problem processing this transaction. Please reach out to the store owner.', 'woocommerce-paypal-payments' ), + ); + $improved_errors = array_filter( + array_keys( $improved_keys_messages ), + function ( $key ) use ( $json ): bool { + foreach ( $json->details as $detail ) { + if ( isset( $detail->issue ) && $detail->issue === $key ) { + return true; + } + } + return false; + } + ); + if ( $improved_errors ) { + $improved_errors = array_values( $improved_errors ); + $json->message = $improved_keys_messages[ $improved_errors[0] ]; + } + return $json; + } }