cancel
Showing results for 
Search instead for 
Did you mean: 

Create An Invoice For An Existing Credit Card order

Create An Invoice For An Existing Credit Card order

I've had several credit card orders come in and they always create an invoice automagically like they are supposed to. However 1 credit card order did not create an invoice. I'm unable to figure out how to force the system to create an invoice for this product. It doesn't give you the "Invoice" button because it's a credit card order.

 

Does anyone know how to do this? Programmatically possibly? I'm using Magento 2.3.1.

 

Thank you.

4 REPLIES 4

Re: Create An Invoice For An Existing Credit Card order

Hello @legacy_polo 

 

Let me help you to create invoice from admin first :

  1. On the Admin sidebar, click Sales.
  2. In the Operations section, choose Orders.
  3. Find the sales order with the status of Processing in the grid. Then, do the following:
  4. In the Action column, click View.
  5. In the header of the sales order, choose the Invoice option.

Note : The Invoice option does not appear if the payment method is set to Authorize and Capture.

 

Now create invoice from code level :

  1. Start the settings with events.xml file in your custom module: /app/code/Vendor/Module/etc/events.xml
    <?xml version="1.0"?>
        <event name="sales_order_invoice_pay">
            <observer name="webpos_sales_order_invoice_pay" instance="Vendor\Module\Observer\SalesOrderInvoicePay" />
        </event>  
    </config>
  2. Setup the observer class
    /app/code/Vendor/Module/Observer/SalesOrderInvoicePay.php
    <?php
    namespace Vendor\Module\Observer\Sales;
    use Magento\Framework\Event\Observer as EventObserver;
    use Magento\Framework\Event\ObserverInterface;
    class SalesOrderInvoicePay implements ObserverInterface
    {   
    /**
    * @param EventObserver $observer
    * @return $this
    */
    public function execute(EventObserver $observer)
    {
         $invoice = $observer->getEvent()->getInvoice();
         $order = $invoice->getOrder();
       
         /* reset total_paid & base_total_paid of order */
         $order->setTotalPaid($order->getTotalPaid() - $invoice->getGrandTotal());
         $order->setBaseTotalPaid($order->getBaseTotalPaid() - $invoice->getBaseGrandTotal());
    }    
    }
  3. Dispatch event (sales_order_invoice_pay) in file in your desired step.
    // Code...
    $this->eventManager->dispatch('sales_order_invoice_pay');
    // More code that sets $data...
    $this->eventManager->dispatch('sales_order_invoice_pay', ['Data' => $data]);

 

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

Re: Create An Invoice For An Existing Credit Card order

That is my problem:

 

The Invoice option does not appear if the payment method is set to Authorize and Capture.

 

The Invoice is from an "Authorize and Capture", so from the admin gui I can't create it. How do I tell the code you gave me that I want to generate an invoice for order_id 86 for example?

 

 

Re: Create An Invoice For An Existing Credit Card order

@legacy_polo 

 

In the above code it is fetching order from event, you can get order by fetching with id from repository.

 

check the answer here for help :

https://magento.stackexchange.com/questions/169494/magento-2-load-order-by-id-in-customer-account-or...

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

Re: Create An Invoice For An Existing Credit Card order

but event observer works when you add it with some functionality, 

you can do this with console command as well where you can pass ID in console command which will not be dependent on other functionality Smiley Happy

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !