AbstractExpress.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Controller\Express;
  7. use Magento\Checkout\Controller\Express\RedirectLoginInterface;
  8. use Magento\Framework\App\Action\Action as AppAction;
  9. use Magento\Framework\App\Action\HttpGetActionInterface;
  10. use Magento\Framework\App\Action\HttpPostActionInterface;
  11. use Magento\Quote\Api\Data\CartInterface;
  12. /**
  13. * Abstract Express Checkout Controller
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. abstract class AbstractExpress extends AppAction implements
  17. RedirectLoginInterface,
  18. HttpGetActionInterface,
  19. HttpPostActionInterface
  20. {
  21. /**
  22. * @var \Magento\Paypal\Model\Express\Checkout
  23. */
  24. protected $_checkout;
  25. /**
  26. * Internal cache of checkout models
  27. *
  28. * @var array
  29. */
  30. protected $_checkoutTypes = [];
  31. /**
  32. * @var \Magento\Paypal\Model\Config
  33. */
  34. protected $_config;
  35. /**
  36. * @var \Magento\Quote\Model\Quote
  37. */
  38. protected $_quote = false;
  39. /**
  40. * Config mode type
  41. *
  42. * @var string
  43. */
  44. protected $_configType;
  45. /**
  46. * Config method type
  47. *
  48. * @var string
  49. */
  50. protected $_configMethod;
  51. /**
  52. * Checkout mode type
  53. *
  54. * @var string
  55. */
  56. protected $_checkoutType;
  57. /**
  58. * @var \Magento\Customer\Model\Session
  59. */
  60. protected $_customerSession;
  61. /**
  62. * @var \Magento\Checkout\Model\Session
  63. */
  64. protected $_checkoutSession;
  65. /**
  66. * @var \Magento\Sales\Model\OrderFactory
  67. */
  68. protected $_orderFactory;
  69. /**
  70. * @var \Magento\Paypal\Model\Express\Checkout\Factory
  71. */
  72. protected $_checkoutFactory;
  73. /**
  74. * @var \Magento\Framework\Session\Generic
  75. */
  76. protected $_paypalSession;
  77. /**
  78. * @var \Magento\Framework\Url\Helper
  79. */
  80. protected $_urlHelper;
  81. /**
  82. * @var \Magento\Customer\Model\Url
  83. */
  84. protected $_customerUrl;
  85. /**
  86. * @param \Magento\Framework\App\Action\Context $context
  87. * @param \Magento\Customer\Model\Session $customerSession
  88. * @param \Magento\Checkout\Model\Session $checkoutSession
  89. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  90. * @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
  91. * @param \Magento\Framework\Session\Generic $paypalSession
  92. * @param \Magento\Framework\Url\Helper\Data $urlHelper
  93. * @param \Magento\Customer\Model\Url $customerUrl
  94. */
  95. public function __construct(
  96. \Magento\Framework\App\Action\Context $context,
  97. \Magento\Customer\Model\Session $customerSession,
  98. \Magento\Checkout\Model\Session $checkoutSession,
  99. \Magento\Sales\Model\OrderFactory $orderFactory,
  100. \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
  101. \Magento\Framework\Session\Generic $paypalSession,
  102. \Magento\Framework\Url\Helper\Data $urlHelper,
  103. \Magento\Customer\Model\Url $customerUrl
  104. ) {
  105. $this->_customerSession = $customerSession;
  106. $this->_checkoutSession = $checkoutSession;
  107. $this->_orderFactory = $orderFactory;
  108. $this->_checkoutFactory = $checkoutFactory;
  109. $this->_paypalSession = $paypalSession;
  110. $this->_urlHelper = $urlHelper;
  111. $this->_customerUrl = $customerUrl;
  112. parent::__construct($context);
  113. $parameters = ['params' => [$this->_configMethod]];
  114. $this->_config = $this->_objectManager->create($this->_configType, $parameters);
  115. }
  116. /**
  117. * Instantiate quote and checkout
  118. *
  119. * @param CartInterface|null $quoteObject
  120. *
  121. * @return void
  122. * @throws \Magento\Framework\Exception\LocalizedException
  123. */
  124. protected function _initCheckout(CartInterface $quoteObject = null)
  125. {
  126. $quote = $quoteObject ? $quoteObject : $this->_getQuote();
  127. if (!$quote->hasItems() || $quote->getHasError()) {
  128. $this->getResponse()->setStatusHeader(403, '1.1', 'Forbidden');
  129. throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t initialize Express Checkout.'));
  130. }
  131. if (!(float)$quote->getGrandTotal()) {
  132. throw new \Magento\Framework\Exception\LocalizedException(
  133. __(
  134. 'PayPal can\'t process orders with a zero balance due. '
  135. . 'To finish your purchase, please go through the standard checkout process.'
  136. )
  137. );
  138. }
  139. if (!isset($this->_checkoutTypes[$this->_checkoutType])) {
  140. $parameters = [
  141. 'params' => [
  142. 'quote' => $quote,
  143. 'config' => $this->_config,
  144. ],
  145. ];
  146. $this->_checkoutTypes[$this->_checkoutType] = $this->_checkoutFactory
  147. ->create($this->_checkoutType, $parameters);
  148. }
  149. $this->_checkout = $this->_checkoutTypes[$this->_checkoutType];
  150. }
  151. /**
  152. * Get Proper Checkout Token
  153. *
  154. * Search for proper checkout token in request or session or (un)set specified one
  155. * Combined getter/setter
  156. *
  157. * @param string|null $setToken
  158. * @return $this|string
  159. * @throws \Magento\Framework\Exception\LocalizedException
  160. */
  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. /**
  190. * PayPal session instance getter
  191. *
  192. * @return \Magento\Framework\Session\Generic
  193. */
  194. protected function _getSession()
  195. {
  196. return $this->_paypalSession;
  197. }
  198. /**
  199. * Return checkout session object
  200. *
  201. * @return \Magento\Checkout\Model\Session
  202. */
  203. protected function _getCheckoutSession()
  204. {
  205. return $this->_checkoutSession;
  206. }
  207. /**
  208. * Return checkout quote object
  209. *
  210. * @return \Magento\Quote\Model\Quote
  211. */
  212. protected function _getQuote()
  213. {
  214. if (!$this->_quote) {
  215. $this->_quote = $this->_getCheckoutSession()->getQuote();
  216. }
  217. return $this->_quote;
  218. }
  219. /**
  220. * @inheritdoc
  221. */
  222. public function getCustomerBeforeAuthUrl()
  223. {
  224. return;
  225. }
  226. /**
  227. * @inheritdoc
  228. */
  229. public function getActionFlagList()
  230. {
  231. return [];
  232. }
  233. /**
  234. * Returns login url parameter for redirect
  235. *
  236. * @return string
  237. */
  238. public function getLoginUrl()
  239. {
  240. return $this->_customerUrl->getLoginUrl();
  241. }
  242. /**
  243. * Returns action name which requires redirect
  244. *
  245. * @return string
  246. */
  247. public function getRedirectActionName()
  248. {
  249. return 'start';
  250. }
  251. /**
  252. * Redirect to login page
  253. *
  254. * @return void
  255. */
  256. public function redirectLogin()
  257. {
  258. $this->_actionFlag->set('', 'no-dispatch', true);
  259. $this->_customerSession->setBeforeAuthUrl($this->_redirect->getRefererUrl());
  260. $this->getResponse()->setRedirect(
  261. $this->_urlHelper->addRequestParam($this->_customerUrl->getLoginUrl(), ['context' => 'checkout'])
  262. );
  263. }
  264. /**
  265. * @inheritdoc
  266. */
  267. abstract public function execute();
  268. }