billingAddressManagement = $billingAddressManagement; $this->paymentMethodManagement = $paymentMethodManagement; $this->cartManagement = $cartManagement; $this->paymentDetailsFactory = $paymentDetailsFactory; $this->cartTotalsRepository = $cartTotalsRepository; } /** * {@inheritDoc} */ public function savePaymentInformationAndPlaceOrder( $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress); try { $orderId = $this->cartManagement->placeOrder($cartId); } catch (\Magento\Framework\Exception\LocalizedException $e) { throw new CouldNotSaveException( __($e->getMessage()), $e ); } catch (\Exception $e) { $this->getLogger()->critical($e); throw new CouldNotSaveException( __('An error occurred on the server. Please try to place the order again.'), $e ); } return $orderId; } /** * {@inheritDoc} */ public function savePaymentInformation( $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { if ($billingAddress) { /** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */ $quoteRepository = $this->getCartRepository(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $quoteRepository->getActive($cartId); $quote->removeAddress($quote->getBillingAddress()->getId()); $quote->setBillingAddress($billingAddress); $quote->setDataChanges(true); $shippingAddress = $quote->getShippingAddress(); if ($shippingAddress && $shippingAddress->getShippingMethod()) { $shippingDataArray = explode('_', $shippingAddress->getShippingMethod()); $shippingCarrier = array_shift($shippingDataArray); $shippingAddress->setLimitCarrier($shippingCarrier); } } $this->paymentMethodManagement->set($cartId, $paymentMethod); return true; } /** * {@inheritDoc} */ public function getPaymentInformation($cartId) { /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */ $paymentDetails = $this->paymentDetailsFactory->create(); $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId)); $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId)); return $paymentDetails; } /** * Get logger instance * * @return \Psr\Log\LoggerInterface * @deprecated */ private function getLogger() { if (!$this->logger) { $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class); } return $this->logger; } /** * Get Cart repository * * @return \Magento\Quote\Api\CartRepositoryInterface * @deprecated */ private function getCartRepository() { if (!$this->cartRepository) { $this->cartRepository = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Quote\Api\CartRepositoryInterface::class); } return $this->cartRepository; } }