我正在测试使用Symfony框架中的Stripe api进行支付。
目前,当我拨打合法电话付款时,Stripe 会返回status 200
.
当我使用其中一张测试卡(或只是一张虚构的卡)强制 Stripe 返回错误时,问题就开始了,例如卡已过期,并且抛出了异常(Stripe_CardError
)。来自 api 的响应代码是402
但即使我为该异常放置了一个 catch 块,Symfony 也会抛出一个500 internal server error
。
这是代码的一部分:
require_once('../vendor/stripe/stripe-php/lib/Stripe/Stripe.php');
try{
Stripe::setApiKey($this->container->getParameter('stripe_api_key'));
$charge = Stripe_Charge::create(array(
"amount" => 1599,
"currency" => "usd",
"description" => "This is a test payment from post data",
"card" => array(
"number" => $data['cardNumber'],
"exp_month" => $data['expMonth'],
"exp_year" => $data['expYear']
)));
} catch(Stripe_CardError $e) {
Do error checking stuff here...
}catch (Exception $e) {
print_r($e->getMessage());
} catch (ErrorException $e) {
print_r($e->getMessage());
}
就像我说的,这段代码在抛出异常之前工作正常,但是当一个异常被抛出时,我似乎无法捕获它并对其采取行动。
在Symfony调试屏幕中显示从api返回正确的错误消息,但它没有正确的代码(应该402
):
Your card was declined.
500 Internal Server Error - Stripe_CardError
我觉得这是一个Symfony配置问题,但大量的搜索没有为我提供任何解决方案。有什么想法吗?
附言我也在使用FOSUserBundle。
Pazi说的。Stripe 错误类未在当前命名空间中注册,因此需要在其前面使用斜杠,就像调用 create 方法时一样。我不敢相信我错过了它,但这就是你剪切和粘贴得到的!谢谢帕齐。