From ed3a91f3330f5aaa1f7b5e547d88db6157cb5b80 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 10 Mar 2021 12:10:12 +0100 Subject: [PATCH] Add client-side vaulting for subscription renewals --- .../js/modules/Renderer/CreditCardRenderer.js | 3 +- .../src/Assets/class-smartbutton.php | 48 +++++++++++++------ modules/ppcp-wc-gateway/services.php | 4 +- .../src/Gateway/class-creditcardgateway.php | 3 +- .../src/Gateway/class-paypalgateway.php | 3 +- .../src/Gateway/class-processpaymenttrait.php | 13 +++++ 6 files changed, 53 insertions(+), 21 deletions(-) diff --git a/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js b/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js index 84c6248fa..32eb4179d 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js @@ -103,8 +103,9 @@ class CreditCardRenderer { }); if (formValid && this.cardValid) { + const vault_card_setting_enabled = this.defaultConfig.vault_card_setting_enabled ? true : false; const vault = document.getElementById('ppcp-credit-card-vault') ? - document.getElementById('ppcp-credit-card-vault').checked : false; + document.getElementById('ppcp-credit-card-vault').checked : vault_card_setting_enabled; hostedFields.submit({ contingencies: ['3D_SECURE'], vault: vault diff --git a/modules/ppcp-button/src/Assets/class-smartbutton.php b/modules/ppcp-button/src/Assets/class-smartbutton.php index 74646858e..47c4119d9 100644 --- a/modules/ppcp-button/src/Assets/class-smartbutton.php +++ b/modules/ppcp-button/src/Assets/class-smartbutton.php @@ -554,7 +554,7 @@ class SmartButton implements SmartButtonInterface { if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) { return false; } - if ( ! $this->settings->has( 'vault_enabled' ) || ! $this->settings->get( 'vault_enabled' ) ) { + if ( ! $this->vault_settings_enabled() ) { return false; } return is_user_logged_in(); @@ -597,8 +597,8 @@ class SmartButton implements SmartButtonInterface { $this->request_data->enqueue_nonce_fix(); $localize = array( - 'script_attributes' => $this->attributes(), - 'data_client_id' => array( + 'script_attributes' => $this->attributes(), + 'data_client_id' => array( 'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() ) || $this->can_save_vault_token(), 'save_paypal_account' => $this->save_paypal_account(), @@ -606,9 +606,9 @@ class SmartButton implements SmartButtonInterface { 'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ), 'user' => get_current_user_id(), ), - 'redirect' => wc_get_checkout_url(), - 'context' => $this->context(), - 'ajax' => array( + 'redirect' => wc_get_checkout_url(), + 'context' => $this->context(), + 'ajax' => array( 'change_cart' => array( 'endpoint' => home_url( \WC_AJAX::get_endpoint( ChangeCartEndpoint::ENDPOINT ) ), 'nonce' => wp_create_nonce( ChangeCartEndpoint::nonce() ), @@ -622,10 +622,11 @@ class SmartButton implements SmartButtonInterface { 'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ), ), ), - 'enforce_vault' => $this->has_subscriptions(), - 'bn_codes' => $this->bn_codes(), - 'payer' => $this->payerData(), - 'button' => array( + 'enforce_vault' => $this->has_subscriptions(), + 'vault_card_setting_enabled' => $this->vault_card_setting_enabled(), + 'bn_codes' => $this->bn_codes(), + 'payer' => $this->payerData(), + 'button' => array( 'wrapper' => '#ppc-button', 'mini_cart_wrapper' => '#ppc-button-minicart', 'cancel_wrapper' => '#ppcp-cancel', @@ -645,7 +646,7 @@ class SmartButton implements SmartButtonInterface { 'tagline' => $this->style_for_context( 'tagline', $this->context() ), ), ), - 'hosted_fields' => array( + 'hosted_fields' => array( 'wrapper' => '#ppcp-hosted-fields', 'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart', 'labels' => array( @@ -663,8 +664,8 @@ class SmartButton implements SmartButtonInterface { ), 'valid_cards' => $this->dcc_applies->valid_cards(), ), - 'messages' => $this->message_values(), - 'labels' => array( + 'messages' => $this->message_values(), + 'labels' => array( 'error' => array( 'generic' => __( 'Something went wrong. Please try again or choose another payment source.', @@ -672,7 +673,7 @@ class SmartButton implements SmartButtonInterface { ), ), ), - 'order_id' => 'pay-now' === $this->context() ? absint( $wp->query_vars['order-pay'] ) : 0, + 'order_id' => 'pay-now' === $this->context() ? absint( $wp->query_vars['order-pay'] ) : 0, ); if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) { @@ -940,4 +941,23 @@ class SmartButton implements SmartButtonInterface { } return (string) $value; } + + /** + * @return bool + * @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException + */ + protected function vault_settings_enabled(): bool { + if ( ! $this->settings->has( 'vault_enabled' ) && ! $this->settings->has( 'dcc_vault_enabled' ) + || ! $this->settings->get( 'dcc_vault_enabled' ) && ! $this->settings->get( 'dcc_vault_enabled' ) ) { + return false; + } + return true; + } + + protected function vault_card_setting_enabled(): bool { + if ( ! $this->settings->get( 'dcc_vault_enabled' ) && ! $this->settings->get( 'dcc_vault_enabled' ) ) { + return false; + } + return true; + } } diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 1a0929966..18b6d10d8 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -654,7 +654,7 @@ return array( 'desc_tip' => true, 'label' => __( 'Enable Vaulting for PayPal Accounts', 'woocommerce-paypal-payments' ), 'description' => __( '', 'woocommerce-paypal-payments' ), - 'default' => true, + 'default' => false, 'screens' => array( State::STATE_ONBOARDED, ), @@ -667,7 +667,7 @@ return array( 'desc_tip' => true, 'label' => __( 'Allow Registered Buyers to Save PayPal Account', 'woocommerce-paypal-payments' ), 'description' => __( 'Buyers that create an account on your store may save their PayPal account for faster checkout. Note that you may not present Pay Later messages when using this feature.', 'woocommerce-paypal-payments' ), - 'default' => true, + 'default' => false, 'screens' => array( State::STATE_ONBOARDED, ), diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index 977e37846..6ba30517e 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php @@ -81,8 +81,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { if ( defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION - && $this->config->has( 'vault_enabled' ) - && $this->config->get( 'vault_enabled' ) + && $this->vault_settings_enabled() ) { $this->supports = array( 'refunds', diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index e9c9ccace..9e39cd376 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -119,8 +119,7 @@ class PayPalGateway extends \WC_Payment_Gateway { if ( defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION - && $this->config->has( 'vault_enabled' ) - && $this->config->get( 'vault_enabled' ) + && $this->vault_settings_enabled() ) { $this->supports = array( 'refunds', diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php b/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php index 4c1260470..f95f0331f 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php @@ -85,4 +85,17 @@ trait ProcessPaymentTrait { return null; } + + /** + * @return bool + * @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException + */ + protected function vault_settings_enabled(): bool { + if ( ! $this->config->has( 'vault_enabled' ) && ! $this->config->has( 'dcc_vault_enabled' ) + || ! $this->config->get( 'dcc_vault_enabled' ) && ! $this->config->get( 'dcc_vault_enabled' ) ) { + return false; + } + + return true; + } }