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 ); 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. * Validates whether a JSON object can be transformed to a Token object.
* *

View file

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

View file

@ -133,7 +133,8 @@ return array(
$webhook_registrar = $container->get( 'webhook.registrar' ); $webhook_registrar = $container->get( 'webhook.registrar' );
$state = $container->get( 'onboarding.state' ); $state = $container->get( 'onboarding.state' );
$cache = new Cache( 'ppcp-paypal-bearer' ); $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 { 'wcgateway.order-processor' => static function ( $container ): OrderProcessor {
@ -182,7 +183,6 @@ return array(
'wcgateway.settings.fields' => static function ( $container ): array { 'wcgateway.settings.fields' => static function ( $container ): array {
$state = $container->get( 'onboarding.state' ); $state = $container->get( 'onboarding.state' );
$settings = $container->get( 'wcgateway.settings' );
$messages_disclaimers = $container->get( 'button.helper.messages-disclaimers' ); $messages_disclaimers = $container->get( 'button.helper.messages-disclaimers' );
$fields = array( $fields = array(
@ -634,8 +634,9 @@ return array(
'title' => __( 'Vaulting', 'woocommerce-paypal-payments' ), 'title' => __( 'Vaulting', 'woocommerce-paypal-payments' ),
'type' => 'checkbox', 'type' => 'checkbox',
'desc_tip' => true, 'desc_tip' => true,
'label' => sprintf( 'label' => sprintf(
__('To use vaulting features, you must %1$senable vaulting on your account%2$s.', 'woocommerce-paypal-payments'), // 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 '<a
href="https://docs.woocommerce.com/document/woocommerce-paypal-payments/#enable-vaulting-on-your-live-account" href="https://docs.woocommerce.com/document/woocommerce-paypal-payments/#enable-vaulting-on-your-live-account"
target="_blank" target="_blank"

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Assets; namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
/** /**
* Class SettingsPageAssets * Class SettingsPageAssets
*/ */
@ -20,6 +22,7 @@ class SettingsPageAssets {
* @var string * @var string
*/ */
private $module_url; private $module_url;
/** /**
* The filesystem path to the module dir. * The filesystem path to the module dir.
* *
@ -27,24 +30,34 @@ class SettingsPageAssets {
*/ */
private $module_path; private $module_path;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/** /**
* Assets constructor. * Assets constructor.
* *
* @param string $module_url The url of this module. * @param string $module_url The url of this module.
* @param string $module_path The filesystem path to 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_url = $module_url;
$this->module_path = $module_path; $this->module_path = $module_path;
$this->bearer = $bearer;
} }
/** /**
* Register assets provided by this module. * Register assets provided by this module.
*/ */
public function register_assets() { public function register_assets() {
$bearer = $this->bearer;
add_action( add_action(
'admin_enqueue_scripts', 'admin_enqueue_scripts',
function() { function() use ( $bearer ) {
if ( ! is_admin() || is_ajax() ) { if ( ! is_admin() || is_ajax() ) {
return; return;
} }
@ -53,7 +66,7 @@ class SettingsPageAssets {
return; return;
} }
$this->register_admin_assets(); $this->register_admin_assets( $bearer );
} }
); );
@ -84,8 +97,10 @@ class SettingsPageAssets {
/** /**
* Register assets for admin pages. * 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'; $gateway_settings_script_path = trailingslashit( $this->module_path ) . 'assets/js/gateway-settings.js';
wp_enqueue_script( wp_enqueue_script(
@ -95,5 +110,14 @@ class SettingsPageAssets {
file_exists( $gateway_settings_script_path ) ? (string) filemtime( $gateway_settings_script_path ) : null, file_exists( $gateway_settings_script_path ) ? (string) filemtime( $gateway_settings_script_path ) : null,
true 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; namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
@ -21,7 +22,6 @@ use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
*/ */
class SettingsListener { class SettingsListener {
const NONCE = 'ppcp-settings'; const NONCE = 'ppcp-settings';
/** /**
@ -59,6 +59,13 @@ class SettingsListener {
*/ */
private $state; private $state;
/**
* The Bearer.
*
* @var Bearer
*/
private $bearer;
/** /**
* SettingsListener constructor. * SettingsListener constructor.
* *
@ -67,13 +74,15 @@ class SettingsListener {
* @param WebhookRegistrar $webhook_registrar The Webhook Registrar. * @param WebhookRegistrar $webhook_registrar The Webhook Registrar.
* @param Cache $cache The Cache. * @param Cache $cache The Cache.
* @param State $state The state. * @param State $state The state.
* @param Bearer $bearer The bearer.
*/ */
public function __construct( public function __construct(
Settings $settings, Settings $settings,
array $setting_fields, array $setting_fields,
WebhookRegistrar $webhook_registrar, WebhookRegistrar $webhook_registrar,
Cache $cache, Cache $cache,
State $state State $state,
Bearer $bearer
) { ) {
$this->settings = $settings; $this->settings = $settings;
@ -81,6 +90,7 @@ class SettingsListener {
$this->webhook_registrar = $webhook_registrar; $this->webhook_registrar = $webhook_registrar;
$this->cache = $cache; $this->cache = $cache;
$this->state = $state; $this->state = $state;
$this->bearer = $bearer;
} }
/** /**
@ -137,6 +147,17 @@ class SettingsListener {
return; 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. * No need to verify nonce here.
* *

View file

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