Stripe如何处理InvalidRequest错误

时间:2015-04-23 12:18:32

标签: php stripe-payments

我想处理这个错误,但是我无法在我的catch中使用它。我尝试使用多个错误短语,例如Stripe\Error\InvalidRequestinvalid_request_error,但它们都不起作用。

注意:我只包含必要的代码,我的支付系统工作正常。

这是我的代码:

try {
        $charge = \Stripe\Charge::create(array(
          'customer' => $customer->id,
          'amount'   => $amount,
          'currency' => strtolower($active_user->currency->currency_id)
        ));
    }
catch (Stripe\Error\InvalidRequest $e) {
        $msg = "Sorry, you cannot make the same payment twice.";
    }

3 个答案:

答案 0 :(得分:3)

有关错误的Stripe API文档部分:

catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $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
}

答案 1 :(得分:3)

可能是命名空间问题。尝试使用前导斜杠来引用与全局范围相关的异常类:

catch (\Stripe\Error\InvalidRequest $e) {

如果失败了,请为基本的PHP Exception类添加一个catch,并查看有关 被抛出的内容的信息。

答案 2 :(得分:0)

条纹示例: 您的回复是否设置为返回json,如果是,则返回} catch (... $e) {

  $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");