From c09c1efc9cc2ea19d2757a96b9231686ac5f95ff Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 30 Sep 2021 09:11:10 +0300 Subject: [PATCH] Improve logging --- .../src/Endpoint/class-paymentsendpoint.php | 31 +++---------------- modules/ppcp-wc-gateway/services.php | 3 +- .../src/Processor/class-refundprocessor.php | 16 ++++++++-- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/class-paymentsendpoint.php b/modules/ppcp-api-client/src/Endpoint/class-paymentsendpoint.php index 6e3ec6b6d..d9e20350b 100644 --- a/modules/ppcp-api-client/src/Endpoint/class-paymentsendpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/class-paymentsendpoint.php @@ -195,10 +195,11 @@ class PaymentsEndpoint { * * @param Refund $refund The refund to be processed. * - * @return bool + * @return void * @throws RuntimeException If the request fails. + * @throws PayPalApiException If the request fails. */ - public function refund( Refund $refund ) : bool { + public function refund( Refund $refund ) : void { $bearer = $this->bearer->bearer(); $url = trailingslashit( $this->host ) . 'v2/payments/captures/' . $refund->for_capture()->id() . '/refund'; $args = array( @@ -215,37 +216,15 @@ class PaymentsEndpoint { $json = json_decode( $response['body'] ); if ( is_wp_error( $response ) ) { - $error = new RuntimeException( - __( 'Could not refund payment.', 'woocommerce-paypal-payments' ) - ); - $this->logger->log( - 'warning', - $error->getMessage(), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + throw new RuntimeException( 'Could not refund payment.' ); } $status_code = (int) wp_remote_retrieve_response_code( $response ); if ( 201 !== $status_code ) { - $error = new PayPalApiException( + throw new PayPalApiException( $json, $status_code ); - $this->logger->log( - 'warning', - $error->getMessage(), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; } - - return true; } } diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 93c04b827..9c3e45a13 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -215,7 +215,8 @@ return array( 'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor { $order_endpoint = $container->get( 'api.endpoint.order' ); $payments_endpoint = $container->get( 'api.endpoint.payments' ); - return new RefundProcessor( $order_endpoint, $payments_endpoint ); + $logger = $container->get( 'woocommerce.logger.woocommerce' ); + return new RefundProcessor( $order_endpoint, $payments_endpoint, $logger ); }, 'wcgateway.processor.authorized-payments' => static function ( $container ): AuthorizedPaymentsProcessor { $order_endpoint = $container->get( 'api.endpoint.order' ); diff --git a/modules/ppcp-wc-gateway/src/Processor/class-refundprocessor.php b/modules/ppcp-wc-gateway/src/Processor/class-refundprocessor.php index 0ace800d0..0c5fc7279 100644 --- a/modules/ppcp-wc-gateway/src/Processor/class-refundprocessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/class-refundprocessor.php @@ -9,6 +9,7 @@ declare( strict_types=1 ); namespace WooCommerce\PayPalCommerce\WcGateway\Processor; +use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount; @@ -36,16 +37,25 @@ class RefundProcessor { */ private $payments_endpoint; + /** + * The logger. + * + * @var LoggerInterface + */ + private $logger; + /** * RefundProcessor constructor. * * @param OrderEndpoint $order_endpoint The order endpoint. * @param PaymentsEndpoint $payments_endpoint The payments endpoint. + * @param LoggerInterface $logger The logger. */ - public function __construct( OrderEndpoint $order_endpoint, PaymentsEndpoint $payments_endpoint ) { + public function __construct( OrderEndpoint $order_endpoint, PaymentsEndpoint $payments_endpoint, LoggerInterface $logger ) { $this->order_endpoint = $order_endpoint; $this->payments_endpoint = $payments_endpoint; + $this->logger = $logger; } /** @@ -91,8 +101,10 @@ class RefundProcessor { new Money( $amount, $wc_order->get_currency() ) ) ); - return $this->payments_endpoint->refund( $refund ); + $this->payments_endpoint->refund( $refund ); + return true; } catch ( RuntimeException $error ) { + $this->logger->error( 'Refund failed: ' . $error->getMessage() ); return false; } }