Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Iframe.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Paypal\Block;
7 
15 {
21  protected $_shouldRender = false;
22 
28  protected $_order;
29 
36 
42  protected $_block;
43 
47  protected $_template = 'Magento_Paypal::hss/js.phtml';
48 
52  protected $_orderFactory;
53 
57  protected $_checkoutSession;
58 
62  protected $_hssHelper;
63 
67  protected $readFactory;
68 
72  protected $reader;
73 
83  public function __construct(
84  \Magento\Framework\View\Element\Template\Context $context,
85  \Magento\Sales\Model\OrderFactory $orderFactory,
86  \Magento\Checkout\Model\Session $checkoutSession,
87  \Magento\Paypal\Helper\Hss $hssHelper,
88  \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
89  \Magento\Framework\Module\Dir\Reader $reader,
90  array $data = []
91  ) {
92  $this->_hssHelper = $hssHelper;
93  $this->_orderFactory = $orderFactory;
94  $this->_checkoutSession = $checkoutSession;
95  $this->_isScopePrivate = true;
96  $this->readFactory = $readFactory;
97  $this->reader = $reader;
98  parent::__construct($context, $data);
99  }
100 
106  protected function _construct()
107  {
108  parent::_construct();
109  $paymentCode = $this->_getCheckout()->getQuote()->getPayment()->getMethod();
110  if (in_array($paymentCode, $this->_hssHelper->getHssMethods())) {
111  $this->_paymentMethodCode = $paymentCode;
112  $templatePath = str_replace('_', '', $paymentCode);
113  $templateFile = "Magento_Paypal::{$templatePath}/iframe.phtml";
114  $directory = $this->readFactory->create($this->reader->getModuleDir('', 'Magento_Paypal'));
115  $file = $this->resolver->getTemplateFileName($templateFile, ['module' => 'Magento_Paypal']);
116  if ($file && $directory->isExist($directory->getRelativePath($file))) {
117  $this->setTemplate($templateFile);
118  } else {
119  $this->setTemplate('Magento_Paypal::hss/iframe.phtml');
120  }
121  }
122  }
123 
130  protected function _getBlock()
131  {
132  if (!$this->_block) {
133  $this->_block = $this->getLayout()->createBlock(
134  'Magento\\Paypal\\Block\\' . str_replace(
135  ' ',
136  '\\',
137  ucwords(str_replace('_', ' ', $this->_paymentMethodCode))
138  ) . '\\Iframe'
139  );
140  if (!$this->_block instanceof \Magento\Paypal\Block\Iframe) {
141  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid block type'));
142  }
143  }
144 
145  return $this->_block;
146  }
147 
153  protected function _getOrder()
154  {
155  if (!$this->_order) {
156  $incrementId = $this->_getCheckout()->getLastRealOrderId();
157  $this->_order = $this->_orderFactory->create()->loadByIncrementId($incrementId);
158  }
159  return $this->_order;
160  }
161 
167  protected function _getCheckout()
168  {
170  }
171 
177  protected function _beforeToHtml()
178  {
179  if ($this->_getOrder()->getId() &&
180  $this->_getOrder()->getQuoteId() == $this->_getCheckout()->getLastQuoteId() &&
181  $this->_paymentMethodCode
182  ) {
183  $this->_shouldRender = true;
184  }
185 
186  if ($this->getGotoSection() || $this->getGotoSuccessPage()) {
187  $this->_shouldRender = true;
188  }
189 
190  return parent::_beforeToHtml();
191  }
192 
198  protected function _toHtml()
199  {
200  if ($this->_isAfterPaymentSave()) {
201  $this->setTemplate('Magento_Paypal::hss/js.phtml');
202  return parent::_toHtml();
203  }
204  if (!$this->_shouldRender) {
205  return '';
206  }
207  return parent::_toHtml();
208  }
209 
215  protected function _isAfterPaymentSave()
216  {
217  $quote = $this->_getCheckout()->getQuote();
218  if ($quote->getPayment()->getMethod() == $this->_paymentMethodCode &&
219  $quote->getIsActive() &&
220  $this->getTemplate() &&
221  $this->getRequest()->getActionName() == 'savePayment'
222  ) {
223  return true;
224  }
225 
226  return false;
227  }
228 
234  public function getFrameActionUrl()
235  {
236  return $this->_getBlock()->getFrameActionUrl();
237  }
238 
244  public function getSecureToken()
245  {
246  return $this->_getBlock()->getSecureToken();
247  }
248 
254  public function getSecureTokenId()
255  {
256  return $this->_getBlock()->getSecureTokenId();
257  }
258 
264  public function getTransactionUrl()
265  {
266  return $this->_getBlock()->getTransactionUrl();
267  }
268 
274  public function isTestMode()
275  {
276  return $this->_getBlock()->isTestMode();
277  }
278 }
$quote
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Paypal\Helper\Hss $hssHelper, \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, \Magento\Framework\Module\Dir\Reader $reader, array $data=[])
Definition: Iframe.php:83