Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetToken.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
18 
23 class GetToken extends AbstractExpress implements HttpGetActionInterface
24 {
30  protected $_configType = \Magento\Paypal\Model\Config::class;
31 
38 
44  protected $_checkoutType = \Magento\Paypal\Model\Express\Checkout::class;
45 
49  private $logger;
50 
62  public function __construct(
63  \Magento\Framework\App\Action\Context $context,
64  \Magento\Customer\Model\Session $customerSession,
65  \Magento\Checkout\Model\Session $checkoutSession,
66  \Magento\Sales\Model\OrderFactory $orderFactory,
67  \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
68  \Magento\Framework\Session\Generic $paypalSession,
69  \Magento\Framework\Url\Helper\Data $urlHelper,
70  \Magento\Customer\Model\Url $customerUrl,
71  \Psr\Log\LoggerInterface $logger = null
72  ) {
73  $this->logger = $logger ?: ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
74  parent::__construct(
75  $context,
76  $customerSession,
77  $checkoutSession,
78  $orderFactory,
79  $checkoutFactory,
80  $paypalSession,
81  $urlHelper,
83  );
84  }
85 
89  public function execute()
90  {
91  $controllerResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
92 
93  try {
94  $token = $this->getToken();
95  if ($token === null) {
96  $token = false;
97  }
98  $url = $this->_checkout->getRedirectUrl();
99  $this->_initToken($token);
100  $controllerResult->setData(['url' => $url]);
101  } catch (LocalizedException $exception) {
102  $this->logger->critical($exception);
103  $controllerResult->setData([
104  'message' => [
105  'text' => $exception->getMessage(),
106  'type' => 'error'
107  ]
108  ]);
109  } catch (\Exception $exception) {
110  $this->messageManager->addExceptionMessage(
111  $exception,
112  __('We can\'t start Express Checkout.')
113  );
114 
115  return $this->getErrorResponse($controllerResult);
116  }
117 
118  return $controllerResult;
119  }
120 
125  protected function getToken()
126  {
127  $this->_initCheckout();
128  $quote = $this->_getQuote();
129  $hasButton = $this->getRequest()->getParam(Checkout::PAYMENT_INFO_BUTTON) == 1;
130 
132  $checkoutHelper = $this->_objectManager->get(Data::class);
133  $quoteCheckoutMethod = $quote->getCheckoutMethod();
134  $customerData = $this->_customerSession->getCustomerDataObject();
135 
136  if ($quote->getIsMultiShipping()) {
137  $quote->setIsMultiShipping(false);
138  $quote->removeAllAddresses();
139  }
140 
141  if ($customerData->getId()) {
142  $this->_checkout->setCustomerWithAddressChange(
144  $quote->getBillingAddress(),
145  $quote->getShippingAddress()
146  );
147  } elseif ((!$quoteCheckoutMethod || $quoteCheckoutMethod !== Onepage::METHOD_REGISTER)
148  && !$checkoutHelper->isAllowedGuestCheckout($quote, $quote->getStoreId())
149  ) {
150  $expressRedirect = $this->_objectManager->get(ExpressRedirect::class);
151 
152  $this->messageManager->addNoticeMessage(
153  __('To check out, please sign in with your email address.')
154  );
155 
156  $expressRedirect->redirectLogin($this);
157  $this->_customerSession->setBeforeAuthUrl(
158  $this->_url->getUrl('*/*/*', ['_current' => true])
159  );
160 
161  return null;
162  }
163 
164  // billing agreement
165  $isBaRequested = (bool)$this->getRequest()
167  if ($customerData->getId()) {
168  $this->_checkout->setIsBillingAgreementRequested($isBaRequested);
169  }
170 
171  // Bill Me Later
172  $this->_checkout->setIsBml((bool)$this->getRequest()->getParam('bml'));
173 
174  // giropay
175  $this->_checkout->prepareGiropayUrls(
176  $this->_url->getUrl('checkout/onepage/success'),
177  $this->_url->getUrl('paypal/express/cancel'),
178  $this->_url->getUrl('checkout/onepage/success')
179  );
180 
181  return $this->_checkout->start(
182  $this->_url->getUrl('*/*/return'),
183  $this->_url->getUrl('*/*/cancel'),
184  $hasButton
185  );
186  }
187 
192  private function getErrorResponse(ResultInterface $controllerResult)
193  {
194  $controllerResult->setHttpResponseCode(Exception::HTTP_BAD_REQUEST);
195  $controllerResult->setData(['message' => __('Sorry, but something went wrong')]);
196 
197  return $controllerResult;
198  }
199 }
$customerData
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$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, \Psr\Log\LoggerInterface $logger=null)
Definition: GetToken.php:62