123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Checkout\Model;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\App\ResourceConnection;
- use Magento\Quote\Api\CartRepositoryInterface;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\Quote\Model\Quote;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPaymentInformationManagementInterface
- {
- /**
- * @var \Magento\Quote\Api\GuestBillingAddressManagementInterface
- */
- protected $billingAddressManagement;
- /**
- * @var \Magento\Quote\Api\GuestPaymentMethodManagementInterface
- */
- protected $paymentMethodManagement;
- /**
- * @var \Magento\Quote\Api\GuestCartManagementInterface
- */
- protected $cartManagement;
- /**
- * @var \Magento\Checkout\Api\PaymentInformationManagementInterface
- */
- protected $paymentInformationManagement;
- /**
- * @var \Magento\Quote\Model\QuoteIdMaskFactory
- */
- protected $quoteIdMaskFactory;
- /**
- * @var CartRepositoryInterface
- */
- protected $cartRepository;
- /**
- * @var \Psr\Log\LoggerInterface
- */
- private $logger;
- /**
- * @var ResourceConnection
- */
- private $connectionPool;
- /**
- * @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
- * @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
- * @param \Magento\Quote\Api\GuestCartManagementInterface $cartManagement
- * @param \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement
- * @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
- * @param CartRepositoryInterface $cartRepository
- * @param ResourceConnection|null
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement,
- \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement,
- \Magento\Quote\Api\GuestCartManagementInterface $cartManagement,
- \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement,
- \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
- CartRepositoryInterface $cartRepository,
- ResourceConnection $connectionPool = null
- ) {
- $this->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);
- }
- }
- }
|