Payment Success but Amount Due in Backend Order [Custom Payment Module] in Magento

adminmagento-1.9orderspayment-gateway

My store redirected to payment site and after payment is success, it send positive respose. and this is my method that handles response.

 public function responseAction() {
         $response = $this->getRequest->getParams(); //captured response
         ............
        if($validated) {
            // Payment was successful, so update the order's state, send order email and move to the success page
            $order = Mage::getModel('sales/order');
            $order->loadByIncrementId($orderId);
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Gateway has authorized the payment.');

            $order->sendNewOrderEmail();
            $order->setEmailSent(true);

            $order->save();

            Mage::getSingleton('checkout/session')->unsQuoteId();

            Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=>true));
        }
        else {
            // There is a problem in the response we got
            $this->cancelAction();
            Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure'=>true));
        }
     .......

Here, all the response parameters are fine and it also redirects me to the success page. But in order it says the payment is due. what do i need to update for my store to recognize at paid.
enter image description here

Suggestion of @R.S

After i invoice the order(via invoice button) it shows all amount is paid and due amount is 0. Why doesn't it show when i first look at order detail ?

I tried to invoice on the response method(reference from above method), but no luck

if($validated) {
            // Payment was successful, so update the order's state, send order email and move to the success page
            $order = Mage::getModel('sales/order');
            $order->loadByIncrementId($orderId);
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Gateway has authorized the payment. Payment reference id '.$response['refId']);
            $order->addStatusHistoryComment("Payment reference id ".$response['refId'], false);
            $order->sendNewOrderEmail();
            $order->setEmailSent(true);

            $order->save();

            /*Invoice an order*/
            $order=Mage::getModel('sales/order')->load($orderId);
            if($order->canInvoice() and $order->getIncrementId()){
                    $items = array();
                    foreach ($order->getAllItems() as $item) {
                        $items[$item->getId()] = $item->getQtyOrdered();
                    }
                $invoiceId=Mage::getModel('sales/order_invoice_api')->create($order->getIncrementId(),$items,null,false,true);
                Mage::getModel('sales/order_invoice_api')->capture($invoiceId);
            }
            $order->save();

            Mage::getSingleton('checkout/session')->unsQuoteId();

            Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=>true));
        }

Best Answer

Until payment is received, your order will always be in a "new" state therefore the balance due will be the order total until the order is invoiced.

Try

if($validated) {
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
    $order->addStatusHistoryComment("Payment reference id ".$response['refId'], false);
    $order->sendNewOrderEmail();
    $order->setEmailSent(true);

    $invoice = $order->prepareInvoice();

    if($invoice->getTotalQty()){
        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
        //$invoice->getOrder()->setCustomerNoteNotify(false);
        $invoice->register();
        Mage::getModel('core/resource_transaction')
                ->addObject($invoice)
                ->addObject($invoice->getOrder())
                ->save();

        $invoice->sendEmail(false); 

    }

    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Gateway has authorized the payment. Payment reference id '.$response['refId']);
    $order->save();
}