Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GuestPaymentInformationManagement.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 namespace Magento\Checkout\Model;
9 
15 
20 {
21 
26 
31 
35  protected $cartManagement;
36 
41 
46 
50  protected $cartRepository;
51 
55  private $logger;
56 
60  private $connectionPool;
61 
72  public function __construct(
73  \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement,
74  \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement,
75  \Magento\Quote\Api\GuestCartManagementInterface $cartManagement,
76  \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement,
77  \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
79  ResourceConnection $connectionPool = null
80  ) {
81  $this->billingAddressManagement = $billingAddressManagement;
82  $this->paymentMethodManagement = $paymentMethodManagement;
83  $this->cartManagement = $cartManagement;
84  $this->paymentInformationManagement = $paymentInformationManagement;
85  $this->quoteIdMaskFactory = $quoteIdMaskFactory;
86  $this->cartRepository = $cartRepository;
87  $this->connectionPool = $connectionPool ?: ObjectManager::getInstance()->get(ResourceConnection::class);
88  }
89 
94  $cartId,
95  $email,
96  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
98  ) {
99  $salesConnection = $this->connectionPool->getConnection('sales');
100  $checkoutConnection = $this->connectionPool->getConnection('checkout');
101  $salesConnection->beginTransaction();
102  $checkoutConnection->beginTransaction();
103 
104  try {
105  $this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
106  try {
107  $orderId = $this->cartManagement->placeOrder($cartId);
108  } catch (\Magento\Framework\Exception\LocalizedException $e) {
109  throw new CouldNotSaveException(
110  __($e->getMessage()),
111  $e
112  );
113  } catch (\Exception $e) {
114  $this->getLogger()->critical($e);
115  throw new CouldNotSaveException(
116  __('An error occurred on the server. Please try to place the order again.'),
117  $e
118  );
119  }
120  $salesConnection->commit();
121  $checkoutConnection->commit();
122  } catch (\Exception $e) {
123  $salesConnection->rollBack();
124  $checkoutConnection->rollBack();
125  throw $e;
126  }
127 
128  return $orderId;
129  }
130 
134  public function savePaymentInformation(
135  $cartId,
136  $email,
137  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
139  ) {
140  $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
142  $quote = $this->cartRepository->getActive($quoteIdMask->getQuoteId());
143 
144  if ($billingAddress) {
145  $billingAddress->setEmail($email);
146  $quote->removeAddress($quote->getBillingAddress()->getId());
147  $quote->setBillingAddress($billingAddress);
148  $quote->setDataChanges(true);
149  } else {
150  $quote->getBillingAddress()->setEmail($email);
151  }
152  $this->limitShippingCarrier($quote);
153 
154  $this->paymentMethodManagement->set($cartId, $paymentMethod);
155  return true;
156  }
157 
162  {
163  $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
164  return $this->paymentInformationManagement->getPaymentInformation($quoteIdMask->getQuoteId());
165  }
166 
173  private function getLogger()
174  {
175  if (!$this->logger) {
176  $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
177  }
178  return $this->logger;
179  }
180 
189  private function limitShippingCarrier(Quote $quote) : void
190  {
191  $shippingAddress = $quote->getShippingAddress();
192  if ($shippingAddress && $shippingAddress->getShippingMethod()) {
193  $shippingDataArray = explode('_', $shippingAddress->getShippingMethod());
194  $shippingCarrier = array_shift($shippingDataArray);
195  $shippingAddress->setLimitCarrier($shippingCarrier);
196  }
197  }
198 }
$billingAddress
Definition: order.php:25
$email
Definition: details.phtml:13
$quote
$shippingAddress
Definition: order.php:40
savePaymentInformationAndPlaceOrder( $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
__()
Definition: __.php:13
$cartId
Definition: quote.php:22
savePaymentInformation( $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
__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)