| Current Path : /home/rtorresani/www/vendor/magento/module-checkout/Model/ |
| Current File : //home/rtorresani/www/vendor/magento/module-checkout/Model/GuestPaymentInformationManagement.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Checkout\Model;
use Magento\Checkout\Api\Exception\PaymentProcessingRateLimitExceededException;
use Magento\Checkout\Api\PaymentProcessingRateLimiterInterface;
use Magento\Checkout\Api\PaymentSavingRateLimiterInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Quote\Model\Quote;
use Psr\Log\LoggerInterface as Logger;
/**
* Guest payment information management model.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
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 Logger
*/
private $logger;
/**
* @var PaymentProcessingRateLimiterInterface
*/
private $paymentsRateLimiter;
/**
* @var PaymentSavingRateLimiterInterface
*/
private $savingRateLimiter;
/**
* @var bool
*/
private $saveRateLimitDisabled = false;
/**
* @var AddressComparatorInterface
*/
private $addressComparator;
/**
* @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 Logger $logger
* @param PaymentProcessingRateLimiterInterface|null $paymentsRateLimiter
* @param PaymentSavingRateLimiterInterface|null $savingRateLimiter
* @param AddressComparatorInterface|null $addressComparator
* @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,
Logger $logger,
?PaymentProcessingRateLimiterInterface $paymentsRateLimiter = null,
?PaymentSavingRateLimiterInterface $savingRateLimiter = null,
?AddressComparatorInterface $addressComparator = null
) {
$this->billingAddressManagement = $billingAddressManagement;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->cartManagement = $cartManagement;
$this->paymentInformationManagement = $paymentInformationManagement;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->cartRepository = $cartRepository;
$this->paymentsRateLimiter = $paymentsRateLimiter
?? ObjectManager::getInstance()->get(PaymentProcessingRateLimiterInterface::class);
$this->savingRateLimiter = $savingRateLimiter
?? ObjectManager::getInstance()->get(PaymentSavingRateLimiterInterface::class);
$this->addressComparator = $addressComparator
?? ObjectManager::getInstance()->get(AddressComparatorInterface::class);
$this->logger = $logger;
}
/**
* @inheritdoc
*/
public function savePaymentInformationAndPlaceOrder(
$cartId,
$email,
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
$this->paymentsRateLimiter->limit();
try {
//Have to do this hack because of savePaymentInformation() plugins.
$this->saveRateLimitDisabled = true;
$this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
} finally {
$this->saveRateLimitDisabled = false;
}
try {
$orderId = $this->cartManagement->placeOrder($cartId);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->logger->critical(
'Placing an order with quote_id ' . $cartId . ' is failed: ' . $e->getMessage()
);
throw new CouldNotSaveException(
__($e->getMessage()),
$e
);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new CouldNotSaveException(
__('An error occurred on the server. Please try to place the order again.'),
$e
);
}
return $orderId;
}
/**
* @inheritdoc
*/
public function savePaymentInformation(
$cartId,
$email,
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
if (!$this->saveRateLimitDisabled) {
try {
$this->savingRateLimiter->limit();
} catch (PaymentProcessingRateLimitExceededException $ex) {
//Limit reached
return false;
}
}
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
/** @var Quote $quote */
$quote = $this->cartRepository->getActive($quoteIdMask->getQuoteId());
$shippingAddress = $quote->getShippingAddress();
if ($this->addressComparator->isEqual($shippingAddress, $billingAddress)) {
$shippingAddress->setSameAsBilling(1);
}
if ($billingAddress) {
$billingAddress->setEmail($email);
$quote->removeAddress($quote->getBillingAddress()->getId());
$quote->setBillingAddress($billingAddress);
$quote->setDataChanges(true);
} else {
$quote->getBillingAddress()->setEmail($email);
}
$this->limitShippingCarrier($quote);
if (!(float)$quote->getItemsQty()) {
throw new CouldNotSaveException(__('Some of the products are disabled.'));
}
$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());
}
/**
* 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()) {
$shippingRate = $shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod());
if ($shippingRate) {
$shippingAddress->setLimitCarrier($shippingRate->getCarrier());
}
}
}
}