IframeConfigProvider.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\Framework\App\RequestInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Framework\View\Asset\Repository;
  12. use Magento\Payment\Helper\Data as PaymentHelper;
  13. use Magento\Payment\Model\Method\TransparentInterface;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Class IframeConfigProvider
  17. * @package Magento\Payment\Model
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. *
  20. * Default implementation of config provider for iframe integrations.
  21. * Use this class for virtual types declaration.
  22. * Extends from this class only in case of urgency.
  23. *
  24. * @api
  25. * @since 100.0.2
  26. */
  27. class IframeConfigProvider implements ConfigProviderInterface
  28. {
  29. /**
  30. * 30 sec
  31. */
  32. const TIMEOUT_TIME = 30000;
  33. /**
  34. * Default length of Cc year field
  35. */
  36. const DEFAULT_YEAR_LENGTH = 2;
  37. /**
  38. * Checkout identifier for transparent iframe payments
  39. */
  40. const CHECKOUT_IDENTIFIER = 'checkout_flow';
  41. /**
  42. * @var Repository
  43. */
  44. protected $assetRepo;
  45. /**
  46. * @var RequestInterface
  47. */
  48. protected $request;
  49. /**
  50. * @var UrlInterface
  51. */
  52. protected $urlBuilder;
  53. /**
  54. * @var LoggerInterface
  55. */
  56. protected $logger;
  57. /**
  58. * Payment method code
  59. *
  60. * @var string
  61. */
  62. protected $methodCode;
  63. /**
  64. * @var \Magento\Payment\Model\Method\AbstractMethod
  65. */
  66. protected $method;
  67. /**
  68. * @param Repository $assetRepo
  69. * @param RequestInterface $request
  70. * @param UrlInterface $urlBuilder
  71. * @param LoggerInterface $logger
  72. * @param PaymentHelper $paymentHelper
  73. * @param string $methodCode
  74. */
  75. public function __construct(
  76. Repository $assetRepo,
  77. RequestInterface $request,
  78. UrlInterface $urlBuilder,
  79. LoggerInterface $logger,
  80. PaymentHelper $paymentHelper,
  81. $methodCode
  82. ) {
  83. $this->assetRepo = $assetRepo;
  84. $this->request = $request;
  85. $this->urlBuilder = $urlBuilder;
  86. $this->logger = $logger;
  87. $this->methodCode = $methodCode;
  88. $this->method = $paymentHelper->getMethodInstance($methodCode);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getConfig()
  94. {
  95. return [
  96. 'payment' => [
  97. 'iframe' => [
  98. 'timeoutTime' => [$this->methodCode => self::TIMEOUT_TIME],
  99. 'dateDelim' => [$this->methodCode => $this->getDateDelim()],
  100. 'cardFieldsMap' => [$this->methodCode => $this->getCardFieldsMap()],
  101. 'source' => [$this->methodCode => $this->getViewFileUrl('blank.html')],
  102. 'controllerName' => [$this->methodCode => self::CHECKOUT_IDENTIFIER],
  103. 'cgiUrl' => [$this->methodCode => $this->getCgiUrl()],
  104. 'placeOrderUrl' => [$this->methodCode => $this->getPlaceOrderUrl()],
  105. 'saveOrderUrl' => [$this->methodCode => $this->getSaveOrderUrl()],
  106. 'expireYearLength' => [$this->methodCode => $this->getExpireDateYearLength()]
  107. ]
  108. ]
  109. ];
  110. }
  111. /**
  112. * Get delimiter for date
  113. *
  114. * @return string
  115. */
  116. protected function getDateDelim()
  117. {
  118. $result = '';
  119. if ($this->method->isAvailable()) {
  120. $configData = $this->getMethodConfigData('date_delim');
  121. if ($configData !== null) {
  122. $result = $configData;
  123. }
  124. }
  125. return $result;
  126. }
  127. /**
  128. * Returns Cc expire year length
  129. *
  130. * @return int
  131. */
  132. protected function getExpireDateYearLength()
  133. {
  134. return (int)$this->getMethodConfigData('cc_year_length') ?: self::DEFAULT_YEAR_LENGTH;
  135. }
  136. /**
  137. * Get map of cc_code, cc_num, cc_expdate for gateway
  138. * Returns json formatted string
  139. *
  140. * @return string
  141. */
  142. protected function getCardFieldsMap()
  143. {
  144. $result = [];
  145. if ($this->method->isAvailable()) {
  146. $configData = $this->getMethodConfigData('ccfields');
  147. $keys = ['cccvv', 'ccexpdate', 'ccnum'];
  148. $result = array_combine($keys, explode(',', $configData));
  149. }
  150. return $result;
  151. }
  152. /**
  153. * Retrieve url of a view file
  154. *
  155. * @param string $fileId
  156. * @param array $params
  157. * @return string[]
  158. */
  159. protected function getViewFileUrl($fileId, array $params = [])
  160. {
  161. try {
  162. $params = array_merge(['_secure' => $this->request->isSecure()], $params);
  163. return $this->assetRepo->getUrlWithParams($fileId, $params);
  164. } catch (LocalizedException $e) {
  165. $this->logger->critical($e);
  166. return $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']);
  167. }
  168. }
  169. /**
  170. * Retrieve place order url on front
  171. *
  172. * @return string
  173. */
  174. protected function getPlaceOrderUrl()
  175. {
  176. return $this->urlBuilder->getUrl(
  177. $this->getMethodConfigData('place_order_url'),
  178. [
  179. '_secure' => $this->request->isSecure()
  180. ]
  181. );
  182. }
  183. /**
  184. * Retrieve save order url on front
  185. *
  186. * @return string
  187. */
  188. protected function getSaveOrderUrl()
  189. {
  190. return $this->urlBuilder->getUrl('checkout/onepage/saveOrder', ['_secure' => $this->request->isSecure()]);
  191. }
  192. /**
  193. * Retrieve gateway url
  194. *
  195. * @return string
  196. */
  197. protected function getCgiUrl()
  198. {
  199. return (bool)$this->getMethodConfigData('sandbox_flag')
  200. ? $this->getMethodConfigData('cgi_url_test_mode')
  201. : $this->getMethodConfigData('cgi_url');
  202. }
  203. /**
  204. * Retrieve config data value by field name
  205. *
  206. * @param string $fieldName
  207. * @return mixed
  208. */
  209. protected function getMethodConfigData($fieldName)
  210. {
  211. if ($this->method instanceof TransparentInterface) {
  212. return $this->method->getConfigInterface()->getValue($fieldName);
  213. }
  214. return $this->method->getConfigData($fieldName);
  215. }
  216. }