mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Consolidate vaulting settings in one single one
This commit is contained in:
parent
267a4704c9
commit
ea23200de6
10 changed files with 36 additions and 166 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue