1

We have to fetch and set a session variable when the user redirects to our magento2 website using session-id from 3rd party site.

It looks like http://magento2.com/?SID=322e00bfqm3g6rfpjumah3939n

For this, we have written a custom router & match method but it's not triggering properly at the first time.

Could you please review the below logic and files and advise please.

Note: The implementation is in the same module only. The concept of this implementation is if the user redirecting to the M2 website from 3rd party we need to save fetch the SID (session-id) & and the user will browse the products on the M2 website. who are coming from 3rd party we don't want to display proceed to checkout button.

I have written a custom router and setting array value in the core session and trying to fetch that value in the plugin which is in the same module.

  1. app/code/Vendor/ModuleName/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="redirect_if_sid" xsi:type="array">
                    <item name="class" xsi:type="string">Vendor\ModuleName\Controller\Router\RewriteSID</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">20</item>
                </item>
            </argument>
            <argument name="cacheable" xsi:type="boolean">false</argument>
        </arguments>
    </type>
    <! -- For Proceed to checkout button -->
    <type name="Magento\Checkout\Helper\Data">
        <plugin name="CartPageProceeedtoToCheckoutButton" type="Vendor\ModuleName\Plugin\AfterProceedToCheckoutButton" disabled="false"/>
    </type>
</config>
  1. Vendor/ModuleName/Controller/Router/RewriteSID.php
<?php
namespace Vendor\ModuleName\Controller\Router;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Session\SessionManagerInterface;
class RewriteSID implements \Magento\Framework\App\RouterInterface
{   

    private $logger;
    private $actionFactory;
    private $response;
    private $formKey; 
    private $coreSession;    

    public function __construct(
        LoggerInterface $logger,
        ActionFactory $actionFactory,
        ResponseInterface $response,
        FormKey $formKey,
        SessionManagerInterface $coreSession

    ) {
        $this->logger = $logger;
        $this->actionFactory = $actionFactory;
        $this->response = $response;
        $this->formKey = $formKey;
        $this->coreSession = $coreSession;

    }

    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = $request->getOriginalPathInfo();
        $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);
        $identifier = $condition->getIdentifier();
        if ($condition->getRedirectUrl()) {
            $this->response->setRedirect($condition->getRedirectUrl());
            $request->setDispatched(true);
            return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');
        }
        if (!$condition->getContinue()) {
            return null;
        }
        if ($request->getParam('SID')) {

            $sessionData = array("Volvo", "BMW", "Toyota");
           try {  
                    $this->coreSession->setVIEUserData($sessionData);
            }catch(\Exception $e) {
                    $this->logger->info("Something went wrong");
                    $this->logger->info($e->getMessage());
            }

        }//get params ends here
        else
        {
            return null;
        }  
    }

}
  1. app/code/Vendor/ModuleName/Plugin/AfterProceedToCheckoutButton.php
<?php
namespace Vendor\ModuleName\Plugin;
use Magento\Framework\Session\SessionManager;
class AfterProceedToCheckoutButton
{


    private $coreSession;

    public function __construct(
        SessionManager $sessionManager
    ) {
        $this->coreSession = $sessionManager;    
    }

    /**
     * @param $subject
     * @param $result
     * @return mixed
    */
    public function afterCanOnepageCheckout(\Magento\Checkout\Helper\Data $subject, $result)
    {
        //Here $this->coreSession->getVIEUserData() unable to fetch here

        if($result) {
            if($this->coreSession->getVIEUserData()) {
                return false;
            }
            return $result;
        }

    }
}
17
  • You want SID value in params, Right ? Dec 13, 2019 at 5:22
  • @NagarajuK, magento.stackexchange.com/a/127617/3368 Dec 13, 2019 at 5:48
  • @SupravatM whether I want to use the customer session concept here? Dec 13, 2019 at 6:31
  • @KishanSavaliya I am able to fetch the param value in the RewriteSID.php & able to set VIEUserData session variable but unable to fetch the value in the plugin file Dec 13, 2019 at 6:33
  • 1
    sure @KishanSavaliya let me check & get back to you shortly Dec 13, 2019 at 6:40

2 Answers 2

0

I see you are using sort_order 20 in your router declaration, try decreasing it to sortOrder 15. Since order 20 is used by URL Rewrites, it could be picking up a URL rewrite instead of your controller on the first visit. Such as redirecting http to https etc.

If that doesn't work, i would advice checking the following things.

  1. Controller is actually firing on first load
  2. Correct execution order (Controller then plugin)
  3. Try a controller_action_predispatch event
0
0

We have fixed the issue by creating and handling by custom controller in my custom module instead of the custom router.

Now we are providing the redirect URL to 3rd party system as mentioned below http://magento2.com/vie/index/redirect/sid/322e00bfqm3g6rfpjumah3939n instead of http://magento2.com/?SID=322e00bfqm3g6rfpjumah3939n so that 3rd party user will call this redirect controller.

From the controller, we are fetching unique id sid & processing further. So we are able to avoid the set session-id issues for the first time.

As well we have unset that session-id once after submitting the cart data to 3rd party on the checkout cart page.

2
  • does this solution work for session clear issue for 3ds redirection? Apr 3, 2020 at 14:04
  • yes it is working @Sree Apr 4, 2020 at 18:52

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.