From e7e80c309504652c5d4bd2768f1d84302b360b11 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Fri, 2 Jul 2021 12:31:20 +0200 Subject: [PATCH 1/6] Fix unhandled exceptions when token creation fails --- .../src/Authentication/class-paypalbearer.php | 6 ++++- .../src/Assets/class-settingspageassets.php | 21 +++++++++------- .../src/Settings/class-settingslistener.php | 24 +++++++++++++++---- .../src/class-wcgatewaymodule.php | 1 + 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/class-paypalbearer.php b/modules/ppcp-api-client/src/Authentication/class-paypalbearer.php index 55e06a045..fb4cac9ac 100644 --- a/modules/ppcp-api-client/src/Authentication/class-paypalbearer.php +++ b/modules/ppcp-api-client/src/Authentication/class-paypalbearer.php @@ -120,7 +120,11 @@ private function newBearer(): Token { if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { $error = new RuntimeException( - __( 'Could not create token.', 'woocommerce-paypal-payments' ) + sprintf( + // translators: %s is the error description. + __( 'Could not create token. %s', 'woocommerce-paypal-payments' ), + isset( json_decode( $response['body'] )->error_description ) ? json_decode( $response['body'] )->error_description : '' + ) ); $this->logger->log( 'warning', diff --git a/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php b/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php index 197c07e03..d4d699398 100644 --- a/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php +++ b/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php @@ -10,6 +10,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Assets; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; +use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; /** * Class SettingsPageAssets @@ -111,13 +112,17 @@ private function register_admin_assets( Bearer $bearer ) { true ); - $token = $bearer->bearer(); - wp_localize_script( - 'ppcp-gateway-settings', - 'PayPalCommerceGatewaySettings', - array( - 'vaulting_features_available' => $token->vaulting_available(), - ) - ); + try { + $token = $bearer->bearer(); + wp_localize_script( + 'ppcp-gateway-settings', + 'PayPalCommerceGatewaySettings', + array( + 'vaulting_features_available' => $token->vaulting_available(), + ) + ); + } catch ( RuntimeException $exception ) { + // Already displaying an admin message in SettingsListener. + } } } diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index 7512c76d7..1bb26eb78 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -11,6 +11,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; +use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; @@ -147,11 +148,24 @@ public function listen_for_vaulting_enabled() { return; } - $token = $this->bearer->bearer(); - if ( ! $token->vaulting_available() ) { - $this->settings->set( 'vault_enabled', false ); - $this->settings->persist(); - return; + try { + $token = $this->bearer->bearer(); + if ( ! $token->vaulting_available() ) { + $this->settings->set( 'vault_enabled', false ); + $this->settings->persist(); + return; + } + } catch ( RuntimeException $exception ) { + add_action( + 'admin_notices', + function () use ( $exception ) { + printf( + // translators: %s is the error message. + '

%s

', + $exception->getMessage() + ); + } + ); } /** diff --git a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php index db0392879..50d8d1f4e 100644 --- a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php +++ b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php @@ -11,6 +11,7 @@ use Dhii\Container\ServiceProvider; use Dhii\Modular\Module\ModuleInterface; +use http\Exception\RuntimeException; use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository; From db4e1422d86cca406dd5657d6091fb2e67f8bacd Mon Sep 17 00:00:00 2001 From: dinamiko Date: Fri, 2 Jul 2021 12:36:59 +0200 Subject: [PATCH 2/6] Remove unused class --- modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php index 50d8d1f4e..db0392879 100644 --- a/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php +++ b/modules/ppcp-wc-gateway/src/class-wcgatewaymodule.php @@ -11,7 +11,6 @@ use Dhii\Container\ServiceProvider; use Dhii\Modular\Module\ModuleInterface; -use http\Exception\RuntimeException; use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository; From 362b782387797946bd34c98849bbf51900a49e06 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 12 Jul 2021 11:22:19 +0200 Subject: [PATCH 3/6] Fix phpcs errors --- modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php | 2 +- modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php b/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php index d4d699398..db22c2c2f 100644 --- a/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php +++ b/modules/ppcp-wc-gateway/src/Assets/class-settingspageassets.php @@ -122,7 +122,7 @@ private function register_admin_assets( Bearer $bearer ) { ) ); } catch ( RuntimeException $exception ) { - // Already displaying an admin message in SettingsListener. + return; } } } diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index 1bb26eb78..e76238b65 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -160,7 +160,6 @@ public function listen_for_vaulting_enabled() { 'admin_notices', function () use ( $exception ) { printf( - // translators: %s is the error message. '

%s

', $exception->getMessage() ); From 3cf78834a34489e23596c3acdda62ffde2487ec6 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 12 Jul 2021 11:30:08 +0200 Subject: [PATCH 4/6] Fix github actions workflow --- .github/workflows/php.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index e2d5f1752..78f1729ba 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,10 +11,19 @@ jobs: runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['7.1'] + steps: - uses: actions/checkout@v2 - - name: Validate composer.json and composer.lock + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + + - name: Validate composer.json and composer.lock run: composer validate - name: Install dependencies From 7b1297a4536ccd5363e675457d7f53bcae34ce79 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 12 Jul 2021 11:30:49 +0200 Subject: [PATCH 5/6] Fix github actions workflow --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 78f1729ba..955552a87 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -23,7 +23,7 @@ jobs: with: php-version: ${{ matrix.php-versions }} - - name: Validate composer.json and composer.lock + - name: Validate composer.json and composer.lock run: composer validate - name: Install dependencies From 060d3561a53227b778d21b6c28798fc97ed53ba5 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 12 Jul 2021 11:37:46 +0200 Subject: [PATCH 6/6] Fix phpcs errors --- .github/workflows/php.yml | 3 --- .../ppcp-wc-gateway/src/Settings/class-settingslistener.php | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 955552a87..ddeaeaf11 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -29,9 +29,6 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-progress --no-suggest - # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" - # Docs: https://getcomposer.org/doc/articles/scripts.md - - name: Run test suite run: ./vendor/bin/phpunit - name: Run Woocommerce coding standards diff --git a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php index e76238b65..e7a559355 100644 --- a/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php +++ b/modules/ppcp-wc-gateway/src/Settings/class-settingslistener.php @@ -161,7 +161,7 @@ public function listen_for_vaulting_enabled() { function () use ( $exception ) { printf( '

%s

', - $exception->getMessage() + esc_attr( $exception->getMessage() ) ); } );