php - 尝试不使用 Stripe 充电

标签 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";

谢谢!

最佳答案

您不太可能遇到另一个错误——如果遇到了,您会看到一个 Uncaught Error 异常。话虽如此,正如 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();
}

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

添加其他 catch 语句,看看是否有任何收获。如果抛出未捕获的异常,您无法到达最后一个 echo 语句的唯一方法......您只是没有看到它。

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

尝试在你的 try catch 之前的某处插入这个:

ini_set('error_reporting', E_ALL);

关于php - 尝试不使用 Stripe 充电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15994930/

相关文章:

php - 多种账户类型?

php - 我怎样才能让 phpunit 从文件夹中的所有文件运行测试?

iphone - 如何在 iphone 应用程序中捕捉 sigpipe?

reactjs - Stripe react native : Merchant display name cannot be an empty string

stripe-payments - 当 SetupIntent 成功时 PaymentIntent 需要身份验证 - Stripe

php - 计算多维数组php的键

php - not null if 语句的最佳实践

java - 安卓/Java : Inexplicably enters catch block but skips line

JAVA - Try/Catch 遇到困难

javascript - 如何将购物车总额从 html/php 传递到 Stripe,其中购物车总额由 JS 计算