Skip to content

Commit

Permalink
Do not cancel partially paid transactions on new payment selection
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakyvv committed Aug 26, 2024
1 parent 6c1ebb9 commit 041a26f
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Controller/StoreApi/Payment/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,36 @@ private function setPaymentMethod(
$context->scope(
Context::SYSTEM_SCOPE,
function () use ($order, $initialStateId, $orderId, $paymentMethodId, $context): void {
if ($order->getTransactions() !== null && $order->getTransactions()->count() >= 1) {
foreach ($order->getTransactions() as $transaction) {
if ($transaction->getStateMachineState()->getTechnicalName()
!== OrderTransactionStates::STATE_CANCELLED) {
$this->orderTransactionStateHandler->cancel(
$transaction->getId(),
$context
);
}
$transactions = $order->getTransactions();
foreach ($order->getTransactions() as $transaction) {
$stateMachineState = $transaction->getStateMachineState();
if (null === $stateMachineState) {
throw new \LogicException('State machine association not loaded');
}
$allowedStates = [OrderTransactionStates::STATE_CANCELLED, OrderTransactionStates::STATE_PARTIALLY_PAID];
if (!in_array($transaction->getStateMachineState()->getTechnicalName(), $allowedStates)) {
$this->orderTransactionStateHandler->cancel(
$transaction->getId(),
$context
);
}
}

// sum total of partially paid transactions
$totalPaid = 0;
foreach ($transactions as $transaction) {
$stateMachineState = $transaction->getStateMachineState();
if (null === $stateMachineState) {
throw new \LogicException('State machine association not loaded');
}
if ($stateMachineState->getTechnicalName() === OrderTransactionStates::STATE_PARTIALLY_PAID) {
$totalPaid += $transaction->getAmount()->getTotalPrice();
}
}

$transactionAmount = new CalculatedPrice(
$order->getPrice()->getTotalPrice(),
$order->getPrice()->getTotalPrice(),
$order->getPrice()->getTotalPrice() - $totalPaid,
$order->getPrice()->getTotalPrice() - $totalPaid,
$order->getPrice()->getCalculatedTaxes(),
$order->getPrice()->getTaxRules()
);
Expand Down

0 comments on commit 041a26f

Please sign in to comment.