Merge pull request #187 from woocommerce/PCP-179-incorrect-api-credentials-cause-

Incorrect API credentials cause fatal error
This commit is contained in:
Emili Castells 2021-07-12 11:41:32 +02:00 committed by GitHub
commit 2efb300be8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 17 deletions

View file

@ -120,7 +120,11 @@ class PayPalBearer implements Bearer {
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',

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
/**
* Class SettingsPageAssets
@ -111,13 +112,17 @@ class SettingsPageAssets {
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 ) {
return;
}
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
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,23 @@ class SettingsListener {
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(
'<div class="notice notice-error"><p>%s</p></div>',
esc_attr( $exception->getMessage() )
);
}
);
}
/**