-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bugfix/ignore-expired-orders' into release-week-19
- Loading branch information
Showing
14 changed files
with
528 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Mollie\Payment\Model\Client\Orders\Processors; | ||
|
||
use Magento\Sales\Api\Data\OrderInterface; | ||
use Mollie\Api\Resources\Order; | ||
use Mollie\Payment\Model\Client\OrderProcessorInterface; | ||
use Mollie\Payment\Model\Client\ProcessTransactionResponse; | ||
use Mollie\Payment\Model\Client\ProcessTransactionResponseFactory; | ||
use Mollie\Payment\Service\Order\ExpiredOrderToTransaction; | ||
|
||
class ExpiredProcessor implements OrderProcessorInterface | ||
{ | ||
/** | ||
* @var CancelledProcessor | ||
*/ | ||
private $cancelledProcessor; | ||
|
||
/** | ||
* @var ProcessTransactionResponseFactory | ||
*/ | ||
private $processTransactionResponseFactory; | ||
|
||
/** | ||
* @var ExpiredOrderToTransaction | ||
*/ | ||
private $expiredOrderToTransaction; | ||
|
||
public function __construct( | ||
CancelledProcessor $cancelledProcessor, | ||
ExpiredOrderToTransaction $expiredOrderToTransaction, | ||
ProcessTransactionResponseFactory $processTransactionResponseFactory | ||
) { | ||
$this->cancelledProcessor = $cancelledProcessor; | ||
$this->processTransactionResponseFactory = $processTransactionResponseFactory; | ||
$this->expiredOrderToTransaction = $expiredOrderToTransaction; | ||
} | ||
|
||
public function process( | ||
OrderInterface $magentoOrder, | ||
Order $mollieOrder, | ||
string $type, | ||
ProcessTransactionResponse $response | ||
): ?ProcessTransactionResponse { | ||
if ($this->shouldCancelProcessing($magentoOrder)) { | ||
return $this->processTransactionResponseFactory->create([ | ||
'success' => false, | ||
'status' => $mollieOrder->status, | ||
'order_id' => $magentoOrder->getEntityId(), | ||
'type' => $type | ||
]); | ||
} | ||
|
||
return $this->cancelledProcessor->process($magentoOrder, $mollieOrder, $type, $response); | ||
} | ||
|
||
private function shouldCancelProcessing(OrderInterface $order): bool | ||
{ | ||
if (!$this->expiredOrderToTransaction->hasMultipleTransactions($order)) { | ||
return false; | ||
} | ||
|
||
$this->expiredOrderToTransaction->markTransactionAsSkipped($order->getMollieTransactionId()); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Model/Client/Payments/Processors/ExpiredStatusProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Mollie\Payment\Model\Client\Payments\Processors; | ||
|
||
use Magento\Sales\Api\Data\OrderInterface; | ||
use Mollie\Api\Resources\Payment; | ||
use Mollie\Payment\Model\Client\PaymentProcessorInterface; | ||
use Mollie\Payment\Model\Client\ProcessTransactionResponse; | ||
use Mollie\Payment\Model\Client\ProcessTransactionResponseFactory; | ||
use Mollie\Payment\Service\Order\ExpiredOrderToTransaction; | ||
|
||
class ExpiredStatusProcessor implements PaymentProcessorInterface | ||
{ | ||
/** | ||
* @var ExpiredOrderToTransaction | ||
*/ | ||
private $expiredOrderToTransaction; | ||
/** | ||
* @var FailedStatusProcessor | ||
*/ | ||
private $failedStatusProcessor; | ||
/** | ||
* @var ProcessTransactionResponseFactory | ||
*/ | ||
private $processTransactionResponseFactory; | ||
|
||
public function __construct( | ||
ExpiredOrderToTransaction $expiredOrderToTransaction, | ||
FailedStatusProcessor $failedStatusProcessor, | ||
ProcessTransactionResponseFactory $processTransactionResponseFactory | ||
) { | ||
$this->expiredOrderToTransaction = $expiredOrderToTransaction; | ||
$this->failedStatusProcessor = $failedStatusProcessor; | ||
$this->processTransactionResponseFactory = $processTransactionResponseFactory; | ||
} | ||
|
||
public function process( | ||
OrderInterface $magentoOrder, | ||
Payment $molliePayment, | ||
string $type, | ||
ProcessTransactionResponse $response | ||
): ?ProcessTransactionResponse { | ||
if ($this->shouldCancelProcessing($magentoOrder)) { | ||
return $this->processTransactionResponseFactory->create([ | ||
'success' => false, | ||
'status' => $molliePayment->status, | ||
'order_id' => $magentoOrder->getEntityId(), | ||
'type' => $type | ||
]); | ||
} | ||
|
||
return $this->failedStatusProcessor->process($magentoOrder, $molliePayment, $type, $response); | ||
} | ||
|
||
private function shouldCancelProcessing(OrderInterface $order): bool | ||
{ | ||
if (!$this->expiredOrderToTransaction->hasMultipleTransactions($order)) { | ||
return false; | ||
} | ||
|
||
$this->expiredOrderToTransaction->markTransactionAsSkipped($order->getMollieTransactionId()); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Mollie\Payment\Service\Order; | ||
|
||
use Magento\Framework\Api\SearchCriteriaBuilderFactory; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Sales\Api\Data\OrderInterface; | ||
use Mollie\Payment\Api\Data\TransactionToOrderInterface; | ||
use Mollie\Payment\Model\TransactionToOrderRepository; | ||
|
||
class ExpiredOrderToTransaction | ||
{ | ||
/** | ||
* @var TransactionToOrderRepository | ||
*/ | ||
private $transactionToOrderRepository; | ||
/** | ||
* @var SearchCriteriaBuilderFactory | ||
*/ | ||
private $criteriaBuilderFactory; | ||
|
||
public function __construct( | ||
TransactionToOrderRepository $transactionToOrderRepository, | ||
SearchCriteriaBuilderFactory $criteriaBuilderFactory | ||
) { | ||
$this->transactionToOrderRepository = $transactionToOrderRepository; | ||
$this->criteriaBuilderFactory = $criteriaBuilderFactory; | ||
} | ||
|
||
public function hasMultipleTransactions(OrderInterface $order): bool | ||
{ | ||
$criteria = $this->criteriaBuilderFactory->create(); | ||
$criteria->addFilter('skipped', '0'); | ||
$criteria->addFilter('order_id', $order->getEntityId()); | ||
|
||
$result = $this->transactionToOrderRepository->getList($criteria->create()); | ||
|
||
return $result->getTotalCount() > 1; | ||
} | ||
|
||
public function getByTransactionId(string $transactionId): TransactionToOrderInterface | ||
{ | ||
$criteria = $this->criteriaBuilderFactory->create(); | ||
$criteria->addFilter('transaction_id', $transactionId); | ||
|
||
$result = $this->transactionToOrderRepository->getList($criteria->create()); | ||
|
||
$items = $result->getItems(); | ||
|
||
if (empty($items)) { | ||
throw new NoSuchEntityException(__("Transaction with ID %1 not found", $transactionId)); | ||
} | ||
|
||
return array_shift($items); | ||
} | ||
|
||
/** | ||
* A transaction can be skipped if there are multiple transactions for a single order, and this transaction | ||
* is expired. In that case, we don't want to cancel the order, but we do want to mark the transaction as skipped. | ||
* When the next transaction is also expired, and there are no other transactions left, we will cancel the order. | ||
*/ | ||
public function markTransactionAsSkipped(string $transactionId): void | ||
{ | ||
$transaction = $this->getByTransactionId($transactionId); | ||
$transaction->setSkipped(true); | ||
|
||
$this->transactionToOrderRepository->save($transaction); | ||
} | ||
} |
Oops, something went wrong.