SmartButtonConfig.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Paypal\Model;
  8. use Magento\Framework\Locale\ResolverInterface;
  9. /**
  10. * Smart button config
  11. */
  12. class SmartButtonConfig
  13. {
  14. /**
  15. * @var \Magento\Framework\Locale\ResolverInterface
  16. */
  17. private $localeResolver;
  18. /**
  19. * @var ConfigFactory
  20. */
  21. private $config;
  22. /**
  23. * @var array
  24. */
  25. private $defaultStyles;
  26. /**
  27. * @var array
  28. */
  29. private $allowedFunding;
  30. /**
  31. * @param ResolverInterface $localeResolver
  32. * @param ConfigFactory $configFactory
  33. * @param array $defaultStyles
  34. * @param array $allowedFunding
  35. */
  36. public function __construct(
  37. ResolverInterface $localeResolver,
  38. ConfigFactory $configFactory,
  39. $defaultStyles = [],
  40. $allowedFunding = []
  41. ) {
  42. $this->localeResolver = $localeResolver;
  43. $this->config = $configFactory->create();
  44. $this->config->setMethod(Config::METHOD_EXPRESS);
  45. $this->defaultStyles = $defaultStyles;
  46. $this->allowedFunding = $allowedFunding;
  47. }
  48. /**
  49. * Get smart button config
  50. *
  51. * @param string $page
  52. * @return array
  53. */
  54. public function getConfig(string $page): array
  55. {
  56. return [
  57. 'merchantId' => $this->config->getValue('merchant_id'),
  58. 'environment' => ((int)$this->config->getValue('sandbox_flag') ? 'sandbox' : 'production'),
  59. 'locale' => $this->localeResolver->getLocale(),
  60. 'allowedFunding' => $this->getAllowedFunding($page),
  61. 'disallowedFunding' => $this->getDisallowedFunding(),
  62. 'styles' => $this->getButtonStyles($page)
  63. ];
  64. }
  65. /**
  66. * Returns disallowed funding from configuration
  67. *
  68. * @return array
  69. */
  70. private function getDisallowedFunding(): array
  71. {
  72. $disallowedFunding = $this->config->getValue('disable_funding_options');
  73. return $disallowedFunding ? explode(',', $disallowedFunding) : [];
  74. }
  75. /**
  76. * Returns allowed funding
  77. *
  78. * @param string $page
  79. * @return array
  80. */
  81. private function getAllowedFunding(string $page): array
  82. {
  83. return array_values(array_diff($this->allowedFunding[$page], $this->getDisallowedFunding()));
  84. }
  85. /**
  86. * Returns button styles based on configuration
  87. *
  88. * @param string $page
  89. * @return array
  90. */
  91. private function getButtonStyles(string $page): array
  92. {
  93. $styles = $this->defaultStyles[$page];
  94. if ((boolean)$this->config->getValue("{$page}_page_button_customize")) {
  95. $styles['layout'] = $this->config->getValue("{$page}_page_button_layout");
  96. $styles['size'] = $this->config->getValue("{$page}_page_button_size");
  97. $styles['color'] = $this->config->getValue("{$page}_page_button_color");
  98. $styles['shape'] = $this->config->getValue("{$page}_page_button_shape");
  99. $styles['label'] = $this->config->getValue("{$page}_page_button_label");
  100. $styles = $this->updateStyles($styles, $page);
  101. }
  102. return $styles;
  103. }
  104. /**
  105. * Update styles based on locale and labels
  106. *
  107. * @param array $styles
  108. * @param string $page
  109. * @return array
  110. */
  111. private function updateStyles(array $styles, string $page): array
  112. {
  113. $locale = $this->localeResolver->getLocale();
  114. $installmentPeriodLocale = [
  115. 'en_MX' => 'mx',
  116. 'es_MX' => 'mx',
  117. 'en_BR' => 'br',
  118. 'pt_BR' => 'br'
  119. ];
  120. // Credit label cannot be used with any custom color option or vertical layout.
  121. if ($styles['label'] === 'credit') {
  122. $styles['color'] = 'darkblue';
  123. $styles['layout'] = 'horizontal';
  124. }
  125. // Installment label is only available for specific locales
  126. if ($styles['label'] === 'installment') {
  127. if (array_key_exists($locale, $installmentPeriodLocale)) {
  128. $styles['installmentperiod'] = (int)$this->config->getValue(
  129. $page .'_page_button_' . $installmentPeriodLocale[$locale] . '_installment_period'
  130. );
  131. } else {
  132. $styles['label'] = 'paypal';
  133. }
  134. }
  135. return $styles;
  136. }
  137. }