Create an Invoice after order is placed

Create an Invoice after order is placed

October 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create an Invoice after order is placed

Creating an Invoice for Magento Orders on Payment Success

As an experienced Magento developer, I often encounter situations where clients need to automatically generate invoices for orders that have been successfully processed through their custom payment gateways. In this article, we’ll explore the correct approach to setting an order as paid and creating an invoice programmatically in response to a successful payment.

Addressing Your Concerns

Let me first clarify your understanding of the Magento sales process:

You’re on the right track! When a payment is successfully processed through your custom gateway, you indeed need to create an invoice for that order. This step is crucial for updating the order status in the Magento admin panel and allowing customers to view their invoices.

The Incorrect Approach

You’ve attempted to generate an invoice using the following code:

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

// ... (other code)

$transactionSave = Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder());

$transactionSave->save();

While this approach seems logical, it’s not the recommended way to create invoices programmatically in Magento. The issue lies in how you’re initializing and saving the invoice object.

The official Magento documentation suggests using the sales/order_invoice_api for creating invoices programmatically. This API provides a more controlled and secure method of generating invoices.

Here’s an example of how to create an invoice using the API:

$invoiceId = Mage::getModel('sales/order_invoice_api')
    ->create($_order->getIncrementId(), array());

Replace $_order with your order object. This code will generate a new invoice for the specified order.

Verifying the Invoice Creation

After generating the invoice, you can verify its creation by checking the following:

  1. Order Status: The order status should be updated to “Processing” or “Complete”, indicating that it has been paid successfully.
  2. Invoice Number: A unique invoice number should be generated for the order.
  3. Email Notifications: Magento will send an email notification with the new invoice attachment to the customer.

Conclusion

Creating invoices programmatically in Magento is a critical step when integrating custom payment gateways. By following the recommended approach of using the sales/order_invoice_api, you can ensure that your invoices are generated accurately and securely.

Remember, it’s essential to thoroughly test your implementation under various scenarios to guarantee the stability and reliability of your system.

For more in-depth information on Magento development best practices, feel free to explore our Magento Development category at Laravel Company.

API Admin Configuration Orders

Our network