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

osc2nuke's avatar

Edit session per session ID

I have a website where i assign the current session id to a cart instance I want to be able to access a specific session ID, find in it the "cart" instance and remove the "cart" parameter.

Reason: payment callbacks (IPN's/webHooks) should be able to clear the items of a user cart whenever the callback takes place, as not all users return after payment back to the origin website.

0 likes
7 replies
osc2nuke's avatar

@iagoEffting

There is no logged-in user id. The call back comes from a payment-merchant what sends back a response according. I store the session-id into the orders table.

I fetch that session id. And then i wanna lookup the stored session. open it, and remove the cart parameter. I already need to call/open the orders table.

osc2nuke's avatar

@iagoEffting Did you just suggest now i let the payment merchant login as the user? Lol....... it would be an option. As it is or would be genius one way..........., well....... dunno what to say for the other way.

iagoEffting's avatar

Hey @osc2nuke I got it wrong, now I understand your point, its not the session login with you wanna, sorry. In this case you can use database to store your items. Just like that: https://github.com/Crinsane/LaravelShoppingcart

Its that you need?

and I believe it is very easy to understand that I was wrong and did not need irony

@inovamedia-opleiding I do not care about points here in laracast, I came to help, only that. In that case I only understand worg. So, I think a comment like yours does not make sense here.

osc2nuke's avatar

I think i have a solution what can solve my problem. I just not yet figure out how to make it work. I store my complete cart inside the order DB, like: //postCheckout

$order = new Order();
        $order->cart = serialize($cart);
        $order->author_id = $request->merchant_id;
        $order->name = Auth::user()->name;
        $order->email = Auth::user()->email;
        $order->source = $source['id'];
        Auth::user()->orders()->save($order);

        Session::forget('cart');

        return redirect($source->redirect->url);/* goes to payment merchant */

webHook sends status. If the payment succeed i do not need to do anything.

Yet if the payment fails, i should be able to read out the $order->cart = serialize($cart); i stored previously, Instead unserialize it, and re-build the cart again.

So the cart rebuilding is where i am now stucked on. My charge controller (just for testing purposes i rebuild the cart, regardless if the payment is a success)

        if ($request->type == 'source.chargeable') {
            $source = $request->data['object'];
            Stripe::setApiKey("*****");
            $charge = Charge::create(array(
                'amount' => $source['amount'],
                'currency' => $source['currency'],
                'source' => $source['id'],
                'description' => "iDEAL payment for " . $source['owner']['name'],
            ));

            Order::where('source', $source['id'])
                    ->update(['charge' => $charge['id']]);
            $cartContent = Order::where('source', $source['id'])->pluck('cart');

            new Cart(unserialize($cartContent));    /* this part not work as i hoped */        
        }

The Cart model:

    public $items = null;
    public $totalQty = 0;
    public $totalPrice = 0;

    public function __construct($oldCart)
    {
        if ($oldCart){
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
        }
    }

    public function add($item, $id)
    {
        $storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item];
        if ($this->items){
            if(array_key_exists($id, $this->items)){
                $storedItem = $this->items[$id];
            }
        }
        $storedItem['qty']++;
        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;
        $this->totalQty++;
        $this->totalPrice += $item->price;
    }
osc2nuke's avatar
osc2nuke
OP
Best Answer
Level 2

I solved the problem by using a Mutator:

https://laravel.com/docs/5.6/eloquent-mutators

In my order model:

    public function getCartAttribute($cart) {
        return unserialize($cart);
    }

This mutator is required to unserialize(). For some reason without the mutator i get errors like:

laravel unserialize(): Error at offset 0 of 2302 bytes

Then in the success (regardless if the callback response is success/failed, as it is only to test) i do:

    public function success()
    {
        $order = Order::find(12);
        new Cart($order->cart);
        session(['cart' => $order->cart]);
        return view('success');

    }

The cart is now filled-Up again with the previous products!!!!!!!!!!!!!!!!!!!!!!!!!

Perhaps this can also help someone else!

Please or to participate in this conversation.