cancel
Showing results for 
Search instead for 
Did you mean: 

There has been an error processing your request after complete payment on magento 2

There has been an error processing your request after complete payment on magento 2

hi i faced problem with my magento after the customers pay.. they faced this errors messages

 

There has been an error processing your request Type Error occurred when creating object: Vendor\AutoInvoice\Observer\OrderObserver Error log record number: 36209293a74e8a1a4c09d455d7907e74f6e256326a6b65b3bb4cff3f84501279

 

 

i open the record number file:

 

{"0":"Type Error occurred when creating object: Vendor\AutoInvoice\Observer\OrderObserver","1":"#1 Magento\Framework\ObjectManager\Factory\Compiled->create() called at [vendor/magento/framework/ObjectManager/ObjectManager.php:70]\n#2 Magento\Framework\ObjectManager\ObjectManager->get() called at [vendor/magento/framework/Event/ObserverFactory.php:33]\n#3 Magento\Framework\Event\ObserverFactory->get() called at [vendor/magento/framework/Event/Invoker/InvokerDefault.php:72]\n#4 Magento\Framework\Event\Invoker\InvokerDefault->dispatch() called at [vendor/magento/framework/Event/Manager.php:66]\n#5 Magento\Framework\Event\Manager->dispatch() called at [generated/code/Magento/Framework/Event/Manager/Proxy.php:95]\n#6 Magento\Framework\Event\Manager\Proxy->dispatch() called at [vendor/magento/module-checkout/Controller/Onepage/Success.php:33]\n#7 Magento\Checkout\Controller\Onepage\Success->execute() called at [vendor/magento/framework/App/Action/Action.php:108]\n#8 Magento\Framework\App\Action\Action->dispatch() called at [vendor/magento/module-checkout/Controller/Onepage.php:161]\n#9 Magento\Checkout\Controller\Onepage->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]\n#10 Magento\Checkout\Controller\Onepage\Success\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]\n#11 Magento\Checkout\Controller\Onepage\Success\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]\n#12 Magento\Checkout\Controller\Onepage\Success\Interceptor->___callPlugins() called at [generated/code/Magento/Checkout/Controller/Onepage/Success/Interceptor.php:26]\n#13 Magento\Checkout\Controller\Onepage\Success\Interceptor->dispatch() called at [vendor/magento/framework/App/FrontController.php:162]\n#14 Magento\Framework\App\FrontController->processRequest() called at [vendor/magento/framework/App/FrontController.php:98]\n#15 Magento\Framework\App\FrontController->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]\n#16 Magento\Framework\App\FrontController\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]\n#17 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php:99]\n#18 Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]\n#19 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [app/code/Mirasvit/CacheWarmer/Plugin/Debug/OnHitPlugin.php:140]\n#20 Mirasvit\CacheWarmer\Plugin\Debug\OnHitPlugin->aroundAroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]\n#21 Magento\PageCache\Model\App\FrontController\BuiltinPlugin\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]\n#22 Magento\PageCache\Model\App\FrontController\BuiltinPlugin\Interceptor->___callPlugins() called at [generated/code/Magento/PageCache/Model/App/FrontController/BuiltinPlugin/Interceptor.php:26]\n#23 Magento\PageCache\Model\App\FrontController\BuiltinPlugin\Interceptor->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]\n#24 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]\n#25 Magento\Framework\App\FrontController\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]\n#26 Magento\Framework\App\FrontController\Interceptor->dispatch() called at [vendor/magento/framework/App/Http.php:116]\n#27 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]\n#28 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]\n#29 Magento\Framework\App\Bootstrap->run() called at [index.php:39]\n","url":"/checkout/onepage/success/","script_name":"/index.php","report_id":"36209293a74e8a1a4c09d455d7907e74f6e256326a6b65b3bb4cff3f84501279"}

how to solve this? with easiest way because i don't have experience with this.. thanks

3 REPLIES 3

Re: There has been an error processing your request after complete payment on magento 2

Hi @mohammed_alahma 

 

Please share the code of Vendor\AutoInvoice\Observer\OrderObserver file

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: There has been an error processing your request after complete payment on magento 2

thanks for your response

 

<?php

namespace Vendor\AutoInvoice\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\ResourceModel\Order\Invoice as InvoiceResource;
use \Magento\Framework\Exception\LocalizedException;
use \Psr\Log\LoggerInterface;

class OrderObserver implements ObserverInterface
{

    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory
     */
    protected $_invoiceCollectionFactory;

    /**
     * @var \Magento\Sales\Api\InvoiceRepositoryInterface
     */
    protected $_invoiceRepository;

    /**
     * @var \Magento\Sales\Model\Service\InvoiceService
     */
    protected $_invoiceService;

    /**
     * @var \Magento\Framework\DB\TransactionFactory
     */
    protected $_transactionFactory;

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $_orderRepository;

    /**
     * @var InvoiceResource
     */
    protected $invoiceResource;

    /**
     * @var \Magento\Sales\Model\OrderFactory
     */
    protected $orderModel;

    /**
     * @var \Magento\Sales\Model\Order\Email\Sender\InvoiceSender
     */
    protected $invoiceSender;

