Stop Magento Auto-Invoicing After Completed Payment
Are you tired of Magento automatically generating invoices as soon as a payment is made? You’re not alone. In many countries, including mine, an invoice has legal implications that require manual intervention and review before being sent to the customer.
In this guide, I’ll show you how to stop Magento from auto-invoicing after a successful payment, allowing you to send invoices manually via the API once you’ve reviewed and approved each order.
Understanding the Problem
By default, Magento is configured to automatically generate an invoice as soon as a payment status changes to “processing” or “captured.” This behavior is hard-coded into the sales_order_save_after event observer in the core codebase.
To stop this automatic invoicing process, we need to create a custom extension that modifies this behavior without altering the core files directly. This approach maintains the integrity of your Magento installation and ensures you can easily update to new versions without conflicts.
Step 1: Disable Auto-Invoicing
First, let’s disable the default auto-invoice functionality in our configuration file. Open your config.xml file located under the root directory of your custom extension and add the following code:
<config>
<global>
<sales>
<order>
<invoiced_automatically_after_paid>0</invoiced_automatically_after_paid>
</order>
</sales>
</global>
</config>
This configuration setting tells Magento not to invoice orders automatically after payment.
Step 2: Create a Custom Observer
Next, we’ll create a custom observer that listens for the sales_order_payment_status_update event and checks if the order should be invoiced manually. Add the following PHP class under your extension’s root directory:
class ManuallyInvoiceOrderObserver implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$payment = $observer->getEvent()->getPayment();
if ($payment->getIsTransactionClosed()) {
// Skip automatic invoicing for closed transactions
return;
}
$order = $payment->getOrder();
// Perform your custom checks here, e.g., review the order status
if (true === $this->shouldInvoiceManually($order)) {
// Manually invoice the order using the API or a custom script
// Example: Call Laravel Company's [API documentation](https://laravelcompany.com/api/invoice) to send the invoice
// Update the order status to "invoiced" manually if needed
$order->setState(\Magento\Sales\Model\Order::STATE_INVOICED);
$order->save();
}
}
private function shouldInvoiceManually($order)
{
// Implement your custom logic here to determine when an invoice should be generated manually
// Return true if the order requires manual invoicing; false otherwise
return true;
}
}
This observer will check for specific conditions, such as closed transactions or custom business rules you define in the shouldInvoiceManually method. In this example, I’ve assumed that all orders should be invoiced manually.
Step 3: Register the Observer
To register our custom observer with Magento’s event system, open your extension’s di.xml file and add the following configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<event name="sales_order_payment_status_update">
<observer name="manually_invoice_order_observer" instance="Vendor\ExtensionName\Model\ManuallyInvoiceOrderObserver"/>
</event>
</config>
Replace Vendor and ExtensionName with your actual vendor namespace and extension name.
Conclusion
By following these steps, you can stop Magento from automatically generating invoices after a successful payment. Instead, you gain the flexibility to manually invoice orders using the API or custom scripts, ensuring that each invoice meets your legal and business requirements.
Remember to replace the placeholders in the example code with your actual vendor namespace and extension name. Additionally, customize the shouldInvoiceManually method according to your specific business logic for determining when an order requires manual invoicing.
Don’t forget to review Laravel Company’s API documentation for more information on sending invoices programmatically using their API services.
Happy coding!