From 7ef5991cb85f7dc2f44c6f2e7db9b005e68e00c7 Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 23 Sep 2021 17:19:46 +0300 Subject: [PATCH] Log endpoint errors --- .../Endpoint/class-approveorderendpoint.php | 5 ++- .../src/Endpoint/class-changecartendpoint.php | 35 +++++++++++----- .../Endpoint/class-createorderendpoint.php | 42 +++++++++++++++---- .../Endpoint/class-dataclientidendpoint.php | 22 ++++++++-- modules/ppcp-onboarding/services.php | 4 +- .../Endpoint/class-loginsellerendpoint.php | 17 +++++++- .../Endpoint/ChangeCartEndpointTest.php | 6 ++- .../Endpoint/CreateOrderEndpointTest.php | 4 +- .../Endpoint/DataClientIdEndpointTest.php | 3 +- 9 files changed, 108 insertions(+), 30 deletions(-) diff --git a/modules/ppcp-button/src/Endpoint/class-approveorderendpoint.php b/modules/ppcp-button/src/Endpoint/class-approveorderendpoint.php index 6953b3a6d..ce309cf9a 100644 --- a/modules/ppcp-button/src/Endpoint/class-approveorderendpoint.php +++ b/modules/ppcp-button/src/Endpoint/class-approveorderendpoint.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button\Endpoint; +use Exception; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; @@ -195,7 +196,9 @@ class ApproveOrderEndpoint implements EndpointInterface { $this->session_handler->replace_order( $order ); wp_send_json_success( $order ); return true; - } catch ( \RuntimeException $error ) { + } catch ( Exception $error ) { + $this->logger->error( 'Order approve failed: ' . $error->getMessage() ); + wp_send_json_error( array( 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', diff --git a/modules/ppcp-button/src/Endpoint/class-changecartendpoint.php b/modules/ppcp-button/src/Endpoint/class-changecartendpoint.php index 1422ce940..1ad4b451d 100644 --- a/modules/ppcp-button/src/Endpoint/class-changecartendpoint.php +++ b/modules/ppcp-button/src/Endpoint/class-changecartendpoint.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button\Endpoint; +use Exception; +use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository; @@ -57,21 +59,30 @@ class ChangeCartEndpoint implements EndpointInterface { */ private $product_data_store; + /** + * The logger. + * + * @var LoggerInterface + */ + protected $logger; + /** * ChangeCartEndpoint constructor. * - * @param \WC_Cart $cart The current WC cart object. - * @param \WC_Shipping $shipping The current WC shipping object. - * @param RequestData $request_data The request data helper. - * @param CartRepository $repository The repository for the current purchase items. - * @param \WC_Data_Store $product_data_store The data store for products. + * @param \WC_Cart $cart The current WC cart object. + * @param \WC_Shipping $shipping The current WC shipping object. + * @param RequestData $request_data The request data helper. + * @param CartRepository $repository The repository for the current purchase items. + * @param \WC_Data_Store $product_data_store The data store for products. + * @param LoggerInterface $logger The logger. */ public function __construct( \WC_Cart $cart, \WC_Shipping $shipping, RequestData $request_data, CartRepository $repository, - \WC_Data_Store $product_data_store + \WC_Data_Store $product_data_store, + LoggerInterface $logger ) { $this->cart = $cart; @@ -79,6 +90,7 @@ class ChangeCartEndpoint implements EndpointInterface { $this->request_data = $request_data; $this->repository = $repository; $this->product_data_store = $product_data_store; + $this->logger = $logger; } /** @@ -94,12 +106,13 @@ class ChangeCartEndpoint implements EndpointInterface { * Handles the request. * * @return bool - * @throws \Exception On error. */ public function handle_request(): bool { try { return $this->handle_data(); - } catch ( RuntimeException $error ) { + } catch ( Exception $error ) { + $this->logger->error( 'Cart updating failed: ' . $error->getMessage() ); + wp_send_json_error( array( 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', @@ -116,7 +129,7 @@ class ChangeCartEndpoint implements EndpointInterface { * Handles the request data. * * @return bool - * @throws \Exception On error. + * @throws Exception On error. */ private function handle_data(): bool { $data = $this->request_data->read_request( $this->nonce() ); @@ -234,7 +247,7 @@ class ChangeCartEndpoint implements EndpointInterface { * @param int $quantity The Quantity. * * @return bool - * @throws \Exception When product could not be added. + * @throws Exception When product could not be added. */ private function add_product( \WC_Product $product, int $quantity ): bool { return false !== $this->cart->add_to_cart( $product->get_id(), $quantity ); @@ -249,7 +262,7 @@ class ChangeCartEndpoint implements EndpointInterface { * @param array $post_variations The variations. * * @return bool - * @throws \Exception When product could not be added. + * @throws Exception When product could not be added. */ private function add_variable_product( \WC_Product $product, diff --git a/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php b/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php index 0e90fc41c..60f11ca77 100644 --- a/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php +++ b/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button\Endpoint; +use Exception; +use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; @@ -102,6 +104,13 @@ class CreateOrderEndpoint implements EndpointInterface { */ private $purchase_units; + /** + * The logger. + * + * @var LoggerInterface + */ + protected $logger; + /** * CreateOrderEndpoint constructor. * @@ -113,6 +122,7 @@ class CreateOrderEndpoint implements EndpointInterface { * @param SessionHandler $session_handler The SessionHandler object. * @param Settings $settings The Settings object. * @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object. + * @param LoggerInterface $logger The logger. */ public function __construct( RequestData $request_data, @@ -122,7 +132,8 @@ class CreateOrderEndpoint implements EndpointInterface { PayerFactory $payer_factory, SessionHandler $session_handler, Settings $settings, - EarlyOrderHandler $early_order_handler + EarlyOrderHandler $early_order_handler, + LoggerInterface $logger ) { $this->request_data = $request_data; @@ -133,6 +144,7 @@ class CreateOrderEndpoint implements EndpointInterface { $this->session_handler = $session_handler; $this->settings = $settings; $this->early_order_handler = $early_order_handler; + $this->logger = $logger; } /** @@ -190,6 +202,8 @@ class CreateOrderEndpoint implements EndpointInterface { wp_send_json_success( $order->to_array() ); return true; } catch ( \RuntimeException $error ) { + $this->logger->error( 'Order creation failed: ' . $error->getMessage() ); + wp_send_json_error( array( 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', @@ -198,7 +212,9 @@ class CreateOrderEndpoint implements EndpointInterface { 'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(), ) ); - } catch ( \Exception $exception ) { + } catch ( Exception $exception ) { + $this->logger->error( 'Order creation failed: ' . $exception->getMessage() ); + wc_add_notice( $exception->getMessage(), 'error' ); } @@ -212,11 +228,16 @@ class CreateOrderEndpoint implements EndpointInterface { * @param \WP_Error $errors The errors, which occurred. * * @return array + * @throws Exception On Error. */ public function after_checkout_validation( array $data, \WP_Error $errors ): array { if ( ! $errors->errors ) { - - $order = $this->create_paypal_order(); + try { + $order = $this->create_paypal_order(); + } catch ( Exception $exception ) { + $this->logger->error( 'Order creation failed: ' . $exception->getMessage() ); + throw $exception; + } /** * In case we are onboarded and everything is fine with the \WC_Order @@ -231,6 +252,8 @@ class CreateOrderEndpoint implements EndpointInterface { return $data; } + $this->logger->error( 'Checkout validation failed: ' . $errors->get_error_message() ); + wp_send_json_error( array( 'name' => '', @@ -336,7 +359,7 @@ class CreateOrderEndpoint implements EndpointInterface { * * @param string $form_values The values of the form. * - * @throws \Exception On Error. + * @throws Exception On Error. */ private function process_checkout_form( string $form_values ) { $form_values = explode( '&', $form_values ); @@ -386,7 +409,7 @@ class CreateOrderEndpoint implements EndpointInterface { * * @param string $form_values The values of the form. * @param \WC_Order|null $wc_order WC order to get data from. - * @throws \Exception On Error. + * @throws Exception On Error. */ private function process_checkout_form_when_creating_account( string $form_values, \WC_Order $wc_order = null ) { $form_values = explode( '&', $form_values ); @@ -406,7 +429,12 @@ class CreateOrderEndpoint implements EndpointInterface { 'woocommerce_after_checkout_validation', function ( array $data, \WP_Error $errors ) use ( $wc_order ) { if ( ! $errors->errors ) { - $order = $this->create_paypal_order( $wc_order ); + try { + $order = $this->create_paypal_order( $wc_order ); + } catch ( Exception $exception ) { + $this->logger->error( 'Order creation failed: ' . $exception->getMessage() ); + throw $exception; + } wp_send_json_success( $order->to_array() ); return true; } diff --git a/modules/ppcp-button/src/Endpoint/class-dataclientidendpoint.php b/modules/ppcp-button/src/Endpoint/class-dataclientidendpoint.php index 439093a06..ad5b1e750 100644 --- a/modules/ppcp-button/src/Endpoint/class-dataclientidendpoint.php +++ b/modules/ppcp-button/src/Endpoint/class-dataclientidendpoint.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button\Endpoint; +use Exception; +use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\IdentityToken; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; @@ -35,19 +37,29 @@ class DataClientIdEndpoint implements EndpointInterface { */ private $identity_token; + /** + * The logger. + * + * @var LoggerInterface + */ + protected $logger; + /** * DataClientIdEndpoint constructor. * - * @param RequestData $request_data The Request Data Helper. - * @param IdentityToken $identity_token The Identity Token. + * @param RequestData $request_data The Request Data Helper. + * @param IdentityToken $identity_token The Identity Token. + * @param LoggerInterface $logger The logger. */ public function __construct( RequestData $request_data, - IdentityToken $identity_token + IdentityToken $identity_token, + LoggerInterface $logger ) { $this->request_data = $request_data; $this->identity_token = $identity_token; + $this->logger = $logger; } /** @@ -77,7 +89,9 @@ class DataClientIdEndpoint implements EndpointInterface { ) ); return true; - } catch ( RuntimeException $error ) { + } catch ( Exception $error ) { + $this->logger->error( 'Client ID retrieval failed: ' . $error->getMessage() ); + wp_send_json_error( array( 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', diff --git a/modules/ppcp-onboarding/services.php b/modules/ppcp-onboarding/services.php index 2e07665b8..3fe8b689a 100644 --- a/modules/ppcp-onboarding/services.php +++ b/modules/ppcp-onboarding/services.php @@ -169,6 +169,7 @@ return array( $login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' ); $partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' ); $settings = $container->get( 'wcgateway.settings' ); + $logger = $container->get( 'woocommerce.logger.woocommerce' ); $cache = new Cache( 'ppcp-paypal-bearer' ); return new LoginSellerEndpoint( @@ -177,7 +178,8 @@ return array( $login_seller_sandbox, $partner_referrals_data, $settings, - $cache + $cache, + $logger ); }, 'api.endpoint.partner-referrals-sandbox' => static function ( $container ) : PartnerReferrals { diff --git a/modules/ppcp-onboarding/src/Endpoint/class-loginsellerendpoint.php b/modules/ppcp-onboarding/src/Endpoint/class-loginsellerendpoint.php index 3b006413f..38b54331b 100644 --- a/modules/ppcp-onboarding/src/Endpoint/class-loginsellerendpoint.php +++ b/modules/ppcp-onboarding/src/Endpoint/class-loginsellerendpoint.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Onboarding\Endpoint; +use Exception; +use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; @@ -67,6 +69,13 @@ class LoginSellerEndpoint implements EndpointInterface { */ private $cache; + /** + * The logger. + * + * @var LoggerInterface + */ + protected $logger; + /** * LoginSellerEndpoint constructor. * @@ -76,6 +85,7 @@ class LoginSellerEndpoint implements EndpointInterface { * @param PartnerReferralsData $partner_referrals_data The Partner Referrals Data. * @param Settings $settings The Settings. * @param Cache $cache The Cache. + * @param LoggerInterface $logger The logger. */ public function __construct( RequestData $request_data, @@ -83,7 +93,8 @@ class LoginSellerEndpoint implements EndpointInterface { LoginSeller $login_seller_sandbox, PartnerReferralsData $partner_referrals_data, Settings $settings, - Cache $cache + Cache $cache, + LoggerInterface $logger ) { $this->request_data = $request_data; @@ -92,6 +103,7 @@ class LoginSellerEndpoint implements EndpointInterface { $this->partner_referrals_data = $partner_referrals_data; $this->settings = $settings; $this->cache = $cache; + $this->logger = $logger; } /** @@ -141,7 +153,8 @@ class LoginSellerEndpoint implements EndpointInterface { ); wp_send_json_success(); return true; - } catch ( \RuntimeException $error ) { + } catch ( Exception $error ) { + $this->logger->error( 'Onboarding completion handling error: ' . $error->getMessage() ); wp_send_json_error( $error->getMessage() ); return false; } diff --git a/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php b/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php index e83f4d54c..7b1132c40 100644 --- a/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php +++ b/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php @@ -8,6 +8,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository; use WooCommerce\PayPalCommerce\TestCase; use Mockery; +use WooCommerce\WooCommerce\Logging\Logger\NullLogger; use function Brain\Monkey\Functions\expect; class ChangeCartEndpointTest extends TestCase @@ -68,7 +69,8 @@ class ChangeCartEndpointTest extends TestCase $shipping, $requestData, $cartRepository, - $dataStore + $dataStore, + new NullLogger() ); expect('wp_send_json_success') @@ -171,4 +173,4 @@ class ChangeCartEndpointTest extends TestCase return $testData; } -} \ No newline at end of file +} diff --git a/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php b/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php index fa6f2f9e9..1ecde79dc 100644 --- a/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php +++ b/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php @@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; +use WooCommerce\WooCommerce\Logging\Logger\NullLogger; use function Brain\Monkey\Functions\expect; class CreateOrderEndpointTest extends TestCase @@ -160,7 +161,8 @@ class CreateOrderEndpointTest extends TestCase $payer_factory, $session_handler, $settings, - $early_order_handler + $early_order_handler, + new NullLogger() ); return array($payer_factory, $testee); } diff --git a/tests/PHPUnit/Button/Endpoint/DataClientIdEndpointTest.php b/tests/PHPUnit/Button/Endpoint/DataClientIdEndpointTest.php index 65f637023..eea5df229 100644 --- a/tests/PHPUnit/Button/Endpoint/DataClientIdEndpointTest.php +++ b/tests/PHPUnit/Button/Endpoint/DataClientIdEndpointTest.php @@ -8,6 +8,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Token; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\TestCase; use Mockery; +use WooCommerce\WooCommerce\Logging\Logger\NullLogger; use function Brain\Monkey\Functions\when; use function Brain\Monkey\Functions\expect; @@ -22,7 +23,7 @@ class DataClientIdEndpointTest extends TestCase parent::setUp(); $this->requestData = Mockery::mock(RequestData::class); $this->identityToken = Mockery::mock(IdentityToken::class); - $this->sut = new DataClientIdEndpoint($this->requestData, $this->identityToken); + $this->sut = new DataClientIdEndpoint($this->requestData, $this->identityToken, new NullLogger()); } public function testHandleRequestSuccess()