Skip to content

Commit

Permalink
Added potential fix for missing email issue (#156)
Browse files Browse the repository at this point in the history
* Defensive code to protect against rare Magneto issue involving corrupted quote table

Co-authored-by: Andrii Onufriichuk <[email protected]>
  • Loading branch information
andrii-onufriichuk and Andrii Onufriichuk authored Aug 14, 2024
1 parent 624a266 commit b0d717d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Model/OrderDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -47,6 +48,9 @@ class OrderDataBuilder
/** @var QuoteValidator */
private $quoteValidator;

/** @var Capture */
private $captureService;

/**
* @param AddressRepositoryInterface $customerAddressRepository
* @param UrlInterface $urlBuilder
Expand All @@ -56,6 +60,7 @@ class OrderDataBuilder
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param QuoteValidator $quoteValidator
* @param Payment $paymentResource
* @param Capture $captureService
*/
public function __construct(
AddressRepositoryInterface $customerAddressRepository,
Expand All @@ -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;
Expand All @@ -75,6 +81,7 @@ public function __construct(
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->quoteValidator = $quoteValidator;
$this->paymentResource = $paymentResource;
$this->captureService = $captureService;
}

/**
Expand All @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions Service/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b0d717d

Please sign in to comment.