如何使用Stripe订阅捕获卡错误?

时间:2019-05-29 17:03:13

标签: php stripe-payments

我正在使用Stripe创建订阅,并已遵循官方文档。请参阅下面的代码。当我使用有效的卡时,它可以很好地工作,但是当我使用被拒绝的故意卡(出于测试目的)时,则会收到未捕获的异常,而不是适当的错误消息。

我已经在网上搜索并使用了各种不同的错误捕获方法,但是它们都不起作用。我正在使用他们网站上详细介绍的try / catch方法。 我用于拒签的条纹卡号是:4000 0084 0000 1629 其他打算降低的测试卡也遇到了同样的问题。 谁能指出我正确的方向?

require_once("stripe-php-6.35.2/init.php");
\Stripe\Stripe::setApiKey("sk_test_XXjeWNYCBa3KyiSdnGtZwYYu");

$token = $_POST['stripeToken'];
$subscription_plan = 'plan_F79GYa3Tz506JJ';

$customer = \Stripe\Customer::create(array(
    "source" => $token,
    "email" => 'andy@arcimedia.co.uk')
);

$payer_id = $customer->id;

try {
$subscription = \Stripe\Subscription::create([
          "customer" => $payer_id,
          "items" => [
            [
              "plan" => $subscription_plan
            ],
          ]
        ]);
} catch(\Stripe\Error\Card $e) {
    echo 'catch';
    $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
}

我希望看到一些错误,例如状态:xx,类型:xx等

相反,我看到了这个

致命错误:消息为“您的卡被拒绝”的未捕获异常“ Stripe \ Error \ Card”。在/var/sites/s/streaming.akmmusic.co.uk/public_html/stripe/stripe-php-6.35.2/lib/ApiRequestor.php:214中,来自API请求“ req_b3CIIpbtkRqOvQ”的堆栈跟踪:#0 / var / sites /s/streaming.akmmusic.co.uk/public_html/stripe/stripe-php-6.35.2/lib/ApiRequestor.php(173):Stripe \ ApiRequestor :: _ specificAPIError('{\ ​​n“ error”:{\ n ...”,402,对象(Stripe \ Util \ CaseInsensitiveArray),Array,Array)#1 /var/sites/s/streaming.akmmusic.co.uk/public_html/stripe/stripe-php-6.35.2/lib /ApiRequestor.php(473):Stripe \ ApiRequestor-> handleErrorResponse('{\ ​​n“ error”:{\ n ...',402,Object(Stripe \ Util \ CaseInsensitiveArray),Array)#2 / var / sites /s/streaming.akmmusic.co.uk/public_html/stripe/stripe-php-6.35.2/lib/ApiRequestor.php(126):Stripe \ ApiRequestor-> _ interpretResponse('{\ ​​n“ error”:{\ n ...',402,对象(Stripe \ Util \ CaseInsensitiveArray))#3 /var/sites/s/streaming.akmmusic.co.uk/public_html/stripe/stripe-php-6.35.2/lib/ApiOperations/Request .php(57):/ var / sites / s / streaming.akmmusic.co.uk / public_html / str中的Stripe \ Api ipe / stripe-php-6.35.2 / lib / ApiRequestor.php在第214行

0 个答案:

没有答案