| Current Path : /var/www/clients/client3/web2/web/vendor/magento/module-paypal/Model/ |
| Current File : /var/www/clients/client3/web2/web/vendor/magento/module-paypal/Model/SmartButtonConfig.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Paypal\Model;
use Magento\Checkout\Helper\Data;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
/**
* Provides configuration values for PayPal in-context checkout
*/
class SmartButtonConfig
{
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
private $localeResolver;
/**
* @var Config
*/
private $config;
/**
* @var array
*/
private $defaultStyles;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var SdkUrl
*/
private $sdkUrl;
/**
* @param ResolverInterface $localeResolver
* @param ConfigFactory $configFactory
* @param ScopeConfigInterface $scopeConfig
* @param SdkUrl $sdkUrl
* @param array $defaultStyles
*/
public function __construct(
ResolverInterface $localeResolver,
ConfigFactory $configFactory,
ScopeConfigInterface $scopeConfig,
SdkUrl $sdkUrl,
$defaultStyles = []
) {
$this->localeResolver = $localeResolver;
$this->config = $configFactory->create();
$this->config->setMethod(Config::METHOD_EXPRESS);
$this->scopeConfig = $scopeConfig;
$this->defaultStyles = $defaultStyles;
$this->sdkUrl = $sdkUrl;
}
/**
* Get smart button config
*
* @param string $page
* @return array
*/
public function getConfig(string $page): array
{
$isGuestCheckoutAllowed = $this->scopeConfig->isSetFlag(
Data::XML_PATH_GUEST_CHECKOUT,
ScopeInterface::SCOPE_STORE
);
return [
'styles' => $this->getButtonStyles($page),
'isVisibleOnProductPage' => (bool)$this->config->getValue('visible_on_product'),
'isGuestCheckoutAllowed' => $isGuestCheckoutAllowed,
'sdkUrl' => $this->sdkUrl->getUrl()
];
}
/**
* Returns button styles based on configuration
*
* @param string $page
* @return array
*/
private function getButtonStyles(string $page): array
{
$styles = $this->defaultStyles[$page];
if ((boolean)$this->config->getValue("{$page}_page_button_customize")) {
$styles['layout'] = $this->config->getValue("{$page}_page_button_layout");
$styles['size'] = $this->config->getValue("{$page}_page_button_size");
$styles['color'] = $this->config->getValue("{$page}_page_button_color");
$styles['shape'] = $this->config->getValue("{$page}_page_button_shape");
$styles['label'] = $this->config->getValue("{$page}_page_button_label");
$styles = $this->updateStyles($styles, $page);
}
return $styles;
}
/**
* Update styles based on locale and labels
*
* @param array $styles
* @param string $page
* @return array
*/
private function updateStyles(array $styles, string $page): array
{
$locale = $this->localeResolver->getLocale();
$installmentPeriodLocale = [
'en_MX' => 'mx',
'es_MX' => 'mx',
'en_BR' => 'br',
'pt_BR' => 'br'
];
// Credit label cannot be used with any custom color option or vertical layout.
if ($styles['label'] === 'credit') {
$styles['color'] = 'darkblue';
$styles['layout'] = 'horizontal';
}
// Installment label is only available for specific locales
if ($styles['label'] === 'installment') {
if (array_key_exists($locale, $installmentPeriodLocale)) {
$styles['period'] = (int)$this->config->getValue(
$page .'_page_button_' . $installmentPeriodLocale[$locale] . '_installment_period'
);
} else {
$styles['label'] = 'paypal';
}
}
return $styles;
}
}