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

theUnforgiven's avatar

Stripe errors and display them to the user

Hi All,

I'm having a little brain freeze with some jQuery....

I have this JS when the button is clicked:

$.ajax({
        type: 'POST',
        url: '/api/plans/auto-billing',
        data: $('#form').serialize(),
        success: function (data) {
            console.log('Success ' + data);
            window.location.href = data.redirect;
        },
        error: function (xhr, status, error) {
        
            console.log('Error! ' + error.message);
        },
});

But within my controller I have Stripe's errors in a try catch statement, but this one mainly is the one I want to return:

catch(\Stripe\Error\InvalidRequest $e) { 
       response()->json(['message' => $e->getMessage()], 400);
}

But I can't for the life of my remember how to return from the controller to the JS, can one of you awesome folks help a brother out :)

Thanks, in advance!

0 likes
4 replies
Sinnbeck's avatar

Can you try checking xhr.responseJSON in your jQuery?

Please or to participate in this conversation.