【问题标题】:Symfony internal server error when Stripe api throws exception当 Stripe api 抛出异常时,Symfony 内部服务器错误
【发布时间】:2013-09-16 17:49:25
【问题描述】:

我正在测试使用 Symfony 框架中的 Stripe api 进行支付。

目前,当我拨打合法电话付款时,Stripe 返回status 200

当我使用 test cards 之一(或只是一个虚构的)来强制 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。

【问题讨论】:

  • 试试\Stripe_CardError 而不是Stripe_CardError

标签: php symfony stripe-payments


【解决方案1】:

这就是帕齐所说的。 Stripe 错误类未在当前命名空间中注册,因此需要在其前面加上斜线,就像调用 create 方法时一样。 我不敢相信我错过了它,但这就是你得到的剪切和粘贴! 谢谢帕子。

【讨论】:

  • 哦,看看this answer。这是一种通过作曲家包含和更新条带并自动加载它的方法。没有更改类名使用,但不再需要require_once
猜你喜欢
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2018-11-03
  • 1970-01-01
相关资源
最近更新 更多