Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentInformationManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Checkout\Model;
8 
10 
15 {
21 
26 
30  protected $cartManagement;
31 
36 
41 
45  private $logger;
46 
50  private $cartRepository;
51 
60  public function __construct(
61  \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement,
62  \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
63  \Magento\Quote\Api\CartManagementInterface $cartManagement,
64  \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory,
66  ) {
67  $this->billingAddressManagement = $billingAddressManagement;
68  $this->paymentMethodManagement = $paymentMethodManagement;
69  $this->cartManagement = $cartManagement;
70  $this->paymentDetailsFactory = $paymentDetailsFactory;
71  $this->cartTotalsRepository = $cartTotalsRepository;
72  }
73 
78  $cartId,
79  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
81  ) {
82  $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
83  try {
84  $orderId = $this->cartManagement->placeOrder($cartId);
85  } catch (\Magento\Framework\Exception\LocalizedException $e) {
86  throw new CouldNotSaveException(
87  __($e->getMessage()),
88  $e
89  );
90  } catch (\Exception $e) {
91  $this->getLogger()->critical($e);
92  throw new CouldNotSaveException(
93  __('A server error stopped your order from being placed. Please try to place your order again.'),
94  $e
95  );
96  }
97  return $orderId;
98  }
99 
103  public function savePaymentInformation(
104  $cartId,
105  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
107  ) {
108  if ($billingAddress) {
110  $quoteRepository = $this->getCartRepository();
112  $quote = $quoteRepository->getActive($cartId);
113  $quote->removeAddress($quote->getBillingAddress()->getId());
114  $quote->setBillingAddress($billingAddress);
115  $quote->setDataChanges(true);
116  $shippingAddress = $quote->getShippingAddress();
117  if ($shippingAddress && $shippingAddress->getShippingMethod()) {
118  $shippingDataArray = explode('_', $shippingAddress->getShippingMethod());
119  $shippingCarrier = array_shift($shippingDataArray);
120  $shippingAddress->setLimitCarrier($shippingCarrier);
121  }
122  }
123  $this->paymentMethodManagement->set($cartId, $paymentMethod);
124  return true;
125  }
126 
130  public function getPaymentInformation($cartId)
131  {
133  $paymentDetails = $this->paymentDetailsFactory->create();
134  $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
135  $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
136  return $paymentDetails;
137  }
138 
145  private function getLogger()
146  {
147  if (!$this->logger) {
148  $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
149  }
150  return $this->logger;
151  }
152 
159  private function getCartRepository()
160  {
161  if (!$this->cartRepository) {
162  $this->cartRepository = \Magento\Framework\App\ObjectManager::getInstance()
163  ->get(\Magento\Quote\Api\CartRepositoryInterface::class);
164  }
165  return $this->cartRepository;
166  }
167 }
savePaymentInformation( $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
$billingAddress
Definition: order.php:25
$quote
$shippingAddress
Definition: order.php:40
__()
Definition: __.php:13
$cartId
Definition: quote.php:22
__construct(\Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement, \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement, \Magento\Quote\Api\CartManagementInterface $cartManagement, \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory, \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository)
savePaymentInformationAndPlaceOrder( $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
$quoteRepository