diff --git a/modules/ppcp-api-client/src/Exception/PayPalApiException.php b/modules/ppcp-api-client/src/Exception/PayPalApiException.php index d2752cb72..36e6c1e57 100644 --- a/modules/ppcp-api-client/src/Exception/PayPalApiException.php +++ b/modules/ppcp-api-client/src/Exception/PayPalApiException.php @@ -119,4 +119,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 ( empty( $this->details() ) ) { + return $error; + } + + $details = ''; + foreach ( $this->details() as $detail ) { + $issue = $detail->issue ?? ''; + $field = $detail->field ?? ''; + $description = $detail->description ?? ''; + $details .= $issue . ' ' . $field . ' ' . $description . '
'; + } + + return $details; + } } diff --git a/modules/ppcp-subscription/src/RenewalHandler.php b/modules/ppcp-subscription/src/RenewalHandler.php index 2b96b2f34..851ea5f7d 100644 --- a/modules/ppcp-subscription/src/RenewalHandler.php +++ b/modules/ppcp-subscription/src/RenewalHandler.php @@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Subscription; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; +use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; @@ -141,17 +142,27 @@ class RenewalHandler { public function renew( \WC_Order $wc_order ) { try { $this->process_order( $wc_order ); - } catch ( \Exception $error ) { - $this->logger->error( - sprintf( - 'An error occurred while trying to renew the subscription for order %1$d: %2$s', - $wc_order->get_id(), - $error->getMessage() - ) + } 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 + ); + $this->logger->error( $error_message ); + return; } + $this->logger->info( sprintf( 'Renewal for order %d is completed.', diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php index 1711b8664..704d978c7 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php @@ -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 . '
'; - } - - $error = $details; + if ( is_a( $exception, PayPalApiException::class ) ) { + $error = $exception->get_details( $error ); } $this->logger->error( $error ); diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php index a135ad993..f328b86d2 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php @@ -274,17 +274,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 . '
'; - } - - $error = $details; + if ( is_a( $exception, PayPalApiException::class ) ) { + $error = $exception->get_details( $error ); } $this->logger->error( $error );