Improve logging

This commit is contained in:
Alex P 2021-09-30 09:11:10 +03:00
parent d38f24ef2b
commit c09c1efc9c
3 changed files with 21 additions and 29 deletions

View file

@ -195,10 +195,11 @@ class PaymentsEndpoint {
* *
* @param Refund $refund The refund to be processed. * @param Refund $refund The refund to be processed.
* *
* @return bool * @return void
* @throws RuntimeException If the request fails. * @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(); $bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/payments/captures/' . $refund->for_capture()->id() . '/refund'; $url = trailingslashit( $this->host ) . 'v2/payments/captures/' . $refund->for_capture()->id() . '/refund';
$args = array( $args = array(
@ -215,37 +216,15 @@ class PaymentsEndpoint {
$json = json_decode( $response['body'] ); $json = json_decode( $response['body'] );
if ( is_wp_error( $response ) ) { if ( is_wp_error( $response ) ) {
$error = new RuntimeException( throw new RuntimeException( 'Could not refund payment.' );
__( 'Could not refund payment.', 'woocommerce-paypal-payments' )
);
$this->logger->log(
'warning',
$error->getMessage(),
array(
'args' => $args,
'response' => $response,
)
);
throw $error;
} }
$status_code = (int) wp_remote_retrieve_response_code( $response ); $status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 201 !== $status_code ) { if ( 201 !== $status_code ) {
$error = new PayPalApiException( throw new PayPalApiException(
$json, $json,
$status_code $status_code
); );
$this->logger->log(
'warning',
$error->getMessage(),
array(
'args' => $args,
'response' => $response,
)
);
throw $error;
} }
return true;
} }
} }

View file

@ -215,7 +215,8 @@ return array(
'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor { 'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor {
$order_endpoint = $container->get( 'api.endpoint.order' ); $order_endpoint = $container->get( 'api.endpoint.order' );
$payments_endpoint = $container->get( 'api.endpoint.payments' ); $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 { 'wcgateway.processor.authorized-payments' => static function ( $container ): AuthorizedPaymentsProcessor {
$order_endpoint = $container->get( 'api.endpoint.order' ); $order_endpoint = $container->get( 'api.endpoint.order' );

View file

@ -9,6 +9,7 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Processor; namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount; use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
@ -36,16 +37,25 @@ class RefundProcessor {
*/ */
private $payments_endpoint; private $payments_endpoint;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/** /**
* RefundProcessor constructor. * RefundProcessor constructor.
* *
* @param OrderEndpoint $order_endpoint The order endpoint. * @param OrderEndpoint $order_endpoint The order endpoint.
* @param PaymentsEndpoint $payments_endpoint The payments 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->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint; $this->payments_endpoint = $payments_endpoint;
$this->logger = $logger;
} }
/** /**
@ -91,8 +101,10 @@ class RefundProcessor {
new Money( $amount, $wc_order->get_currency() ) new Money( $amount, $wc_order->get_currency() )
) )
); );
return $this->payments_endpoint->refund( $refund ); $this->payments_endpoint->refund( $refund );
return true;
} catch ( RuntimeException $error ) { } catch ( RuntimeException $error ) {
$this->logger->error( 'Refund failed: ' . $error->getMessage() );
return false; return false;
} }
} }