Refactoring

This commit is contained in:
dinamiko 2022-10-05 09:46:16 +02:00
parent b946386eaf
commit abf3a0e5d0
4 changed files with 38 additions and 38 deletions

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Exception;
use stdClass;
/**
* Class PayPalApiException
*/
@ -119,4 +121,26 @@ class PayPalApiException extends RuntimeException {
public function status_code(): int {
return $this->status_code;
}
/**
* Return exception details if exists.
*
* @param string $error The error to return in case no details found.
* @return string
*/
public function get_details( string $error ): string {
if ( ! is_array( $this->details() ) || empty( $this->details() ) ) {
return $error;
}
$details = '';
foreach ( $this->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
return $details;
}
}

View file

@ -142,27 +142,21 @@ class RenewalHandler {
public function renew( \WC_Order $wc_order ) {
try {
$this->process_order( $wc_order );
} catch ( \Exception $error ) {
$error_details = $error->getMessage();
if ( is_a( $error, PayPalApiException::class ) ) {
$details = '';
foreach ( $error->details() as $detail ) {
$details .= $detail->issue . ' ' . $detail->description;
}
if ( $details ) {
$error_details = $details;
}
} catch ( \Exception $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) ) {
$error = $exception->get_details( $error );
}
$wc_order->update_status(
'failed',
$error
);
$error_message = sprintf(
'An error occurred while trying to renew the subscription for order %1$d: %2$s',
$wc_order->get_id(),
$error_details
);
$wc_order->update_status(
'failed',
$error_details
$error
);
$this->logger->error( $error_message );

View file

@ -167,17 +167,8 @@ class OXXOGateway extends WC_Payment_Gateway {
}
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) {
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
if ( is_a( $exception, PayPalApiException::class ) ) {
$error = $exception->get_details( $error );
}
$this->logger->error( $error );

View file

@ -246,17 +246,8 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
);
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) {
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
if ( is_a( $exception, PayPalApiException::class ) ) {
$error = $exception->get_details( $error );
}
$this->logger->error( $error );