Skip to content

Commit

Permalink
PISHPS-397: use unconfirmed payment status instead of in_progress (#897)
Browse files Browse the repository at this point in the history
* NTR: PISHPS-397: use unconfirmed payment status instead of in_progress

* NTR: fix error

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Dec 12, 2024
1 parent 399c51b commit a7015b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
7 changes: 6 additions & 1 deletion src/Components/OrderExpiration/ExpireAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
Expand Down Expand Up @@ -80,7 +81,11 @@ private function expireOrdersInSalesChannel(SalesChannelEntity $salesChannelEnti
$criteria = new Criteria();
$criteria->addAssociation('transactions.stateMachineState');
$criteria->addAssociation('transactions.paymentMethod');
$criteria->addFilter(new EqualsFilter('transactions.stateMachineState.technicalName', OrderTransactionStates::STATE_IN_PROGRESS));
$criteria->addFilter(new OrFilter([
new EqualsFilter('transactions.stateMachineState.technicalName', OrderTransactionStates::STATE_IN_PROGRESS),
new EqualsFilter('transactions.stateMachineState.technicalName', OrderTransactionStates::STATE_UNCONFIRMED)
]));

$criteria->addFilter(new EqualsFilter('salesChannelId', $salesChannelEntity->getId()));
$criteria->addFilter(new RangeFilter('orderDateTime', [RangeFilter::GTE => $date->format(Defaults::STORAGE_DATE_TIME_FORMAT)]));
$criteria->addSorting(new FieldSorting('orderDateTime', FieldSorting::DESCENDING));
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Order/OrderExpireService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Kiener\MolliePayments\Struct\Order\OrderAttributes;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\Checkout\Order\OrderCollection;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderStates;
use Shopware\Core\Framework\Context;

class OrderExpireService
Expand Down Expand Up @@ -106,7 +106,7 @@ public function cancelExpiredOrders(OrderCollection $orders, Context $context):
$lastStatus = $stateMachineState->getTechnicalName();

// disregard any orders that are not in progress
if ($lastStatus !== OrderStates::STATE_IN_PROGRESS) {
if (!in_array($lastStatus, [OrderTransactionStates::STATE_IN_PROGRESS,OrderTransactionStates::STATE_UNCONFIRMED])) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/Order/OrderStatusUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function updatePaymentStatus(OrderTransactionEntity $transaction, string
{
# if we are already in_progress...then don't switch to OPEN again
# otherwise SEPA bank transfer would switch back to OPEN
if ($currentShopwareStatusKey !== OrderTransactionStates::STATE_IN_PROGRESS || $context->hasState(self::ORDER_STATE_FORCE_OPEN)) {
if (! in_array($currentShopwareStatusKey, [OrderTransactionStates::STATE_IN_PROGRESS, OrderTransactionStates::STATE_UNCONFIRMED]) || $context->hasState(self::ORDER_STATE_FORCE_OPEN)) {
$addLog = true;
$this->transactionTransitionService->reOpenTransaction($transaction, $context);
}
Expand Down
42 changes: 6 additions & 36 deletions src/Service/Transition/TransactionTransitionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,20 @@ public function __construct(

public function processTransaction(OrderTransactionEntity $transaction, Context $context): void
{
// Shopware added in_progress status with version 6.2, so this ensures backward compatibility
if (!defined('Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates::STATE_IN_PROGRESS')) {
// set open status in < sw6.2
$this->reOpenTransaction($transaction, $context);

return;
}

$technicalName = ($transaction->getStateMachineState() instanceof StateMachineStateEntity) ? $transaction->getStateMachineState()->getTechnicalName() : '';

if ($this->isFinalOrTargetStatus($technicalName, [OrderTransactionStates::STATE_IN_PROGRESS])) {
if ($this->isFinalOrTargetStatus($technicalName, [OrderTransactionStates::STATE_UNCONFIRMED])) {
return;
}

$entityId = $transaction->getId();
$availableTransitions = $this->getAvailableTransitions($entityId, $context);

if (!$this->transitionIsAllowed(StateMachineTransitionActions::ACTION_DO_PAY, $availableTransitions)) {
if (!$this->transitionIsAllowed(StateMachineTransitionActions::ACTION_PROCESS_UNCONFIRMED, $availableTransitions)) {
$this->reOpenTransaction($transaction, $context);
}

$this->performTransition($entityId, StateMachineTransitionActions::ACTION_DO_PAY, $context);
$this->performTransition($entityId, StateMachineTransitionActions::ACTION_PROCESS_UNCONFIRMED, $context);
}

public function reOpenTransaction(OrderTransactionEntity $transaction, Context $context): void
Expand Down Expand Up @@ -96,28 +88,20 @@ public function reOpenTransaction(OrderTransactionEntity $transaction, Context $

public function payTransaction(OrderTransactionEntity $transaction, Context $context): void
{
// backwards compatibility, the former status StateMachineTransitionActions::ACTION_PAY='pay' does not exist any more
// the constant ACTION_PAID has been added with sw 6.2 and should be used instead of legacy ACTION_PAY
$payActionName = 'pay';

if (defined('Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions::ACTION_PAID')) {
$payActionName = StateMachineTransitionActions::ACTION_PAID;
}

$currentStatus = ($transaction->getStateMachineState() instanceof StateMachineStateEntity) ? $transaction->getStateMachineState()->getTechnicalName() : '';

if ($this->isFinalOrTargetStatus($currentStatus, [$payActionName])) {
if ($this->isFinalOrTargetStatus($currentStatus, [StateMachineTransitionActions::ACTION_PAID])) {
return;
}

$entityId = $transaction->getId();
$availableTransitions = $this->getAvailableTransitions($entityId, $context);

if (!$this->transitionIsAllowed($payActionName, $availableTransitions)) {
if (!$this->transitionIsAllowed(StateMachineTransitionActions::ACTION_PAID, $availableTransitions)) {
$this->reOpenTransaction($transaction, $context);
}

$this->performTransition($entityId, $payActionName, $context);
$this->performTransition($entityId, StateMachineTransitionActions::ACTION_PAID, $context);
}

public function cancelTransaction(OrderTransactionEntity $transaction, Context $context): void
Expand All @@ -140,13 +124,6 @@ public function cancelTransaction(OrderTransactionEntity $transaction, Context $

public function failTransaction(OrderTransactionEntity $transaction, Context $context): void
{
// Shopware added failed status with version 6.2, so this ensures backward compatibility
if (!defined('Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates::STATE_FAILED')) {
$this->cancelTransaction($transaction, $context);

return;
}

$currentStatus = ($transaction->getStateMachineState() instanceof StateMachineStateEntity) ? $transaction->getStateMachineState()->getTechnicalName() : '';

if ($this->isFinalOrTargetStatus($currentStatus, [OrderTransactionStates::STATE_CANCELLED, OrderTransactionStates::STATE_FAILED])) {
Expand All @@ -165,13 +142,6 @@ public function failTransaction(OrderTransactionEntity $transaction, Context $co

public function authorizeTransaction(OrderTransactionEntity $transaction, Context $context): void
{
// Shopware added authorized status with version 6.4.1, so this ensures backward compatibility
if (!defined('Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates::STATE_AUTHORIZED')) {
$this->payTransaction($transaction, $context);

return;
}

$authorizedState = OrderTransactionStates::STATE_AUTHORIZED;

$currentStatus = ($transaction->getStateMachineState() instanceof StateMachineStateEntity) ? $transaction->getStateMachineState()->getTechnicalName() : '';
Expand Down

0 comments on commit a7015b4

Please sign in to comment.