0

I created one module for bank transfer which will collect data from customer. This is working fine when i placed an order. But when i edit an order from admin and try to add comments to order then it is giving below error message.

1 exception(s):
Exception #0 (Magento\Framework\Webapi\Exception): Request does not match any route.

Exception #0 (Magento\Framework\Webapi\Exception): Request does not match any route.
#0 /var/www/html/b2b/vendor/magento/module-webapi/Controller/Rest/RequestValidator.php(83): Magento\Webapi\Controller\Rest\Router->match(Object(Magento\Framework\Webapi\Rest\Request))
#1 /var/www/html/b2b/vendor/magento/module-webapi/Controller/Rest/RequestValidator.php(68): Magento\Webapi\Controller\Rest\RequestValidator->checkPermissions()
#2 /var/www/html/b2b/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php(86): Magento\Webapi\Controller\Rest\RequestValidator->validate()
#3 /var/www/html/b2b/app/code/Ibnab/Additional/Observer/SaveBankInfoToOrderObserver.php(21): Magento\Webapi\Controller\Rest\InputParamsResolver->resolve()

Here is the complete code of my observer .

<?php
namespace Ibnab\Additional\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\OfflinePayments\Model\Banktransfer;
class SaveBankInfoToOrderObserver implements ObserverInterface {

    protected $_inputParamsResolver;
    protected $_quoteRepository;
    protected $logger;
    protected $_state;

    public function __construct(\Magento\Webapi\Controller\Rest\InputParamsResolver $inputParamsResolver, \Magento\Quote\Model\QuoteRepository $quoteRepository, \Psr\Log\LoggerInterface $logger,\Magento\Framework\App\State $state) {
        $this->_inputParamsResolver = $inputParamsResolver;
        $this->_quoteRepository = $quoteRepository;
        $this->logger = $logger;
        $this->_state = $state;
    }

    public function execute(EventObserver $observer) {
        $inputParams = $this->_inputParamsResolver->resolve();
        if($this->_state->getAreaCode() != \Magento\Framework\App\Area::AREA_ADMINHTML){
        foreach ($inputParams as $inputParam) {
            if ($inputParam instanceof \Magento\Quote\Model\Quote\Payment) {
                $paymentData = $inputParam->getData('additional_data');
                $paymentOrder = $observer->getEvent()->getPayment();
                $order = $paymentOrder->getOrder();
                $quote = $this->_quoteRepository->get($order->getQuoteId());
                $paymentQuote = $quote->getPayment();
                $method = $paymentQuote->getMethodInstance()->getCode();

                if ($method == Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE) {
                    if(isset($paymentData['bankowner'])){
                    $paymentQuote->setData('bankowner', $paymentData['bankowner']);
                    $paymentOrder->setData('bankowner', $paymentData['bankowner']);
                    }
                    if(isset($paymentData['banknumber'])){
                    $paymentQuote->setData('banknumber', $paymentData['banknumber']);
                    $paymentOrder->setData('banknumber', $paymentData['banknumber']);
                    }
                    if(isset($paymentData['banktransid'])){
                    $paymentQuote->setData('banktransid', $paymentData['banktransid']);
                    $paymentOrder->setData('banktransid', $paymentData['banktransid']);
                    }
                    if(isset($paymentData['bankname'])){
                    $paymentQuote->setData('bankname', $paymentData['bankname']);
                    $paymentOrder->setData('bankname', $paymentData['bankname']);
                    }
                }
            }
        }

       // print_r($paymentQuote);exit;
       }
    }
}

Can any one help me on how to resolve this error?

  • error happens before or after you save the edit? – fmsthird May 2 at 5:36
  • I am calling above observer on sales_order_payment_save_before – Dinesh May 2 at 5:40
  • the error is about the webapi route – fmsthird May 2 at 5:42
  • yes but i didn't created any web_api.xml file. can i create one? – Dinesh May 2 at 5:43
  • <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="sales_order_payment_save_before"> <observer name="save_bank_infos" instance="Ibnab\Additional\Observer\SaveBankInfoToOrderObserver"/> </event> </config> – Dinesh May 2 at 5:44
0

You are getting error due using Magento\Webapi\Controller\Rest\InputParamsResolver.

Please update your Observer code as follows

step 1:

Update SaveBankInfoToOrderObserver.php file code as given below.

File:/app/code/Ibnab/Additional/Observer/SaveBankInfoToOrderObserver.php

<?php
namespace Ibnab\Additional\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\OfflinePayments\Model\Banktransfer;



class SaveBankInfoToOrderObserver implements ObserverInterface {

    protected $_inputParamsResolver;
    protected $_quoteRepository;
    protected $logger;
    protected $_state;
    protected $_request;

    public function __construct(
    \Magento\Webapi\Controller\Rest\InputParamsResolver $inputParamsResolver,
     \Magento\Quote\Model\QuoteRepository $quoteRepository,
      \Psr\Log\LoggerInterface $logger,
      \Magento\Framework\App\State $state,
      \Magento\Framework\App\Request\Http $request
      ) {
        $this->_inputParamsResolver = $inputParamsResolver;
        $this->_quoteRepository = $quoteRepository;
        $this->logger = $logger;
        $this->_state = $state;
         $this->_request = $request;
    }


       public function execute(EventObserver $observer) {        
         $route      = $this->_request->getRouteName();          
        if($route ==''){ 
        $inputParams = $this->_inputParamsResolver->resolve();        
        if($this->_state->getAreaCode() != \Magento\Framework\App\Area::AREA_ADMINHTML){
        foreach ($inputParams as $inputParam) {
            if ($inputParam instanceof \Magento\Quote\Model\Quote\Payment) {
                $paymentData = $inputParam->getData('additional_data');
                $paymentOrder = $observer->getEvent()->getPayment();
                $order = $paymentOrder->getOrder();
                $quote = $this->_quoteRepository->get($order->getQuoteId());
                $paymentQuote = $quote->getPayment();
                $method = $paymentQuote->getMethodInstance()->getCode();
                if ($method == Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE) {
                    if(isset($paymentData['bankowner'])){
                    $paymentQuote->setData('bankowner', $paymentData['bankowner']);
                    $paymentOrder->setData('bankowner', $paymentData['bankowner']);
                    }
                }
            }
        }
       }
      }

    }

}

Step 2: Run DI compile

sudo php bin/magento setup:di:compile

Now you can cancel order from Frontend and backend without getting any error.

1

You may try to move the events.xml file

from

app/code/Ibnab/Additional/etc/

to

app/code/Ibnab/Additional/etc/frontend/

So the event observer observes event for frontend only.

  • I tried this code , it is working for backend. In frontend when customer wants to cancel an order it is giving error. Any other solution? – Dinesh May 2 at 9:46
  • Are you getting the same error (i.e. Request does not match any route)? when canceling an order from the frontend – Pritam Info 24 May 2 at 9:54
  • yes getting same error when i cancel order from frontend customer. – Dinesh May 2 at 10:08
  • Please check my 2nd answer. – Pritam Info 24 May 2 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.