    /**
     * Logger
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * Message Manager
     * @var MessageManager
     */
    protected $messageManager;

    /**
     * @param \Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory $invoiceCollectionFactory
     * @param \Magento\Sales\Model\Service\InvoiceService $invoiceService
     * @param \Magento\Framework\DB\TransactionFactory $transactionFactory
     * @param \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     * @param \Magento\Sales\Model\OrderFactory $orderModel
     * @param InvoiceResource $invoiceResource
     * @param InvoiceSender $invoiceSender
     * @param LoggerInterface $logger
     */
    public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory $invoiceCollectionFactory,
        \Magento\Sales\Model\Service\InvoiceService $invoiceService,
        \Magento\Framework\DB\TransactionFactory $transactionFactory,
        \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Model\OrderFactory $orderModel,
        \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        InvoiceResource $invoiceResource,
        LoggerInterface $logger
    ) {
        $this->_invoiceCollectionFactory = $invoiceCollectionFactory;
        $this->_invoiceService = $invoiceService;
        $this->_transactionFactory = $transactionFactory;
        $this->_invoiceRepository = $invoiceRepository;
        $this->_orderRepository = $orderRepository;
        $this->orderModel = $orderModel;
        $this->invoiceSender = $invoiceSender;
        $this->messageManager = $messageManager;
        $this->invoiceResource = $invoiceResource;
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderId = $observer->getEvent()->getOrder()->getId();
        if (!$orderId) {
            throw new LocalizedException(__('The order no longer exists.'));
        }
        $this->createInvoice($orderId);
    }

    /**
     * Create Invoice Based on Order Object
     * @param \Magento\Sales\Model\Order $order
     * @return $this
     */
    protected function createInvoice($orderId)
    {
        try {
            $order = $this->_orderRepository->get($orderId);

            if (!$order->canInvoice()) {
                return false;
            }

            if ($order && $this->checkPaymentMethod($order) && $this->checkStateOrder($order)) {
                $invoices = $this->_invoiceCollectionFactory->create()
                    ->addAttributeToFilter('order_id', array('eq' => $order->getId()));

                $invoices->getSelect()->limit(1);

                if ((int)$invoices->count() !== 0) {
                    $invoices = $invoices->getFirstItem();
                    $invoice = $this->_invoiceRepository->get($invoices->getId());
                    $this->sendInvoiceMail($invoice);
                    return $invoice;
                }

                $invoice = $this->_invoiceService->prepareInvoice($order);
                if (!$invoice) {
                    throw new LocalizedException(__('We can\'t save the invoice right now.'));
                }

                if (!$invoice->getTotalQty()) {
                    throw new LocalizedException(
                        __('You can\'t create an invoice without products.')
                    );
                }

                $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
                $invoice->register();
                $invoice->getOrder()->setCustomerNoteNotify(false);
                // $invoice->getOrder()->setIsInProcess(true);
                $order->addStatusHistoryComment(__('Automatically INVOICED by kamilha.com'), true);
                $transactionSave = $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder());
                $transactionSave->save();

                // send invoice emails, If you want to stop mail disable below try/catch code
                try {
                    $this->invoiceSender->send($invoice);
                    $this->invoiceResource->saveAttribute($invoice, 'send_email');
                } catch (\Exception $e) {
                    $this->messageManager->addError(__('We can\'t send the invoice email right now.'));
                }
            }
        } catch (\Exception $e) {
            throw new LocalizedException(__($e->getMessage()));
        }
        return $invoice;
    }

    /**
     * Check State Order
     * @param $order
     * @return bool
     */
    protected function checkStateOrder($order)
    {
        if ($order->getState() !== Order::STATE_PENDING_PAYMENT && ($order->getState() === Order::STATE_NEW || $order->getState() === Order::STATE_PROCESSING)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Validate payment methods
     * Not banktransfer, cashondelivery or bankcheck etc
     * Offline Payment methods are Cash on delivery, Check/Money order, Bank transfer.
     * @param $order
     * @return bool
     */
    protected function checkPaymentMethod($order)
    {
        // get the payment method for corresponding order
        $offlinepayment = array('cashondelivery', 'checkmo', 'banktransfer');
        $payment_code   = $order->getPayment()->getMethodInstance()->getCode();
        //$isOffline = $order->getPayment()->getMethodInstance()->isOffline();
        if (in_array($payment_code, $offlinepayment)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 
     * @comment Send invoice mail to customer
     * @param $invoice
     * @throws LocalizedException
     */
    protected function sendInvoiceMail($invoice)
    {
        if (!$invoice->getEmailSent()) {
            try {
                $this->invoiceSender->send($invoice);
                $this->invoiceResource->saveAttribute($invoice, 'send_email');
            } catch (\Exception $e) {
                $this->logger->error($e->getMessage());
            }
        }
    }
}

Re: There has been an error processing your request after complete payment on magento 2

Hello @mohammed_alahma ,

 

Looks like some issue with the module "Mirasvit_CacheWarmer"

in file "app/code/Mirasvit/CacheWarmer/Plugin/Debug/OnHitPlugin.php"

at line "140"

 

Share code for that file.

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy