Programmatically Create Invoice in Magento 2 module [updated]

Magento 2 order management system creates an invoice when an order is paid online. If you are using PayPal, authorize.net, stripe, etc payment methods, it will create an invoice upon the capture of the amount. However, when we are using offline payment methods, we have to create invoices in Magento 2 manually.

In that case, we have two options. The first option is creating an invoice from the backend order page and the second option is programmatically to create an invoice in Magento 2 module.

Therefore, Today we will see how to programmatically create invoices in Magento 2 module.
 

Programmatically Create Invoice in Magento 2

<?php

namespace CodextblogDemoCron;

use MagentoSalesApiOrderRepositoryInterface;
use MagentoSalesModelServiceInvoiceService;
use MagentoFrameworkDBTransactionFactory;
use CodextblogDemoLoggerLogger;

class Invoicecreation
{

  /**
   * @var OrderRepositoryInterface
   */
  protected $orderRepository;

  /**
   * @var InvoiceService
   */
  protected $invoiceService;

  /**
   * @var TransactionFactory
   */
  protected $transactionFactory;

  /**
   * @var Logger
   */
  protected $customLogger;

  public function __construct(
    OrderRepositoryInterface $orderRepository,
    InvoiceService $invoiceService,
    TransactionFactory $transactionFactory,
    Logger $customLogger
  )
  {
    $this->orderRepository = $orderRepository;
    $this->invoiceService = $invoiceService;
    $this->transactionFactory = $transactionFactory;
    $this->customLogger = $customLogger;
  }

  public function execute()
  {
    $orderId = 111;
    $order = $this->orderRepository->get($orderId);
    if ($order->canInvoice()) 
    {
      $invoice = $this->invoiceService->prepareInvoice($order);
                  $invoice->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
                  $invoice->register();
                  $invoice->getOrder()->setCustomerNoteNotify(false);
                  $invoice->getOrder()->setIsInProcess(true);
                  $order->addCommentToStatusHistory(__('Automatically INVOICED'), false);
                  $transactionSave = $this->transactionFactory->create();
                  $transactionSave->addObject($invoice)->addObject($invoice->getOrder());
                  $transactionSave->save();
                  $this->customLogger->info('Invoice created for order '.$orderId);
    } 
    else 
    {
      $this->customLogger->info('Invoice can not be created for order '.$orderId);
    }
  }

}

In line 55, we have set customer notification to false. That means invoice email will be not sent to the customer and the customer will not get an email notification. However, you can turn it on by setting it to true.

In conclusion, programmatically create invoice in Magento 2 is the best automation way for offline payment methods.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.