From c9e9577f797abfd501e5e3f244626f3acb232944 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Mon, 9 Dec 2024 15:39:12 +0100 Subject: [PATCH 1/3] NTR: fix order expire for sepa --- src/Resources/config/services/services.xml | 4 +++- src/Service/Order/OrderExpireService.php | 6 +++++- src/Service/Order/OrderTimeService.php | 19 +++++++++++++++++-- .../Service/Order/OrderTimeServiceTest.php | 3 ++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Resources/config/services/services.xml b/src/Resources/config/services/services.xml index fd0d003d2..c01f8c939 100644 --- a/src/Resources/config/services/services.xml +++ b/src/Resources/config/services/services.xml @@ -128,7 +128,9 @@ - + + + diff --git a/src/Service/Order/OrderExpireService.php b/src/Service/Order/OrderExpireService.php index b342ae4d2..b7bd6a3ce 100644 --- a/src/Service/Order/OrderExpireService.php +++ b/src/Service/Order/OrderExpireService.php @@ -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) { diff --git a/src/Service/Order/OrderTimeService.php b/src/Service/Order/OrderTimeService.php index f1bc1c11e..2cc386715 100644 --- a/src/Service/Order/OrderTimeService.php +++ b/src/Service/Order/OrderTimeService.php @@ -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 { @@ -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; } /** @@ -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; @@ -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; } } diff --git a/tests/PHPUnit/Service/Order/OrderTimeServiceTest.php b/tests/PHPUnit/Service/Order/OrderTimeServiceTest.php index e09c50c13..8b04ecb47 100644 --- a/tests/PHPUnit/Service/Order/OrderTimeServiceTest.php +++ b/tests/PHPUnit/Service/Order/OrderTimeServiceTest.php @@ -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; @@ -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); } From 09073114d8bc8dd59992ee320e4b0b636599599f Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Mon, 9 Dec 2024 15:58:02 +0100 Subject: [PATCH 2/3] NTR: fix order expire --- src/Components/OrderExpiration/ExpireAction.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Components/OrderExpiration/ExpireAction.php b/src/Components/OrderExpiration/ExpireAction.php index 654f444db..fb3c4fc3b 100644 --- a/src/Components/OrderExpiration/ExpireAction.php +++ b/src/Components/OrderExpiration/ExpireAction.php @@ -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; @@ -32,7 +33,8 @@ public function __construct( OrderExpireService $orderExpireService, SettingsService $settingsService, LoggerInterface $logger - ) { + ) + { $this->orderRepository = $orderRepository; $this->salesChannelRepository = $salesChannelRepository; $this->orderExpireService = $orderExpireService; @@ -74,7 +76,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'); @@ -85,7 +87,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) { From 0fba0e17a5d2ea568546108a874955e68615fd2f Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Mon, 9 Dec 2024 16:05:14 +0100 Subject: [PATCH 3/3] NTR: Cs fix --- src/Components/OrderExpiration/ExpireAction.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Components/OrderExpiration/ExpireAction.php b/src/Components/OrderExpiration/ExpireAction.php index fb3c4fc3b..a2888044b 100644 --- a/src/Components/OrderExpiration/ExpireAction.php +++ b/src/Components/OrderExpiration/ExpireAction.php @@ -33,8 +33,7 @@ public function __construct( OrderExpireService $orderExpireService, SettingsService $settingsService, LoggerInterface $logger - ) - { + ) { $this->orderRepository = $orderRepository; $this->salesChannelRepository = $salesChannelRepository; $this->orderExpireService = $orderExpireService; @@ -87,7 +86,7 @@ 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 older than date',[ + $this->logger->debug('Search for orders with payment status in progress older than date', [ 'date' => $date->format(Defaults::STORAGE_DATE_TIME_FORMAT), ]);