woocommerce-paypal-payments/modules.local/ppcp-button/src/Assets/SmartButton.php

655 lines
23 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
2020-04-28 12:31:12 +03:00
2020-04-02 08:38:00 +03:00
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Assets;
2020-04-30 15:28:48 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
2020-04-13 09:07:20 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
2020-07-10 12:33:13 +03:00
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
2020-08-19 09:35:48 +03:00
use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
2020-07-28 12:27:42 +03:00
use Inpsyde\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
2020-04-02 08:38:00 +03:00
2020-08-21 10:38:48 +03:00
//phpcs:disable Inpsyde.CodeQuality.PropertyPerClassLimit.TooManyProperties
class SmartButton implements SmartButtonInterface
2020-04-02 08:38:00 +03:00
{
private $moduleUrl;
private $sessionHandler;
private $settings;
2020-04-13 09:07:20 +03:00
private $payeeRepository;
2020-04-30 15:28:48 +03:00
private $identityToken;
private $payerFactory;
private $clientId;
2020-07-10 12:33:13 +03:00
private $requestData;
private $dccApplies;
2020-07-28 12:27:42 +03:00
private $subscriptionHelper;
2020-08-19 09:35:48 +03:00
private $messagesApply;
2020-04-02 08:38:00 +03:00
public function __construct(
string $moduleUrl,
SessionHandler $sessionHandler,
2020-04-13 09:07:20 +03:00
Settings $settings,
2020-04-30 15:28:48 +03:00
PayeeRepository $payeeRepository,
IdentityToken $identityToken,
PayerFactory $payerFactory,
2020-07-10 12:33:13 +03:00
string $clientId,
RequestData $requestData,
2020-07-28 12:27:42 +03:00
DccApplies $dccApplies,
2020-08-19 09:35:48 +03:00
SubscriptionHelper $subscriptionHelper,
MessagesApply $messagesApply
2020-04-02 08:38:00 +03:00
) {
2020-04-06 11:16:18 +03:00
2020-04-02 08:38:00 +03:00
$this->moduleUrl = $moduleUrl;
$this->sessionHandler = $sessionHandler;
$this->settings = $settings;
2020-04-13 09:07:20 +03:00
$this->payeeRepository = $payeeRepository;
2020-04-30 15:28:48 +03:00
$this->identityToken = $identityToken;
$this->payerFactory = $payerFactory;
$this->clientId = $clientId;
2020-07-10 12:33:13 +03:00
$this->requestData = $requestData;
$this->dccApplies = $dccApplies;
2020-07-28 12:27:42 +03:00
$this->subscriptionHelper = $subscriptionHelper;
2020-08-19 09:35:48 +03:00
$this->messagesApply = $messagesApply;
2020-04-02 08:38:00 +03:00
}
/**
* @return bool
*/
public function renderWrapper(): bool
2020-04-02 08:38:00 +03:00
{
2020-04-30 15:28:48 +03:00
if (! $this->canSaveVaultToken() && $this->hasSubscription()) {
return false;
}
if ($this->settings->has('enabled') && $this->settings->get('enabled')) {
$this->renderButtonWrapperRegistrar();
$this->renderMessageWrapperRegistrar();
}
if (
$this->settings->has('dcc_gateway_enabled')
&& $this->settings->get('dcc_gateway_enabled')
&& ! $this->sessionHandler->order()
) {
add_action(
'woocommerce_review_order_after_submit',
[
$this,
'dccRenderer',
],
11
);
}
return true;
}
2020-08-21 09:41:12 +03:00
private function renderMessageWrapperRegistrar(): bool
{
$notEnabledOnCart = $this->settings->has('message_cart_enabled') &&
!$this->settings->get('message_cart_enabled');
if (
is_cart()
&& !$notEnabledOnCart
) {
add_action(
'woocommerce_proceed_to_checkout',
[
$this,
'messageRenderer',
],
19
);
}
$notEnabledOnProductPage = $this->settings->has('message_product_enabled') &&
!$this->settings->get('message_product_enabled');
if (
(is_product() || wc_post_content_has_shortcode('product_page'))
&& !$notEnabledOnProductPage
) {
add_action(
'woocommerce_single_product_summary',
[
$this,
'messageRenderer',
],
30
);
}
$notEnabledOnCheckout = $this->settings->has('message_enabled') &&
!$this->settings->get('message_enabled');
if (! $notEnabledOnCheckout) {
add_action(
'woocommerce_review_order_after_submit',
[
$this,
'messageRenderer',
],
11
);
}
return true;
}
2020-08-21 09:41:12 +03:00
private function renderButtonWrapperRegistrar(): bool
{
$notEnabledOnCart = $this->settings->has('button_cart_enabled') &&
!$this->settings->get('button_cart_enabled');
2020-06-15 11:48:37 +03:00
if (
is_cart()
&& !$notEnabledOnCart
2020-06-15 11:48:37 +03:00
) {
2020-04-08 12:33:34 +03:00
add_action(
'woocommerce_proceed_to_checkout',
2020-08-18 15:55:54 +03:00
[
$this,
'buttonRenderer',
],
20
);
}
$notEnabledOnProductPage = $this->settings->has('button_single_product_enabled') &&
!$this->settings->get('button_single_product_enabled');
2020-06-15 11:48:37 +03:00
if (
(is_product() || wc_post_content_has_shortcode('product_page'))
&& !$notEnabledOnProductPage
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_single_product_summary',
2020-08-18 15:55:54 +03:00
[
$this,
'buttonRenderer',
],
31
);
}
2020-08-18 10:12:07 +03:00
$notEnabledOnMiniCart = $this->settings->has('button_mini_cart_enabled') &&
!$this->settings->get('button_mini_cart_enabled');
2020-06-15 11:48:37 +03:00
if (
2020-08-21 09:41:12 +03:00
! $notEnabledOnMiniCart
2020-06-15 11:48:37 +03:00
) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
2020-04-28 12:31:12 +03:00
static function () {
2020-08-21 10:38:48 +03:00
echo '<p
id="ppc-button-minicart"
class="woocommerce-mini-cart__buttons buttons"
></p>';
},
30
);
}
2020-08-21 09:41:12 +03:00
add_action('woocommerce_review_order_after_submit', [$this, 'buttonRenderer'], 10);
2020-04-08 12:33:34 +03:00
return true;
2020-04-02 08:38:00 +03:00
}
2020-04-09 18:15:57 +03:00
public function enqueue(): bool
2020-04-02 08:38:00 +03:00
{
$buttonsEnabled = $this->settings->has('enabled') && $this->settings->get('enabled');
if (! is_checkout() && !$buttonsEnabled) {
return false;
}
if (! $this->canSaveVaultToken() && $this->hasSubscription()) {
return false;
}
wp_enqueue_style(
2020-07-17 11:47:00 +03:00
'ppcp-hosted-fields',
2020-07-21 09:14:13 +03:00
$this->moduleUrl . '/assets/css/hosted-fields.css',
[],
1
2020-07-17 11:47:00 +03:00
);
2020-04-02 08:38:00 +03:00
wp_enqueue_script(
2020-06-15 11:48:37 +03:00
'ppcp-smart-button',
2020-04-28 12:31:12 +03:00
$this->moduleUrl . '/assets/js/button.js',
['jquery'],
1,
true
2020-04-02 08:38:00 +03:00
);
2020-04-13 09:07:20 +03:00
wp_localize_script(
2020-06-15 11:48:37 +03:00
'ppcp-smart-button',
2020-04-13 09:07:20 +03:00
'PayPalCommerceGateway',
$this->localizeScript()
);
return true;
}
2020-04-02 08:38:00 +03:00
2020-08-19 05:39:45 +03:00
public function buttonRenderer()
{
2020-08-18 15:55:54 +03:00
$product = wc_get_product();
if (
! is_checkout() && is_a($product, \WC_Product::class)
&& (
$product->is_type(['external', 'grouped'])
|| ! $product->is_in_stock()
)
) {
return;
}
echo '<div id="ppc-button"></div>';
}
2020-08-21 09:41:12 +03:00
public function messageRenderer()
{
echo '<div id="ppcp-messages"></div>';
2020-08-19 05:19:29 +03:00
}
2020-08-21 09:41:12 +03:00
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
private function messageValues(): array
{
2020-08-18 15:55:54 +03:00
2020-08-19 10:27:53 +03:00
if (
$this->settings->has('disable_funding')
&& in_array('credit', (array) $this->settings->get('disable_funding'), true)
) {
return [];
}
2020-08-18 15:55:54 +03:00
$placement = 'product';
2020-08-19 05:19:29 +03:00
$product = wc_get_product();
2020-08-18 15:55:54 +03:00
$amount = (is_a($product, \WC_Product::class)) ? wc_get_price_including_tax($product) : 0;
2020-08-19 05:39:45 +03:00
$layout = $this->settings->has('message_product_layout') ?
$this->settings->get('message_product_layout') : 'text';
$logoType = $this->settings->has('message_product_logo') ?
$this->settings->get('message_product_logo') : 'primary';
$logoPosition = $this->settings->has('message_product_position') ?
$this->settings->get('message_product_position') : 'left';
$textColor = $this->settings->has('message_product_color') ?
$this->settings->get('message_product_color') : 'black';
$styleColor = $this->settings->has('message_product_flex_color') ?
$this->settings->get('message_product_flex_color') : 'blue';
$ratio = $this->settings->has('message_product_flex_ratio') ?
$this->settings->get('message_product_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_product_enabled')
&& $this->settings->get('message_product_enabled');
2020-08-18 15:55:54 +03:00
if (is_checkout()) {
$placement = 'payment';
$amount = WC()->cart->get_total('raw');
2020-08-19 05:39:45 +03:00
$layout = $this->settings->has('message_layout') ?
$this->settings->get('message_layout') : 'text';
$logoType = $this->settings->has('message_logo') ?
$this->settings->get('message_logo') : 'primary';
$logoPosition = $this->settings->has('message_position') ?
$this->settings->get('message_position') : 'left';
$textColor = $this->settings->has('message_color') ?
$this->settings->get('message_color') : 'black';
$styleColor = $this->settings->has('message_flex_color') ?
$this->settings->get('message_flex_color') : 'blue';
$ratio = $this->settings->has('message_flex_ratio') ?
$this->settings->get('message_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_enabled')
&& $this->settings->get('message_enabled');
2020-08-18 15:55:54 +03:00
}
if (is_cart()) {
$placement = 'cart';
$amount = WC()->cart->get_total('raw');
2020-08-19 05:39:45 +03:00
$layout = $this->settings->has('message_cart_layout') ?
$this->settings->get('message_cart_layout') : 'text';
$logoType = $this->settings->has('message_cart_logo') ?
$this->settings->get('message_cart_logo') : 'primary';
$logoPosition = $this->settings->has('message_cart_position') ?
$this->settings->get('message_cart_position') : 'left';
$textColor = $this->settings->has('message_cart_color') ?
$this->settings->get('message_cart_color') : 'black';
$styleColor = $this->settings->has('message_cart_flex_color') ?
$this->settings->get('message_cart_flex_color') : 'blue';
$ratio = $this->settings->has('message_cart_flex_ratio') ?
$this->settings->get('message_cart_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_cart_enabled')
&& $this->settings->get('message_cart_enabled');
2020-08-18 15:55:54 +03:00
}
2020-08-19 05:19:29 +03:00
if (! $shouldShow) {
2020-08-19 10:27:53 +03:00
return [];
2020-08-19 05:19:29 +03:00
}
2020-08-19 10:27:53 +03:00
$values = [
'wrapper' => '#ppcp-messages',
'amount' => $amount,
'placement' => $placement,
'style' => [
'layout' => $layout,
'logo' => [
'type' => $logoType,
'position' => $logoPosition,
],
'text' => [
'color' => $textColor,
],
'color' => $styleColor,
'ratio' => $ratio,
],
2020-08-19 05:19:29 +03:00
];
2020-08-19 10:27:53 +03:00
return $values;
2020-08-18 15:55:54 +03:00
}
2020-08-21 09:41:12 +03:00
//phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
2020-08-19 05:19:29 +03:00
public function dccRenderer()
{
$id = 'ppcp-hosted-fields';
$canRenderDcc = $this->dccApplies->forCountryCurrency()
&& $this->settings->has('client_id')
&& $this->settings->get('client_id');
if (! $canRenderDcc) {
return;
}
$saveCard = $this->canSaveVaultToken() ? sprintf(
2020-07-23 14:29:20 +03:00
'<div>
<label for="ppcp-vault-%1$s">%2$s</label>
<input
type="checkbox"
id="ppcp-vault-%1$s"
class="ppcp-credit-card-vault"
name="vault"
>
</div>',
esc_attr($id),
esc_html__('Save your card', 'woocommerce-paypal-commerce-gateway')
) : '';
printf(
'<form id="%1$s">
<div class="ppcp-dcc-credit-card-wrapper" style="display: none">
2020-08-18 12:08:45 +03:00
<label for="ppcp-credit-card-%1$s">%2$s</label>
<span id="ppcp-credit-card-%1$s" class="ppcp-credit-card"></span>
<label for="ppcp-expiration-date-%1$s">%3$s</label>
2020-08-21 10:38:48 +03:00
<span
id="ppcp-expiration-date-%1$s"
class="ppcp-expiration-date"
></span>
2020-08-18 12:08:45 +03:00
<label for="ppcp-cvv-%1$s">%4$s</label>
<span id="ppcp-cvv-%1$s" class="ppcp-cvv"></span>
%5$s
<button class="button alt">%6$s</button>
</div>
</form><div id="payments-sdk__contingency-lightbox"></div>',
esc_attr($id),
2020-08-18 12:08:45 +03:00
esc_html__('Credit Card number', 'woocommerce-paypal-commerce-gateway'),
esc_html__('Expiration', 'woocommerce-paypal-commerce-gateway'),
esc_html__('CVV', 'woocommerce-paypal-commerce-gateway'),
//phpcs:ignore
$saveCard,
2020-08-18 11:38:59 +03:00
esc_html__('Place order', 'woocommerce')
);
}
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
public function canSaveVaultToken(): bool
{
if (! $this->settings->has('client_id') || ! $this->settings->get('client_id')) {
return false;
}
if (! $this->settings->has('vault_enabled') || ! $this->settings->get('vault_enabled')) {
return false;
}
return is_user_logged_in();
}
2020-07-28 12:27:42 +03:00
private function hasSubscription(): bool
{
2020-07-28 12:27:42 +03:00
if (! $this->subscriptionHelper->acceptOnlyAutomaticPaymentGateways()) {
return false;
}
2020-07-28 12:27:42 +03:00
if (is_product()) {
return $this->subscriptionHelper->currentProductIsSubscription();
}
2020-07-28 12:27:42 +03:00
return $this->subscriptionHelper->cartContainsSubscription();
}
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
2020-04-28 12:31:12 +03:00
private function localizeScript(): array
2020-04-13 09:07:20 +03:00
{
2020-07-10 12:33:13 +03:00
$this->requestData->enqueueNonceFix();
2020-04-02 08:38:00 +03:00
$localize = [
2020-04-30 15:28:48 +03:00
'script_attributes' => $this->attributes(),
'data_client_id' => [
2020-08-21 10:38:48 +03:00
'set_attribute' => (is_checkout() && $this->dccIsEnabled())
|| $this->canSaveVaultToken(),
'endpoint' => home_url(\WC_AJAX::get_endpoint(DataClientIdEndpoint::ENDPOINT)),
'nonce' => wp_create_nonce(DataClientIdEndpoint::nonce()),
'user' => get_current_user_id(),
],
2020-04-02 08:38:00 +03:00
'redirect' => wc_get_checkout_url(),
'context' => $this->context(),
'ajax' => [
'change_cart' => [
2020-04-06 11:16:18 +03:00
'endpoint' => home_url(\WC_AJAX::get_endpoint(ChangeCartEndpoint::ENDPOINT)),
2020-04-02 08:38:00 +03:00
'nonce' => wp_create_nonce(ChangeCartEndpoint::nonce()),
],
'create_order' => [
2020-04-06 11:16:18 +03:00
'endpoint' => home_url(\WC_AJAX::get_endpoint(CreateOrderEndpoint::ENDPOINT)),
2020-04-02 08:38:00 +03:00
'nonce' => wp_create_nonce(CreateOrderEndpoint::nonce()),
],
'approve_order' => [
'endpoint' => home_url(\WC_AJAX::get_endpoint(ApproveOrderEndpoint::ENDPOINT)),
'nonce' => wp_create_nonce(ApproveOrderEndpoint::nonce()),
],
2020-04-02 08:38:00 +03:00
],
'enforce_vault' => $this->hasSubscription(),
2020-07-15 19:49:32 +03:00
'bn_codes' => $this->bnCodes(),
'payer' => $this->payerData(),
2020-04-02 08:38:00 +03:00
'button' => [
'wrapper' => '#ppc-button',
2020-04-08 12:33:34 +03:00
'mini_cart_wrapper' => '#ppc-button-minicart',
'cancel_wrapper' => '#ppcp-cancel',
2020-04-13 09:07:20 +03:00
'url' => $this->url(),
2020-08-14 10:28:24 +03:00
'mini_cart_style' => [
'layout' => $this->styleForContext('layout', 'mini-cart'),
'color' => $this->styleForContext('color', 'mini-cart'),
'shape' => $this->styleForContext('shape', 'mini-cart'),
'label' => $this->styleForContext('label', 'mini-cart'),
2020-08-18 14:32:19 +03:00
'tagline' => $this->styleForContext('tagline', 'mini-cart'),
2020-08-14 10:28:24 +03:00
],
'style' => [
2020-08-14 10:28:24 +03:00
'layout' => $this->styleForContext('layout', $this->context()),
'color' => $this->styleForContext('color', $this->context()),
'shape' => $this->styleForContext('shape', $this->context()),
'label' => $this->styleForContext('label', $this->context()),
2020-08-18 14:32:19 +03:00
'tagline' => $this->styleForContext('tagline', $this->context()),
],
2020-04-06 11:16:18 +03:00
],
2020-04-30 15:28:48 +03:00
'hosted_fields' => [
'wrapper' => '#ppcp-hosted-fields',
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
2020-04-30 15:28:48 +03:00
'labels' => [
2020-08-18 12:16:10 +03:00
'credit_card_number' => '',
'cvv' => '',
2020-04-30 15:28:48 +03:00
'mm_yyyy' => __('MM/YYYY', 'woocommerce-paypal-commerce-gateway'),
2020-07-22 14:12:49 +03:00
'fields_not_valid' => __(
'Unfortunatly, your credit card details are not valid.',
'woocommerce-paypal-commerce-gateway'
),
2020-04-30 15:28:48 +03:00
],
],
2020-08-19 10:27:53 +03:00
'messages' => $this->messageValues(),
2020-07-28 08:05:18 +03:00
'labels' => [
'error' => [
'generic' => __(
'Something went wrong. Please try again or choose another payment source',
'woocommerce-paypal-commerce-gateway'
),
],
],
2020-04-02 08:38:00 +03:00
];
2020-07-10 12:33:13 +03:00
2020-08-18 14:32:19 +03:00
if ($this->styleForContext('layout', 'mini-cart') !== 'horizontal') {
unset($localize['button']['mini_cart_style']['tagline']);
}
if ($this->styleForContext('layout', $this->context()) !== 'horizontal') {
unset($localize['button']['style']['tagline']);
}
2020-07-10 12:33:13 +03:00
$this->requestData->dequeueNonceFix();
2020-04-13 09:07:20 +03:00
return $localize;
}
//phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
2020-04-13 09:07:20 +03:00
2020-04-28 12:31:12 +03:00
private function payerData(): ?array
{
$customer = WC()->customer;
if (! is_user_logged_in() || ! is_a($customer, \WC_Customer::class)) {
return null;
}
return $this->payerFactory->fromCustomer($customer)->toArray();
}
2020-04-28 12:31:12 +03:00
private function url(): string
2020-04-13 09:07:20 +03:00
{
$params = [
'client-id' => $this->clientId,
2020-04-13 09:07:20 +03:00
'currency' => get_woocommerce_currency(),
'locale' => get_user_locale(),
//'debug' => (defined('WP_DEBUG') && WP_DEBUG) ? 'true' : 'false',
//ToDo: Update date on releases.
'integration-date' => date('Y-m-d'),
2020-04-30 15:28:48 +03:00
'components' => implode(',', $this->components()),
2020-08-19 05:39:45 +03:00
'vault' => (is_checkout() && $this->dccIsEnabled()) || $this->canSaveVaultToken() ?
'true' : 'false',
2020-04-13 09:07:20 +03:00
'commit' => is_checkout() ? 'true' : 'false',
2020-08-21 10:38:48 +03:00
'intent' => ($this->settings->has('intent')) ?
$this->settings->get('intent') : 'capture',
2020-04-13 09:07:20 +03:00
];
if (defined('WP_DEBUG') && \WP_DEBUG && WC()->customer && WC()->customer->get_billing_country()) {
2020-04-30 15:28:48 +03:00
$params['buyer-country'] = WC()->customer->get_billing_country();
}
2020-04-13 09:07:20 +03:00
$payee = $this->payeeRepository->payee();
if ($payee->merchantId()) {
$params['merchant-id'] = $payee->merchantId();
}
2020-06-29 13:35:37 +03:00
$disableFunding = $this->settings->has('disable_funding') ?
$this->settings->get('disable_funding') : [];
2020-07-23 11:12:55 +03:00
$disableFunding[] = 'venmo';
if (! is_checkout()) {
$disableFunding[] = 'card';
}
2020-08-19 10:27:53 +03:00
$params['disable-funding'] = implode(',', array_unique($disableFunding));
2020-04-13 09:07:20 +03:00
$smartButtonUrl = add_query_arg($params, 'https://www.paypal.com/sdk/js');
return $smartButtonUrl;
2020-04-02 08:38:00 +03:00
}
2020-06-29 13:35:37 +03:00
private function attributes(): array
{
return [
2020-07-15 19:49:32 +03:00
'data-partner-attribution-id' => $this->bnCodeForContext($this->context()),
];
2020-04-30 15:28:48 +03:00
}
2020-07-15 19:49:32 +03:00
/**
* @param string $context
* @return string
*/
private function bnCodeForContext(string $context): string
{
$codes = $this->bnCodes();
return (isset($codes[$context])) ? $codes[$context] : '';
}
/**
* BN Codes
*
* @return array
*/
private function bnCodes(): array
{
return [
'checkout' => 'Woo_PPCP',
'cart' => 'Woo_PPCP',
'mini-cart' => 'Woo_PPCP',
'product' => 'Woo_PPCP',
2020-07-15 19:49:32 +03:00
];
}
2020-06-29 13:35:37 +03:00
private function components(): array
2020-04-30 15:28:48 +03:00
{
2020-08-19 09:35:48 +03:00
$components = ['buttons'];
if ($this->messagesApply->forCountry()) {
$components[] = 'messages';
}
2020-04-30 15:28:48 +03:00
if ($this->dccIsEnabled()) {
$components[] = 'hosted-fields';
}
return $components;
}
2020-04-09 18:15:57 +03:00
private function context(): string
2020-04-06 11:16:18 +03:00
{
2020-04-02 08:38:00 +03:00
$context = 'mini-cart';
if (is_product() || wc_post_content_has_shortcode('product_page')) {
2020-04-02 08:38:00 +03:00
$context = 'product';
}
if (is_cart()) {
$context = 'cart';
}
2020-04-09 18:15:57 +03:00
if (is_checkout() && !$this->sessionHandler->order()) {
2020-04-02 08:38:00 +03:00
$context = 'checkout';
}
return $context;
}
2020-04-30 15:28:48 +03:00
2020-06-29 13:35:37 +03:00
private function dccIsEnabled(): bool
2020-04-30 15:28:48 +03:00
{
if (! $this->dccApplies->forCountryCurrency()) {
return false;
}
2020-06-15 11:48:37 +03:00
$keys = [
'dcc_gateway_enabled' => 'is_checkout',
2020-06-15 11:48:37 +03:00
];
2020-07-23 14:53:37 +03:00
foreach ($keys as $key => $callback) {
if ($this->settings->has($key) && $this->settings->get($key) && $callback()) {
2020-06-15 11:48:37 +03:00
return true;
}
}
return false;
2020-04-30 15:28:48 +03:00
}
2020-08-14 10:28:24 +03:00
2020-08-14 10:41:53 +03:00
private function styleForContext(string $style, string $context): string
{
2020-08-14 10:28:24 +03:00
$defaults = [
'layout' => 'vertical',
'size' => 'responsive',
'color' => 'gold',
'shape' => 'pill',
'label' => 'paypal',
2020-08-18 14:32:19 +03:00
'tagline' => true,
2020-08-14 10:28:24 +03:00
];
2020-08-14 10:41:53 +03:00
$value = isset($defaults[$style]) ?
$defaults[$style] : '';
$value = $this->settings->has('button_' . $style) ?
$this->settings->get('button_' . $style) : $value;
$value = $this->settings->has('button_' . $context . '_' . $style) ?
$this->settings->get('button_' . $context . '_' . $style) : $value;
2020-08-18 14:32:19 +03:00
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
2020-08-14 10:28:24 +03:00
return $value;
}
2020-04-06 11:16:18 +03:00
}