4

Getting Invalid Form Key. Please refresh the page error not able to do any action after upgrade of Magento 2.2.5 to Magento 2.3.

  • Are you on local server ? like xampp ? – anonymous Feb 13 at 8:19
  • Yes, im using xampp for local – Bangaram sandhya Feb 13 at 8:21
  • 2
    What's your base Url ? If its localhost/your-project-name than you can give a try to replace localhost with 127.0.0.1 Run setup:upgrade It should solve your issue. – anonymous Feb 13 at 8:28
  • i don't think so its a server issue, because till now i'm using magento 2.2.5 there is no issues when i upgraded to 2.3 then only this issue came. – Bangaram sandhya Feb 13 at 9:49
4

Finally I got the solution

I have a custom payment method that uses the cc-form to take credit card payments with and without 3dsecure. After placing order with 3dsecure, I am re-directing to 3dsecure page as normal, but on returning from 3dsecure, I am re-directed to the home page with "Invalid Form Key. Please refresh the page why because in Magento 2.3 core payment methods are using CsrfAwareActionInterface for each controller."

So now i have implemented same thing in my custom payment method as below

use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;

class CustomPaymentResponse extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface

/** 
 * @inheritDoc
 */
public function createCsrfValidationException(
    RequestInterface $request 
): ?InvalidRequestException {
    return null;
}

/**
 * @inheritDoc
 */
public function validateForCsrf(RequestInterface $request): ?bool
{
    return true;
}

Note : you can get reference from core module. Here is the core file path vendor\magento\module-authorizenet\Controller\Directpost\Payment\BackendResponse.php.

  • I have tried this but this is not working with me . I used a custom payment method (paytab) and the call back url from the payment method is giving me a 302 found and redirects to home page with the same message. – Melvin Apr 24 at 7:35
  • Thanks @bangaram you have saved our time. – Nagaraju K Oct 25 at 16:45
  • validateForCsrf is there for security reasons! you must not always return true! – Philipp Sander Nov 5 at 13:21
2
    use Magento\Framework\App\CsrfAwareActionInterface;
    use Magento\Framework\App\Request\InvalidRequestException;
    use Magento\Framework\App\RequestInterface;

    class CustomPaymentResponse extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface

    /** 
     * @inheritDoc
     */
    public function createCsrfValidationException(
        RequestInterface $request 
    ): ?InvalidRequestException {
        return null;
    }

    /**
     * @inheritDoc
     */
    public function validateForCsrf(RequestInterface $request): ?bool
    {
        return true;
    }
   /**
     * Dispatch request
     *
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
   public function execute()
    {
    //your response check
    }

Your response controller should be like this, then only form key issue will fix.

Referrence: https://github.com/magento/magento2/issues/19712

0

I am facing issue several times my working URL is here https://magento-331546-1017597.cloudwaysapps.com/paint-mediums/oil-paint-mediums.html

please any one help me. @Bangaram sandhya

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.