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

CookieMonster's avatar

cannot retrieve session data in my controller

I have an initial form where I fill in some input data and proceed to checkout payment where user enter their credit card credentials.At this view, I do not really want to display the session data. After submitting the payment, it should hit the makeOrder method. Inside, the method, I want to verify if the session data are retrieved. Then process the data accordingly.

My controller looks like this:

  //checkout for credit card payment
    public function checkout(){
        if (!session()->has('rate')) {
            return redirect()->route('order.index');
          }

        $data = session([
            'rate' => session()->get('rate'),
            'hub' => session()->get('hub'),
            'content' => session()->get('content'),
            'value_content' => session()->get('value_content'),
            'pick_up_date' =>session()->get('pick_up_date'),
            'pick_up_time' =>session()->get('pick_up_time'),
            'weight' => session()->get('weight'),
            'delivery_note' => session()->get('delivery_note'),
            'sender_contact_num' => session()->get('sender_contact_num'),
            'recipient_address' => session()->get('recipient_address'),
            'city' => session()->get('city'),
            'state' => session()->get('state'),
            'postcode' => session()->get('postcode'),
            'delivery_date' => session()->get('delivery_date'),
            'delivery_time' => session()->get('delivery_time'),
            'recipient_name' =>session()->get('recipient_name'),
            'recipient_contact_number' => session()->get('recipient_contact_number'),
            'recipient_email' => session()->get('recipient_email')
        ]);

       

        $amount = Rate::find(session()->get('rate'));
        $amount = $amount->cost;

        //create payment intent for payment

        \Stripe\Stripe::setApiKey(env('STRIPE_SECRET_KEY'));

        $intent = \Stripe\PaymentIntent::create([
            'amount' => $amount * 100,
            'currency' => 'myr',
            // Verify your integration in this guide by including this parameter
            'metadata' => ['integration_check' => 'accept_a_payment'],
        ]);
        $intent = $intent->client_secret;
        
        return view('payments.card-payments.index')
                    ->with('intent',$intent)
                    ->with('amount',$amount);
                   
    }

    public function makeOrder(Request $request){
        //process order once payment by card is successful

        dd($request->session()->all());
    }

my index.blade:

<h1>Checkout Payment</h1>

  <form id="payment-form" method="POST" action="{{route('orders.payment.gateway-request')}}" data-secret="{{ $intent }}" class="pt-40">
    @csrf
    <div class="row">
        <div class="col-md-12">
            <h4>Enter your card details.<small>(card information is not stored in our server.)</small></h4>
        </div>
    </div>
   {{session('postcode')}}
   {{session('recipient_email')}}
    Card Holder Name: <input type="text" required placeholder="Card Holder Name" name="cardHolderName" class="block mt-1 w-full mb-4"
                 id="card-holder-name"
                 autofocus
                 autocomplete="name"/> 
    <div id="card-element">
        <!-- Elements will create input elements here -->
    </div>

    <!-- We'll put the error messages in this element -->
    <div id="card-errors" role="alert" class="text-danger"></div>

    
    <button class="btn btn-success mt-5"  name="payment"  value="paymentbycard" type="submit">Pay RM{{$amount}}</button>
</form>

<script>
    var stripe = Stripe('{{ env('STRIPE_PUB_KEY') }}');
    var elements = stripe.elements();

    // Set up Stripe.js and Elements to use in checkout form
    var elements = stripe.elements();
    var style = {
        base: {
            color: "#32325d",
        }
    };

    var card = elements.create("card", { style: style });
    card.mount("#card-element");
    const cardHolderName = document.getElementById('card-holder-name');

    card.on('change', ({error}) => {
            let displayError = document.getElementById('card-errors');
            if (error) {
                displayError.textContent = error.message;
            } else {
                displayError.textContent = '';
            }
        });

        var form = document.getElementById('payment-form');
        //var payButton = document.getElementById('payStripe');

        form.addEventListener('submit', function(ev) {
            ev.preventDefault();
            // If the client secret was rendered server-side as a data-secret attribute
            // on the <form> element, you can retrieve it here by calling `form.dataset.secret`
            stripe.confirmCardPayment(form.dataset.secret, {
                payment_method: {
                    card: card,
                    billing_details: {
                        name: cardHolderName.value
                    }
                }
            }).then(function(result) {
                if (result.error) {
                    // Show error to your customer (e.g., insufficient funds)
                    let displayError = document.getElementById('card-errors');
                    displayError.textContent = result.error.message;
                    console.log(result.error.message);

                } else {
                    // The payment has been processed!
                    if (result.paymentIntent.status === 'succeeded') {

                        form.submit();
                        
                        // Show a success message to your customer
                        // There's a risk of the customer closing the window before callback
                        // execution. Set up a webhook or plugin to listen for the
                        // payment_intent.succeeded event that handles any business critical
                        // post-payment actions.
                    }
                }
            });
        });


 </script>

While I do not need to pass the session to my view, I can retrieve the session value. However, upon submitting the form, the session data is not returned based on my dd(). I noticed in the blade, if I add the session to it like {{Session::put('postcode',session('postcode')}} for example then it would return the session value but I do not think that's a good practice to store session in the blade.

So, why it is not returning the session data?

0 likes
2 replies

Please or to participate in this conversation.