diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ad9cc7..a7555b58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. +## [unreleased] +### Fixed +- Fix: Issue where failed AltaPay orders stayed in pending status and were not cancelled automatically. +- Improved the cancellation logic to keep stock in sync correctly. + ## [4.2.1] ### Added - Added a **Back to Shopping Cart** button on the payment form, allowing customers to cancel the payment and return to the cart. diff --git a/Controller/Adminhtml/System/Config/ChangeOrderStatusButton.php b/Controller/Adminhtml/System/Config/ChangeOrderStatusButton.php index ab8079f3..8ea90bce 100644 --- a/Controller/Adminhtml/System/Config/ChangeOrderStatusButton.php +++ b/Controller/Adminhtml/System/Config/ChangeOrderStatusButton.php @@ -18,6 +18,7 @@ use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; +use SDM\Altapay\Model\SystemConfig; class ChangeOrderStatusButton extends Action { @@ -90,8 +91,16 @@ public function execute() try { $orderCollection = $this->orderCollection->create(); - $orderCollection->addAttributeToFilter('status','pending') - ->addAttributeToFilter('altapay_payment_form_url', ['neq' => 'NULL']); + $orderCollection->addFieldToFilter('state', ['in' => ['new', 'pending_payment']]); + + $orderCollection->getSelect() + ->join( + ['payment' => $orderCollection->getTable('sales_order_payment')], + 'main_table.entity_id = payment.parent_id', + [] + ) + ->where('payment.method IN (?)', SystemConfig::getTerminalCodes()) + ->where('payment.last_trans_id IS NULL OR payment.last_trans_id = ?', ''); if (array_filter($orderCollection->getData())) { foreach ($orderCollection as $order) { diff --git a/Model/Cron/UpdateOrderStatus.php b/Model/Cron/UpdateOrderStatus.php index 86ee4f18..09201b4e 100644 --- a/Model/Cron/UpdateOrderStatus.php +++ b/Model/Cron/UpdateOrderStatus.php @@ -15,6 +15,7 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; use Magento\Sales\Api\OrderManagementInterface; +use SDM\Altapay\Model\SystemConfig; class UpdateOrderStatus { @@ -91,10 +92,17 @@ public function execute() $orderCollection = $this->orderCollection->create(); $orderCollection->addFieldToSelect('entity_id') ->addFieldToFilter('created_at', ['lt' => date('Y-m-d H:i:s', $cutoffTime)]) - ->addAttributeToFilter('status','pending') - ->addAttributeToFilter('state','new') - ->addAttributeToFilter('altapay_payment_form_url', ['neq' => 'NULL']); + ->addFieldToFilter('state', ['in' => ['new', 'pending_payment']]); + $orderCollection->getSelect() + ->join( + ['payment' => $orderCollection->getTable('sales_order_payment')], + 'main_table.entity_id = payment.parent_id', + [] + ) + ->where('payment.method IN (?)', SystemConfig::getTerminalCodes()) + ->where('payment.last_trans_id IS NULL OR payment.last_trans_id = ?', ''); + if ($this->scopeConfig->getValue(self::EXCLUDE_ADMIN_ORDER, $storeScope)) { $orderCollection->addFieldToFilter('remote_ip', ['neq' => null]); }