PHP中的条带错误处理错误

时间:2016-11-22 12:22:06

标签: php stripe-payments

我使用以下代码创建条带用户。

但是当卡号错误时我无法处理错误。

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

此代码出现以下错误。

  

致命错误:Class' Stripe \ Error \ Card'找不到   /home/user/public_html/stripe-php-master/lib/ApiRequestor.php上线   114

1 个答案:

答案 0 :(得分:1)

您未正确加载Stripe的PHP库。您需要将init.php文件包含在库文件夹的根目录中,如README文件中所示。

我建议更换

$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);

使用:

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