如果用户输入错误的卡详细信息,则无法捕获条带错误


Unable to Catch Stripe Errors if user enters wrong card details

对于Charging客户,我构建了此代码并获得了PCI Compliance服务器。当用户输入正确的抄送信息时,一切都按预期进行,但当用户输入任何错误的信息,如抄送到期或错误的卡号时,它无法捕捉错误并在页面上抛出错误,如

致命错误:未捕获异常"Stripe_CardError",并显示消息"您的卡号不正确"在/home/treehouse/workspace/lib/Stripe/ApiRequestor.php:148堆栈跟踪:#0/home/tree house/workspace/lib/Strip/ApiRequestor.php(254):Stripe_ApiRequestor->handleApiError:Stripe_ApiRequestor->request('post','/v1/Token',Array)#3/home/treehouse/workspace/lib/Stripe/Token.php

<?php
$ccn = $_POST['ccn'];
$ccm = $_POST['ccm'];
$ccy = $_POST['ccy'];
$cvv = $_POST['cvv'];
$api = 'sk_test_yEW8dYhX4xiAzpaRiWCr7UbC';
$amount = '$_POST['amount'];
require_once('./lib/Stripe.php');
Stripe::setApiKey($api);
$token = Stripe_Token::create(@array(
"card" => array(
"number" => $ccn,
"exp_month" => $ccm,
"exp_year" => $ccy,
"cvc" => $cvv
)
));
try {
$charge = Stripe_Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => "payinguser@example.com")
);
} 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
}
?>

我还尝试用最新的条纹库3.4做所有这些事情,并使用网站上规定的代码收费。

<?php
$ccn = $_POST['ccn'];
$ccm = $_POST['ccm'];
$ccy = $_POST['ccy'];
$cvv = $_POST['cvv'];
$api = 'sk_test_yEW8dYhX4xiAzpaRiWCr7UbC';
$amount = '$_POST['amount'];
require_once('./stripe/init.php');
Stripe::setApiKey($api);
$token = 'Stripe'Token::create(@array(
"card" => array(
"number" => $ccn,
"exp_month" => $ccm,
"exp_year" => $ccy,
"cvc" => $cvv
)
));
try {
$charge = 'Stripe'Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => "payinguser@example.com")
);
} catch('Stripe'Error'Card $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'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' $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
}
?>

对Stripe_Charge使用TryCatch::创建

  <?php
    $ccn = $_POST['ccn'];
    $ccm = $_POST['ccm'];
    $ccy = $_POST['ccy'];
    $cvv = $_POST['cvv'];
    $api = 'sk_test_yEW8dYhX4xiAzpaRiWCr7UbC';
    $amount = '$_POST['amount'];
    require_once('./stripe/init.php');
    Stripe::setApiKey($api);
    try{
    $token = 'Stripe'Token::create(@array(
    "card" => array(
    "number" => $ccn,
    "exp_month" => $ccm,
    "exp_year" => $ccy,
    "cvc" => $cvv
    )
    ));
   $charge = 'Stripe'Charge::create(array(
    "amount" => $amount, // amount in cents, again
    "currency" => "usd",
    "card" => $token,
    "description" => "payinguser@example.com")
    );
    } catch('Stripe'Error'Card $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'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' $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
    }
    ?>