GetToken.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  8. use Magento\Checkout\Helper\Data;
  9. use Magento\Checkout\Helper\ExpressRedirect;
  10. use Magento\Checkout\Model\Type\Onepage;
  11. use Magento\Framework\Controller\ResultFactory;
  12. use Magento\Framework\Controller\ResultInterface;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Framework\Webapi\Exception;
  15. use Magento\Paypal\Model\Express\Checkout;
  16. use Magento\Framework\App\ObjectManager;
  17. /**
  18. * Class GetToken
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class GetToken extends AbstractExpress implements HttpGetActionInterface
  22. {
  23. /**
  24. * Config mode type
  25. *
  26. * @var string
  27. */
  28. protected $_configType = \Magento\Paypal\Model\Config::class;
  29. /**
  30. * Config method type
  31. *
  32. * @var string
  33. */
  34. protected $_configMethod = \Magento\Paypal\Model\Config::METHOD_WPP_EXPRESS;
  35. /**
  36. * Checkout mode type
  37. *
  38. * @var string
  39. */
  40. protected $_checkoutType = \Magento\Paypal\Model\Express\Checkout::class;
  41. /**
  42. * @var \Psr\Log\LoggerInterface
  43. */
  44. private $logger;
  45. /**
  46. * @param \Magento\Framework\App\Action\Context $context
  47. * @param \Magento\Customer\Model\Session $customerSession
  48. * @param \Magento\Checkout\Model\Session $checkoutSession
  49. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  50. * @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
  51. * @param \Magento\Framework\Session\Generic $paypalSession
  52. * @param \Magento\Framework\Url\Helper\Data $urlHelper
  53. * @param \Magento\Customer\Model\Url $customerUrl
  54. * @param \Psr\Log\LoggerInterface|null $logger
  55. */
  56. public function __construct(
  57. \Magento\Framework\App\Action\Context $context,
  58. \Magento\Customer\Model\Session $customerSession,
  59. \Magento\Checkout\Model\Session $checkoutSession,
  60. \Magento\Sales\Model\OrderFactory $orderFactory,
  61. \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
  62. \Magento\Framework\Session\Generic $paypalSession,
  63. \Magento\Framework\Url\Helper\Data $urlHelper,
  64. \Magento\Customer\Model\Url $customerUrl,
  65. \Psr\Log\LoggerInterface $logger = null
  66. ) {
  67. $this->logger = $logger ?: ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
  68. parent::__construct(
  69. $context,
  70. $customerSession,
  71. $checkoutSession,
  72. $orderFactory,
  73. $checkoutFactory,
  74. $paypalSession,
  75. $urlHelper,
  76. $customerUrl
  77. );
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function execute()
  83. {
  84. $controllerResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  85. try {
  86. $token = $this->getToken();
  87. if ($token === null) {
  88. $token = false;
  89. }
  90. $url = $this->_checkout->getRedirectUrl();
  91. $this->_initToken($token);
  92. $controllerResult->setData(['url' => $url]);
  93. } catch (LocalizedException $exception) {
  94. $this->logger->critical($exception);
  95. $controllerResult->setData([
  96. 'message' => [
  97. 'text' => $exception->getMessage(),
  98. 'type' => 'error'
  99. ]
  100. ]);
  101. } catch (\Exception $exception) {
  102. $this->messageManager->addExceptionMessage(
  103. $exception,
  104. __('We can\'t start Express Checkout.')
  105. );
  106. return $this->getErrorResponse($controllerResult);
  107. }
  108. return $controllerResult;
  109. }
  110. /**
  111. * @return string|null
  112. * @throws LocalizedException
  113. */
  114. protected function getToken()
  115. {
  116. $this->_initCheckout();
  117. $quote = $this->_getQuote();
  118. $hasButton = $this->getRequest()->getParam(Checkout::PAYMENT_INFO_BUTTON) == 1;
  119. /** @var Data $checkoutHelper */
  120. $checkoutHelper = $this->_objectManager->get(Data::class);
  121. $quoteCheckoutMethod = $quote->getCheckoutMethod();
  122. $customerData = $this->_customerSession->getCustomerDataObject();
  123. if ($quote->getIsMultiShipping()) {
  124. $quote->setIsMultiShipping(false);
  125. $quote->removeAllAddresses();
  126. }
  127. if ($customerData->getId()) {
  128. $this->_checkout->setCustomerWithAddressChange(
  129. $customerData,
  130. $quote->getBillingAddress(),
  131. $quote->getShippingAddress()
  132. );
  133. } elseif ((!$quoteCheckoutMethod || $quoteCheckoutMethod !== Onepage::METHOD_REGISTER)
  134. && !$checkoutHelper->isAllowedGuestCheckout($quote, $quote->getStoreId())
  135. ) {
  136. $expressRedirect = $this->_objectManager->get(ExpressRedirect::class);
  137. $this->messageManager->addNoticeMessage(
  138. __('To check out, please sign in with your email address.')
  139. );
  140. $expressRedirect->redirectLogin($this);
  141. $this->_customerSession->setBeforeAuthUrl(
  142. $this->_url->getUrl('*/*/*', ['_current' => true])
  143. );
  144. return null;
  145. }
  146. // billing agreement
  147. $isBaRequested = (bool)$this->getRequest()
  148. ->getParam(Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT);
  149. if ($customerData->getId()) {
  150. $this->_checkout->setIsBillingAgreementRequested($isBaRequested);
  151. }
  152. // Bill Me Later
  153. $this->_checkout->setIsBml((bool)$this->getRequest()->getParam('bml'));
  154. // giropay
  155. $this->_checkout->prepareGiropayUrls(
  156. $this->_url->getUrl('checkout/onepage/success'),
  157. $this->_url->getUrl('paypal/express/cancel'),
  158. $this->_url->getUrl('checkout/onepage/success')
  159. );
  160. return $this->_checkout->start(
  161. $this->_url->getUrl('*/*/return'),
  162. $this->_url->getUrl('*/*/cancel'),
  163. $hasButton
  164. );
  165. }
  166. /**
  167. * @param ResultInterface $controllerResult
  168. * @return ResultInterface
  169. */
  170. private function getErrorResponse(ResultInterface $controllerResult)
  171. {
  172. $controllerResult->setHttpResponseCode(Exception::HTTP_BAD_REQUEST);
  173. $controllerResult->setData(['message' => __('Sorry, but something went wrong')]);
  174. return $controllerResult;
  175. }
  176. }