Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thecodefreak's avatar

User logs out and session terminated after successful payment in laravel

Hello geniuses, I am new to laravel and laracast both so I know its a weird question but went through most of answers on google but nothing helped!. I am working on a shopping cart with cart data put in session. Everything works fine until the user checkout and make the payment but when returns back to the success url route nothing except _token value is found inside the session. All data of user login as well as cart data is lost everytime.

Here is my controller :


namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Model\Cart;
use App\Model\Course;
use Session;
use App\Model\Category;
use App\Model\SubCategory;
use App\Model\Order;

class CartController extends Controller
{
    public function addCourseToCart(Request $request, $id)
    {
    	$this->course = new Course();
    	$course = $this->course->getCourseData($id)->first();
    	$oldCart = Session::has('cart') ? Session::get('cart') : null;
    	$cart = new Cart($oldCart);
    	$cart->add($course, $course->id);
    	$request->session()->put('cart', $cart);
        $request->session()->save();
    	return redirect()->back();
    }

    public function getCart()
    {
    	if(!Session::has('cart'))
    	{
    		$products = null;
            return view('learner.shopping-cart')->with('products', $products);
    	}
    	$oldCart = Session::get('cart');
    	$cart = new Cart($oldCart);
    	$products = $cart->items;
    	$totalPrice = $cart->totalPrice;
    	return view('learner.shopping-cart')->with('products', $products)->with('totalPrice', $totalPrice)->with('totalQty', $cart->totalQty);
    }

    public function removeCourseFromCart(Request $request, $id)
    {
    	$cart = Session::get('cart');
        dd($cart);
        $quantity = $cart->items[$id]['Qty'];
        $value = $cart->items[$id]['price']; 
        $cart->totalQty = $cart->totalQty - $quantity;
        $cart->totalPrice = $cart->totalPrice - $value;
        unset($cart->items[$id]);
        $request->session()->put('cart', $cart);
        return redirect()->back();
    }

    public function orderSuccess(Request $request)
    {
        dd($request->session()->all()); 
        // Here is the problem cart data lost and user logs out so cant even store the order data in database properly

        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);
        $order_ref_no = 'LMS'.date("Ymd").mt_rand(100000, 999999);
        $order = Order::create([

                    'student_id'        =>  '1213112',
                    //'student_id'        =>  Auth::guard('learner')->user()->id,
                    // 'course_id'         =>  ,
                    // 'test_series_id'    =>  ,
                    // 'ebook_id'          =>  ,
                    'payment_txn_id'    =>  $request->txnid,
                    'amount_paid'       =>  $request->amount,
                    'order_ref_no'      =>  $order_ref_no,
                    //'cart_details'      =>  serialize($cart),
                    'payment_status'    =>  1,
                    'order_from_ip'     =>  request()->ip(),
                    'payment_mode'      =>  request()->mode,
                    'bank_ref_num'      =>  request()->bank_ref_num,
                    'payuMoneyId'       =>  request()->payuMoneyId,
                    'source_id'         =>  request()->mihpayid,
                    'payment_date_time' =>  request()->addedon,
                    

                ]);

        return view('learner.order-success');
    }

    public function checkout(Request $request)
    {
        
        if(!Session::has('cart'))
        {
            $products = null;
            return view('learner.shopping-cart')->with('products', $products);
        }
        
        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);
        $products = $cart->items;
        $totalPrice = $cart->totalPrice;


        $SALT = " xxxxxxxxx ";   // Salt Key

        $PAYU_BASE_URL = "https://sandboxsecure.payu.in";       // For Sandbox Mode
        //$PAYU_BASE_URL = "https://secure.payu.in";            // For Production Mode
        
        $action = '';
        $hash = '';

        $key = ' xxxxxxxx ';   // Merchant Key 
        $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
        $amount = Session::get('cart')->totalPrice;
        $firstname = Auth::guard('learner')->user()->fullname;
        $email = Auth::guard('learner')->user()->contact->email;
        $phone = Auth::guard('learner')->user()->contact->mobile;
        $productinfo = Session::get('cart')->items[array_key_first(Session::get('cart')->items)]['item']['title'].' and '. ((Session::get('cart')->totalQty-1)).' other item(s)';
        $surl = "http://test.com/learner/order-success";
        $furl = route('shopping-cart');
        $service_provider = 'payu_paisa';
        $udf1  = '';
        $udf2  = '';
        $udf3  = '';
        $udf4  = '';
        $udf5  = '';
        $udf6  = '';
        $udf7  = '';
        $udf8  = '';
        $udf9  = '';
        $udf10 = '';

        $hash = hash('sha512', $key.'|'.$txnid.'|'.$amount.'|'.$productinfo.'|'.$firstname.'|'.$email.'|'.$udf1.'|'.$udf2.'|'.$udf3.'|'.$udf4.'|'.$udf5.'|'.$udf6.'|'.$udf7.'|'.$udf8.'|'.$udf9.'|'.$udf10.'|'.$SALT);
        $action = $PAYU_BASE_URL . '/_payment';

        $payment = [
                        'key'                   =>  $key,
                        'txnid'                 =>  $txnid,
                        'amount'                =>  $amount,
                        'firstname'             =>  $firstname,
                        'email'                 =>  $email,
                        'phone'                 =>  $phone,
                        'productinfo'           =>  $productinfo,
                        'surl'                  =>  $surl,
                        'furl'                  =>  $furl,
                        'service_provider'      =>  $service_provider,
                        'udf1'                  =>  '',
                        'udf2'                  =>  '',
                        'udf3'                  =>  '',
                        'udf4'                  =>  '',
                        'udf5'                  =>  '',
                        'udf6'                  =>  '',
                        'udf7'                  =>  '',
                        'udf8'                  =>  '',
                        'udf9'                  =>  '',
                        'udf10'                 =>  '',
                        'hash'                  =>   $hash,
                        'action'                =>  $action,

                    ];
        $token = Session::get('_token');
        return view('learner.order-summary')
                ->with('products', $products)
                ->with('totalPrice', $totalPrice)
                ->with('totalQty', $cart->totalQty)
                ->with('payment', $payment);
    }
  
}




and my config/session.php settings

```'driver' => env('SESSION_DRIVER', 'file'),    also in env file
'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE'),
'http_only' => true,
'same_site' => 'lax',
0 likes
1 reply
thecodefreak's avatar

before checkout the session looks like this

array:5 [▼
  "_token" => "A8CxBmpJzFWtroZMjgGqn1FFGwDsrcbJc3klSLJh"
  "_flash" => array:2 [▶]
  "login_learner_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 16
  "_previous" => array:1 [▶]
  "cart" => App\Model\Cart {#1390 ▶}
]

and after checkout it looks like this

array:1 [▼
  "_token" => "Vs9qKStZD879u9jn89cS2E16VobxoSDiNFjjm6Ag"
]

Please or to participate in this conversation.