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

adamjhn's avatar

StripeCheckout with ajax instead of redirect to other page

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,
            ]);
        }

Route:

    Route::post('/charge', [
        'uses' => 'RegistrationController@charge',
        'as'   => 'registration.charge'
    ]);

When the user clicks in pay the stripe modal appears the user fills the form and click in Pay button the Stripe validates and send the token and the user is redirected to another page (http://proj.test/charge) because of the charge().

Do you know how to instead of redirecting the user to (http://proj.test/charge) change Stripe code to use Ajax so the user remains on the same page? So that is possible to show in that some page a success message, for example, informing that the payment was completed.

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();
    });

Like below is not working (it shows Parse error: syntax error, unexpected ':', expecting ',' or ')')

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

                    $.ajax({
                        type: "POST",
                        url: '{{route('registration.charge}}',
                        data: {tokenid: token.id, email: token.email},
                        success: function(data) {
                            if (data == 'success') {
                                console.log("success");
                            }
                            else {
                                console.log("error");
                               console.log("Ajax Error!");

                            }

                        },
                        error: function(data) {
                            console.log(data);
                        }
                    }); 
                }
            });


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

                stripe.open({
                    name: 'test',
                    description: 'test',
                    amount: '{{session('total')}}'
                });
                e.preventDefault();
            });
0 likes
9 replies
Cronix's avatar
Cronix
Best Answer
Level 67

You're missing the end quote, as well as the end parenthesis, on the route() which is why there is a syntax error.

url: '{{route('conferences.charge}}',

should be

url: '{{ route('conferences.charge') }}',
1 like
adamjhn's avatar

Thanks, now the error dont appears, but the ajax is not working, the user is redirected to "http://proj.test/charge" after fill the Stripe modal fields and click in the Pay button. Do you know why? It appears the message " console.log("Ajax Error!")" and then the user is redirected to "http://proj.test/charge".

adamjhn's avatar

Removing this " document.getElementById('paymentForm').submit(); " the user is not redirected to another page. But its not working, on the console appears "jquery.min.js:4 POST proj.test/charge 419 (unknown status)".

adamjhn's avatar

Thanks, I dont have ssl enabled on the site, the site is on localhost using laravel valet. Its possible to have and test with https on localhot?

Cronix's avatar

Sure, all of mine do. I want to be sure mixed content errors are taken care of before it hits production. It's so much easier to know everything will work when your dev env mirrors the production site as close as possible. It's the main reason I use homestead vm, which is an Ubuntu OS.

I don't use valet, but it's in the docs: https://laravel.com/docs/5.6/valet#securing-sites

1 like
adamjhn's avatar

Thanks with "valet secure proj/" it appears some errors. Maybe is also necessary to have some certificate to use valet secure to test on localhost?

    Warning: file_put_contents(/Users/adam/.valet/Certificates/layout/.test.conf): failed to open stream: No such file or directory in /Users/adam/.composer/vendor/laravel/valet/cli/Valet/Filesystem.php on line 125

    Warning: chown(): No such file or directory in /Users/adam/.composer/vendor/laravel/valet/cli/Valet/Filesystem.php on line 254

    Warning: file_put_contents(/Users/adam/.valet/Nginx/layout/.test): failed to open stream: No such file or directory in /Users/adam/.composer/vendor/laravel/valet/cli/Valet/Filesystem.php on line 125

    Warning: chown(): No such file or directory in /Users/adam/.composer/vendor/laravel/valet/cli/Valet/Filesystem.php on line 254

    In Brew.php line 179:
                                   
    Unable to determine linked PHP.  
                                   

Cronix's avatar

Yes, ssl requires a ssl certificate. I have no idea about whether valet does it automatically or you have to create your own though. I haven't ever used it. It's too limited for my needs and I prefer using actual VMs.

That issue seems to have more to do with composer, or where you ran the command from. Did you restart valet?

Reached's avatar

Valet automatically assigns an SSL.

As to your other issue, the fix for me was to globally update my composer requirements.

Please or to participate in this conversation.