Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
[change] (MAGE2-311) Fetch last order instead of first to make sure p…
Browse files Browse the repository at this point in the history
…revious, cancelled orders are ignored and only the last (supposedly) valid order is used.
  • Loading branch information
sixer1182 committed May 8, 2020
1 parent 31f8a0f commit eca525b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Controller/Index/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function execute()
$quote = $this->quoteRepository->get($transactionId);

// create order if it doesn't exists already.
$order = $this->orderHelper->fetchOrder($transactionId);
$order = $this->orderHelper->fetchOrderByQuoteId($transactionId);
if ($order === null || $order->isEmpty()) {
$transactionData = $this->_paymentHelper->getDataFromResponse($pushResponse);
$this->_paymentHelper->saveHeidelpayTransaction($pushResponse, $transactionData, 'PUSH');
Expand Down
12 changes: 10 additions & 2 deletions Controller/Index/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Heidelpay\Gateway\Controller\HgwAbstract;
use Heidelpay\Gateway\Helper\Order as OrderHelper;
use Heidelpay\Gateway\Helper\Payment as HeidelpayHelper;
use Heidelpay\Gateway\Model\ResourceModel\Transaction\CollectionFactory;
use Heidelpay\Gateway\Model\Transaction as HeidelpayTransaction;
Expand Down Expand Up @@ -50,6 +51,9 @@ class Redirect extends HgwAbstract
/** @var CollectionFactory */
private $transactionCollectionFactory;

/** @var OrderHelper */
private $orderHelper;

/**
* heidelpay Redirect constructor.
*
Expand All @@ -69,6 +73,7 @@ class Redirect extends HgwAbstract
* @param Encryptor $encryptor
* @param Url $customerUrl
* @param CollectionFactory $transactionCollectionFactory
* @param OrderHelper $orderHelper
*/
public function __construct(
Context $context,
Expand All @@ -86,7 +91,8 @@ public function __construct(
OrderCommentSender $orderCommentSender,
Encryptor $encryptor,
Url $customerUrl,
CollectionFactory $transactionCollectionFactory
CollectionFactory $transactionCollectionFactory,
OrderHelper $orderHelper
) {
parent::__construct(
$context,
Expand All @@ -107,6 +113,7 @@ public function __construct(
);

$this->transactionCollectionFactory = $transactionCollectionFactory;
$this->orderHelper = $orderHelper;
}

/**
Expand Down Expand Up @@ -156,7 +163,8 @@ public function execute()
/** @var Order $order */
$order = null;
try {
$order = $this->_orderFactory->create()->loadByAttribute('quote_id', $quoteId);
$this->_logger->warning('Load order by QuoteId: ' . $quoteId);
$order = $this->orderHelper->fetchOrderByQuoteId($quoteId);
} catch (Exception $e) {
$this->_logger->error(
'Heidelpay - Redirect: Cannot receive order.' . $e->getMessage()
Expand Down
26 changes: 22 additions & 4 deletions Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Data\Collection;
use Magento\Framework\DataObject;
use Magento\Sales\Helper\Data as SalesHelper;
use Magento\Sales\Model\Order as MagentoOrder;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
Expand Down Expand Up @@ -117,15 +119,31 @@ public function handleOrderMail($order)
}

/**
* @param $transactionId
* @return MagentoOrder
* Returns the last Order to the quote with the given quoteId or an empty order object.
*
* @param $quoteId
* @return MagentoOrder|DataObject
*/
public function fetchOrder($transactionId)
public function fetchOrderByQuoteId($quoteId)
{
$criteria = $this->searchCriteriaBuilder->addFilter('quote_id', $transactionId)->create();
$orderList = $this->fetchOrdersByQuoteId($quoteId);

return $orderList->getLastItem();
}

/**
* @param $quoteId
* @return Collection
*/
protected function fetchOrdersByQuoteId($quoteId): Collection
{
$criteria = $this->searchCriteriaBuilder->addFilter('quote_id', $quoteId)->create();

/** @var Collection $orderList */
$orderList = $this->orderRepository->getList($criteria);
return $orderList;
}


return $orderList->getFirstItem();
}
Expand Down
7 changes: 4 additions & 3 deletions Helper/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,10 @@ public function handleOrderCreation($quote, $context = null)
$this->lockManager->lock($lockName);
try{
/** @var Order $order */
$order = $this->orderHelper->fetchOrder($quote->getId());
// Ensure to use the currency of the quote.
if ($order === null || $order->isEmpty()) {
$order = $this->orderHelper->fetchOrderByQuoteId($quote->getId());

if ($order === null || $order->isEmpty() || $order->isCanceled()) {
// Ensure to use the currency of the quote.
$quote->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
$quote->collectTotals();
// in case of guest checkout, set some customer related data.
Expand Down
6 changes: 3 additions & 3 deletions Test/Integration/Controller/Index/PushHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function PushCreatesNewTransaction($paymentCode, $paymentMethod)
$this->assertQuoteActive($quote->getId(), false);

/** @var Order $order */
$fetchedOrder = $this->orderHelper->fetchOrder($quote->getId());
$fetchedOrder = $this->orderHelper->fetchOrderByQuoteId($quote->getId());
$this->assertFalse($fetchedOrder->isEmpty(), 'Order creation failed: Order is empty');

$uniqueId = $xml->Transaction->Identification->UniqueID;
Expand Down Expand Up @@ -200,7 +200,7 @@ public function CreateNoOrderFromInvalidTransactionTypes($paymentCode, $paymentM
$this->assertQuoteActive($quote->getId(), true);

/** @var Order $order */
$fetchedOrder = $this->orderHelper->fetchOrder($quote->getId());
$fetchedOrder = $this->orderHelper->fetchOrderByQuoteId($quote->getId());
$this->assertTrue($fetchedOrder->isEmpty(), 'No Order should be created here');

// Check Transaction
Expand Down Expand Up @@ -322,7 +322,7 @@ private function assertMagentoTransactionExists($uniqueId)
private function assertQuoteHasNoOrder(Quote $quote)
{
/** @var Order $fetchedOrder */
$fetchedOrder = $this->orderHelper->fetchOrder($quote->getId());
$fetchedOrder = $this->orderHelper->fetchOrderByQuoteId($quote->getId());
$this->assertTrue($fetchedOrder->isEmpty());
$this->assertNotNull($quote);
}
Expand Down

0 comments on commit eca525b

Please sign in to comment.