Move improved error to ApiException

This commit is contained in:
carmenmaymo 2023-06-12 08:46:45 +02:00
parent 027def77b4
commit fa0508d572
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
2 changed files with 41 additions and 39 deletions

View file

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

View file

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