OnAuthorization.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Controller\ResultFactory;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. use Magento\Framework\Controller\ResultInterface;
  11. use Magento\Paypal\Model\Config as PayPalConfig;
  12. use Magento\Paypal\Model\Express\Checkout as PayPalCheckout;
  13. use Magento\Paypal\Model\Api\ProcessableException as ApiProcessableException;
  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\Quote\Api\CartRepositoryInterface;
  23. use Magento\Framework\UrlInterface;
  24. use Magento\Quote\Api\GuestCartRepositoryInterface;
  25. /**
  26. * Processes data after returning from PayPal
  27. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  28. */
  29. class OnAuthorization extends AbstractExpress implements HttpPostActionInterface
  30. {
  31. /**
  32. * @inheritdoc
  33. */
  34. protected $_configType = PayPalConfig::class;
  35. /**
  36. * @inheritdoc
  37. */
  38. protected $_configMethod = PayPalConfig::METHOD_WPP_EXPRESS;
  39. /**
  40. * @inheritdoc
  41. */
  42. protected $_checkoutType = PayPalCheckout::class;
  43. /**
  44. * @var CartRepositoryInterface
  45. */
  46. private $cartRepository;
  47. /**
  48. * Url Builder
  49. *
  50. * @var UrlInterface
  51. */
  52. private $urlBuilder;
  53. /**
  54. * @var GuestCartRepositoryInterface
  55. */
  56. private $guestCartRepository;
  57. /**
  58. * @param Context $context
  59. * @param CustomerSession $customerSession
  60. * @param CheckoutSession $checkoutSession
  61. * @param OrderFactory $orderFactory
  62. * @param CheckoutFactory $checkoutFactory
  63. * @param PayPalSession $paypalSession
  64. * @param UrlHelper $urlHelper
  65. * @param CustomerUrl $customerUrl
  66. * @param CartRepositoryInterface $cartRepository
  67. * @param UrlInterface $urlBuilder
  68. * @param GuestCartRepositoryInterface $guestCartRepository
  69. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  70. */
  71. public function __construct(
  72. Context $context,
  73. CustomerSession $customerSession,
  74. CheckoutSession $checkoutSession,
  75. OrderFactory $orderFactory,
  76. CheckoutFactory $checkoutFactory,
  77. PayPalSession $paypalSession,
  78. UrlHelper $urlHelper,
  79. CustomerUrl $customerUrl,
  80. CartRepositoryInterface $cartRepository,
  81. UrlInterface $urlBuilder,
  82. GuestCartRepositoryInterface $guestCartRepository
  83. ) {
  84. parent::__construct(
  85. $context,
  86. $customerSession,
  87. $checkoutSession,
  88. $orderFactory,
  89. $checkoutFactory,
  90. $paypalSession,
  91. $urlHelper,
  92. $customerUrl
  93. );
  94. $this->cartRepository = $cartRepository;
  95. $this->urlBuilder = $urlBuilder;
  96. $this->guestCartRepository = $guestCartRepository;
  97. }
  98. /**
  99. * Place order or redirect on Paypal review page
  100. *
  101. * @return ResultInterface
  102. */
  103. public function execute(): ResultInterface
  104. {
  105. $controllerResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  106. $quoteId = $this->getRequest()->getParam('quoteId');
  107. $payerId = $this->getRequest()->getParam('payerId');
  108. $tokenId = $this->getRequest()->getParam('paymentToken');
  109. $customerId = $this->getRequest()->getParam('customerId') ?: $this->_customerSession->getId();
  110. try {
  111. if ($quoteId) {
  112. $quote = $customerId ? $this->cartRepository->get($quoteId) : $this->guestCartRepository->get($quoteId);
  113. } else {
  114. $quote = $this->_getQuote();
  115. }
  116. $responseContent = [
  117. 'success' => true,
  118. 'error_message' => '',
  119. ];
  120. /** Populate checkout object with new data */
  121. $this->_initCheckout($quote);
  122. /** Populate quote with information about billing and shipping addresses*/
  123. $this->_checkout->returnFromPaypal($tokenId, $payerId);
  124. if ($this->_checkout->canSkipOrderReviewStep()) {
  125. $this->_checkout->place($tokenId);
  126. $order = $this->_checkout->getOrder();
  127. /** "last successful quote" */
  128. $this->_getCheckoutSession()->setLastQuoteId($quote->getId())->setLastSuccessQuoteId($quote->getId());
  129. $this->_getCheckoutSession()->setLastOrderId($order->getId())
  130. ->setLastRealOrderId($order->getIncrementId())
  131. ->setLastOrderStatus($order->getStatus());
  132. $this->_eventManager->dispatch(
  133. 'paypal_express_place_order_success',
  134. [
  135. 'order' => $order,
  136. 'quote' => $quote
  137. ]
  138. );
  139. $responseContent['redirectUrl'] = $this->urlBuilder->getUrl('checkout/onepage/success/');
  140. } else {
  141. $responseContent['redirectUrl'] = $this->urlBuilder->getUrl('paypal/express/review');
  142. $this->_checkoutSession->setQuoteId($quote->getId());
  143. }
  144. } catch (ApiProcessableException $e) {
  145. $responseContent['success'] = false;
  146. $responseContent['error_message'] = $e->getUserMessage();
  147. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  148. $responseContent['success'] = false;
  149. $responseContent['error_message'] = $e->getMessage();
  150. } catch (\Exception $e) {
  151. $responseContent['success'] = false;
  152. $responseContent['error_message'] = __('We can\'t process Express Checkout approval.');
  153. }
  154. return $controllerResult->setData($responseContent);
  155. }
  156. }