magento create unpaid invoice programmatically

magento create unpaid invoice programmatically

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento create unpaid invoice programmatically

Magento: Creating Unpaid Invoices Programmatically

As a Magento developer, you might find yourself in a situation where you need to create invoices programmatically but want them to remain unpaid until an admin manually sets the status. This is especially useful for scenarios like pending payments or scheduled invoices.

The Challenge

In your current implementation using the sales_order_save_after event and the Mage::getModel('sales/order_invoice_api')->create() method, the invoice immediately gets marked as paid. Your code looks something like this:

$invoiceId = Mage::getModel('sales/order_invoice_api')
    ->create($order->getIncrementId(), array());
$invoice = Mage::getModel('sales/order_invoice')
    ->loadByIncrementId($invoiceId);
$invoice->capture()->save();

The issue arises because the capture() method automatically marks the invoice as paid. To resolve this, we need to prevent the automatic payment capture.

The Solution

Step 1: Understand the Invoice Lifecycle

In Magento, an invoice goes through several states: Pending, Processing, Invoiced, and Cancelled. You want to create an invoice in the “Processing” state, which means it’s waiting for manual confirmation before being finalized.

Step 2: Modify Your Observer Method

You need to modify your observer method so that it doesn’t automatically capture the payment when creating the invoice. Update your code as follows:

public function order_placed($observer)
{
    $event = $observer->getEvent();

    // ... your existing code ...

    $emailInvoice = false;
    $captureInvoice = true; // Change this to false

    $order = Mage::getModel("sales/order")->load($data['entity_id']);
    if ($order->canInvoice() && $order->getIncrementId()) {
        $invoiceApi = Mage::getModel('sales/order_invoice_api');
        $invoiceId = $invoiceApi->create(
            $order->getIncrementId(),
            array(),
            Mage::helper('sales')->__('Pending Invoice created!'),
            $emailInvoice,
            false
        );

        if (!$captureInvoice) {  // This is the key change
            $invoiceApi->setPaymentState($invoiceId, 'processing');
        }
    }
}

Step 3: Set Payment State to Processing

Notice the new line $invoiceApi->setPaymentState($invoiceId, 'processing');. This method sets the payment state of the invoice to “Processing”, which means it’s waiting for manual confirmation.

Why This Works

By avoiding the automatic capture and setting the payment state to processing, you ensure that the newly created invoice remains open. An admin can then manually review the order details and set the status to “Paid” if necessary using Magento’s built-in interface.

Conclusion

Creating unpaid invoices programmatically in Magento requires a careful understanding of the invoice lifecycle and the available API methods. By setting the payment state of the invoice to processing, you ensure that it remains open for manual review and confirmation by an admin.

Remember, this approach is useful when you want to create invoices that aren’t tied to immediate payment, such as pending orders or scheduled invoices. For most standard e-commerce scenarios, you would typically use the default invoice creation process which automatically captures payments based on your store’s settings.

For more detailed tutorials and best practices in Magento development, check out our blog at Laravel Company. We cover a wide range of topics to help you get the most out of your Magento projects.

API Extensions Admin Orders

Our network