50

During my testing of STRIPE in a website, I built the code like this:

   try {
        $charge = Stripe_Charge::create(array(
          "amount" => $clientPriceStripe, // amount in cents
          "currency" => "usd",
          "customer" => $customer->id,
          "description" => $description));
          $success = 1;
          $paymentProcessor="Credit card (www.stripe.com)";
    } 
    catch (Stripe_InvalidRequestError $a) {
        // Since it's a decline, Stripe_CardError will be caught
        $error3 = $a->getMessage();
    }

    catch (Stripe_Error $e) {
        // Since it's a decline, Stripe_CardError will be caught
        $error2 = $e->getMessage();
        $error = 1;
    }
if ($success!=1)
{
    $_SESSION['error3'] = $error3;
    $_SESSION['error2'] = $error2;
    header('Location: checkout.php');
    exit();
}

The problem is that sometimes there is an error with the card (not catched by the "catch" arguments I have there) and the "try" fails and the page immediately posts the error in the screen instead of going into the "if" and redirecting back to checkout.php.

How should I structure my error handling so I get the error and immediately redirect back to checkout.php and display the error there?

Thanks!


Error thrown:

Fatal error: Uncaught exception 'Stripe_CardError' with message 'Your card was declined.' in ............
/lib/Stripe/ApiRequestor.php on line 92
samyb8
  • 2,560
  • 10
  • 40
  • 68

8 Answers8

104

If you're using the Stripe PHP libraries and they have been namespaced (such as when they're installed via Composer) you can catch all Stripe exceptions with:

