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

lat4732's avatar
Level 12

How to execute controller actions after success ajax return

Hello everyone! I've integrated stripe payment and everything looks great! The only thing I don't know how to continue is how to execute some controller code when the AJAX query return success response. Any ideas? Here is the javascript code:

stripe.confirmCardPayment(clientSecret, {
                                payment_method: {
                                    card: card,
                                    billing_details: {
                                        name: '{{ Auth::user()->name }}'
                                    }
                                }
                            }).then(function(result) {
                                if (result.error) {
                                    $("#card-errors").text(result.error.message);
                                } else {
                                    if (result.paymentIntent.status === 'succeeded') {
                                        $("#card-successes").html('<i class="icon-check-1"></i> You have successfully paid!');
                                        $("#submit_payment").empty();
                                        $("#submit_payment").html('<i class="icon-spinner1 animate-spin"></i> Redirecting...');
                                        setTimeout(function() {
                                            location.href = "{{ route('home') }}";
                                        }, 2500);
                                    }
                                }
                            });

(I need to update some database values)

0 likes
8 replies
martinbean's avatar

@laralex What do you mean? If a controller returns a response then it’s done. A controller doesn’t return a response and then do more stuff.

What is it you’re actually trying to do? Because a snippet of Stripe Elements code doesn’t really explain anything about what you want to do in your controller.

lat4732's avatar
Level 12

@martinbean Currently I have a controller with a method that creates a Payment Intent and gets the "Client Secret" which is required for the AJAX request that I posted above. How can I do something with my database after the payment is successful?

Tray2's avatar

@LarAlex You need to make another ajax call.

An example on how it could look (Never used stripe myself so not sure on what it returns)

let response = await paymentRequest(params);
if (successfullResponse(response) {
	setPaymentDone();
}
martinbean's avatar

@LarAlex You don’t. You use webhooks to listen for the payment intent completing like the Stripe docs say to.

lat4732's avatar
Level 12

@martinbean Have you had any experience with webhooks? Is it difficult to understand? Can a beginner listen to webhooks easily?

Sinnbeck's avatar

@LarAlex It is just another POST route where you disable CSRF to allow the webhook to call it. Then you process the data in there. The docs normally tell you want kind of data to expect.

lat4732's avatar
Level 12

@Sinnbeck Okay thanks, I'll try deal with them. Otherwise I'll keep it with the AJAX call as @tray2 suggested.

OriOn's avatar

@LarAlex implementing yourself other AJAX to records paiment success/failure whatever the first call will reply need to foresee all types of response the payment service can send back to your request.

following @sinnbeck 's approach you keep the responsibility on the service you pay to do so.

Please or to participate in this conversation.