split up where to show the credit cards

This commit is contained in:
David Remer 2020-04-30 16:30:23 +03:00
parent bc59efeb3c
commit 9938103edb
9 changed files with 115 additions and 36 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -20,7 +20,9 @@ class CartBootstrap {
}
shouldRender() {
return document.querySelector(this.gateway.button.wrapper) !== null;
return document.querySelector(this.gateway.button.wrapper) !==
null || document.querySelector(this.gateway.hosted_fields.wrapper) !==
null;
}
render() {

View file

@ -5,9 +5,15 @@ class MiniCartBootstap {
constructor(gateway, renderer) {
this.gateway = gateway;
this.renderer = renderer;
this.actionHandler = null;
}
init() {
this.actionHandler = new CartActionHandler(
PayPalCommerceGateway,
new ErrorHandler(),
);
this.render();
jQuery(document.body).on('wc_fragments_loaded wc_fragments_refreshed', () => {
@ -17,7 +23,8 @@ class MiniCartBootstap {
shouldRender() {
return document.querySelector(this.gateway.button.mini_cart_wrapper) !==
null;
null || document.querySelector(this.gateway.hosted_fields.mini_cart_wrapper) !==
null;
}
render() {
@ -25,15 +32,10 @@ class MiniCartBootstap {
return;
}
const actionHandler = new CartActionHandler(
PayPalCommerceGateway,
new ErrorHandler(),
);
this.renderer.render(
this.gateway.button.mini_cart_wrapper,
null,
actionHandler.configuration()
this.gateway.hosted_fields.mini_cart_wrapper,
this.actionHandler.configuration()
);
}
}

View file

@ -21,7 +21,7 @@ class SingleProductBootstap {
return false;
}
return document.querySelector(this.gateway.button.wrapper) !== null;
return true;
}
render() {

View file

@ -20,15 +20,15 @@ class CreditCardRenderer {
createOrder: contextConfig.createOrder,
fields: {
number: {
selector: '#ppcp-credit-card',
selector: wrapper + ' .ppcp-credit-card',
placeholder: this.defaultConfig.hosted_fields.labels.credit_card_number,
},
cvv: {
selector: '#ppcp-cvv',
selector: wrapper + ' .ppcp-cvv',
placeholder: this.defaultConfig.hosted_fields.labels.cvv,
},
expirationDate: {
selector: '#ppcp-expiration-date',
selector: wrapper + ' .ppcp-expiration-date',
placeholder: this.defaultConfig.hosted_fields.labels.mm_yyyy,
}
}

View file

@ -5,7 +5,13 @@ class Renderer {
}
render(wrapper, hostedFieldsWrapper, contextConfig) {
if (this.isAlreadyRendered(wrapper)) {
this.renderButtons(wrapper, contextConfig);
this.creditCardRenderer.render(hostedFieldsWrapper, contextConfig);
}
renderButtons(wrapper, contextConfig) {
if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper)) {
return;
}
@ -14,9 +20,7 @@ class Renderer {
style,
...contextConfig,
}).render(wrapper);
this.creditCardRenderer.render(hostedFieldsWrapper, contextConfig);
}
}
isAlreadyRendered(wrapper) {
return document.querySelector(wrapper).hasChildNodes();

View file

@ -39,31 +39,63 @@ class SmartButton implements SmartButtonInterface
public function renderWrapper(): bool
{
$hostedFieldsEnabled = $this->dccIsEnabled();
$renderer = static function () use ($hostedFieldsEnabled) {
$buttonRenderer = static function () {
echo '<div id="ppc-button"></div>';
if (! $hostedFieldsEnabled) {
return;
};
$dccRenderer = static function ($id = null, $class = null) {
static $times;
if (!$id) {
$id = 'ppcp-hosted-fields';
}
if (! $times) {
$times = 0;
}
$times++;
printf(
'<form id="ppc-hosted-fields"><label for="ppcp-credit-card">%s</label><div id="ppcp-credit-card"></div><label for="ppcp-expiration-date">%s</label><div id="ppcp-expiration-date"></div><label for="ppcp-cvv">%s</label><div id="ppcp-cvv"></div><button>%s</button></form>',
__('Card number', 'woocommerce-paypal-commerce-gateway'),
__('Expiration Date', 'woocommerce-paypal-commerce-gateway'),
__('CVV', 'woocommerce-paypal-commerce-gateway'),
__('Pay with Card', 'woocommerce-paypal-commerce-gateway')
'<form id="%2$s" class="%3$s">
<label for="ppcp-credit-card-%1$d">%4$s</label>
<div id="ppcp-credit-card-%1$d" class="ppcp-credit-card"></div>
<label for="ppcp-expiration-date-%1$d">%5$s</label>
<div id="ppcp-expiration-date-%1$d" class="ppcp-expiration-date"></div>
<label for="ppcp-cvv">%6$s</label>
<div id="ppcp-cvv" class="ppcp-cvv"></div>
<button>%7$s</button>
</form>',
$times,
esc_attr($id),
esc_attr($class),
esc_html__('Card number', 'woocommerce-paypal-commerce-gateway'),
esc_html__('Expiration Date', 'woocommerce-paypal-commerce-gateway'),
esc_html__('CVV', 'woocommerce-paypal-commerce-gateway'),
esc_html__('Pay with Card', 'woocommerce-paypal-commerce-gateway')
);
};
if (is_cart() && wc_string_to_bool($this->settings->get('button_cart_enabled'))) {
add_action(
'woocommerce_proceed_to_checkout',
$renderer,
$buttonRenderer,
20
);
}
if (is_cart() && wc_string_to_bool($this->settings->get('dcc_cart_enabled'))) {
add_action(
'woocommerce_proceed_to_checkout',
$dccRenderer,
20
);
}
if (is_product() && wc_string_to_bool($this->settings->get('button_single_product_enabled'))) {
add_action(
'woocommerce_single_product_summary',
$renderer,
$buttonRenderer,
31
);
}
if (is_product() && wc_string_to_bool($this->settings->get('dcc_single_product_enabled'))) {
add_action(
'woocommerce_single_product_summary',
$dccRenderer,
31
);
}
@ -76,11 +108,27 @@ class SmartButton implements SmartButtonInterface
30
);
}
if (wc_string_to_bool($this->settings->get('dcc_mini_cart_enabled'))) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
static function () use ($dccRenderer) {
$dccRenderer('ppcp-hosted-fields-mini-cart', 'woocommerce-mini-cart__buttons buttons');
},
31
);
}
add_action(
'woocommerce_review_order_after_submit',
$renderer,
$buttonRenderer,
10
);
if (wc_string_to_bool($this->settings->get('dcc_checkout_enabled'))) {
add_action(
'woocommerce_review_order_after_submit',
$dccRenderer,
11
);
}
return true;
}
@ -136,7 +184,8 @@ class SmartButton implements SmartButtonInterface
],
],
'hosted_fields' => [
'wrapper' => '#ppc-hosted-fields',
'wrapper' => '#ppcp-hosted-fields',
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
'labels' => [
'credit_card_number' => __('Credit Card Number', 'woocommerce-paypal-commerce-gateway'),
'cvv' => __('CVV', 'woocommerce-paypal-commerce-gateway'),
@ -247,6 +296,10 @@ class SmartButton implements SmartButtonInterface
private function dccIsEnabled() : bool
{
return wc_string_to_bool($this->settings->get('enable_dcc'));
return
wc_string_to_bool($this->settings->get('dcc_cart_enabled'))
|| wc_string_to_bool($this->settings->get('dcc_mini_cart_enabled'))
|| wc_string_to_bool($this->settings->get('dcc_checkout_enabled'))
|| wc_string_to_bool($this->settings->get('dcc_single_product_enabled'));
}
}

View file

@ -187,10 +187,28 @@ class SettingsFields
'woocommerce-paypal-gateway'
),
],
'enable_dcc' => [
'title' => __('Enable credit card payment', 'woocommerce-paypal-gateway'),
'dcc_cart_enabled' => [
'title' => __('Enable credit card on cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable credit card payments.', 'woocommerce-paypal-gateway'),
'label' => __('Allow your customers to pay with credit card directly in your cart.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'dcc_mini_cart_enabled' => [
'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card directly in your mini cart.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'dcc_checkout_enabled' => [
'title' => __('Enable credit card on checkout', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card in the checkout.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'dcc_single_product_enabled' => [
'title' => __('Enable credit card on products', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card instantly on the product page.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'disable_cards' => [