Form.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Block\Transparent;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Payment\Model\Method\Adapter;
  9. use Magento\Payment\Model\Method\TransparentInterface;
  10. use Magento\Checkout\Model\Session;
  11. use Magento\Payment\Model\Config;
  12. use Magento\Framework\View\Element\Template\Context;
  13. /**
  14. * Transparent form block
  15. *
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Form extends \Magento\Payment\Block\Form\Cc
  20. {
  21. /**
  22. * @var Session
  23. */
  24. private $checkoutSession;
  25. /**
  26. * @var string
  27. */
  28. protected $_template = 'Magento_Payment::transparent/form.phtml';
  29. /**
  30. * @param Context $context
  31. * @param Config $paymentConfig
  32. * @param Session $checkoutSession
  33. * @param array $data
  34. */
  35. public function __construct(
  36. Context $context,
  37. Config $paymentConfig,
  38. Session $checkoutSession,
  39. array $data = []
  40. ) {
  41. parent::__construct($context, $paymentConfig, $data);
  42. $this->checkoutSession = $checkoutSession;
  43. }
  44. /**
  45. * {inheritdoc}
  46. */
  47. protected function _toHtml()
  48. {
  49. if ($this->shouldRender()) {
  50. return $this->processHtml();
  51. }
  52. return '';
  53. }
  54. /**
  55. * Checks whether block should be rendered
  56. * basing on TransparentInterface presence in checkout session
  57. *
  58. * @return bool
  59. */
  60. protected function shouldRender()
  61. {
  62. $quote = $this->checkoutSession->getQuote();
  63. if ($quote) {
  64. $payment = $quote->getPayment();
  65. return $payment && $payment->getMethodInstance() instanceof TransparentInterface;
  66. }
  67. return false;
  68. }
  69. /**
  70. * Initializes method
  71. *
  72. * @return void
  73. */
  74. protected function initializeMethod()
  75. {
  76. $this->setData(
  77. 'method',
  78. $this->checkoutSession
  79. ->getQuote()
  80. ->getPayment()
  81. ->getMethodInstance()
  82. );
  83. }
  84. /**
  85. * Parent rendering wrapper
  86. *
  87. * @return string
  88. */
  89. protected function processHtml()
  90. {
  91. $this->initializeMethod();
  92. return parent::_toHtml();
  93. }
  94. /**
  95. * Get type of request
  96. *
  97. * @return bool
  98. */
  99. public function isAjaxRequest()
  100. {
  101. return $this->getRequest()->getParam('isAjax');
  102. }
  103. /**
  104. * Get delimiter for date
  105. *
  106. * @return string
  107. */
  108. public function getDateDelim()
  109. {
  110. return $this->getMethodConfigData('date_delim');
  111. }
  112. /**
  113. * Get map of cc_code, cc_num, cc_expdate for gateway
  114. * Returns json formatted string
  115. *
  116. * @return string
  117. */
  118. public function getCardFieldsMap()
  119. {
  120. $keys = ['cccvv', 'ccexpdate', 'ccnum'];
  121. $ccfields = array_combine($keys, explode(',', $this->getMethodConfigData('ccfields')));
  122. return json_encode($ccfields);
  123. }
  124. /**
  125. * Retrieve place order url on front
  126. *
  127. * @return string
  128. */
  129. public function getOrderUrl()
  130. {
  131. return $this->_urlBuilder->getUrl(
  132. $this->getMethodConfigData('place_order_url'),
  133. [
  134. '_secure' => $this->getRequest()->isSecure()
  135. ]
  136. );
  137. }
  138. /**
  139. * Retrieve gateway url
  140. *
  141. * @return string
  142. */
  143. public function getCgiUrl()
  144. {
  145. return (bool)$this->getMethodConfigData('sandbox_flag')
  146. ? $this->getMethodConfigData('cgi_url_test_mode')
  147. : $this->getMethodConfigData('cgi_url');
  148. }
  149. /**
  150. * Retrieve config data value by field name
  151. *
  152. * @param string $fieldName
  153. * @return mixed
  154. */
  155. public function getMethodConfigData($fieldName)
  156. {
  157. $method = $this->getMethod();
  158. if ($method instanceof TransparentInterface) {
  159. return $method->getConfigInterface()->getValue($fieldName);
  160. }
  161. return $method->getConfigData($fieldName);
  162. }
  163. /**
  164. * Returns transparent method service
  165. *
  166. * @return TransparentInterface
  167. * @throws LocalizedException
  168. */
  169. public function getMethod()
  170. {
  171. $method = parent::getMethod();
  172. if (!$method instanceof TransparentInterface && !$method instanceof Adapter) {
  173. throw new LocalizedException(
  174. __('We cannot retrieve the transparent payment method model object.')
  175. );
  176. }
  177. return $method;
  178. }
  179. }