0

I have created a redirect controller for the payment gateway for redirection to the payment method host but that controller is being cached when I enable the full page cache of Magento. If I remove full page cache then it is working fine.

Due to the controller being cached, the order total amount is showing the previous order's total on the payment gateway page.

Redirect Controller:-

<?php

namespace ST\Migs\Controller\Hosted;

class Redirect extends \ST\Migs\Controller\Hosted
{

    public function execute()
    {
        $incrementId = $this->_checkoutSession->getLastRealOrderId();
        if (!empty($incrementId)) {
            $order = $this->_getOrder();
            $response = $this->_view->getLayout()
                ->createBlock('ST\Migs\Block\Form\Hosted','st-form-hosted')
                ->setTemplate('ST_Migs::form/redirect.phtml')
                ->setFormUrl($this->_hosted->getFormUrl())
                ->setFormFields($this->_hosted->getFormFields($order))
                ->toHtml();
            $this->getResponse()->setBody($response);
        }

    }

}

Layout st_hosted_redirect.xml :-

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <container name="root">
        <block class="ST\Migs\Block\Form\Hosted" name="st-migs-from-hosted" template="ST_Migs::form/redirect.phtml" cacheable="false"/>
    </container>
</layout>

Is there any solution to disable the cache for this controller and block ?

I have tried adding following solution but none of them worked:-

a) add $this->addData(array('cache_lifetime' => null)); in block constructor.

b) add cacheable="false" in layout for block.

c) add '$page->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0', true);' in controller.

Please let me know if you solved the same problem.

Magento version 2.4.3-p3.

2
  • 1
    When you are redirecting, pass param with redirected url it will bypass from cache. try it I also did to resolve this issue. Commented Nov 15, 2022 at 11:15
  • @ZahidHussainMagento I didn't try this solution but I think it will work also. Commented Nov 16, 2022 at 4:45

1 Answer 1

2

Inside your execute function add this as the first line,

$this->resultPageFactory->create();

This method kicks off some code that loads the layout handles for the request. Then your layout xml will be used and the cache attribute of the blocks will be taken into account and full page caching will be correctly disabled.

Obviously make sure $this->resultPageFactory is included via the constructor and instantiated as \Magento\Framework\View\Result\PageFactory

2
  • 1
    Thanks Luke, It works like charm! Commented Nov 16, 2022 at 4:44
  • I have everything in place, but still, it's getting cached. Even though I've tried the methods from the question, nothing works for me. Basically, I am using a payment module from third-party gateway.
    – Duke
    Commented Oct 31, 2023 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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