Config.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Model;
  7. use Magento\Framework\Locale\Bundle\DataBundle;
  8. use Magento\Payment\Model\Method\AbstractMethod;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Payment configuration model
  12. *
  13. * Used for retrieving configuration data by payment models
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Config
  19. {
  20. /**
  21. * Years range
  22. */
  23. const YEARS_RANGE = 10;
  24. /**
  25. * @var array
  26. */
  27. protected $_methods;
  28. /**
  29. * Core store config
  30. *
  31. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  32. */
  33. protected $_scopeConfig;
  34. /**
  35. * @var \Magento\Framework\Config\DataInterface
  36. */
  37. protected $_dataStorage;
  38. /**
  39. * Locale model
  40. *
  41. * @var \Magento\Framework\Locale\ResolverInterface
  42. */
  43. protected $localeResolver;
  44. /**
  45. * Payment method factory
  46. *
  47. * @var \Magento\Payment\Model\Method\Factory
  48. */
  49. protected $_paymentMethodFactory;
  50. /**
  51. * DateTime
  52. *
  53. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  54. */
  55. protected $_date;
  56. /**
  57. * Construct
  58. *
  59. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  60. * @param \Magento\Payment\Model\Method\Factory $paymentMethodFactory
  61. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  62. * @param \Magento\Framework\Config\DataInterface $dataStorage
  63. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  64. */
  65. public function __construct(
  66. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  67. \Magento\Payment\Model\Method\Factory $paymentMethodFactory,
  68. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  69. \Magento\Framework\Config\DataInterface $dataStorage,
  70. \Magento\Framework\Stdlib\DateTime\DateTime $date
  71. ) {
  72. $this->_scopeConfig = $scopeConfig;
  73. $this->_dataStorage = $dataStorage;
  74. $this->_paymentMethodFactory = $paymentMethodFactory;
  75. $this->localeResolver = $localeResolver;
  76. $this->_date = $date;
  77. }
  78. /**
  79. * Retrieve active system payments
  80. *
  81. * @return array
  82. * @api
  83. */
  84. public function getActiveMethods()
  85. {
  86. $methods = [];
  87. foreach ($this->_scopeConfig->getValue('payment', ScopeInterface::SCOPE_STORE, null) as $code => $data) {
  88. if (isset($data['active'], $data['model']) && (bool)$data['active']) {
  89. /** @var MethodInterface $methodModel Actually it's wrong interface */
  90. $methodModel = $this->_paymentMethodFactory->create($data['model']);
  91. $methodModel->setStore(null);
  92. if ($methodModel->getConfigData('active', null)) {
  93. $methods[$code] = $methodModel;
  94. }
  95. }
  96. }
  97. return $methods;
  98. }
  99. /**
  100. * Get list of credit card types
  101. *
  102. * @return array
  103. * @api
  104. */
  105. public function getCcTypes()
  106. {
  107. return $this->_dataStorage->get('credit_cards');
  108. }
  109. /**
  110. * Retrieve array of payment methods information
  111. *
  112. * @return array
  113. * @api
  114. */
  115. public function getMethodsInfo()
  116. {
  117. return $this->_dataStorage->get('methods');
  118. }
  119. /**
  120. * Get payment groups
  121. *
  122. * @return array
  123. * @api
  124. */
  125. public function getGroups()
  126. {
  127. return $this->_dataStorage->get('groups');
  128. }
  129. /**
  130. * Retrieve list of months translation
  131. *
  132. * @return array
  133. * @api
  134. */
  135. public function getMonths()
  136. {
  137. $data = [];
  138. $months = (new DataBundle())->get(
  139. $this->localeResolver->getLocale()
  140. )['calendar']['gregorian']['monthNames']['format']['wide'];
  141. foreach ($months as $key => $value) {
  142. $monthNum = ++$key < 10 ? '0' . $key : $key;
  143. $data[$key] = $monthNum . ' - ' . $value;
  144. }
  145. return $data;
  146. }
  147. /**
  148. * Retrieve array of available years
  149. *
  150. * @return array
  151. * @api
  152. */
  153. public function getYears()
  154. {
  155. $years = [];
  156. $first = (int)$this->_date->date('Y');
  157. for ($index = 0; $index <= self::YEARS_RANGE; $index++) {
  158. $year = $first + $index;
  159. $years[$year] = $year;
  160. }
  161. return $years;
  162. }
  163. }