4

In laravel 5.7, session is getting disappeared after redirecting to another page. I am working on an application in which I am pushing users to the payment gateway page before which I am storing the data in a session as per laravel documentation. After coming back from the payment gateway when I try to retrieve that session it returns empty. Can anyone please tell me how can I resolve this issue.

My code is something like this

public function processPayment(Request $request)
 {
    //...........
    session()->put('order_checkout_data', [
        'gateway' => 'paypal',
        'data' => $paypalData
    ]);

    //$request->session()->save();  <!-- This i tried after reading some solution but didnt help

    //print_r(session('order_checkout_data')) <!-- I can see the session here

    $paypal = new PayPal();
    $response = $paypal->purchase($paypalData);

    if ($response->isRedirect()) {
        $response->redirect(); //This is where redireting to paypal
            }
}

public function handleGatewayResponse(Request $request){
    print_r(session('order_checkout_data')); //No data
}

I tried with session global function and facade as well , like these

Session::put('order_checkout_data', [
            'gateway' => 'paypal',
            'data' => $paypalData
        ])

and also

session(['order_checkout_data'=>[
            'gateway' => 'paypal',
            'data' => $paypalData
        ]])

But no value. My env settings likes this

SESSION_DRIVER=file
SESSION_LIFETIME=360

I tried to go through some of the links with a similar problem but that didn't help. Here are the links that I have followed :

user7747472
  • 1,874
  • 6
  • 36
  • 80
  • Do the `handleGatewayResponse` method is called by the PayPal servers ? If so it's normal that your session is empty as they are 2 different clients (one is the user and the other PayPal) – Adrien Mar 06 '19 at 16:35
  • `handleGatewayResponse` is the function handle the response that is being redirected by PayPal gateway after successful payment. But i still think the session should persist with the application right ? since it is not a `flash session`. – user7747472 Mar 06 '19 at 16:37
  • Can you add the code from `$response->redirect();` to your question and try `return $response->redirect();` – Remul Mar 06 '19 at 16:42
  • You can try to use something like this: return redirect()->route('your route', $response); – mindmaster Mar 06 '19 at 16:46
  • @Remul `$response->redirect();` is a method of `paypal` package SDK. It is gatway default function. I am using this package https://github.com/thephpleague/omnipay – user7747472 Mar 06 '19 at 16:48
  • Whats your session driver in .env ? – faizan.sh Mar 06 '19 at 17:54

1 Answers1

1

When you use sessions, a SESSION_ID (or similar) cookie is sent to the browser to know what session is associated with each request.

Your handleGatewayResponse method is called after a request from a user (it's certainly your js script that issues the request but it's the same), and you store data in the session linked to this particular user.

After PayPal finished its job, it does a request to a callback URL. This request is done by PayPal but not by your user you stored the data for. PayPal has no idea of the session cookie, so Laravel start a new empty session.

Adrien
  • 497
  • 4
  • 9
  • Does this mean PayPal should redirect to an unprotected route from which user should continue navigating the site? Will that skip the illusion of an empty session? – I Want Answers Jan 15 '20 at 07:31
  • If I've understood correctly the OP question, the `handleGatewayResponse` function is called by Paypal servers, after having completed the transaction, to confirm the status of the payment. Thus there are two completely different sessions : one between your server and the client and one between your server and Paypal. There must be a unique payment identifier sent by Paypal to the server and known by the client API to link both – Adrien Jan 18 '20 at 19:52