<?php 
try {
  // Use a Stripe PHP library method that may throw an exception....
  \Stripe\Customer::create($args);
} catch (\Stripe\Error\Base $e) {
  // Code to do something with the $e exception object when an error occurs
  echo($e->getMessage());
} catch (Exception $e) {
  // Catch any other non-Stripe exceptions
}
leepowers
  • 37,828
  • 23
  • 98
  • 129
  • 2
    See the other answer (http://stackoverflow.com/a/17750537/470749), which refers to the Stripe docs (https://stripe.com/docs/api?lang=php#errors), which show the different error objects to catch. `Stripe\Error\Base` is not enough. – Ryan Mar 12 '16 at 02:22
  • 4
    @Ryan all the Stripe error classes inherit from `Stripe\Error\Base`, and the first `catch` block will match all these sub-classes. I've added a second `catch` which is more robust, and handles cases where a Stripe API call doesn't return a Stripe exception. – leepowers May 06 '16 at 19:38
  • @leepowers Per the docs, I did have to prepend the first catch library call with \, so it was: `} catch (\Stripe\Error\Base $e) {` You may care to change if that would error for other folks as well. – Jeremy L. Dec 07 '17 at 16:51
  • This is the right answer. `Base` solves it for most errors, and the rest of the `Exception` classes from `Stripe` can be used for logging specific issues. – Ali Gajani Jun 06 '18 at 23:57
  • Tried this but not catching the errors. Please have a look at this:- https://stackoverflow.com/questions/52695268/can-not-catch-errors-in-stripe-payment-gateway-in-codeigniter-3 – Rahat Islam Khan Oct 08 '18 at 06:10
  • After experimenting, I like this too: } catch (\Stripe\Error\Base $e) { So it catches any errors, then I have it redirecting to a failed payment page where they can either pay with PayPal link, or contact us so can investigate and help them out. – Joe's Ideas Nov 24 '18 at 20:40
69

I think there is more than these exceptions (Stripe_InvalidRequestError and Stripe_Error) to catch.

The code below is from Stripe's web site. Probably, these additional exceptions, which you didn't consider, occurs and your code fails sometimes.

try {
  // Use Stripe's bindings...
} catch(Stripe_CardError $e) {
  // Since it's a decline, Stripe_CardError 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_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
} catch (Stripe_Error $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
}

EDIT:

try {
    $charge = Stripe_Charge::create(array(
    "amount" => $clientPriceStripe, // amount in cents
    "currency" => "usd",
    "customer" => $customer->id,
    "description" => $description));
    $success = 1;
    $paymentProcessor="Credit card (www.stripe.com)";
} catch(Stripe_CardError $e) {
  $error1 = $e->getMessage();
} catch (Stripe_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
  $error2 = $e->getMessage();
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  $error3 = $e->getMessage();
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
  $error4 = $e->getMessage();
} catch (Stripe_Error $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
  $error5 = $e->getMessage();
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
  $error6 = $e->getMessage();
}

if ($success!=1)
{
    $_SESSION['error1'] = $error1;
    $_SESSION['error2'] = $error2;
    $_SESSION['error3'] = $error3;
    $_SESSION['error4'] = $error4;
    $_SESSION['error5'] = $error5;
    $_SESSION['error6'] = $error6;
    header('Location: checkout.php');
    exit();
}

Now, you will catch all possible exceptions and you can display error message as you wish. And also $error6 is for unrelated exceptions.

Oguzhan Ozel
  • 1,144
  • 11
  • 14
  • 1
    I added the error thrown on screen. It comes from one of the Stripe files handling errors. The question would be, how I could catch the error myself and then redirect, instead of throwing Stripe's message... – samyb8 Jul 19 '13 at 16:04
  • I've edited code. You didn't consider all exceptions (for example **Stripe_CarError**) and so you cannot catch them all to display your own error message. – Oguzhan Ozel Jul 19 '13 at 16:26
  • 2
    The problem is that the code goes through ApiRequestor.php (Stripe's file) and it kind of fails there and does not keep on going through my "catches" – samyb8 Jul 19 '13 at 16:40
  • Then maybe trying to catch exceptions in ApiRequestor.php will help? – Oguzhan Ozel Jul 19 '13 at 16:53
  • 2
    Your technique to set and then check $success and redirect based on the result was a big help for me in another situation. Thanks! – David Taiaroa Dec 27 '14 at 16:05
  • I love it, but what does `$paymentProcessor="Credit card (www.stripe.com)";` do? – Jonathon Philip Chambers Sep 06 '18 at 05:46
14

This is an update to another answer, but the docs have changed very slightly so I had success using the following method:

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $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
}

You can find the source of this in the Stripe docs right here:

https://stripe.com/docs/api/errors/handling?lang=php

Djave
  • 8,595
  • 8
  • 70
  • 124
  • I think this ought to be the correct answer (especially as I'd rather use Stripe's own recommended method!) Worth noting the docs page has a blue bar along the top if your version of the API is out of date. – William Turrell Aug 02 '18 at 17:37
  • This works on my Laravel 8 – STA Oct 20 '21 at 15:42
12

I may be late to this question, but I ran into the same issue and found this.

You just need to use "Stripe_Error" class.

use Stripe_Error;

After declaring that, I was able to catch errors successfully.

user2788389
  • 121
  • 1
  • 2
8

This is how Stripe catches errors: Documentation.

try {
    // make Stripe API calls
} catch(\Stripe\Exception\ApiErrorException $e) {
    $return_array = [
        "status" => $e->getHttpStatus(),
        "type" => $e->getError()->type,
        "code" => $e->getError()->code,
        "param" => $e->getError()->param,
        "message" => $e->getError()->message,
    ];
    $return_str = json_encode($return_array);          
    http_response_code($e->getHttpStatus());
    echo $return_str;
}

You can then catch the error in ajax with the following code:

$(document).ajaxError(function ajaxError(event, jqXHR, ajaxSettings, thrownError) {
    try {
        var url = ajaxSettings.url;
        var http_status_code = jqXHR.status;
        var response = jqXHR.responseText;
        var message = "";
        if (isJson(response)) {     // see here for function: https://stackoverflow.com/a/32278428/4056146
            message = "  " + (JSON.parse(response)).message;
        }
        var error_str = "";

        // 1. handle HTTP status code
        switch (http_status_code) {
            case 0: {
                error_str = "No Connection.  Cannot connect to " + new URL(url).hostname + ".";
                break;
            }   // No Connection
            case 400: {
                error_str = "Bad Request." + message + "  Please see help.";
                break;
            }   // Bad Request
            case 401: {
                error_str = "Unauthorized." + message + "  Please see help.";
                break;
            }   // Unauthorized
            case 402: {
                error_str = "Request Failed." + message;
                break;
            }   // Request Failed
            case 404: {
                error_str = "Not Found." + message + "  Please see help.";
                break;
            }   // Not Found
            case 405: {
                error_str = "Method Not Allowed." + message + "  Please see help.";
                break;
            }   // Method Not Allowed
            case 409: {
                error_str = "Conflict." + message + "  Please see help.";
                break;
            }   // Conflict
            case 429: {
                error_str = "Too Many Requests." + message + "  Please try again later.";
                break;
            }   // Too Many Requests
            case 500: {
                error_str = "Internal Server Error." + message + "  Please see help.";
                break;
            }   // Internal Server Error
            case 502: {
                error_str = "Bad Gateway." + message + "  Please see help.";
                break;
            }   // Bad Gateway
            case 503: {
                error_str = "Service Unavailable." + message + "  Please see help.";
                break;
            }   // Service Unavailable
            case 504: {
                error_str = "Gateway Timeout." + message + "  Please see help.";
                break;
            }   // Gateway Timeout
            default: {
                console.error(loc + "http_status_code unhandled >> http_status_code = " + http_status_code);
                error_str = "Unknown Error." + message + "  Please see help.";
                break;
            }
        }

        // 2. show popup
        alert(error_str);
        console.error(arguments.callee.name + " >> http_status_code = " + http_status_code.toString() + "; thrownError = " + thrownError + "; URL = " + url + "; Response = " + response);

    }
    catch (e) {
        console.error(arguments.callee.name + " >> ERROR >> " + e.toString());
        alert("Internal Error.  Please see help.");
    }
});
xinthose
  • 3,213
  • 3
  • 40
  • 59
3

I think all you really need to check is the base error class of Stripe and the exception if it's unrelated to Stripe. Here's how I do it.

/**
 * Config.
 */
require_once( dirname( __FILE__ ) . '/config.php' );

// Hit Stripe API.
try {
  // Register a Customer.
  $customer = \Stripe\Customer::create(array(
    'email'    => 'AA@TESTING.com',
    'source'   => $token,
    'metadata' => array( // Note: You can specify up to 20 keys, with key names up to 40 characters long and values up to 500 characters long.
        'NAME'          => 'AA',
        'EMAIL'         => 'a@a.c.o',
        'ORDER DETAILS' => $order_details,
    )
  ));

  // Charge a customer.
  $charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => 5000, // In cents.
    'currency' => 'usd'
  ));



  // If there is an error from Stripe.
} catch ( Stripe\Error\Base $e ) {
  // Code to do something with the $e exception object when an error occurs.
  echo $e->getMessage();

  // DEBUG.
  $body = $e->getJsonBody();
  $err  = $body['error'];
  echo '<br> ——— <br>';
  echo '<br>THE ERROR DEFINED — <br>';
  echo '— Status is: ' . $e->getHttpStatus() . '<br>';
  echo '— Message is: ' . $err['message'] . '<br>';
  echo '— Type is: ' . $err['type'] . '<br>';
  echo '— Param is: ' . $err['param'] . '<br>';
  echo '— Code is: ' . $err['code'] . '<br>';
  echo '<br> ——— <br>';

// Catch any other non-Stripe exceptions.
} catch ( Exception $e ) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    echo '<br> ——— <br>';
    echo '<br>THE ERROR DEFINED — <br>';
    echo '— Status is: ' . $e->getHttpStatus() . '<br>';
    echo '— Message is: ' . $err['message'] . '<br>';
    echo '— Type is: ' . $err['type'] . '<br>';
    echo '— Param is: ' . $err['param'] . '<br>';
    echo '— Code is: ' . $err['code'] . '<br>';
    echo '<br> ——— <br>';
}
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
  • `getJsonBody()` and `getHttpStatus()` are stripe functions and do not apply when catching the standard exception. – xinthose Oct 09 '17 at 13:31
