Iframe.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block;
  7. /**
  8. * HSS iframe block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Iframe extends \Magento\Payment\Block\Form
  14. {
  15. /**
  16. * Whether the block should be eventually rendered
  17. *
  18. * @var bool
  19. */
  20. protected $_shouldRender = false;
  21. /**
  22. * Order object
  23. *
  24. * @var \Magento\Sales\Model\Order
  25. */
  26. protected $_order;
  27. /**
  28. * Payment method code
  29. *
  30. * @var string
  31. */
  32. protected $_paymentMethodCode;
  33. /**
  34. * Current iframe block instance
  35. *
  36. * @var \Magento\Payment\Block\Form
  37. */
  38. protected $_block;
  39. /**
  40. * @var string
  41. */
  42. protected $_template = 'Magento_Paypal::hss/js.phtml';
  43. /**
  44. * @var \Magento\Sales\Model\OrderFactory
  45. */
  46. protected $_orderFactory;
  47. /**
  48. * @var \Magento\Checkout\Model\Session
  49. */
  50. protected $_checkoutSession;
  51. /**
  52. * @var \Magento\Paypal\Helper\Hss
  53. */
  54. protected $_hssHelper;
  55. /**
  56. * @var \Magento\Framework\Filesystem\Directory\ReadFactory
  57. */
  58. protected $readFactory;
  59. /**
  60. * @var \Magento\Framework\Module\Dir\Reader
  61. */
  62. protected $reader;
  63. /**
  64. * @param \Magento\Framework\View\Element\Template\Context $context
  65. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  66. * @param \Magento\Checkout\Model\Session $checkoutSession
  67. * @param \Magento\Paypal\Helper\Hss $hssHelper
  68. * @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
  69. * @param \Magento\Framework\Module\Dir\Reader $reader
  70. * @param array $data
  71. */
  72. public function __construct(
  73. \Magento\Framework\View\Element\Template\Context $context,
  74. \Magento\Sales\Model\OrderFactory $orderFactory,
  75. \Magento\Checkout\Model\Session $checkoutSession,
  76. \Magento\Paypal\Helper\Hss $hssHelper,
  77. \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
  78. \Magento\Framework\Module\Dir\Reader $reader,
  79. array $data = []
  80. ) {
  81. $this->_hssHelper = $hssHelper;
  82. $this->_orderFactory = $orderFactory;
  83. $this->_checkoutSession = $checkoutSession;
  84. $this->_isScopePrivate = true;
  85. $this->readFactory = $readFactory;
  86. $this->reader = $reader;
  87. parent::__construct($context, $data);
  88. }
  89. /**
  90. * Internal constructor
  91. *
  92. * @return void
  93. */
  94. protected function _construct()
  95. {
  96. parent::_construct();
  97. $paymentCode = $this->_getCheckout()->getQuote()->getPayment()->getMethod();
  98. if (in_array($paymentCode, $this->_hssHelper->getHssMethods())) {
  99. $this->_paymentMethodCode = $paymentCode;
  100. $templatePath = str_replace('_', '', $paymentCode);
  101. $templateFile = "Magento_Paypal::{$templatePath}/iframe.phtml";
  102. $directory = $this->readFactory->create($this->reader->getModuleDir('', 'Magento_Paypal'));
  103. $file = $this->resolver->getTemplateFileName($templateFile, ['module' => 'Magento_Paypal']);
  104. if ($file && $directory->isExist($directory->getRelativePath($file))) {
  105. $this->setTemplate($templateFile);
  106. } else {
  107. $this->setTemplate('Magento_Paypal::hss/iframe.phtml');
  108. }
  109. }
  110. }
  111. /**
  112. * Get current block instance
  113. *
  114. * @return \Magento\Payment\Block\Form
  115. * @throws \Magento\Framework\Exception\LocalizedException
  116. */
  117. protected function _getBlock()
  118. {
  119. if (!$this->_block) {
  120. $this->_block = $this->getLayout()->createBlock(
  121. 'Magento\\Paypal\\Block\\' . str_replace(
  122. ' ',
  123. '\\',
  124. ucwords(str_replace('_', ' ', $this->_paymentMethodCode))
  125. ) . '\\Iframe'
  126. );
  127. if (!$this->_block instanceof \Magento\Paypal\Block\Iframe) {
  128. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid block type'));
  129. }
  130. }
  131. return $this->_block;
  132. }
  133. /**
  134. * Get order object
  135. *
  136. * @return \Magento\Sales\Model\Order
  137. */
  138. protected function _getOrder()
  139. {
  140. if (!$this->_order) {
  141. $incrementId = $this->_getCheckout()->getLastRealOrderId();
  142. $this->_order = $this->_orderFactory->create()->loadByIncrementId($incrementId);
  143. }
  144. return $this->_order;
  145. }
  146. /**
  147. * Get frontend checkout session object
  148. *
  149. * @return \Magento\Checkout\Model\Session
  150. */
  151. protected function _getCheckout()
  152. {
  153. return $this->_checkoutSession;
  154. }
  155. /**
  156. * Before rendering html, check if is block rendering needed
  157. *
  158. * @return \Magento\Framework\View\Element\AbstractBlock
  159. */
  160. protected function _beforeToHtml()
  161. {
  162. if ($this->_getOrder()->getId() &&
  163. $this->_getOrder()->getQuoteId() == $this->_getCheckout()->getLastQuoteId() &&
  164. $this->_paymentMethodCode
  165. ) {
  166. $this->_shouldRender = true;
  167. }
  168. if ($this->getGotoSection() || $this->getGotoSuccessPage()) {
  169. $this->_shouldRender = true;
  170. }
  171. return parent::_beforeToHtml();
  172. }
  173. /**
  174. * Render the block if needed
  175. *
  176. * @return string
  177. */
  178. protected function _toHtml()
  179. {
  180. if ($this->_isAfterPaymentSave()) {
  181. $this->setTemplate('Magento_Paypal::hss/js.phtml');
  182. return parent::_toHtml();
  183. }
  184. if (!$this->_shouldRender) {
  185. return '';
  186. }
  187. return parent::_toHtml();
  188. }
  189. /**
  190. * Check whether block is rendering after save payment
  191. *
  192. * @return bool
  193. */
  194. protected function _isAfterPaymentSave()
  195. {
  196. $quote = $this->_getCheckout()->getQuote();
  197. if ($quote->getPayment()->getMethod() == $this->_paymentMethodCode &&
  198. $quote->getIsActive() &&
  199. $this->getTemplate() &&
  200. $this->getRequest()->getActionName() == 'savePayment'
  201. ) {
  202. return true;
  203. }
  204. return false;
  205. }
  206. /**
  207. * Get iframe action URL
  208. *
  209. * @return string
  210. */
  211. public function getFrameActionUrl()
  212. {
  213. return $this->_getBlock()->getFrameActionUrl();
  214. }
  215. /**
  216. * Get secure token
  217. *
  218. * @return string
  219. */
  220. public function getSecureToken()
  221. {
  222. return $this->_getBlock()->getSecureToken();
  223. }
  224. /**
  225. * Get secure token ID
  226. *
  227. * @return string
  228. */
  229. public function getSecureTokenId()
  230. {
  231. return $this->_getBlock()->getSecureTokenId();
  232. }
  233. /**
  234. * Get payflow transaction URL
  235. *
  236. * @return string
  237. */
  238. public function getTransactionUrl()
  239. {
  240. return $this->_getBlock()->getTransactionUrl();
  241. }
  242. /**
  243. * Check sandbox mode
  244. *
  245. * @return bool
  246. */
  247. public function isTestMode()
  248. {
  249. return $this->_getBlock()->isTestMode();
  250. }
  251. }