Skip to content

Commit

Permalink
NTR: fix order expire for sepa (#910)
Browse files Browse the repository at this point in the history
* NTR: fix order expire for sepa

* NTR: fix order expire

* NTR: Cs fix

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Dec 9, 2024
1 parent ccde869 commit e2f6ced
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/Components/OrderExpiration/ExpireAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Kiener\MolliePayments\Components\OrderExpiration;

use Kiener\MolliePayments\Handler\Method\BankTransferPayment;
use Kiener\MolliePayments\Repository\Order\OrderRepositoryInterface;
use Kiener\MolliePayments\Repository\SalesChannel\SalesChannelRepositoryInterface;
use Kiener\MolliePayments\Service\Order\OrderExpireService;
Expand Down Expand Up @@ -74,7 +75,7 @@ private function expireOrdersInSalesChannel(SalesChannelEntity $salesChannelEnti
$this->logger->info('Start expire orders for saleschannel', ['salesChannel' => $salesChannelEntity->getName()]);

$date = new \DateTime();
$date->modify('-2 months');
$date->modify(sprintf('-%d days', (BankTransferPayment::DUE_DATE_MAX_DAYS + 1)));

$criteria = new Criteria();
$criteria->addAssociation('transactions.stateMachineState');
Expand All @@ -85,7 +86,9 @@ private function expireOrdersInSalesChannel(SalesChannelEntity $salesChannelEnti
$criteria->addSorting(new FieldSorting('orderDateTime', FieldSorting::DESCENDING));
$criteria->setLimit(10);

$this->logger->debug('Search for orders with payment status in progress');
$this->logger->debug('Search for orders with payment status in progress older than date', [
'date' => $date->format(Defaults::STORAGE_DATE_TIME_FORMAT),
]);

$searchResult = $this->orderRepository->search($criteria, $context);
if ($searchResult->count() === 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@
<argument type="service" id="Kiener\MolliePayments\Service\UrlParsingService"/>
</service>

<service id="Kiener\MolliePayments\Service\Order\OrderTimeService"/>
<service id="Kiener\MolliePayments\Service\Order\OrderTimeService">
<argument type="service" id="mollie_payments.logger"/>
</service>

<service id="Kiener\MolliePayments\Service\Order\OrderStatusUpdater">
<argument type="service" id="Kiener\MolliePayments\Service\Order\OrderStateService"/>
Expand Down
6 changes: 5 additions & 1 deletion src/Service/Order/OrderExpireService.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ public function cancelExpiredOrders(OrderCollection $orders, Context $context):
$finalizeTransactionTimeInMinutes = $settings->getPaymentFinalizeTransactionTime();

if ($this->orderUsesSepaPayment($lastTransaction)) {
$finalizeTransactionTimeInMinutes = (int)ceil($settings->getPaymentMethodBankTransferDueDateDays() / 24 / 60);
$bankTransferDueDays = $settings->getPaymentMethodBankTransferDueDateDays();
if ($bankTransferDueDays === null) {
$bankTransferDueDays = BankTransferPayment::DUE_DATE_MIN_DAYS;
}
$finalizeTransactionTimeInMinutes = 60 * 60 * 24 * $bankTransferDueDays;
}

if ($this->orderTimeService->isOrderAgeGreaterThan($order, $finalizeTransactionTimeInMinutes) === false) {
Expand Down
19 changes: 17 additions & 2 deletions src/Service/Order/OrderTimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
namespace Kiener\MolliePayments\Service\Order;

use DateTime;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Defaults;

class OrderTimeService
{
Expand All @@ -14,9 +16,15 @@ class OrderTimeService
*/
private $now;

public function __construct(?DateTime $now = null)
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(LoggerInterface $logger, ?DateTime $now = null)
{
$this->now = $now ?? new DateTime();
$this->logger = $logger;
}

/**
Expand All @@ -42,7 +50,7 @@ public function isOrderAgeGreaterThan(OrderEntity $order, int $minutes): bool
return false;
}

$transitionDate = $lastTransaction->getCreatedAt();
$transitionDate = $lastTransaction->getUpdatedAt() ?? $lastTransaction->getCreatedAt();

if ($transitionDate === null) {
return false;
Expand All @@ -52,6 +60,13 @@ public function isOrderAgeGreaterThan(OrderEntity $order, int $minutes): bool
$diffInHours = $interval->h + ($interval->days * 24);
$diffInMinutes = $interval->i + ($diffInHours * 60);

$this->logger->debug('Check if order is expired', [
'lastTransactionTime' => $transitionDate->format(Defaults::STORAGE_DATE_TIME_FORMAT),
'now' => $this->now->format(Defaults::STORAGE_DATE_TIME_FORMAT),
'diffInMinutes' => $diffInMinutes,
'minutes'=>$minutes
]);

return $diffInMinutes > $minutes;
}
}
3 changes: 2 additions & 1 deletion tests/PHPUnit/Service/Order/OrderTimeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Kiener\MolliePayments\Service\Order\OrderTimeService;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
Expand All @@ -24,7 +25,7 @@ public function testDateComparisonLogic(\DateTime $now, \DateTime $orderDate, bo
{
$order = $this->orderMockWithLastTransactionTimestamp($orderDate);

$result = (new OrderTimeService($now))->isOrderAgeGreaterThan($order, $compareValueInMinutes);
$result = (new OrderTimeService(new NullLogger(),$now))->isOrderAgeGreaterThan($order, $compareValueInMinutes);

$this->assertSame($expected, $result);
}
Expand Down

0 comments on commit e2f6ced

Please sign in to comment.