Add client-side vaulting for subscription renewals

This commit is contained in:
dinamiko 2021-03-10 12:10:12 +01:00
parent e7f592efe3
commit ed3a91f333
6 changed files with 53 additions and 21 deletions

View file

@ -103,8 +103,9 @@ class CreditCardRenderer {
}); });
if (formValid && this.cardValid) { 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') ? 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({ hostedFields.submit({
contingencies: ['3D_SECURE'], contingencies: ['3D_SECURE'],
vault: vault vault: vault

View file

@ -554,7 +554,7 @@ class SmartButton implements SmartButtonInterface {
if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) { if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) {
return false; return false;
} }
if ( ! $this->settings->has( 'vault_enabled' ) || ! $this->settings->get( 'vault_enabled' ) ) { if ( ! $this->vault_settings_enabled() ) {
return false; return false;
} }
return is_user_logged_in(); return is_user_logged_in();
@ -597,8 +597,8 @@ class SmartButton implements SmartButtonInterface {
$this->request_data->enqueue_nonce_fix(); $this->request_data->enqueue_nonce_fix();
$localize = array( $localize = array(
'script_attributes' => $this->attributes(), 'script_attributes' => $this->attributes(),
'data_client_id' => array( 'data_client_id' => array(
'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() ) 'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() )
|| $this->can_save_vault_token(), || $this->can_save_vault_token(),
'save_paypal_account' => $this->save_paypal_account(), 'save_paypal_account' => $this->save_paypal_account(),
@ -606,9 +606,9 @@ class SmartButton implements SmartButtonInterface {
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ), 'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
'user' => get_current_user_id(), 'user' => get_current_user_id(),
), ),
'redirect' => wc_get_checkout_url(), 'redirect' => wc_get_checkout_url(),
'context' => $this->context(), 'context' => $this->context(),
'ajax' => array( 'ajax' => array(
'change_cart' => array( 'change_cart' => array(
'endpoint' => home_url( \WC_AJAX::get_endpoint( ChangeCartEndpoint::ENDPOINT ) ), 'endpoint' => home_url( \WC_AJAX::get_endpoint( ChangeCartEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( ChangeCartEndpoint::nonce() ), 'nonce' => wp_create_nonce( ChangeCartEndpoint::nonce() ),
@ -622,10 +622,11 @@ class SmartButton implements SmartButtonInterface {
'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ), 'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ),
), ),
), ),
'enforce_vault' => $this->has_subscriptions(), 'enforce_vault' => $this->has_subscriptions(),
'bn_codes' => $this->bn_codes(), 'vault_card_setting_enabled' => $this->vault_card_setting_enabled(),
'payer' => $this->payerData(), 'bn_codes' => $this->bn_codes(),
'button' => array( 'payer' => $this->payerData(),
'button' => array(
'wrapper' => '#ppc-button', 'wrapper' => '#ppc-button',
'mini_cart_wrapper' => '#ppc-button-minicart', 'mini_cart_wrapper' => '#ppc-button-minicart',
'cancel_wrapper' => '#ppcp-cancel', 'cancel_wrapper' => '#ppcp-cancel',
@ -645,7 +646,7 @@ class SmartButton implements SmartButtonInterface {
'tagline' => $this->style_for_context( 'tagline', $this->context() ), 'tagline' => $this->style_for_context( 'tagline', $this->context() ),
), ),
), ),
'hosted_fields' => array( 'hosted_fields' => array(
'wrapper' => '#ppcp-hosted-fields', 'wrapper' => '#ppcp-hosted-fields',
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart', 'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
'labels' => array( 'labels' => array(
@ -663,8 +664,8 @@ class SmartButton implements SmartButtonInterface {
), ),
'valid_cards' => $this->dcc_applies->valid_cards(), 'valid_cards' => $this->dcc_applies->valid_cards(),
), ),
'messages' => $this->message_values(), 'messages' => $this->message_values(),
'labels' => array( 'labels' => array(
'error' => array( 'error' => array(
'generic' => __( 'generic' => __(
'Something went wrong. Please try again or choose another payment source.', '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' ) { if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) {
@ -940,4 +941,23 @@ class SmartButton implements SmartButtonInterface {
} }
return (string) $value; 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;
}
} }

View file

@ -654,7 +654,7 @@ return array(
'desc_tip' => true, 'desc_tip' => true,
'label' => __( 'Enable Vaulting for PayPal Accounts', 'woocommerce-paypal-payments' ), 'label' => __( 'Enable Vaulting for PayPal Accounts', 'woocommerce-paypal-payments' ),
'description' => __( '', 'woocommerce-paypal-payments' ), 'description' => __( '', 'woocommerce-paypal-payments' ),
'default' => true, 'default' => false,
'screens' => array( 'screens' => array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
@ -667,7 +667,7 @@ return array(
'desc_tip' => true, 'desc_tip' => true,
'label' => __( 'Allow Registered Buyers to Save PayPal Account', 'woocommerce-paypal-payments' ), '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' ), '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( 'screens' => array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),

View file

@ -81,8 +81,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
if ( if (
defined( 'PPCP_FLAG_SUBSCRIPTION' ) defined( 'PPCP_FLAG_SUBSCRIPTION' )
&& PPCP_FLAG_SUBSCRIPTION && PPCP_FLAG_SUBSCRIPTION
&& $this->config->has( 'vault_enabled' ) && $this->vault_settings_enabled()
&& $this->config->get( 'vault_enabled' )
) { ) {
$this->supports = array( $this->supports = array(
'refunds', 'refunds',

View file

@ -119,8 +119,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
if ( if (
defined( 'PPCP_FLAG_SUBSCRIPTION' ) defined( 'PPCP_FLAG_SUBSCRIPTION' )
&& PPCP_FLAG_SUBSCRIPTION && PPCP_FLAG_SUBSCRIPTION
&& $this->config->has( 'vault_enabled' ) && $this->vault_settings_enabled()
&& $this->config->get( 'vault_enabled' )
) { ) {
$this->supports = array( $this->supports = array(
'refunds', 'refunds',

View file

@ -85,4 +85,17 @@ trait ProcessPaymentTrait {
return null; 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;
}
} }