0

I am customizing the address form to add custom arithmetic captcha. We are using ClassyLlama\AvaTax module to validate the customer address in Model File.

Now, to validate my custom captcha on server side i have used plugin method around. To validate the captcha, i have to check post data. using around plugin method How can i check posted data response? I did try $this->getRequest()->getPostValues() and $subject->getRequest()->getPOstValues() but it throws call to undefined argument 4 error.

Here is my plugin file code:

   class ValidAddressManagement 
   {
   /**
  * @var ValidateInterface
 */
protected $validate;

/**
 * @var RemoteAddress
 */
protected $remoteAddress;

/**
 * @var Config
 */
  protected $config;

   /**
   * @var \Magento\Framework\Controller\Result\RedirectFactory
  */
   protected $resultRedirectFactory;

   /**
   * @var \Magento\Framework\Module\Manager
   */
  protected $moduleManager;

   /**
    * @var \Magento\Framework\Message\ManagerInterface
    */
   private $messageManager;


    public function __construct(
    ValidateInterface $validate,
    RemoteAddress $remoteAddress,
    \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    \Magento\Framework\Module\Manager $moduleManager
)
{
    $this->validate = $validate;
    $this->remoteAddress = $remoteAddress;
    $this->resultRedirectFactory = $resultRedirectFactory;
    $this->messageManager = $messageManager;
    $this->moduleManager = $moduleManager;
}


/**
 * @param \ClassyLlama\AvaTax\Model\ValidAddressManagement $subject
 * @param \Closure $proceed
 * @param \Magento\Customer\Api\Data\AddressInterface $address
 * @param null $storeId
 * @return \Magento\Framework\Controller\Result\Redirect|mixed
 */
public function aroundSaveValidAddress(
    \ClassyLlama\AvaTax\Model\ValidAddressManagement $subject,
    \Closure $proceed,
    \Magento\Customer\Api\Data\AddressInterface $address,
    $storeId = null
)
{
      /*Here Trying to get the Post Data Response but it throws error.  How can i get post reponse. */
        $reCaptchaResponse = $subject->getRequest()->getPostValues();
        if (!isset($reCaptchaResponse)) {
            throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('reCaptcha validation failed, please try again.'));
        }
        $remoteIp = $this->remoteAddress->getRemoteAddress();
        if (!$this->validate->validate($reCaptchaResponse, $remoteIp)) {
            throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('reCaptcha validation failed, please try again.'));
        }

     return $proceed($address,$storeId);
     }

        }

Is there anything i am missing?

Any help would be appreciated.

Thank you,

0

Try using Magento\Framework\App\Request\Http for getting request.


class ValidAddressManagement
{
    /**
     * @var ValidateInterface
     */
    protected $validate;

    /**
     * @var RemoteAddress
     */
    protected $remoteAddress;

    /**
     * @var Config
     */
    protected $config;

    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    private $messageManager;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    private $request;

    /**
     * ValidAddressManagement constructor.
     * 
     * @param ValidateInterface $validate
     * @param RemoteAddress $remoteAddress
     * @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param \Magento\Framework\App\Request\Http $request
     */
    public function __construct(
        ValidateInterface $validate,
        RemoteAddress $remoteAddress,
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->validate = $validate;
        $this->remoteAddress = $remoteAddress;
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->messageManager = $messageManager;
        $this->moduleManager = $moduleManager;
        $this->request = $request;
    }


    /**
     * @param \ClassyLlama\AvaTax\Model\ValidAddressManagement $subject
     * @param \Closure $proceed
     * @param \Magento\Customer\Api\Data\AddressInterface $address
     * @param null $storeId
     * @return \Magento\Framework\Controller\Result\Redirect|mixed
     */
    public function aroundSaveValidAddress(
        \ClassyLlama\AvaTax\Model\ValidAddressManagement $subject,
        \Closure $proceed,
        \Magento\Customer\Api\Data\AddressInterface $address,
        $storeId = null
    ) {
        /*Here Trying to get the Post Data Response but it throws error.  How can i get post reponse. */
        $reCaptchaResponse = $this->request->getParam('YourFieldName');
        if (!isset($reCaptchaResponse)) {
            throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('reCaptcha validation failed, please try again.'));
        }
        $remoteIp = $this->remoteAddress->getRemoteAddress();
        if (!$this->validate->validate($reCaptchaResponse, $remoteIp)) {
            throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('reCaptcha validation failed, please try again.'));
        }

        return $proceed($address, $storeId);
    }
}
  • Setting up the Http Request is also giving same error: 'Uncaught Error: Call to undefined method Magento\\Framework\\App\\Request\\Http::getPostValues() in – Jackson Wyss Feb 20 at 7:01
  • What about $this->request->getParam('YourFieldName');? – Sohel Rana Feb 20 at 7:06
  • That Returns empty and if i enter $this->request->getParam() then we are getting Warning: Missing argument 1 for Magento\Framework\HTTP\PhpEnvironment\Request::getParam(), called in – Jackson Wyss Feb 20 at 7:38
  • Also checked $this->request->getPost() which is also not working. – Jackson Wyss Feb 20 at 7:39
  • Sorry then, I have no copy of this custom module for checking. – Sohel Rana Feb 20 at 7:40

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.