Magento2 – How to Automatically Create Invoice from Order Observer

invoicemagento2PHP

I'm testing on Magento 2.2.3 and I've created an observer for the event sales_order_save_after which I'm using to automatically create an invoice.

Here is the current error that I'm receiving after placing an order:

Order saving error: Rolled back transaction has not been completed correctly.

And my MyCompany/MyModule/Observer/SalesOrderSaveAfter.php

<?php
namespace MyCompany\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalesOrderSaveAfter implements ObserverInterface
{
    protected $_invoiceService;
    protected $_transactionFactory;

    public function __construct(
      \Magento\Sales\Model\Service\InvoiceService $invoiceService,
      \Magento\Framework\DB\TransactionFactory $transactionFactory
    ) {
       $this->_invoiceService = $invoiceService;
       $this->_transactionFactory = $transactionFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        try {
            if(!$order->canInvoice()) {
                return null;
            }
            if(!$order->getState() == 'new') {
                return null;
            }

            $invoice = $this->_invoiceService->prepareInvoice($order);
            $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
            $invoice->register();

            $transaction = $this->_transactionFactory->create()
              ->addObject($invoice)
              ->addObject($invoice->getOrder());

            $transaction->save();

        } catch (\Exception $e) {
            $order->addStatusHistoryComment('Exception message: '.$e->getMessage(), false);
            $order->save();
            return null;
        }
    }
}

If I remove the transaction portion of the code, eg:

$transaction = $this->_transactionFactory->create()
    ->addObject($invoice)
    ->addObject($invoice->getOrder());

    $transaction->save();

then the order will pass through with the products marked as invoiced, but no invoice is actually created or saved to the order.

Any ideas what I could be missing?

Best Answer

As pointed out, the answer to this is that I was using the wrong event. With the event sales_order_save_after the order hasn't been committed to the Database yet.

I changed my event to fire on checkout_submit_all_after and my observer is now working.