render dcc fields and settings only when dcc is applicable in the country

This commit is contained in:
David Remer 2020-07-23 11:10:37 +03:00
parent 7c5f412e30
commit 6705d2cb26
4 changed files with 57 additions and 7 deletions

View file

@ -56,6 +56,7 @@ return [
$requestData = $container->get('button.request-data'); $requestData = $container->get('button.request-data');
$clientId = $container->get('button.client_id'); $clientId = $container->get('button.client_id');
$dccApplies = $container->get('api.helpers.dccapplies');
return new SmartButton( return new SmartButton(
$container->get('button.url'), $container->get('button.url'),
$container->get('session.handler'), $container->get('session.handler'),
@ -64,7 +65,8 @@ return [
$identityToken, $identityToken,
$payerFactory, $payerFactory,
$clientId, $clientId,
$requestData $requestData,
$dccApplies
); );
}, },
'button.url' => static function (ContainerInterface $container): string { 'button.url' => static function (ContainerInterface $container): string {

View file

@ -7,6 +7,7 @@ namespace Inpsyde\PayPalCommerce\Button\Assets;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException; use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory; use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository; use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint; use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint; use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
@ -25,6 +26,7 @@ class SmartButton implements SmartButtonInterface
private $payerFactory; private $payerFactory;
private $clientId; private $clientId;
private $requestData; private $requestData;
private $dccApplies;
public function __construct( public function __construct(
string $moduleUrl, string $moduleUrl,
@ -34,7 +36,8 @@ class SmartButton implements SmartButtonInterface
IdentityToken $identityToken, IdentityToken $identityToken,
PayerFactory $payerFactory, PayerFactory $payerFactory,
string $clientId, string $clientId,
RequestData $requestData RequestData $requestData,
DccApplies $dccApplies
) { ) {
$this->moduleUrl = $moduleUrl; $this->moduleUrl = $moduleUrl;
@ -45,6 +48,7 @@ class SmartButton implements SmartButtonInterface
$this->payerFactory = $payerFactory; $this->payerFactory = $payerFactory;
$this->clientId = $clientId; $this->clientId = $clientId;
$this->requestData = $requestData; $this->requestData = $requestData;
$this->dccApplies = $dccApplies;
} }
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
@ -70,7 +74,7 @@ class SmartButton implements SmartButtonInterface
echo '<div id="ppc-button"></div>'; echo '<div id="ppc-button"></div>';
}; };
$canRenderDcc = $this->settings->has('client_id') && $this->settings->get('client_id'); $canRenderDcc = $this->dccApplies->forCountryCurrency() && $this->settings->has('client_id') && $this->settings->get('client_id');
$dccRenderer = static function (bool $miniCart = false) use ($canRenderDcc) { $dccRenderer = static function (bool $miniCart = false) use ($canRenderDcc) {
$id = ($miniCart) ? 'ppcp-hosted-fields-mini-cart' : 'ppcp-hosted-fields'; $id = ($miniCart) ? 'ppcp-hosted-fields-mini-cart' : 'ppcp-hosted-fields';
if (! $canRenderDcc) { if (! $canRenderDcc) {
@ -305,7 +309,7 @@ class SmartButton implements SmartButtonInterface
'integration-date' => date('Y-m-d'), 'integration-date' => date('Y-m-d'),
'components' => implode(',', $this->components()), 'components' => implode(',', $this->components()),
//ToDo: Probably only needed, when DCC //ToDo: Probably only needed, when DCC
'vault' => is_user_logged_in() ? 'true' : 'false', 'vault' => $this->dccIsEnabled() ? 'true' : 'false',
'commit' => is_checkout() ? 'true' : 'false', 'commit' => is_checkout() ? 'true' : 'false',
'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture', 'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture',
]; ];
@ -331,7 +335,7 @@ class SmartButton implements SmartButtonInterface
'data-partner-attribution-id' => $this->bnCodeForContext($this->context()), 'data-partner-attribution-id' => $this->bnCodeForContext($this->context()),
]; ];
try { try {
if (! is_user_logged_in()) { if (! is_user_logged_in() || ! $this->dccIsEnabled()) {
return $attributes; return $attributes;
} }
$clientToken = $this->identityToken->generateForCustomer((int) get_current_user_id()); $clientToken = $this->identityToken->generateForCustomer((int) get_current_user_id());
@ -395,6 +399,9 @@ class SmartButton implements SmartButtonInterface
private function dccIsEnabled(): bool private function dccIsEnabled(): bool
{ {
if (! $this->dccApplies->forCountryCurrency()) {
return false;
}
$keys = [ $keys = [
'dcc_cart_enabled', 'dcc_cart_enabled',
'dcc_mini_cart_enabled', 'dcc_mini_cart_enabled',

View file

@ -57,7 +57,8 @@ return [
$settings = $container->get('wcgateway.settings'); $settings = $container->get('wcgateway.settings');
$state = $container->get('onboarding.state'); $state = $container->get('onboarding.state');
$fields = $container->get('wcgateway.settings.fields'); $fields = $container->get('wcgateway.settings.fields');
return new SettingsRenderer($settings, $state, $fields); $dccApplies = $container->get('api.helpers.dccapplies');
return new SettingsRenderer($settings, $state, $fields, $dccApplies);
}, },
'wcgateway.settings.listener' => static function (ContainerInterface $container): SettingsListener { 'wcgateway.settings.listener' => static function (ContainerInterface $container): SettingsListener {
$settings = $container->get('wcgateway.settings'); $settings = $container->get('wcgateway.settings');
@ -133,6 +134,7 @@ return [
'screens' => [ 'screens' => [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
], ],
'requirements' => [],
], ],
'sandbox_on' => [ 'sandbox_on' => [
'title' => __('Sandbox', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Sandbox', 'woocommerce-paypal-commerce-gateway'),
@ -142,6 +144,7 @@ return [
'screens' => [ 'screens' => [
State::STATE_START, State::STATE_START,
], ],
'requirements' => [],
], ],
'sandbox_on_info' => [ 'sandbox_on_info' => [
'title' => __('Sandbox', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Sandbox', 'woocommerce-paypal-commerce-gateway'),
@ -152,6 +155,7 @@ return [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'hidden' => 'sandbox_on', 'hidden' => 'sandbox_on',
'requirements' => [],
], ],
'merchant_email' => [ 'merchant_email' => [
'title' => __('Email address', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Email address', 'woocommerce-paypal-commerce-gateway'),
@ -162,6 +166,7 @@ return [
'screens' => [ 'screens' => [
State::STATE_START, State::STATE_START,
], ],
'requirements' => [],
], ],
'merchant_email_info' => [ 'merchant_email_info' => [
'title' => __('Email address', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Email address', 'woocommerce-paypal-commerce-gateway'),
@ -172,6 +177,7 @@ return [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'hidden' => 'merchant_email', 'hidden' => 'merchant_email',
'requirements' => [],
], ],
'title' => [ 'title' => [
'title' => __('Title', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Title', 'woocommerce-paypal-commerce-gateway'),
@ -186,6 +192,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'description' => [ 'description' => [
'title' => __('Description', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Description', 'woocommerce-paypal-commerce-gateway'),
@ -203,6 +210,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'intent' => [ 'intent' => [
'title' => __('Intent', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Intent', 'woocommerce-paypal-commerce-gateway'),
@ -221,6 +229,7 @@ return [
'screens' => [ 'screens' => [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'button_single_product_enabled' => [ 'button_single_product_enabled' => [
'title' => __('Buttons on Single Product', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Buttons on Single Product', 'woocommerce-paypal-commerce-gateway'),
@ -231,6 +240,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'button_mini_cart_enabled' => [ 'button_mini_cart_enabled' => [
'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-commerce-gateway'),
@ -241,6 +251,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'button_cart_enabled' => [ 'button_cart_enabled' => [
'title' => __('Buttons on Cart', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Buttons on Cart', 'woocommerce-paypal-commerce-gateway'),
@ -251,6 +262,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'button_label' => [ 'button_label' => [
'title' => __('Button Label', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Button Label', 'woocommerce-paypal-commerce-gateway'),
@ -272,6 +284,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'brand_name' => [ 'brand_name' => [
'title' => __('Brand Name', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Brand Name', 'woocommerce-paypal-commerce-gateway'),
@ -286,6 +299,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'landing_page' => [ 'landing_page' => [
'title' => __('Landing Page', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Landing Page', 'woocommerce-paypal-commerce-gateway'),
@ -305,6 +319,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'button_color' => [ 'button_color' => [
'title' => __('Color', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Color', 'woocommerce-paypal-commerce-gateway'),
@ -326,6 +341,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'button_shape' => [ 'button_shape' => [
'title' => __('Shape', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Shape', 'woocommerce-paypal-commerce-gateway'),
@ -345,6 +361,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'disable_funding' => [ 'disable_funding' => [
'title' => __('Disable funding sources', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Disable funding sources', 'woocommerce-paypal-commerce-gateway'),
@ -373,6 +390,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
'dcc_cart_enabled' => [ 'dcc_cart_enabled' => [
'title' => __('Enable credit card on cart', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Enable credit card on cart', 'woocommerce-paypal-commerce-gateway'),
@ -382,6 +400,9 @@ return [
'screens' => [ 'screens' => [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [
'dcc',
],
], ],
'dcc_mini_cart_enabled' => [ 'dcc_mini_cart_enabled' => [
'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-commerce-gateway'),
@ -391,6 +412,9 @@ return [
'screens' => [ 'screens' => [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [
'dcc',
],
], ],
'dcc_checkout_enabled' => [ 'dcc_checkout_enabled' => [
'title' => __('Enable credit card on checkout', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Enable credit card on checkout', 'woocommerce-paypal-commerce-gateway'),
@ -400,6 +424,9 @@ return [
'screens' => [ 'screens' => [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [
'dcc',
],
], ],
'dcc_single_product_enabled' => [ 'dcc_single_product_enabled' => [
'title' => __('Enable credit card on products', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Enable credit card on products', 'woocommerce-paypal-commerce-gateway'),
@ -409,6 +436,9 @@ return [
'screens' => [ 'screens' => [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [
'dcc',
],
], ],
'disable_cards' => [ 'disable_cards' => [
'title' => __('Disable specific credid cards', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Disable specific credid cards', 'woocommerce-paypal-commerce-gateway'),
@ -432,6 +462,9 @@ return [
'screens' => [ 'screens' => [
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [
'dcc',
],
], ],
'logging_enabled' => [ 'logging_enabled' => [
'title' => __('Logging', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Logging', 'woocommerce-paypal-commerce-gateway'),
@ -445,6 +478,7 @@ return [
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
], ],
'requirements' => [],
], ],
]; ];
}, },

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings; namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
use Inpsyde\PayPalCommerce\Onboarding\State; use Inpsyde\PayPalCommerce\Onboarding\State;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
@ -13,15 +14,18 @@ class SettingsRenderer
private $settings; private $settings;
private $state; private $state;
private $fields; private $fields;
private $dccApplies;
public function __construct( public function __construct(
ContainerInterface $settings, ContainerInterface $settings,
State $state, State $state,
array $fields array $fields,
DccApplies $dccApplies
) { ) {
$this->settings = $settings; $this->settings = $settings;
$this->state = $state; $this->state = $state;
$this->fields = $fields; $this->fields = $fields;
$this->dccApplies = $dccApplies;
} }
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType //phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
@ -67,6 +71,9 @@ class SettingsRenderer
if (! in_array($this->state->currentState(), $config['screens'], true)) { if (! in_array($this->state->currentState(), $config['screens'], true)) {
continue; continue;
} }
if (in_array('dcc', $config['requirements'], true) && ! $this->dccApplies->forCountryCurrency()) {
continue;
}
$value = $this->settings->has($field) ? $this->settings->get($field) : null; $value = $this->settings->has($field) ? $this->settings->get($field) : null;
$id = 'ppcp[' . $field . ']'; $id = 'ppcp[' . $field . ']';
?> ?>