Stripe error handling


#1

Hi,
I’m a bit suck with stripes error handling. the normal payment flow works great. but now i try to implement the different errors.

my code looks like the one suggested from the api:

       try {            
            $charge = \Stripe\Charge::create(array(
                "amount" => $amount,
                "currency" => $currency,
                "customer" => $customer->id,
            ));
        } catch(\Stripe\Error\Card $e) {
            // Since it's a decline, \Stripe\Error\Card will be caught
            $body = $e->getJsonBody();
            $err  = $body['error'];

            print('Status is:' . $e->getHttpStatus() . "\n");
            print('Type is:' . $err['type'] . "\n");
            print('Code is:' . $err['code'] . "\n");
            // param is '' in this case
            print('Param is:' . $err['param'] . "\n");
            print('Message is:' . $err['message'] . "\n");
        } catch (\Stripe\Error\RateLimit $e) {
            // Too many requests made to the API too quickly
        } catch (\Stripe\Error\InvalidRequest $e) {
            // Invalid parameters were supplied to Stripe's API
        } catch (\Stripe\Error\Authentication $e) {
            // Authentication with Stripe's API failed
            // (maybe you changed API keys recently)
        } catch (\Stripe\Error\ApiConnection $e) {
            // Network communication with Stripe failed
        } catch (\Stripe\Error\Base $e) {
            // Display a very generic error to the user, and maybe send
            // yourself an email
        } catch (Exception $e) {
            // Something else happened, completely unrelated to Stripe
        }

If there is no error, the payment works fine. but with en error I get the whoops error handling which tells mit the error. but how can I bypass the whoops error and catch it with my error handling?
I’m a bit confused, why the whoops kicks in.

btw: i’m using stripe elements and not checkout.


#2

What’s the whooops error and what’s you PHP version?

Anyways, I would try this first:

try {
    $charge = \Stripe\Charge::create(array(
        "amount" => $amount,
        "currency" => $currency,
        "customer" => $customer->id,
    ));
} catch (\Exception $e) {
    if ($e instanceof \Stripe\Error\Card) {
    // Since it's a decline, \Stripe\Error\Card will be caught
        
    } elseif ($e instanceof \Stripe\Error\RateLimit) {
    // Too many requests made to the API too quickly

    } elseif ($e instanceof \Stripe\Error\InvalidRequest) {
    // Invalid parameters were supplied to Stripe's API

    } elseif ($e instanceof \Stripe\Error\Authentication) {
    // Authentication with Stripe's API failed

    } elseif ($e instanceof \Stripe\Error\ApiConnection) {
    // Network communication with Stripe failed

    } elseif ($e instanceof \Stripe\Error\Base) {
    // Display a very generic error to the user, and maybe send
        
    } else {
        
    }
}

#3

The whoops error is

 \Stripe\Error\Card

and the corresponding message from stripe. for example: card_declined

I’m working with php 7.2.4

I’ll try your suggestion and keep you posted. thanks


#4

During your error search you can set an site\config\config.php option:

c::set(‘whoops’,false);

to prevent the whoops message.


#5

Thanks for your help. I could solve it. The stripe error appears when creating the customer

$customer = Stripe\Customer::create(...

so I have to catch it there and not on charging the card.