From b0d717d97b12aba9a4fc686477c2325c1449792b Mon Sep 17 00:00:00 2001 From: andrii-onufriichuk <117644505+andrii-onufriichuk@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:38:07 +0300 Subject: [PATCH] Added potential fix for missing email issue (#156) * Defensive code to protect against rare Magneto issue involving corrupted quote table Co-authored-by: Andrii Onufriichuk --- Model/OrderDataBuilder.php | 13 ++++++++++++- Service/Capture.php | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Model/OrderDataBuilder.php b/Model/OrderDataBuilder.php index 7bb44f7a..2ab155a2 100644 --- a/Model/OrderDataBuilder.php +++ b/Model/OrderDataBuilder.php @@ -20,6 +20,7 @@ use Magento\Sales\Api\OrderRepositoryInterface; use Rvvup\Payments\Exception\QuoteValidationException; use Rvvup\Payments\Gateway\Method; +use Rvvup\Payments\Service\Capture; class OrderDataBuilder { @@ -47,6 +48,9 @@ class OrderDataBuilder /** @var QuoteValidator */ private $quoteValidator; + /** @var Capture */ + private $captureService; + /** * @param AddressRepositoryInterface $customerAddressRepository * @param UrlInterface $urlBuilder @@ -56,6 +60,7 @@ class OrderDataBuilder * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param QuoteValidator $quoteValidator * @param Payment $paymentResource + * @param Capture $captureService */ public function __construct( AddressRepositoryInterface $customerAddressRepository, @@ -65,7 +70,8 @@ public function __construct( OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $searchCriteriaBuilder, QuoteValidator $quoteValidator, - Payment $paymentResource + Payment $paymentResource, + Capture $captureService ) { $this->customerAddressRepository = $customerAddressRepository; $this->urlBuilder = $urlBuilder; @@ -75,6 +81,7 @@ public function __construct( $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->quoteValidator = $quoteValidator; $this->paymentResource = $paymentResource; + $this->captureService = $captureService; } /** @@ -89,6 +96,10 @@ public function __construct( */ public function build(CartInterface $quote, bool $express = false, bool $validate = true): array { + if (!$quote->getCustomerEmail()) { + $this->captureService->saveCustomerEmail($quote); + } + $billingAddress = $quote->getBillingAddress(); if ($validate) { diff --git a/Service/Capture.php b/Service/Capture.php index e27c2829..c00f5ff0 100644 --- a/Service/Capture.php +++ b/Service/Capture.php @@ -10,6 +10,7 @@ use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Quote\Api\CartRepositoryInterface; +use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Model\Quote; use Magento\Quote\Model\Quote\Payment; use Magento\Quote\Model\QuoteManagement; @@ -202,6 +203,9 @@ public function createOrder(string $rvvupId, Quote $quote, string $origin): Vali $payment = $quote->getPayment(); try { + if (!$quote->getCustomerEmail()) { + $this->saveCustomerEmail($quote); + } if ($this->orderIncrementChecker->isIncrementIdUsed($quote->getReservedOrderId())) { return $this->validationInterfaceFactory->create( [ @@ -264,6 +268,28 @@ public function createOrder(string $rvvupId, Quote $quote, string $origin): Vali } } + /** + * @param CartInterface $quote + * @return void + */ + public function saveCustomerEmail(CartInterface $quote): void + { + $email = $quote->getBillingAddress()->getEmail(); + + if (!$email) { + $email = $quote->getShippingAddress()->getEmail(); + } + + if (!$email && $quote->getCustomerId()) { + $email = $quote->getCustomer()->getEmail(); + } + + if ($email) { + $quote->setCustomerEmail($email); + $this->cartRepository->save($quote); + } + } + /** * @param Payment $payment * @param string $lastTransactionId