1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Paypal\Model;
use Magento\Framework\Locale\ResolverInterface;
/**
* Smart button config
*/
class SmartButtonConfig
{
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
private $localeResolver;
/**
* @var ConfigFactory
*/
private $config;
/**
* @var array
*/
private $defaultStyles;
/**
* @var array
*/
private $allowedFunding;
/**
* @param ResolverInterface $localeResolver
* @param ConfigFactory $configFactory
* @param array $defaultStyles
* @param array $allowedFunding
*/
public function __construct(
ResolverInterface $localeResolver,
ConfigFactory $configFactory,
$defaultStyles = [],
$allowedFunding = []
) {
$this->localeResolver = $localeResolver;
$this->config = $configFactory->create();
$this->config->setMethod(Config::METHOD_EXPRESS);
$this->defaultStyles = $defaultStyles;
$this->allowedFunding = $allowedFunding;
}
/**
* Get smart button config
*
* @param string $page
* @return array
*/
public function getConfig(string $page): array
{
return [
'merchantId' => $this->config->getValue('merchant_id'),
'environment' => ((int)$this->config->getValue('sandbox_flag') ? 'sandbox' : 'production'),
'locale' => $this->localeResolver->getLocale(),
'allowedFunding' => $this->getAllowedFunding($page),
'disallowedFunding' => $this->getDisallowedFunding(),
'styles' => $this->getButtonStyles($page)
];
}
/**
* Returns disallowed funding from configuration
*
* @return array
*/
private function getDisallowedFunding(): array
{
$disallowedFunding = $this->config->getValue('disable_funding_options');
return $disallowedFunding ? explode(',', $disallowedFunding) : [];
}
/**
* Returns allowed funding
*
* @param string $page
* @return array
*/
private function getAllowedFunding(string $page): array
{
return array_values(array_diff($this->allowedFunding[$page], $this->getDisallowedFunding()));
}
/**
* 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['installmentperiod'] = (int)$this->config->getValue(
$page .'_page_button_' . $installmentPeriodLocale[$locale] . '_installment_period'
);
} else {
$styles['label'] = 'paypal';
}
}
return $styles;
}
}