0

How to create a controller action that can be requested by POST properly in own custom payment module in Magento 2.3? How to create a parent class that will be extended by classes corresponding to controller actions which can be requested by POST properly in this version of Magento? I have created such a parent class:

<?php
namespace Billon\Payment\Controller;
/**
 * Ajax source
 */
class AjaxAction extends \Magento\Framework\App\Action\Action
{
    public function __construct(\Magento\Framework\App\Action\Context $context)
    {
        return parent::__construct($context);
    }
    public function execute()
    {
        //(...)
    }
}

The classes of the controller actions looks like this:

<?php
namespace Billon\Payment\Controller\Multishipping;
/**
 * Ajax source that initiate payment in multishipping checkout
 */
class Initiation extends \Billon\Payment\Controller\AjaxAction
{
    /**
     * Initiates the payment in multishipping checkout
     */
    public function execute()
    {
        parent::execute();
        //(...)
    }
}

Such actions can be request in Magento 2.0 - 2.2 by POST (above action by '(store address)/billon/multishipping/initiation'). However I can send only GET request properly to my controller actions in Magento 2.3. After I send POST request to my action, the action redirects to the referrer with HTTP code 302. I found a ugly solution in the parent class:

<?php
namespace Billon\Payment\Controller;
/**
 * Ajax source
 */
class AjaxAction extends \Magento\Framework\App\Action\Action
{
    public function __construct()
    {
        $this->execute();
        die();
    }
    public function execute()
    {
        //(...)
    }
}

This solution disables the translation with a function '__'. How to resolve this problem? How to disable the redirection of POST requests from the controller actions to the referrers in Magento 2.3 in proper way? How should I change my action classes to receive POST request? Could you help me?

0

To create AJAX action for POST requests in Magento 2.3, Please see the code below.

namespace Billon\Payment\Controller\Multishipping;

class Initiation extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;
    protected $jsonHelper;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\Json\Helper\Data $jsonHelper
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->jsonHelper = $jsonHelper;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        try {
            return $this->jsonResponse('your response');
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            return $this->jsonResponse($e->getMessage());
        } catch (\Exception $e) {
            $this->logger->critical($e);
            return $this->jsonResponse($e->getMessage());
        }
    }

    /**
     * Create json response
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function jsonResponse($response = '')
    {
        return $this->getResponse()->representJson(
            $this->jsonHelper->jsonEncode($response)
        );
    }
}
  • The problem is that the method „execute” of parent class, which extends '\Magento\Framework\App\Action\Action', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request? – Kamil Szmit Jan 9 at 14:15
2

For future reference,

I was only able to make my request work by doing the following steps in Magento 2.3.1 otherwise I was getting a 404. I checked class vendor/magento/module-authorizenet/Controller/Directpost/Payment/BackendResponse.php for reference.

  1. Add the following interfaces
    CsrfAwareActionInterface, (especially important because all post requests are using by default CSRF validation)
    HttpGetActionInterface,
    HttpPostActionInterface
  1. Implement the CSRF handling functions
public function validateForCsrf(RequestInterface $request): ?bool
{
    return true;
}

public function createCsrfValidationException(
    RequestInterface $request
): ?InvalidRequestException {
    return null;
}
  1. Execute
php bin/magento setup:upgrade
php cache:flush
0

In Magento 2.3, controllers must implement \Magento\Framework\App\Action\HttpPostActionInterface to be able to handle POST requests - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html

  • Adding 'implements \Magento\Framework\App\Action\HttpPostActionInterface' to the parent class, which extends '\Magento\Framework\App\Action\Action', and to child class, which corresponds to the controller action, change nothing. What should I change additionally? – Kamil Szmit Jan 9 at 15:01

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.