123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Paypal\Controller\Express;
- use Magento\Checkout\Controller\Express\RedirectLoginInterface;
- use Magento\Framework\App\Action\Action as AppAction;
- use Magento\Framework\App\Action\HttpGetActionInterface;
- use Magento\Framework\App\Action\HttpPostActionInterface;
- use Magento\Quote\Api\Data\CartInterface;
- /**
- * Abstract Express Checkout Controller
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- abstract class AbstractExpress extends AppAction implements
- RedirectLoginInterface,
- HttpGetActionInterface,
- HttpPostActionInterface
- {
- /**
- * @var \Magento\Paypal\Model\Express\Checkout
- */
- protected $_checkout;
- /**
- * Internal cache of checkout models
- *
- * @var array
- */
- protected $_checkoutTypes = [];
- /**
- * @var \Magento\Paypal\Model\Config
- */
- protected $_config;
- /**
- * @var \Magento\Quote\Model\Quote
- */
- protected $_quote = false;
- /**
- * Config mode type
- *
- * @var string
- */
- protected $_configType;
- /**
- * Config method type
- *
- * @var string
- */
- protected $_configMethod;
- /**
- * Checkout mode type
- *
- * @var string
- */
- protected $_checkoutType;
- /**
- * @var \Magento\Customer\Model\Session
- */
- protected $_customerSession;
- /**
- * @var \Magento\Checkout\Model\Session
- */
- protected $_checkoutSession;
- /**
- * @var \Magento\Sales\Model\OrderFactory
- */
- protected $_orderFactory;
- /**
- * @var \Magento\Paypal\Model\Express\Checkout\Factory
- */
- protected $_checkoutFactory;
- /**
- * @var \Magento\Framework\Session\Generic
- */
- protected $_paypalSession;
- /**
- * @var \Magento\Framework\Url\Helper
- */
- protected $_urlHelper;
- /**
- * @var \Magento\Customer\Model\Url
- */
- protected $_customerUrl;
- /**
- * @param \Magento\Framework\App\Action\Context $context
- * @param \Magento\Customer\Model\Session $customerSession
- * @param \Magento\Checkout\Model\Session $checkoutSession
- * @param \Magento\Sales\Model\OrderFactory $orderFactory
- * @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
- * @param \Magento\Framework\Session\Generic $paypalSession
- * @param \Magento\Framework\Url\Helper\Data $urlHelper
- * @param \Magento\Customer\Model\Url $customerUrl
- */
- public function __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
- ) {
- $this->_customerSession = $customerSession;
- $this->_checkoutSession = $checkoutSession;
- $this->_orderFactory = $orderFactory;
- $this->_checkoutFactory = $checkoutFactory;
- $this->_paypalSession = $paypalSession;
- $this->_urlHelper = $urlHelper;
- $this->_customerUrl = $customerUrl;
- parent::__construct($context);
- $parameters = ['params' => [$this->_configMethod]];
- $this->_config = $this->_objectManager->create($this->_configType, $parameters);
- }
- /**
- * Instantiate quote and checkout
- *
- * @param CartInterface|null $quoteObject
- *
- * @return void
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- protected function _initCheckout(CartInterface $quoteObject = null)
- {
- $quote = $quoteObject ? $quoteObject : $this->_getQuote();
- if (!$quote->hasItems() || $quote->getHasError()) {
- $this->getResponse()->setStatusHeader(403, '1.1', 'Forbidden');
- throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t initialize Express Checkout.'));
- }
- if (!(float)$quote->getGrandTotal()) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __(
- 'PayPal can\'t process orders with a zero balance due. '
- . 'To finish your purchase, please go through the standard checkout process.'
- )
- );
- }
- if (!isset($this->_checkoutTypes[$this->_checkoutType])) {
- $parameters = [
- 'params' => [
- 'quote' => $quote,
- 'config' => $this->_config,
- ],
- ];
- $this->_checkoutTypes[$this->_checkoutType] = $this->_checkoutFactory
- ->create($this->_checkoutType, $parameters);
- }
- $this->_checkout = $this->_checkoutTypes[$this->_checkoutType];
- }
- /**
- * Get Proper Checkout Token
- *
- * Search for proper checkout token in request or session or (un)set specified one
- * Combined getter/setter
- *
- * @param string|null $setToken
- * @return $this|string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- protected function _initToken($setToken = null)
- {
- if (null !== $setToken) {
- if (false === $setToken) {
- // security measure for avoid unsetting token twice
- if (!$this->_getSession()->getExpressCheckoutToken()) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('PayPal Express Checkout Token does not exist.')
- );
- }
- $this->_getSession()->unsExpressCheckoutToken();
- } else {
- $this->_getSession()->setExpressCheckoutToken($setToken);
- }
- return $this;
- }
- $setToken = $this->getRequest()->getParam('token');
- if ($setToken) {
- if ($setToken !== $this->_getSession()->getExpressCheckoutToken()) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('A wrong PayPal Express Checkout Token is specified.')
- );
- }
- } else {
- $setToken = $this->_getSession()->getExpressCheckoutToken();
- }
- return $setToken;
- }
- /**
- * PayPal session instance getter
- *
- * @return \Magento\Framework\Session\Generic
- */
- protected function _getSession()
- {
- return $this->_paypalSession;
- }
- /**
- * Return checkout session object
- *
- * @return \Magento\Checkout\Model\Session
- */
- protected function _getCheckoutSession()
- {
- return $this->_checkoutSession;
- }
- /**
- * Return checkout quote object
- *
- * @return \Magento\Quote\Model\Quote
- */
- protected function _getQuote()
- {
- if (!$this->_quote) {
- $this->_quote = $this->_getCheckoutSession()->getQuote();
- }
- return $this->_quote;
- }
- /**
- * @inheritdoc
- */
- public function getCustomerBeforeAuthUrl()
- {
- return;
- }
- /**
- * @inheritdoc
- */
- public function getActionFlagList()
- {
- return [];
- }
- /**
- * Returns login url parameter for redirect
- *
- * @return string
- */
- public function getLoginUrl()
- {
- return $this->_customerUrl->getLoginUrl();
- }
- /**
- * Returns action name which requires redirect
- *
- * @return string
- */
- public function getRedirectActionName()
- {
- return 'start';
- }
- /**
- * Redirect to login page
- *
- * @return void
- */
- public function redirectLogin()
- {
- $this->_actionFlag->set('', 'no-dispatch', true);
- $this->_customerSession->setBeforeAuthUrl($this->_redirect->getRefererUrl());
- $this->getResponse()->setRedirect(
- $this->_urlHelper->addRequestParam($this->_customerUrl->getLoginUrl(), ['context' => 'checkout'])
- );
- }
- /**
- * @inheritdoc
- */
- abstract public function execute();
- }
|