web
web

Reputation: 25

Stripe Error Handling Error in PHP

I am using following code to create stripe user.

But I am unable to handle error when Card Number is wrong .

        $stripeClassesDir = __DIR__ . '/stripe-php-master/lib/';
        $stripeUtilDir    = $stripeClassesDir . 'Util/';
        $stripeErrorDir   = $stripeClassesDir . 'Error/';
        $stripeHttpClientDir   = $stripeClassesDir . 'HttpClient/';

        set_include_path($stripeClassesDir . PATH_SEPARATOR . $stripeUtilDir . PATH_SEPARATOR . $stripeErrorDir . PATH_SEPARATOR . $stripeHttpClientDir);

        function __autoload($class)
        {
            $parts = explode('\\', $class);
            require end($parts) . '.php';

        }

       try {

                \Stripe\Stripe::setApiKey("sk_test_key");

                $t = \Stripe\Token::create(array( "card" => array(
                    "number" => $cardNumber,
                    "exp_month" => $cardMonth,
                    "exp_year" => $cardYear,
                    "cvc" => $cardCVC )));

                //echo $t;

            } 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
        }

This code gives following error .

Fatal error: Class 'Stripe\Error\Card' not found in /home/user/public_html/stripe-php-master/lib/ApiRequestor.php on line 114

Upvotes: 2

Views: 564

Answers (1)

Ywain
Ywain

Reputation: 17533

You're not correctly loading Stripe's PHP library. You need to include the init.php file at the root of the library's folder, as indicated in the README file.

I'd recommend replacing

$stripeClassesDir = __DIR__ . '/stripe-php-master/lib/';
$stripeUtilDir    = $stripeClassesDir . 'Util/';
$stripeErrorDir   = $stripeClassesDir . 'Error/';
$stripeHttpClientDir   = $stripeClassesDir . 'HttpClient/';

set_include_path($stripeClassesDir . PATH_SEPARATOR . $stripeUtilDir . PATH_SEPARATOR . $stripeErrorDir . PATH_SEPARATOR . $stripeHttpClientDir);

with:

require_once('./stripe-php-master/init.php');

Upvotes: 1

Related Questions