CcGenericConfigProvider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Payment\Helper\Data as PaymentHelper;
  9. /**
  10. * Default implementation of credits card configuration provider.
  11. * Use this class to register payment method that supports credit cards.
  12. * Direct injection as a dependency or extending of this class is not recommended.
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class CcGenericConfigProvider implements ConfigProviderInterface
  18. {
  19. /**
  20. * @var CcConfig
  21. */
  22. protected $ccConfig;
  23. /**
  24. * @var MethodInterface[]
  25. */
  26. protected $methods = [];
  27. /**
  28. * @param CcConfig $ccConfig
  29. * @param PaymentHelper $paymentHelper
  30. * @param array $methodCodes
  31. */
  32. public function __construct(
  33. CcConfig $ccConfig,
  34. PaymentHelper $paymentHelper,
  35. array $methodCodes = []
  36. ) {
  37. $this->ccConfig = $ccConfig;
  38. foreach ($methodCodes as $code) {
  39. $this->methods[$code] = $paymentHelper->getMethodInstance($code);
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getConfig()
  46. {
  47. $config = [];
  48. foreach ($this->methods as $methodCode => $method) {
  49. if ($method->isAvailable()) {
  50. $config = array_merge_recursive($config, [
  51. 'payment' => [
  52. 'ccform' => [
  53. 'availableTypes' => [$methodCode => $this->getCcAvailableTypes($methodCode)],
  54. 'months' => [$methodCode => $this->getCcMonths()],
  55. 'years' => [$methodCode => $this->getCcYears()],
  56. 'hasVerification' => [$methodCode => $this->hasVerification($methodCode)],
  57. 'cvvImageUrl' => [$methodCode => $this->getCvvImageUrl()]
  58. ]
  59. ]
  60. ]);
  61. }
  62. }
  63. return $config;
  64. }
  65. /**
  66. * Solo/switch card start years
  67. *
  68. * @return array
  69. * @deprecated 100.1.0 unused
  70. */
  71. protected function getSsStartYears()
  72. {
  73. return $this->ccConfig->getSsStartYears();
  74. }
  75. /**
  76. * Retrieve credit card expire months
  77. *
  78. * @return array
  79. */
  80. protected function getCcMonths()
  81. {
  82. return $this->ccConfig->getCcMonths();
  83. }
  84. /**
  85. * Retrieve credit card expire years
  86. *
  87. * @return array
  88. */
  89. protected function getCcYears()
  90. {
  91. return $this->ccConfig->getCcYears();
  92. }
  93. /**
  94. * Retrieve CVV tooltip image url
  95. *
  96. * @return string
  97. */
  98. protected function getCvvImageUrl()
  99. {
  100. return $this->ccConfig->getCvvImageUrl();
  101. }
  102. /**
  103. * Retrieve available credit card types
  104. *
  105. * @param string $methodCode
  106. * @return array
  107. */
  108. protected function getCcAvailableTypes($methodCode)
  109. {
  110. $types = $this->ccConfig->getCcAvailableTypes();
  111. $availableTypes = $this->methods[$methodCode]->getConfigData('cctypes');
  112. if ($availableTypes) {
  113. $availableTypes = explode(',', $availableTypes);
  114. foreach (array_keys($types) as $code) {
  115. if (!in_array($code, $availableTypes)) {
  116. unset($types[$code]);
  117. }
  118. }
  119. }
  120. return $types;
  121. }
  122. /**
  123. * Retrieve has verification configuration
  124. *
  125. * @param string $methodCode
  126. * @return bool
  127. */
  128. protected function hasVerification($methodCode)
  129. {
  130. $result = $this->ccConfig->hasVerification();
  131. $configData = $this->methods[$methodCode]->getConfigData('useccv');
  132. if ($configData !== null) {
  133. $result = (bool)$configData;
  134. }
  135. return $result;
  136. }
  137. /**
  138. * Whether switch/solo card type available
  139. *
  140. * @param string $methodCode
  141. * @return bool
  142. * @deprecated 100.1.0 unused
  143. */
  144. protected function hasSsCardType($methodCode)
  145. {
  146. $result = false;
  147. $availableTypes = explode(',', $this->methods[$methodCode]->getConfigData('cctypes'));
  148. $ssPresentations = array_intersect(['SS', 'SM', 'SO'], $availableTypes);
  149. if ($availableTypes && count($ssPresentations) > 0) {
  150. $result = true;
  151. }
  152. return $result;
  153. }
  154. }