Search This Blog

Wednesday 16 January 2013

Capture invoice offline programatically

 Script to Capture invoice offline, this will change the invoice status from pending to paid and update the paid totals related to the invoice in the database.

   require_once 'app/Mage.php';
    $path = Mage::app()->getConfig()->getTempVarDir(); // This line is just to initiate the Magento APP

    try {
        $orderId = '100000066';
        $invoiceId = '100000006';
        $order  = Mage::getModel('sales/order')->loadByIncrementId($orderId);
        //print_r( $order->getData());     exit;
        if (!$order->canCreditmemo()) {
            if ($invoiceId) {
                $invoice = Mage::getModel('sales/order_invoice')
                    ->loadByIncrementId($invoiceId)
                    ->setOrder($order);
                //print_r( $invoice->getData());     exit;
                $capture_case = 'offline';
                //$invoice->setRequestedCaptureCase($capture_case)->capture()->save();
                $invoice->setRequestedCaptureCase($capture_case)->setCanVoidFlag(false)->pay();//->save();

                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder());
                $transactionSave->save();
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }


No comments:

Post a Comment