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

adamjhn's avatar

Get the total price of a registration properly to show in the Stripe button

I have a multistep form, in the step 3 there is a button "Pay" that when is clicked it shows a Stripe modal using the jQuery below:

    <form action="{{ route('registration.charge') }}" method="post" id="paymentForm">
            {{csrf_field()}}
            <input type="hidden" name="stripeToken" id="stripeToken"/>
            <input type="submit" href="" id="payment" class="btn btn-primary float-right"
                   value="Pay"/>
        </form>

Charge method to handle the Stripe charge:

    public function charge(Request $request)
        {
            Stripe::setApiKey(config('services.stripe.secret'));
            $source = $request->stripeToken;
            Charge::create([
                'currency' => 'eur',
                'description' => 'Example charge',
                'amount' => 2500,
                'source' => $source,
            ]);
        }

My doubt is how to set in amount instead of a static value '2500' insert the correct total price of the registration. And also when the Stripe modal appears I want to show the total price of the registration in Stripe modal button. Do you know how to achieve that?

Inside the charge() maybe a correct approah can be get the the value from the session like:

    $selectedRtypes =  Session::get('selectedRtypes');
    $amount = (collect($selectedRtypes)->first()['total']) * 100;

But do you know how to set the total value also in Stripe button? In the amount of the stripe.open.

Stripe code:

let stripe = StripeCheckout.configure({
  key: "{{config('services.stripe.key')}}",
  image: "",
  locale: "auto",
  token: (token) => {
      document.getElementById('stripeToken').value = token.id;
      document.getElementById('paymentForm').submit();
  }
});

document.getElementById('payment').addEventListener('click', function(e){

  stripe.open({
      name: 'test',
      description: 'test',
      amount: 1000
  });
  e.preventDefault();
});
0 likes
7 replies
Cronix's avatar

$selectedRtypes = Session::get('selectedRtypes');

Where is that being determined (selectedRtypes)? Typically, in javascript, as the user is choosing their product(s) or whatever, I just add them up in the javascript and send that to the button as a variable, as well as creating a hidden form field and populating the total there, which gets sent to the controller. Not sure why you're using session for this, but you didn't give all of the details like how you're determining the amount to charge in the first place.

1 like
adamjhn's avatar

Thanks.

I have a conference details page with a select menu for the user select the tickets that he want to buy for the conference. When the user selects the tickets and clicks "Next" the code goes to the storeQuantities() that stores the selectedRtypes info in the session and returns the user to the registration page where the multistep form is:

class RegistrationController extends Controller
{
public function storeQuantities(Request $request, $id, $slug = null){
    $ttypeQuantities = $request->get('ttypes');
    ...
    $selectedRtypes[$ttype->name]['quantity'] = $quantity;
    $selectedRtypes[$ttype->name]['price'] = $price;
    $selectedRtypes[$ttype->name]['subtotal'] = $price * $quantity;
    $total+=  $selectedRtypes[$ttype->name]['subtotal'];
    $selectedRtypes[$ttype->name]['total'] = $total;
    ...

    Session::put('selectedRtypes', $selectedRtypes);
    ...
    return redirect(route('conferences.registration',['id' => $id, 'slug' => $slug]));
    }
}

Then there is the method associated with the above route 'conferences.registration' that actually displays the registration page that is the page with the multistep form:

public function displayRegistrationPage(Request $request, $id, $slug=null){

        $selectedRtypes =  Session::get('selectedRtypes');
       ...
       return redirect(route('conferences.show',['id' => $id, 'slug' => $slug]));
     
    }
Cronix's avatar

Well you put the data in session, so in the view you'd just need to retrieve it from session and set js variables which you can then pass to the button to charge the correct amount?

1 like
adamjhn's avatar

Ah ok, thanks! Do you know why with:

amount: '{{Session::get('selectedRtypes')->total}}'

shows:

Trying to get property of non-object

Cronix's avatar

Well, you stored it as an array, not an object.

$selectedRtypes[$ttype->name]['total'] = $total;

It's also a multidimensional array, so you'd need to access whatever $ttype->name is too.

@php
    $rTypes = Session::get('selectedRtypes');
    $amount = $rTypes['the-key']['total'];
@endphp
amount: '{{ $amount }}'

Something like that. I don't know what 'the-key' would be for you since it's a variable in the controller where you're setting the session.

1 like
adamjhn's avatar

Thanks the session was stored in the storeQuantities() like:

        Session::put('selectedRtypes', $selectedRtypes);

The dd($selectedRtypes) in storeQuantities() shows:

array:1 [▼
  "geral" => array:6 [▼
    "quantity" => "2"
    "price" => 10
    "subtotal" => 20
    "total" => 20
    "questions" => Collection {#255 ▶}
    "id" => 1
  ]
]

So maybe neeeds to be like:

document.getElementById('payment').addEventListener('click', function(e){

                @php
                    $rTypes = Session::get('selectedRtypes');
                    $amount = collect($rTypes)->first()['total'];
                @endphp

                stripe.open({
                    name: 'test',
                    description: 'test',
                    amount: '{{$amount}}'
                });
                e.preventDefault();
            });

?

Please or to participate in this conversation.