Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractExpress.php
Go to the documentation of this file.
1 <?php
7 
10 
15 abstract class AbstractExpress extends AppAction implements RedirectLoginInterface
16 {
20  protected $_checkout;
21 
27  protected $_checkoutTypes = [];
28 
32  protected $_config;
33 
37  protected $_quote = false;
38 
44  protected $_configType;
45 
51  protected $_configMethod;
52 
58  protected $_checkoutType;
59 
63  protected $_customerSession;
64 
68  protected $_checkoutSession;
69 
73  protected $_orderFactory;
74 
78  protected $_checkoutFactory;
79 
83  protected $_paypalSession;
84 
88  protected $_urlHelper;
89 
93  protected $_customerUrl;
94 
105  public function __construct(
106  \Magento\Framework\App\Action\Context $context,
107  \Magento\Customer\Model\Session $customerSession,
108  \Magento\Checkout\Model\Session $checkoutSession,
109  \Magento\Sales\Model\OrderFactory $orderFactory,
110  \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
111  \Magento\Framework\Session\Generic $paypalSession,
112  \Magento\Framework\Url\Helper\Data $urlHelper,
113  \Magento\Customer\Model\Url $customerUrl
114  ) {
115  $this->_customerSession = $customerSession;
116  $this->_checkoutSession = $checkoutSession;
117  $this->_orderFactory = $orderFactory;
118  $this->_checkoutFactory = $checkoutFactory;
119  $this->_paypalSession = $paypalSession;
120  $this->_urlHelper = $urlHelper;
121  $this->_customerUrl = $customerUrl;
122  parent::__construct($context);
123  $parameters = ['params' => [$this->_configMethod]];
124  $this->_config = $this->_objectManager->create($this->_configType, $parameters);
125  }
126 
133  protected function _initCheckout()
134  {
135  $quote = $this->_getQuote();
136  if (!$quote->hasItems() || $quote->getHasError()) {
137  $this->getResponse()->setStatusHeader(403, '1.1', 'Forbidden');
138  throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t initialize Express Checkout.'));
139  }
140  if (!isset($this->_checkoutTypes[$this->_checkoutType])) {
141  $parameters = [
142  'params' => [
143  'quote' => $quote,
144  'config' => $this->_config,
145  ],
146  ];
147  $this->_checkoutTypes[$this->_checkoutType] = $this->_checkoutFactory
148  ->create($this->_checkoutType, $parameters);
149  }
150  $this->_checkout = $this->_checkoutTypes[$this->_checkoutType];
151  }
152 
161  protected function _initToken($setToken = null)
162  {
163  if (null !== $setToken) {
164  if (false === $setToken) {
165  // security measure for avoid unsetting token twice
166  if (!$this->_getSession()->getExpressCheckoutToken()) {
167  throw new \Magento\Framework\Exception\LocalizedException(
168  __('PayPal Express Checkout Token does not exist.')
169  );
170  }
171  $this->_getSession()->unsExpressCheckoutToken();
172  } else {
173  $this->_getSession()->setExpressCheckoutToken($setToken);
174  }
175  return $this;
176  }
177  $setToken = $this->getRequest()->getParam('token');
178  if ($setToken) {
179  if ($setToken !== $this->_getSession()->getExpressCheckoutToken()) {
180  throw new \Magento\Framework\Exception\LocalizedException(
181  __('A wrong PayPal Express Checkout Token is specified.')
182  );
183  }
184  } else {
185  $setToken = $this->_getSession()->getExpressCheckoutToken();
186  }
187  return $setToken;
188  }
189 
195  protected function _getSession()
196  {
197  return $this->_paypalSession;
198  }
199 
205  protected function _getCheckoutSession()
206  {
208  }
209 
215  protected function _getQuote()
216  {
217  if (!$this->_quote) {
218  $this->_quote = $this->_getCheckoutSession()->getQuote();
219  }
220  return $this->_quote;
221  }
222 
227  public function getCustomerBeforeAuthUrl()
228  {
229  return;
230  }
231 
236  public function getActionFlagList()
237  {
238  return [];
239  }
240 
245  public function getLoginUrl()
246  {
247  return $this->_customerUrl->getLoginUrl();
248  }
249 
254  public function getRedirectActionName()
255  {
256  return 'start';
257  }
258 
264  public function redirectLogin()
265  {
266  $this->_actionFlag->set('', 'no-dispatch', true);
267  $this->_customerSession->setBeforeAuthUrl($this->_redirect->getRefererUrl());
268  $this->getResponse()->setRedirect(
269  $this->_urlHelper->addRequestParam($this->_customerUrl->getLoginUrl(), ['context' => 'checkout'])
270  );
271  }
272 }
_redirect($path, $arguments=[])
Definition: Action.php:167
$customerUrl
Definition: info.phtml:28
$quote
__()
Definition: __.php:13
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory, \Magento\Framework\Session\Generic $paypalSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl)