![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryCheckout/Plugin/Model/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See COPYING.txt for license details. */ namespace Ecombricks\InventoryCheckout\Plugin\Model; /** * Payment information management plugin */ class PaymentInformationManagement { /** * Payment method management * * @var \Magento\Quote\Api\PaymentMethodManagementInterface */ protected $paymentMethodManagement; /** * Cart repository * * @var \Magento\Quote\Api\CartRepositoryInterface */ protected $cartRepository; /** * Payment rate limiter * * @var \Magento\Checkout\Api\PaymentProcessingRateLimiterInterface */ protected $paymentRateLimiter; /** * Product metadata * * @var \Magento\Framework\App\ProductMetadata */ protected $productMetadata; /** * Constructor * * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement * @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository * @param \Magento\Framework\App\ProductMetadata $productMetadata * @return void */ public function __construct( \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement, \Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Magento\Framework\App\ProductMetadata $productMetadata ) { $this->paymentMethodManagement = $paymentMethodManagement; $this->cartRepository = $cartRepository; $this->productMetadata = $productMetadata; if ( version_compare($this->productMetadata->getVersion(), '2.4.1', '>=') || ( version_compare($this->productMetadata->getVersion(), '2.3.6', '>=') && version_compare($this->productMetadata->getVersion(), '2.4.0', '<') ) ) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->paymentRateLimiter = $objectManager->get(\Magento\Checkout\Api\PaymentProcessingRateLimiterInterface::class); } } /** * Save payment information * * @param integer $cartId * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress * @return int * @throws \Magento\Framework\Exception\CouldNotSaveException */ protected function savePaymentInformation( $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { if ( version_compare($this->productMetadata->getVersion(), '2.4.1', '>=') || ( version_compare($this->productMetadata->getVersion(), '2.3.6', '>=') && version_compare($this->productMetadata->getVersion(), '2.4.0', '<') ) ) { $this->paymentRateLimiter->limit(); } if (empty($billingAddress)) { $this->paymentMethodManagement->set($cartId, $paymentMethod); return true; } $quote = $this->cartRepository->getActive($cartId); if (version_compare($this->productMetadata->getVersion(), '2.3.2', '>=')) { $customerId = $quote->getBillingAddress()->getCustomerId(); if (!$billingAddress->getCustomerId() && $customerId) { $billingAddress->setCustomerId($customerId); } } $quote->removeAddress($quote->getBillingAddress()->getId()); $quote->setBillingAddress($billingAddress); $quote->setDataChanges(true); $shippingAddress = $quote->getShippingAddress(); $shippingMethods = ($shippingAddress) ? $shippingAddress->getShippingMethod() : []; if (!empty($shippingMethods)) { $limitCarriers = []; foreach ($shippingMethods as $sourceCode => $shippingMethod) { $sourceShippingRate = $shippingAddress->getSourceShippingRateByCode($sourceCode, $shippingMethod); $limitCarriers[$sourceCode] = $sourceShippingRate ? $sourceShippingRate->getCarrier() : $shippingMethod; } $shippingAddress->setLimitCarrier($limitCarriers); } $this->paymentMethodManagement->set($cartId, $paymentMethod); return true; } /** * Around save payment information * * @param \Magento\Checkout\Model\PaymentInformationManagement $subject * @param \Closure $proceed * @param integer $cartId * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress * @return int * @throws \Magento\Framework\Exception\CouldNotSaveException */ public function aroundSavePaymentInformation( \Magento\Checkout\Model\PaymentInformationManagement $subject, \Closure $proceed, $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null ) { return $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress); } }