Skip to content

Commit

Permalink
NTR: fix order expire for sepa
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Dec 9, 2024
1 parent ccde869 commit c9e9577
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
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 c9e9577

Please sign in to comment.