I want to change the order status to 'canceled' when payment failed. I've tried the code below but the order status gets saved as processing:
$orderId = $number8; //order id
$order = Mage::getModel('sales/order')->load($orderId); //load order
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
$state = 'canceled';
$status = $state;
$comment = "Changing state to $state and status to $status Status";
$isCustomerNotified = false; //whether customer to be notified
$order->setState($state, $status, $comment, $isCustomerNotified);
$order->addStatusHistoryComment("Payment Failed",
Mage_Sales_Model_Order::STATE_CANCELED);
$order->setStatus($status);
$order->save();
$this->loadLayout();
$this->_initLayoutMessages('checkout/session')->addError(Mage::helper('checkout')->__($errorMessage));
$this->renderLayout();
Please assist. I have a condition in my model if(payment fails) execute code above but the order gets saved as processing instead of canceled.