From cec69d64da917a73eb0b68841801a3ecb5bed5a5 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Fri, 5 May 2023 11:41:07 +0200 Subject: [PATCH] Bail if json property not set & fix ci PCP-696 --- .../src/Endpoint/OrderEndpoint.php | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index b80e1ac06..437c98714 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -324,7 +324,7 @@ class OrderEndpoint { $json = json_decode( $response['body'] ); $status_code = (int) wp_remote_retrieve_response_code( $response ); if ( 201 !== $status_code ) { - $json = $this->addImprovedErrorMessage($json); + $json = $this->add_improved_error_message( $json ); $error = new PayPalApiException( $json, $status_code @@ -404,6 +404,7 @@ 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 @@ -622,32 +623,36 @@ class OrderEndpoint { } /** - * @param $json + * Adds an improved error message to the response if the error detail is known. + * + * @param Object $json The response. * @return Object */ - public function addImprovedErrorMessage($json): Object - { + public function add_improved_error_message( $json ): object { + 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'), + '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) { + $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]]; + if ( $improved_errors ) { + $improved_errors = array_values( $improved_errors ); + $json->message = $improved_keys_messages[ $improved_errors[0] ]; } return $json; }