Magento – Programmatically change order status if transaction is successful

magento-1.8order-statuspaypalpaypal-expresspaypal-payments-pro

I am using Website Payments Pro Payflow Edition for my store and wanted to know if there is a way to automatically change the order status from Processing to Ready to ship if the transaction it successful as currently our admin is having to do this manually.

I was thinking to use an Observer to change the status but so far I can only find the event to invoice:

<?php

class MyCompany_OrdersObserver_Model_Observer {

    public $order;//the order...

    function afterSalesOrderSaveCommitAfter(&$event) {
        return $this->__process($event);
    }

    protected function __process($event) {
        $this->order = $event->getEvent()->getOrder();
        if (!$this->order->getId()) {
            //order is not saved in the database
            return $this;
        }
        else {

            $this->createInvoice();
        }
    }

    protected function createInvoice() {
        $orderState = $this->order->getState();
        if ($orderState === Mage_Sales_Model_Order::STATE_NEW) { // Check for state new.
            if ($this->order->canInvoice()) {
                $this->order->getPayment()->setSkipTransactionCreation(false);
                $invoice = $this->order->prepareInvoice();
                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
                $invoice->register();
                Mage::getModel('core/resource_transaction')
                   ->addObject($invoice)
                   ->addObject($this->order)
                   ->save();
            }
            else {
                //we can not invoice it so the process is normal.
            }
        }
    }
}

Is there another event I could use to achieve this? Or is it a configuration issue?

Any advice would be greatly appreciated

Best Answer

Try the below code. It working Fine in my project

public function invoice($realOrderId)
{

    $order = Mage::getModel('sales/order')->load($realOrderId);
    try {
        if(!$order->canInvoice())
        {
            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
        }

        $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

        if (!$invoice->getTotalQty()) {
            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
        }

        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
        //Or you can use
       //$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
        $invoice->register();
        $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());
        $transactionSave->save();
        $order->setStatus("complete");
        $order->save();

    }
    catch (Mage_Core_Exception $e) {
    }

}