尽量不要使用Stripe充电

sam*_*yb8 2 php try-catch stripe-payments

我在我的网站上测试Stripe但无法通过下面的"尝试".基本上,我从未在'尝试'之后看到"测试".

我在这里做错了吗?

   try {
$charge = Stripe_Charge::create(array(
  "amount" => 50, // amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id,
  "card" => $token,
  "description" => $email));
echo "TEST1";
} catch (Stripe_CardError $e) {
    // Since it's a decline, Stripe_CardError will be caught
    $body = $e->getJsonBody();
    $err  = $body['error'];
    echo 'Status is:' . $e->getHttpStatus() . "\n";
    echo 'Type is:' . $err['type'] . "\n";
    echo 'Code is:' . $err['code'] . "\n";
    // param is '' in this case
    echo 'Param is:' . $err['param'] . "\n";
    echo 'Message is:' . $err['message'] . "\n";
}
echo "TEST";
Run Code Online (Sandbox Code Playgroud)

谢谢!

Cod*_*rus 5

你不可能再收到另一个错误 - 如果是的话,你会看到一个未被捕获的错误异常.话虽这么说,你应该像periklis建议的那样,捕捉其他类型的例外.

什么是最有可能的情况是,一个Stripe_CardError 抛出,并抓住了,但因为你不是在catch语句呼应东西,它的失败默默.试试这个:

} catch (Stripe_CardError $e) {
    // Since it's a decline, Stripe_CardError will be caught
    $body = $e->getJsonBody();
    $err  = $body['error'];

    echo 'Status is:' . $e->getHttpStatus() . "\n";
    echo 'Type is:' . $err['type'] . "\n";
    echo 'Code is:' . $err['code'] . "\n";
    // param is '' in this case
    echo 'Param is:' . $err['param'] . "\n";
    echo 'Message is:' . $err['message'] . "\n";
} catch (Exception $e) {
    echo $e->getMessage();
} catch (ErrorException $e) {
    echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)

编辑4/16/13 - 你在评论中指出你甚至没有在try语句中看到你的回声.因此要么a)你的echo语句在视图中隐藏,要么b)代码块在它失败之前.但是,由于您没有捕获Stripe_CardError,因此它必须是一种不同类型的异常.我现在的猜测是,你传递给Stripe函数的变量之一存在问题......要么是未声明的,要么是试图访问非对象的属性.

添加其他catch语句,看看你是否得到任何东西.你无法获得最后一个echo语句的唯一方法是抛出未被捕获的异常...你只是没有看到它.

这是一个黑暗中的镜头,但你的php环境是否有可能被异常报告抑制或以其他方式处理,这是不明显的?有时,生产环境设置为忽略或记录异常,而不是将其显示给用户.检查一下,因为如果你没有看到它,它会被捕获到其他地方,并从视图中记录或隐藏.

尝试在尝试捕获之前插入此处:

ini_set('error_reporting', E_ALL);
Run Code Online (Sandbox Code Playgroud)