0

How to get error message if your provided token is invalid. It break and show some exception in laravel. so i used stripe exception by using try and catch. It will work fine. try this code.You can show your own custom message instead of stripe message.

try{
    \Stripe\Stripe::setApiKey ("your stripe secret key");
    $charge =   \Stripe\Charge::create ( array (
                "amount" => 100,
                "currency" => "USD",
                "source" => 'sdf', // obtained with Stripe.js
                "description" => "Test payment."
        ) );
      $order_information = array(
                            'paid'=>'true',
                            'transaction_id'=>$charge->id,
                            'type'=>$charge->outcome->type,
                            'balance_transaction'=>$charge->balance_transaction,
                            'status'=>$charge->status,
                            'currency'=>$charge->currency,
                            'amount'=>$charge->amount,
                            'created'=>date('d M,Y', $charge->created),
                            'dispute'=>$charge->dispute,
                            'customer'=>$charge->customer,
                            'address_zip'=>$charge->source->address_zip,
                            'seller_message'=>$charge->outcome->seller_message,
                            'network_status'=>$charge->outcome->network_status,
                            'expirationMonth'=>$charge->outcome->type
                        );

        $result['status'] = 1;
        $result['message'] = 'success';
        $result['transactions'] = $order_information;

      }
      catch(\Stripe\Exception\InvalidRequestException $e){
        $result['message'] = $e->getMessage();
        $result['status'] = 0;

      }
Kaleemullah
  • 446
  • 3
  • 8
0

Here is a functional demo of the try / catch with all the possible errors, just add your own functionality in each catch

try {
  // Use Stripe's library to make requests...
  $charge = \Stripe\Charge::create([
    'amount' => $amount,
    'currency' => "usd",
    'description' => $description,
    "receipt_email" => $mail,
   ]);
} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $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
}

You can get the official code from here