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

mikemenard's avatar

Catch Cashier Charge Errors?

Hi, I try to catch errors throw by the charge method on Stripe.... impossible!

I have try everything...

   try {
        $user->charge(dollars_to_cents($product->price));
    }  catch (Exception $e) {

    }

   try {
        $user->charge(dollars_to_cents($product->price));
    }  catch (\Exception $e) {

    }

   try {
        $user->charge(dollars_to_cents($product->price));
    }  catch (\Stripe\Error\Card $e) {

    }
0 likes
3 replies
jekinney's avatar

Have you looked in the source code? That's not how errors are handled. It returns a Json object with a status code.

Dd() the request and you'll see it.

Check the status code in an if statement then handle your error then.

mikemenard's avatar

@jekinney I have try and it doesn't work. I see the same error: Card in ApiRequestor.php line 112: Your card was declined.

And this is the line 112 in ApiRequestor.php

  case 402:
                throw new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders);
mikemenard's avatar
mikemenard
OP
Best Answer
Level 11

Finally , the problem was not from the charge method. It was from the createAsStripeCustomer().

We can't generate a stripe id on a failed paiement... Obviously.

Please or to participate in this conversation.