Disable vault setting if vaulting feature is not available in access token

This commit is contained in:
dinamiko 2021-04-29 17:40:41 +02:00
parent e2cf37297a
commit ccf357d790
6 changed files with 78 additions and 11 deletions

View file

@ -99,6 +99,22 @@ class Token {
return new Token( $json );
}
/**
* Checks if vaulting is available in access token scope.
*
* @return bool Whether vaulting features are enabled or not.
*/
public function vaulting_available() {
if ( strpos(
$this->json->scope,
'https://uri.paypal.com/services/vault/payment-tokens/readwrite'
) !== false ) {
return true;
}
return false;
}
/**
* Validates whether a JSON object can be transformed to a Token object.
*

View file

@ -24,6 +24,10 @@
function updateCheckboxes() {
atLeastOneChecked(payLaterMessagingCheckboxes) ? disableAll(vaultingCheckboxes) : enableAll(vaultingCheckboxes)
atLeastOneChecked(vaultingCheckboxes) ? disableAll(payLaterMessagingCheckboxes) : enableAll(payLaterMessagingCheckboxes)
if(PayPalCommerceGatewaySettings.vaulting_features_available !== '1' ) {
disableAll(vaultingCheckboxes)
}
}
updateCheckboxes()

View file

@ -133,7 +133,8 @@ return array(
$webhook_registrar = $container->get( 'webhook.registrar' );
$state = $container->get( 'onboarding.state' );
$cache = new Cache( 'ppcp-paypal-bearer' );
return new SettingsListener( $settings, $fields, $webhook_registrar, $cache, $state );
$bearer = $container->get( 'api.bearer' );
return new SettingsListener( $settings, $fields, $webhook_registrar, $cache, $state, $bearer );
},
'wcgateway.order-processor' => static function ( $container ): OrderProcessor {
@ -182,7 +183,6 @@ return array(
'wcgateway.settings.fields' => static function ( $container ): array {
$state = $container->get( 'onboarding.state' );
$settings = $container->get( 'wcgateway.settings' );
$messages_disclaimers = $container->get( 'button.helper.messages-disclaimers' );
$fields = array(
@ -635,6 +635,7 @@ return array(
'type' => 'checkbox',
'desc_tip' => true,
'label' => sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__( 'To use vaulting features, you must %1$senable vaulting on your account%2$s.', 'woocommerce-paypal-payments' ),
'<a
href="https://docs.woocommerce.com/document/woocommerce-paypal-payments/#enable-vaulting-on-your-live-account"

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
/**
* Class SettingsPageAssets
*/
@ -20,6 +22,7 @@ class SettingsPageAssets {
* @var string
*/
private $module_url;
/**
* The filesystem path to the module dir.
*
@ -27,24 +30,34 @@ class SettingsPageAssets {
*/
private $module_path;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/**
* Assets constructor.
*
* @param string $module_url The url of this module.
* @param string $module_path The filesystem path to this module.
* @param Bearer $bearer The bearer.
*/
public function __construct( string $module_url, string $module_path ) {
public function __construct( string $module_url, string $module_path, Bearer $bearer ) {
$this->module_url = $module_url;
$this->module_path = $module_path;
$this->bearer = $bearer;
}
/**
* Register assets provided by this module.
*/
public function register_assets() {
$bearer = $this->bearer;
add_action(
'admin_enqueue_scripts',
function() {
function() use ( $bearer ) {
if ( ! is_admin() || is_ajax() ) {
return;
}
@ -53,7 +66,7 @@ class SettingsPageAssets {
return;
}
$this->register_admin_assets();
$this->register_admin_assets( $bearer );
}
);
@ -84,8 +97,10 @@ class SettingsPageAssets {
/**
* Register assets for admin pages.
*
* @param Bearer $bearer The bearer.
*/
private function register_admin_assets() {
private function register_admin_assets( Bearer $bearer ) {
$gateway_settings_script_path = trailingslashit( $this->module_path ) . 'assets/js/gateway-settings.js';
wp_enqueue_script(
@ -95,5 +110,14 @@ class SettingsPageAssets {
file_exists( $gateway_settings_script_path ) ? (string) filemtime( $gateway_settings_script_path ) : null,
true
);
$token = $bearer->bearer();
wp_localize_script(
'ppcp-gateway-settings',
'PayPalCommerceGatewaySettings',
array(
'vaulting_features_available' => $token->vaulting_available(),
)
);
}
}

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Onboarding\State;
@ -21,7 +22,6 @@ use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
*/
class SettingsListener {
const NONCE = 'ppcp-settings';
/**
@ -59,6 +59,13 @@ class SettingsListener {
*/
private $state;
/**
* The Bearer.
*
* @var Bearer
*/
private $bearer;
/**
* SettingsListener constructor.
*
@ -67,13 +74,15 @@ class SettingsListener {
* @param WebhookRegistrar $webhook_registrar The Webhook Registrar.
* @param Cache $cache The Cache.
* @param State $state The state.
* @param Bearer $bearer The bearer.
*/
public function __construct(
Settings $settings,
array $setting_fields,
WebhookRegistrar $webhook_registrar,
Cache $cache,
State $state
State $state,
Bearer $bearer
) {
$this->settings = $settings;
@ -81,6 +90,7 @@ class SettingsListener {
$this->webhook_registrar = $webhook_registrar;
$this->cache = $cache;
$this->state = $state;
$this->bearer = $bearer;
}
/**
@ -137,6 +147,17 @@ class SettingsListener {
return;
}
$redirect_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' );
$token = $this->bearer->bearer();
if ( ! $token->vaulting_available() ) {
$this->settings->set( 'vault_enabled', false );
$this->settings->persist();
wp_safe_redirect( $redirect_url, 302 );
exit;
}
/**
* No need to verify nonce here.
*

View file

@ -76,7 +76,8 @@ class WcGatewayModule implements ModuleInterface {
if ( $container->has( 'wcgateway.url' ) ) {
$assets = new SettingsPageAssets(
$container->get( 'wcgateway.url' ),
$container->get( 'wcgateway.absolute-path' )
$container->get( 'wcgateway.absolute-path' ),
$container->get( 'api.bearer' )
);
$assets->register_assets();
}