We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    One of our client came up with a requirement where he adjusted payment type as authorize along with new orders to be placed as processing status.

    Our work was to do execute cronjob such a way that it generates an invoice and capture the payment under technical limitation capped by client.

    To execute the job we created a cronjob & set it at every minute.

    Have a look on step-by-step explaination.

    Step 1:  We create a crontab.xml in module at app/code/Magemonkeys/Generateinvoice/etc/crontab.xml and add the following code in the XML.

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
        <group id="default">
            <job name="magemonkeys_generateinvoice_programmatically" instance="MagemonkeysGenerateinvoiceCronInvoicegenerate" method="execute">
                <schedule>* * * * *</schedule>
            </job>
        </group>
    </config>
    

    Step 2: Then, we create an Invoicegenerate.php at app/code/Magemonkeys/Generateinvoice/Cron/Invoicegenerate.php and add the following code in this file.

    <?php
    
    namespace MagemonkeysGenerateinvoiceCron;
    
    use MagentoFrameworkExceptionLocalizedException;
    
    class Invoicegenerate
    {
        /**
         * @var MagentoSalesApiOrderRepositoryInterface 
         */
        private $_orderRepository;
    
        /**
         * @var MagentoSalesModelServiceInvoiceService 
         */
        private $_invoiceService;
    
        /**
         * @var MagentoFrameworkDBTransactionFactory 
         */
        private $_transactionFactory;
    
        /**
         * @var MagentoSalesModelOrderEmailSenderInvoiceSender 
         */
        private $_invoiceSender;
    
        /**
         * @var MagentoSalesModelResourceModelOrderCollectionFactory 
         */
        private $_orderCollectionFactory;
    
        public function __construct(
            MagentoSalesApiOrderRepositoryInterface $orderRepository,
            MagentoSalesModelServiceInvoiceService $invoiceService,
            MagentoFrameworkDBTransactionFactory $transactionFactory,
            MagentoSalesModelOrderEmailSenderInvoiceSender $invoiceSender,
            MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
        )
        {
            $this->_orderRepository = $orderRepository;
            $this->_invoiceService = $invoiceService;
            $this->_transactionFactory = $transactionFactory;
            $this->_invoiceSender = $invoiceSender;
            $this->_orderCollectionFactory = $orderCollectionFactory;
        } 
    
        public function execute()
        {
    
            $orderCollection = $this->_orderCollectionFactory->create()
                ->addAttributeToSelect('*')
                ->addFieldToFilter('status', 'processing');
    
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $logger = $objectManager->get("PsrLogLoggerInterface");
            
            foreach ($orderCollection as $order) {
                if($order->hasInvoices()) { 
                    $logger->info('Order #'.$order->getId().' has already been invoiced.'); 
                    continue;
                }
    
                
                $payment = $order->getPayment();
                $method = $payment->getMethodInstance();
                $methodTitle = $method->getTitle();
                if(strtolower($methodTitle) == 'credit card'){
                    try {
                        
                        $order = $this->_orderRepository->get($order->getId());
    
                        if (!$order->getId()) {
                            $logger->info('Order #'.$order->getId().' no longer exists.'); 
                            continue;
                        }
    
                        if(!$order->canInvoice()) {
                            $logger->info('Order #'.$order->getId().' does not allow an invoice to be created.');
                            continue;
                        }
    
                        $invoice = $this->_invoiceService->prepareInvoice($order);
                        
                        if (!$invoice) {
                            $logger->info("Order #".$order->getId()." can't save the invoice right now.");
                            continue;
                        }
                        
                        if (!$invoice->getTotalQty()) {
                            $logger->info("Order #".$order->getId()." create an invoice without products.");
                            continue;
                        }
    
                        $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::CAPTURE_ONLINE);
                        $invoice->register();
                        $invoice->getOrder()->setCustomerNoteNotify(false);
                        $invoice->getOrder()->setIsInProcess(true);
                        $order->addStatusHistoryComment('Automatically INVOICED', false);
                        $transactionSave = $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder());
                        $transactionSave->save();
    
                        // send invoice emails
                        try {
                            $this->_invoiceSender->send($invoice);
                            $logger->info("Order #".$order->getId()." - Invoice has been created successfully.");
                        } catch (Exception $e) {
                            $logger->info("Order #".$order->getId()." - We can't send the invoice email right now.");
                        }
                    } catch (Exception $e) {
                        
                       $logger->info($e->getMessage());
                    }
                }
            }
        }
    }
    

     

    field_5bfb909c5ccae

      Get a Free Quote