billingAddressManagement = $billingAddressManagement; $this->paymentMethodManagement = $paymentMethodManagement; $this->cartManagement = $cartManagement; $this->paymentInformationManagement = $paymentInformationManagement; $this->quoteIdMaskFactory = $quoteIdMaskFactory; $this->cartRepository = $cartRepository; $this->connectionPool = $connectionPool ?: ObjectManager::getInstance()->get(ResourceConnection::class); } /** * {@inheritDoc} */ public function savePaymentInformationAndPlaceOrder( $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { $salesConnection = $this->connectionPool->getConnection('sales'); $checkoutConnection = $this->connectionPool->getConnection('checkout'); $salesConnection->beginTransaction(); $checkoutConnection->beginTransaction(); try { $this->savePaymentInformation($cartId, $email, $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 ); } $salesConnection->commit(); $checkoutConnection->commit(); } catch (\Exception $e) { $salesConnection->rollBack(); $checkoutConnection->rollBack(); throw $e; } return $orderId; } /** * {@inheritDoc} */ public function savePaymentInformation( $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); /** @var Quote $quote */ $quote = $this->cartRepository->getActive($quoteIdMask->getQuoteId()); if ($billingAddress) { $billingAddress->setEmail($email); $quote->removeAddress($quote->getBillingAddress()->getId()); $quote->setBillingAddress($billingAddress); $quote->setDataChanges(true); } else { $quote->getBillingAddress()->setEmail($email); } $this->limitShippingCarrier($quote); $this->paymentMethodManagement->set($cartId, $paymentMethod); return true; } /** * {@inheritDoc} */ public function getPaymentInformation($cartId) { $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); return $this->paymentInformationManagement->getPaymentInformation($quoteIdMask->getQuoteId()); } /** * Get logger instance * * @return \Psr\Log\LoggerInterface * @deprecated 100.1.8 */ private function getLogger() { if (!$this->logger) { $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class); } return $this->logger; } /** * Limits shipping rates request by carrier from shipping address. * * @param Quote $quote * * @return void * @see \Magento\Shipping\Model\Shipping::collectRates */ private function limitShippingCarrier(Quote $quote) : void { $shippingAddress = $quote->getShippingAddress(); if ($shippingAddress && $shippingAddress->getShippingMethod()) { $shippingDataArray = explode('_', $shippingAddress->getShippingMethod()); $shippingCarrier = array_shift($shippingDataArray); $shippingAddress->setLimitCarrier($shippingCarrier); } } }