123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Model;
- use Magento\Framework\Exception\CouldNotSaveException;
- /**
- * Payment information management
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInformationManagementInterface
- {
- /**
- * @var \Magento\Quote\Api\BillingAddressManagementInterface
- * @deprecated 100.1.0 This call was substituted to eliminate extra quote::save call
- */
- protected $billingAddressManagement;
- /**
- * @var \Magento\Quote\Api\PaymentMethodManagementInterface
- */
- protected $paymentMethodManagement;
- /**
- * @var \Magento\Quote\Api\CartManagementInterface
- */
- protected $cartManagement;
- /**
- * @var PaymentDetailsFactory
- */
- protected $paymentDetailsFactory;
- /**
- * @var \Magento\Quote\Api\CartTotalRepositoryInterface
- */
- protected $cartTotalsRepository;
- /**
- * @var \Psr\Log\LoggerInterface
- */
- private $logger;
- /**
- * @var \Magento\Quote\Api\CartRepositoryInterface
- */
- private $cartRepository;
- /**
- * @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
- * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
- * @param \Magento\Quote\Api\CartManagementInterface $cartManagement
- * @param PaymentDetailsFactory $paymentDetailsFactory
- * @param \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
- * @codeCoverageIgnore
- */
- public function __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
- ) {
- $this->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(
- __('A server error stopped your order from being placed. Please try to place your 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()) {
- $shippingRate = $shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod());
- $shippingAddress->setLimitCarrier(
- $shippingRate ? $shippingRate->getCarrier() : $shippingAddress->getShippingMethod()
- );
- }
- }
- $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 100.1.8
- */
- 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 100.2.0
- */
- private function getCartRepository()
- {
- if (!$this->cartRepository) {
- $this->cartRepository = \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Quote\Api\CartRepositoryInterface::class);
- }
- return $this->cartRepository;
- }
- }
|