cartRepository = $cartRepository; $this->urlBuilder = $urlBuilder; $this->guestCartRepository = $guestCartRepository; } /** * Place order or redirect on Paypal review page * * @return ResultInterface */ public function execute(): ResultInterface { $controllerResult = $this->resultFactory->create(ResultFactory::TYPE_JSON); $quoteId = $this->getRequest()->getParam('quoteId'); $payerId = $this->getRequest()->getParam('payerId'); $tokenId = $this->getRequest()->getParam('paymentToken'); $customerId = $this->getRequest()->getParam('customerId') ?: $this->_customerSession->getId(); try { if ($quoteId) { $quote = $customerId ? $this->cartRepository->get($quoteId) : $this->guestCartRepository->get($quoteId); } else { $quote = $this->_getQuote(); } $responseContent = [ 'success' => true, 'error_message' => '', ]; /** Populate checkout object with new data */ $this->_initCheckout($quote); /** Populate quote with information about billing and shipping addresses*/ $this->_checkout->returnFromPaypal($tokenId, $payerId); if ($this->_checkout->canSkipOrderReviewStep()) { $this->_checkout->place($tokenId); $order = $this->_checkout->getOrder(); /** "last successful quote" */ $this->_getCheckoutSession()->setLastQuoteId($quote->getId())->setLastSuccessQuoteId($quote->getId()); $this->_getCheckoutSession()->setLastOrderId($order->getId()) ->setLastRealOrderId($order->getIncrementId()) ->setLastOrderStatus($order->getStatus()); $this->_eventManager->dispatch( 'paypal_express_place_order_success', [ 'order' => $order, 'quote' => $quote ] ); $responseContent['redirectUrl'] = $this->urlBuilder->getUrl('checkout/onepage/success/'); } else { $responseContent['redirectUrl'] = $this->urlBuilder->getUrl('paypal/express/review'); $this->_checkoutSession->setQuoteId($quote->getId()); } } catch (ApiProcessableException $e) { $responseContent['success'] = false; $responseContent['error_message'] = $e->getUserMessage(); } catch (\Magento\Framework\Exception\LocalizedException $e) { $responseContent['success'] = false; $responseContent['error_message'] = $e->getMessage(); } catch (\Exception $e) { $responseContent['success'] = false; $responseContent['error_message'] = __('We can\'t process Express Checkout approval.'); } return $controllerResult->setData($responseContent); } }