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

Swaz's avatar
Level 20

Cashier: catching stripe errors

How do you catch stripe errors with cashier? I have used stripe (without cashier) in the past and was able to catch errors just fine. I have this code for example:

try {

    $user->subscription('monthly')->withCoupon($coupon)->create($token);

} catch(\Stripe\Error\Card $e) {
    dd("Card declined");
} catch (\Stripe\Error\InvalidRequest $e) {
    dd("Invalid parameters were supplied to Stripe's API");
} catch (\Stripe\Error\Authentication $e) {
    dd("Authentication with Stripe's API failed");
} catch (\Stripe\Error\ApiConnection $e) {
    dd("Network communication with Stripe failed");
} catch (\Stripe\Error\Base $e) {
    dd("Display a very generic error to the user");
} catch (Exception $e) {
    dd("Something else happened, completely unrelated to Stripe");
}

I have tried adding these at the top:

use Stripe;
use Stripe_Error;

But I still keep getting the default laravel error page.

0 likes
6 replies
Kryptonit3's avatar

here is what I did. in app\Exceptions\Handler.php this is what I have and it works.

<?php namespace cablework\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Stripe\Error as Stripe;

class Handler extends ExceptionHandler {

    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {

        if ($e instanceof Stripe\Card)
        {
            session()->flash('error_msg',$e->getMessage() . ' <a class="alert-link" href="'.route('settings.company.creditcard').'">Click here</a> to manage your card.');
            return redirect()->back();
        }

        if ($e instanceof Stripe\RateLimit)
        {
            session()->flash('error_msg','It looks like our payment processor was busy. Please try again in a few minutes.');
            return redirect()->back();
        }

        if ($e instanceof Stripe\Api ||
            $e instanceof Stripe\ApiConnection ||
            $e instanceof Stripe\Authentication ||
            $e instanceof Stripe\InvalidRequest ||
            $e instanceof Stripe\Base)
        {
            session()->flash('error_msg',$e->getMessage());
            return redirect()->back();
        }

        return parent::render($request, $e);
    }

}
5 likes
weareframework's avatar

Hello and I can second that this just sped up a good few hours of work in helping to handle stripe errors. Thank you @Kryptonit3 and I agree with @joelennon that this should be in the docs as handling stripe errors is quite an important one. Nice one and thanks

1 like
xelha110's avatar

Well for me I had to create my own catcher of errors while I was creating a StripeCustomer because ApiRequestor was throwing the errors yes, but I couldnt catch them so I commented the HandleApiError method and

when you get the $customer back from Stripe, there are two options, you get actually the Customer object with the information you need to store OR you get an error object so I catch it

if($customer->error){ return response()->json($customer->error, 402); }

adding the 402 ( payment required ) , so with this in the client side you can read the message and display it to the user,

the response should look something like this.

Object code:"card_declined", decline_code:"generic_decline", message:"Your card was declined." param:"" type:"card_error"

if you use one of the test credit cards in stripe like the next one: 4000000000000341.

hope it helps someone ;)

split19's avatar

As of today, this worked well for me:

/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        // handle stripe errors
        if ($e instanceof \Stripe\Error\Card ||
            $e instanceof \Stripe\Error\Api ||
            $e instanceof \Stripe\Error\ApiConnection ||
            $e instanceof \Stripe\Error\Authentication ||
            $e instanceof \Stripe\Error\InvalidRequest ||
            $e instanceof \Stripe\Error\Base)
        {
            $body = $e->getJsonBody();
            $err  = $body['error']['message'];
            session()->flash('alert','There was a problem processing your request: ' . $err);
            return back();
        }
        
        return parent::render($request, $exception);
    }

Please or to participate in this conversation.