Refactor logging, add missing logging

This commit is contained in:
Alex P 2021-10-04 19:16:50 +03:00
parent 07928c9935
commit 06e437e0c3
5 changed files with 37 additions and 59 deletions

View file

@ -138,6 +138,7 @@ class PaymentsEndpoint {
* *
* @return Authorization * @return Authorization
* @throws RuntimeException If the request fails. * @throws RuntimeException If the request fails.
* @throws PayPalApiException If the request fails.
*/ */
public function capture( string $authorization_id ): Authorization { public function capture( string $authorization_id ): Authorization {
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
@ -155,39 +156,18 @@ 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 capture authorized payment.' );
__( 'Could not capture authorized 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;
} }
$authorization = $this->authorizations_factory->from_paypal_response( $json ); return $this->authorizations_factory->from_paypal_response( $json );
return $authorization;
} }
/** /**

View file

@ -220,7 +220,8 @@ return array(
'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' );
$payments_endpoint = $container->get( 'api.endpoint.payments' ); $payments_endpoint = $container->get( 'api.endpoint.payments' );
return new AuthorizedPaymentsProcessor( $order_endpoint, $payments_endpoint ); $logger = $container->get( 'woocommerce.logger.woocommerce' );
return new AuthorizedPaymentsProcessor( $order_endpoint, $payments_endpoint, $logger );
}, },
'wcgateway.admin.render-authorize-action' => static function ( $container ): RenderAuthorizeAction { 'wcgateway.admin.render-authorize-action' => static function ( $container ): RenderAuthorizeAction {

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Processor; namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Exception; use Exception;
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\Authorization; use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
@ -42,19 +43,29 @@ class AuthorizedPaymentsProcessor {
*/ */
private $payments_endpoint; private $payments_endpoint;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/** /**
* AuthorizedPaymentsProcessor constructor. * AuthorizedPaymentsProcessor 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( public function __construct(
OrderEndpoint $order_endpoint, OrderEndpoint $order_endpoint,
PaymentsEndpoint $payments_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;
} }
/** /**
@ -83,6 +94,7 @@ class AuthorizedPaymentsProcessor {
try { try {
$this->capture_authorizations( ...$authorizations ); $this->capture_authorizations( ...$authorizations );
} catch ( Exception $exception ) { } catch ( Exception $exception ) {
$this->logger->error( 'Failed to capture authorization: ' . $exception->getMessage() );
return self::FAILED; return self::FAILED;
} }

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\NullLogger;
use Requests_Utility_CaseInsensitiveDictionary; use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization; use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
@ -235,10 +236,6 @@ class PaymentsEndpointTest extends TestCase
$authorizationFactory = Mockery::mock(AuthorizationFactory::class); $authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log');
$logger->expects('debug');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class); $headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll'); $headers->shouldReceive('getAll');
$rawResponse = [ $rawResponse = [
@ -250,7 +247,7 @@ class PaymentsEndpointTest extends TestCase
$host, $host,
$bearer, $bearer,
$authorizationFactory, $authorizationFactory,
$logger new NullLogger()
); );
expect('wp_remote_get')->andReturn($rawResponse); expect('wp_remote_get')->andReturn($rawResponse);
@ -281,15 +278,11 @@ class PaymentsEndpointTest extends TestCase
'headers' => $headers, 'headers' => $headers,
]; ];
$logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log');
$logger->expects('debug');
$testee = new PaymentsEndpoint( $testee = new PaymentsEndpoint(
$host, $host,
$bearer, $bearer,
$authorizationFactory, $authorizationFactory,
$logger new NullLogger()
); );
expect('wp_remote_get')->andReturn($rawResponse); expect('wp_remote_get')->andReturn($rawResponse);

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Processor; namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Psr\Log\NullLogger;
use WC_Order; use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
@ -29,6 +30,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase
private $paymentsEndpoint; private $paymentsEndpoint;
private $testee;
public function setUp(): void { public function setUp(): void {
parent::setUp(); parent::setUp();
@ -42,25 +45,24 @@ class AuthorizedPaymentsProcessorTest extends TestCase
->with($this->paypalOrderId) ->with($this->paypalOrderId)
->andReturnUsing(function () { ->andReturnUsing(function () {
return $this->paypalOrder; return $this->paypalOrder;
}); })
->byDefault();
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class); $this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$this->testee = new AuthorizedPaymentsProcessor($this->orderEndpoint, $this->paymentsEndpoint, new NullLogger());
} }
public function testSuccess() { public function testSuccess() {
$testee = new AuthorizedPaymentsProcessor($this->orderEndpoint, $this->paymentsEndpoint);
$this->paymentsEndpoint $this->paymentsEndpoint
->expects('capture') ->expects('capture')
->with($this->authorizationId) ->with($this->authorizationId)
->andReturn($this->createAuthorization($this->authorizationId, AuthorizationStatus::CAPTURED)); ->andReturn($this->createAuthorization($this->authorizationId, AuthorizationStatus::CAPTURED));
$this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $testee->process($this->wcOrder)); $this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $this->testee->process($this->wcOrder));
} }
public function testCapturesAllCaptureable() { public function testCapturesAllCaptureable() {
$testee = new AuthorizedPaymentsProcessor($this->orderEndpoint, $this->paymentsEndpoint);
$authorizations = [ $authorizations = [
$this->createAuthorization('id1', AuthorizationStatus::CREATED), $this->createAuthorization('id1', AuthorizationStatus::CREATED),
$this->createAuthorization('id2', AuthorizationStatus::VOIDED), $this->createAuthorization('id2', AuthorizationStatus::VOIDED),
@ -79,50 +81,40 @@ class AuthorizedPaymentsProcessorTest extends TestCase
->andReturn($this->createAuthorization($authorization->id(), AuthorizationStatus::CAPTURED)); ->andReturn($this->createAuthorization($authorization->id(), AuthorizationStatus::CAPTURED));
} }
$this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $testee->process($this->wcOrder)); $this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $this->testee->process($this->wcOrder));
} }
public function testInaccessible() { public function testInaccessible() {
$orderEndpoint = Mockery::mock(OrderEndpoint::class); $this->orderEndpoint
$orderEndpoint
->expects('order') ->expects('order')
->with($this->paypalOrderId) ->with($this->paypalOrderId)
->andThrow(RuntimeException::class); ->andThrow(RuntimeException::class);
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $this->paymentsEndpoint); $this->assertEquals(AuthorizedPaymentsProcessor::INACCESSIBLE, $this->testee->process($this->wcOrder));
$this->assertEquals(AuthorizedPaymentsProcessor::INACCESSIBLE, $testee->process($this->wcOrder));
} }
public function testNotFound() { public function testNotFound() {
$orderEndpoint = Mockery::mock(OrderEndpoint::class); $this->orderEndpoint
$orderEndpoint
->expects('order') ->expects('order')
->with($this->paypalOrderId) ->with($this->paypalOrderId)
->andThrow(new RuntimeException('text', 404)); ->andThrow(new RuntimeException('text', 404));
$testee = new AuthorizedPaymentsProcessor($orderEndpoint, $this->paymentsEndpoint); $this->assertEquals(AuthorizedPaymentsProcessor::NOT_FOUND, $this->testee->process($this->wcOrder));
$this->assertEquals(AuthorizedPaymentsProcessor::NOT_FOUND, $testee->process($this->wcOrder));
} }
public function testCaptureFails() { public function testCaptureFails() {
$testee = new AuthorizedPaymentsProcessor($this->orderEndpoint, $this->paymentsEndpoint);
$this->paymentsEndpoint $this->paymentsEndpoint
->expects('capture') ->expects('capture')
->with($this->authorizationId) ->with($this->authorizationId)
->andThrow(RuntimeException::class); ->andThrow(RuntimeException::class);
$this->assertEquals(AuthorizedPaymentsProcessor::FAILED, $testee->process($this->wcOrder)); $this->assertEquals(AuthorizedPaymentsProcessor::FAILED, $this->testee->process($this->wcOrder));
} }
public function testAlreadyCaptured() { public function testAlreadyCaptured() {
$testee = new AuthorizedPaymentsProcessor($this->orderEndpoint, $this->paymentsEndpoint);
$this->paypalOrder = $this->createPaypalOrder([$this->createAuthorization($this->authorizationId, AuthorizationStatus::CAPTURED)]); $this->paypalOrder = $this->createPaypalOrder([$this->createAuthorization($this->authorizationId, AuthorizationStatus::CAPTURED)]);
$this->assertEquals(AuthorizedPaymentsProcessor::ALREADY_CAPTURED, $testee->process($this->wcOrder)); $this->assertEquals(AuthorizedPaymentsProcessor::ALREADY_CAPTURED, $this->testee->process($this->wcOrder));
} }
private function createWcOrder(string $paypalOrderId): WC_Order { private function createWcOrder(string $paypalOrderId): WC_Order {