Bail if json property not set & fix ci PCP-696

This commit is contained in:
carmenmaymo 2023-05-05 11:41:07 +02:00
parent 57620bb0ee
commit cec69d64da
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E

View file

@ -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;
}