Consolidate vaulting settings in one single one

This commit is contained in:
dinamiko 2021-04-23 10:33:28 +02:00
parent 267a4704c9
commit ea23200de6
10 changed files with 36 additions and 166 deletions

View file

@ -89,7 +89,7 @@ document.addEventListener(
}
);
if (PayPalCommerceGateway.data_client_id.set_attribute || PayPalCommerceGateway.data_client_id.save_paypal_account) {
if (PayPalCommerceGateway.data_client_id.set_attribute) {
dataClientIdAttributeHandler(script, PayPalCommerceGateway.data_client_id);
return;
}

View file

@ -103,9 +103,9 @@ class CreditCardRenderer {
});
if (formValid && this.cardValid) {
const vault_card_setting_enabled = this.defaultConfig.vault_card_setting_enabled ? true : false;
const save_card = this.defaultConfig.save_card ? true : false;
const vault = document.getElementById('ppcp-credit-card-vault') ?
document.getElementById('ppcp-credit-card-vault').checked : vault_card_setting_enabled;
document.getElementById('ppcp-credit-card-vault').checked : save_card;
hostedFields.submit({
contingencies: ['3D_SECURE'],
vault: vault

View file

@ -204,7 +204,7 @@ class SmartButton implements SmartButtonInterface {
add_filter(
'woocommerce_credit_card_form_fields',
function ( $default_fields, $id ) use ( $payment_token_repository ) {
if ( $this->can_save_credit_card() ) {
if ( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) ) {
$default_fields['card-vault'] = sprintf(
'<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>',
esc_html__( 'Save your Credit Card', 'woocommerce-paypal-payments' )
@ -573,7 +573,7 @@ class SmartButton implements SmartButtonInterface {
* Whether we can store vault tokens or not.
*
* @return bool
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasnt been found.
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasn't been found.
*/
public function can_save_vault_token(): bool {
@ -581,31 +581,13 @@ class SmartButton implements SmartButtonInterface {
return false;
}
if ( $this->context() === 'checkout' && ! $this->vault_settings_enabled() ) {
return false;
} elseif ( ! $this->vault_setting_enabled_for_paypal() ) {
if ( ! $this->settings->has( 'vault_enabled' ) || ! $this->settings->get( 'vault_enabled' ) ) {
return false;
}
return is_user_logged_in();
}
/**
* Checks whether it can save credit cards.
*
* @return bool
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When nothing was found.
*/
private function can_save_credit_card() {
if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) {
return false;
}
if ( ! $this->settings->has( 'dcc_save_card' ) || ! $this->settings->get( 'dcc_save_card' ) ) {
return false;
}
return is_user_logged_in();
}
/**
* Whether we need to initialize the script to enable tokenization for subscriptions or not.
*
@ -632,18 +614,16 @@ class SmartButton implements SmartButtonInterface {
$this->request_data->enqueue_nonce_fix();
$localize = 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(),
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
'user' => get_current_user_id(),
'script_attributes' => $this->attributes(),
'data_client_id' => array(
'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() ) || $this->can_save_vault_token(),
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
'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() ),
@ -657,11 +637,11 @@ class SmartButton implements SmartButtonInterface {
'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ),
),
),
'enforce_vault' => $this->has_subscriptions(),
'vault_card_setting_enabled' => $this->vault_card_setting_enabled(),
'bn_codes' => $this->bn_codes(),
'payer' => $this->payerData(),
'button' => array(
'enforce_vault' => $this->has_subscriptions(),
'save_card' => $this->can_save_vault_token(),
'bn_codes' => $this->bn_codes(),
'payer' => $this->payerData(),
'button' => array(
'wrapper' => '#ppc-button',
'mini_cart_wrapper' => '#ppc-button-minicart',
'cancel_wrapper' => '#ppcp-cancel',
@ -681,7 +661,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(
@ -699,8 +679,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.',
@ -708,7 +688,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' ) {
@ -931,19 +911,6 @@ class SmartButton implements SmartButtonInterface {
return true;
}
/**
* Checks if can save PayPal accounts.
*
* @return bool Whether it can save it or not.
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found.
*/
private function save_paypal_account(): bool {
if ( ! $this->settings->has( 'save_paypal_account' ) || ! $this->settings->get( 'save_paypal_account' ) ) {
return false;
}
return true;
}
/**
* Determines the style for a given indicator in a given context.
*
@ -976,53 +943,6 @@ class SmartButton implements SmartButtonInterface {
return (string) $value;
}
/**
* Checks if vault enabled setting for PayPal or credit card is enabled.
*
* @return bool Whether any of them is enabled or not.
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn't been found.
*/
protected function vault_settings_enabled(): bool {
if ( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) ) {
return true;
}
if ( $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) && $this->settings->has( 'dcc_vault_enabled' ) && $this->settings->get( 'dcc_vault_enabled' ) ) {
return true;
}
return false;
}
/**
* Checks if vault enabled setting for PayPal is enabled.
*
* @return bool Wheter vault setting is enabled for PayPal.
* @throws NotFoundException When a setting hasn't been found.
*/
protected function vault_setting_enabled_for_paypal() {
if ( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) ) {
return true;
}
return false;
}
/**
* Checks if vaulting for credit card is enabled.
*
* @return bool Whether if it is enabled or not.
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn't been found.
*/
protected function vault_card_setting_enabled(): bool {
try {
if ( ! $this->settings->has( 'dcc_vault_enabled' ) && ! $this->settings->get( 'dcc_vault_enabled' ) ) {
return false;
}
} catch ( NotFoundException $exception ) {
return false;
}
return true;
}
/**
* Check if tokens has card source.
*

View file

@ -6,7 +6,7 @@
)
const vaultingCheckboxes = document.querySelectorAll(
"#ppcp-vault_enabled, #ppcp-save_paypal_account"
"#ppcp-vault_enabled"
)
function atLeastOneChecked(checkboxesNodeList) {

View file

@ -488,34 +488,6 @@ return array(
),
'gateway' => 'dcc',
),
'dcc_vault_enabled' => array(
'title' => __( 'Vaulting For Credit Cards', 'woocommerce-paypal-payments' ),
'desc_tip' => true,
'description' => __( 'Enable Payment Tokens for WooCommerce Subscription renewals', 'woocommerce-paypal-payments' ),
'label' => __( 'Enable Vaulting For Credit Cards', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'default' => false,
'gateway' => 'dcc',
'requirements' => array(
'dcc',
),
'screens' => array(
State::STATE_ONBOARDED,
),
),
'dcc_save_card' => array(
'title' => __( 'Save Credit Card', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'desc_tip' => true,
'label' => __( 'Allow Registered Buyers to Save Credit Card', 'woocommerce-paypal-payments' ),
'description' => __( 'Buyers that create an account on your store may save their Credit Card for faster checkout.', 'woocommerce-paypal-payments' ),
'default' => false,
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => 'dcc',
),
'description' => array(
'title' => __( 'Description', 'woocommerce-paypal-payments' ),
'type' => 'text',
@ -659,30 +631,17 @@ return array(
'gateway' => 'paypal',
),
'vault_enabled' => array(
'title' => __( 'Vaulting for PayPal Accounts', 'woocommerce-paypal-payments' ),
'title' => __( 'Vaulting', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'desc_tip' => true,
'label' => __( 'Enable Vaulting for PayPal Accounts', 'woocommerce-paypal-payments' ),
'description' => __( 'Enable Payment Tokens for WooCommerce Subscription renewals', 'woocommerce-paypal-payments' ),
'label' => __( 'Enable Vaulting', 'woocommerce-paypal-payments' ),
'description' => __( 'Allow registered buyers to save PayPal and Credit Card accounts. Allow Subscription renewals.', 'woocommerce-paypal-payments' ),
'default' => false,
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => 'paypal',
),
'save_paypal_account' => array(
'title' => __( 'Save PayPal Account', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'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' => false,
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => 'paypal',
'gateway' => 'all',
),
'logging_enabled' => array(
'title' => __( 'Logging', 'woocommerce-paypal-payments' ),

View file

@ -131,7 +131,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
defined( 'PPCP_FLAG_SUBSCRIPTION' )
&& PPCP_FLAG_SUBSCRIPTION
&& $this->gateways_enabled()
&& $this->vault_settings_enabled()
&& $this->vault_setting_enabled()
) {
$this->supports = array(
'refunds',

View file

@ -130,7 +130,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
defined( 'PPCP_FLAG_SUBSCRIPTION' )
&& PPCP_FLAG_SUBSCRIPTION
&& $this->gateways_enabled()
&& $this->vault_settings_enabled()
&& $this->vault_setting_enabled()
) {
$this->supports = array(
'refunds',

View file

@ -163,18 +163,15 @@ trait ProcessPaymentTrait {
}
/**
* Checks if vault enabled setting for PayPal or credit card is enabled.
* Checks if vault setting is enabled.
*
* @return bool Whether vault settings are enabled or not.
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn't been found.
*/
protected function vault_settings_enabled(): bool {
protected function vault_setting_enabled(): bool {
if ( $this->config->has( 'vault_enabled' ) && $this->config->get( 'vault_enabled' ) ) {
return true;
}
if ( $this->config->has( 'dcc_vault_enabled' ) && $this->config->get( 'dcc_vault_enabled' ) ) {
return true;
}
return false;
}
}

View file

@ -143,7 +143,7 @@ class SettingsListener {
* phpcs:disable WordPress.Security.NonceVerification.Missing
* phpcs:disable WordPress.Security.NonceVerification.Recommended
*/
if ( ! isset( $_POST['ppcp']['vault_enabled'] ) && ! isset( $_POST['ppcp']['save_paypal_account'] ) ) {
if ( ! isset( $_POST['ppcp']['vault_enabled'] ) ) {
return;
}

View file

@ -141,18 +141,12 @@ class SettingsRenderer {
}
/**
* Check whether PayPal vaulting is enabled.
* Check whether vaulting is enabled.
*
* @return bool
*/
private function paypal_vaulting_is_enabled(): bool {
$saving_paypal_account_is_enabled = $this->settings->has( 'save_paypal_account' ) &&
(bool) $this->settings->get( 'save_paypal_account' );
$vault_is_enabled = $this->settings->has( 'vault_enabled' ) &&
(bool) $this->settings->get( 'vault_enabled' );
return $saving_paypal_account_is_enabled || $vault_is_enabled;
return $this->settings->has( 'vault_enabled' ) && (bool) $this->settings->get( 'vault_enabled' );
}
/**