TimeSocks
4 years ago

How to override Stripe's API error on card decline?

Posted 4 years ago by TimeSocks

I'm putting together a simple one off card payment system with Stripe. So far so good - when a successful payment is made. I'm now writing the code to handle card declines etc. Here's the relevant code:

try {
    $charge = \Stripe\Charge::create([
    "amount" => $grandTotal,
    "currency" => 'usd',
    "source" => $request->stripeToken,
    "description" => '$description'
    ]); 
        
} catch (Stripe_CardError $e) {
    return redirect::refresh()->withFlashMessage($e->getMessage());
}

As you can see, I'm just using the code from Jeffery's series on Stripe - refresh the page with a flash message in the session which I can check for and display a message if it's present.

However, before the app gets a chance to do that, Laravel jumps in with a 500 error:

Card in ApiRequestor.php line 101:
Your card was declined.

And my payment page and error message is thus not displayed. How can I fix this? TIA

Please sign in or create an account to participate in this conversation.