我有一个移动应用程序,其中包含使用 CodeIgniter 开发的 API,该 API 通过 Stripe 执行支付。
这些付款是递归的,所以我每月向用户收费一次。但是有些用户不能被收费(例如,如果用户的卡上没有足够的资金(,在这种情况下,Stripe 会引发异常。
我希望能够在我的控制器中执行尝试/捕获。现在我有这个代码:
try {
$charge = StripeCharge::create(array(
"amount" => xxx,
"currency" => "eur",
"customer" => xxx)
);
} catch (Exception $e) {
// If user has insufficient funds, perfom this code
}
我最近看到 catch 中的这段代码从未执行过,我看到 CI 有自己的错误处理系统,我可以在我的日志中看到来自 cli 运行的控制器的错误显示在此视图上:application/views/errors/cli/error_php.php
。那么我怎样才能简单地检测我的 Stripe 代码是否返回异常呢?
提前感谢您的任何答案:)
尝试对异常使用全局命名空间,如下所示:
catch (\Exception $e(//注意反斜杠
请仔细阅读 Stripe 的 API 文档。
他们以非常详细的方式描述了错误处理。你可以在这里阅读它: https://stripe.com/docs/api/php#error_handling
try {
// Use Stripe's library to make requests...
} catch(StripeErrorCard $e) {
// Since it's a decline, StripeErrorCard 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 (StripeErrorRateLimit $e) {
// Too many requests made to the API too quickly
} catch (StripeErrorInvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API
} catch (StripeErrorAuthentication $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (StripeErrorApiConnection $e) {
// Network communication with Stripe failed
} catch (StripeErrorBase $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
}