GetTokenData.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Paypal\Controller\Express;
  8. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  9. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\Framework\Controller\ResultInterface;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Paypal\Model\Express\Checkout;
  13. use Magento\Paypal\Model\Config;
  14. use Magento\Framework\App\Action\Context;
  15. use Magento\Customer\Model\Session as CustomerSession;
  16. use Magento\Checkout\Model\Session as CheckoutSession;
  17. use Magento\Sales\Model\OrderFactory;
  18. use Magento\Paypal\Model\Express\Checkout\Factory as CheckoutFactory;
  19. use Magento\Framework\Session\Generic as PayPalSession;
  20. use Magento\Framework\Url\Helper\Data as UrlHelper;
  21. use Magento\Customer\Model\Url as CustomerUrl;
  22. use Magento\Customer\Model\ResourceModel\CustomerRepository;
  23. use Magento\Quote\Api\CartRepositoryInterface;
  24. use Magento\Quote\Api\GuestCartRepositoryInterface;
  25. use Psr\Log\LoggerInterface;
  26. /**
  27. * Retrieve paypal token
  28. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  29. */
  30. class GetTokenData extends AbstractExpress implements HttpGetActionInterface
  31. {
  32. /**
  33. * Config mode type
  34. *
  35. * @var string
  36. */
  37. protected $_configType = Config::class;
  38. /**
  39. * Config method type
  40. *
  41. * @var string
  42. */
  43. protected $_configMethod = Config::METHOD_WPP_EXPRESS;
  44. /**
  45. * Checkout mode type
  46. *
  47. * @var string
  48. */
  49. protected $_checkoutType = Checkout::class;
  50. /**
  51. * @var \Psr\Log\LoggerInterface
  52. */
  53. private $logger;
  54. /**
  55. * @var CustomerRepository
  56. */
  57. private $customerRepository;
  58. /**
  59. * @var CartRepositoryInterface
  60. */
  61. private $cartRepository;
  62. /**
  63. * @var GuestCartRepositoryInterface
  64. */
  65. private $guestCartRepository;
  66. /**
  67. * @param Context $context
  68. * @param CustomerSession $customerSession
  69. * @param CheckoutSession $checkoutSession
  70. * @param OrderFactory $orderFactory
  71. * @param CheckoutFactory $checkoutFactory
  72. * @param PayPalSession $paypalSession
  73. * @param UrlHelper $urlHelper
  74. * @param CustomerUrl $customerUrl
  75. * @param LoggerInterface $logger
  76. * @param CustomerRepository $customerRepository
  77. * @param CartRepositoryInterface $cartRepository
  78. * @param GuestCartRepositoryInterface $guestCartRepository
  79. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  80. */
  81. public function __construct(
  82. Context $context,
  83. CustomerSession $customerSession,
  84. CheckoutSession $checkoutSession,
  85. OrderFactory $orderFactory,
  86. CheckoutFactory $checkoutFactory,
  87. PayPalSession $paypalSession,
  88. UrlHelper $urlHelper,
  89. CustomerUrl $customerUrl,
  90. LoggerInterface $logger,
  91. CustomerRepository $customerRepository,
  92. CartRepositoryInterface $cartRepository,
  93. GuestCartRepositoryInterface $guestCartRepository
  94. ) {
  95. parent::__construct(
  96. $context,
  97. $customerSession,
  98. $checkoutSession,
  99. $orderFactory,
  100. $checkoutFactory,
  101. $paypalSession,
  102. $urlHelper,
  103. $customerUrl
  104. );
  105. $this->logger = $logger;
  106. $this->customerRepository = $customerRepository;
  107. $this->cartRepository = $cartRepository;
  108. $this->guestCartRepository = $guestCartRepository;
  109. }
  110. /**
  111. * Get token data
  112. *
  113. * @return ResultInterface
  114. */
  115. public function execute(): ResultInterface
  116. {
  117. $controllerResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  118. $responseContent = [
  119. 'success' => true,
  120. 'error_message' => '',
  121. ];
  122. try {
  123. $token = $this->getToken();
  124. if ($token === null) {
  125. $token = false;
  126. }
  127. $this->_initToken($token);
  128. $responseContent['token'] = $token;
  129. } catch (LocalizedException $exception) {
  130. $this->logger->critical($exception);
  131. $responseContent['success'] = false;
  132. $responseContent['error_message'] = $exception->getMessage();
  133. } catch (\Exception $exception) {
  134. $this->logger->critical($exception);
  135. $responseContent['success'] = false;
  136. $responseContent['error_message'] = __('Sorry, but something went wrong');
  137. }
  138. return $controllerResult->setData($responseContent);
  139. }
  140. /**
  141. * Get paypal token
  142. *
  143. * @return string|null
  144. * @throws LocalizedException
  145. */
  146. private function getToken(): ?string
  147. {
  148. $quoteId = $this->getRequest()->getParam('quote_id');
  149. $customerId = $this->getRequest()->getParam('customer_id') ?: $this->_customerSession->getId();
  150. $hasButton = (bool)$this->getRequest()->getParam(Checkout::PAYMENT_INFO_BUTTON);
  151. if ($quoteId) {
  152. $quote = $customerId ? $this->cartRepository->get($quoteId) : $this->guestCartRepository->get($quoteId);
  153. } else {
  154. $quote = $this->_getQuote();
  155. }
  156. $this->_initCheckout($quote);
  157. if ($quote->getIsMultiShipping()) {
  158. $quote->setIsMultiShipping(0);
  159. $quote->removeAllAddresses();
  160. }
  161. if ($customerId) {
  162. $customerData = $this->customerRepository->getById((int)$customerId);
  163. $this->_checkout->setCustomerWithAddressChange(
  164. $customerData,
  165. $quote->getBillingAddress(),
  166. $quote->getShippingAddress()
  167. );
  168. }
  169. // giropay urls
  170. $this->_checkout->prepareGiropayUrls(
  171. $this->_url->getUrl('checkout/onepage/success'),
  172. $this->_url->getUrl('paypal/express/cancel'),
  173. $this->_url->getUrl('checkout/onepage/success')
  174. );
  175. return $this->_checkout->start(
  176. $this->_url->getUrl('*/*/return'),
  177. $this->_url->getUrl('*/*/cancel'),
  178. $hasButton
  179. );
  180. }
  181. }