【问题标题】:Stripe error catching for GET or Retrieve为 GET 或 Retrieve 捕获条带错误
【发布时间】:2019-01-20 04:12:57
【问题描述】:

我正在使用 Stripe API,以下代码行检查客户:

  $StripeCustomer = \Stripe\Customer::retrieve($cust_id);

如果客户 ID 未找到/不存在,则返回错误。那时我想简单地创建一个客户 ID。 但我如何捕捉和评估错误?关于捕捉卡错误的 SO 有多种答案,但它们并不像这个 GET 请求那样起作用。

Laravel 错误处理程序确实返回错误。它看起来像这样:

在 ApiRequestor.php 第 181 行 在 ApiRequestor::_specificAPIError('{ "error": { "code": "resource_missing", "doc_url": "https://stripe.com/docs/error-codes/resource-missing", "message": "没有这样的客户:cus_CHDZD223OmY75y", "param": "id" , "type": "invalid_request_error" } } ', '404', array('Server' => 'nginx', 'Date' => 'Mon, 13 Aug 2018 11:55:49 GMT', 'Content-Type ' => 'application/json', 'Content-Length' => '234', 'Connection' => 'keep-alive', 'Access-Control-Allow-Credentials' => 'true', 'Access-Control -Allow-Methods' => 'GET, POST, HEAD, OPTIONS, DELETE', 'Access-Control-Allow-Origin' => '*', 'Access-Control-Expose-Headers' => 'Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required', 'Access-Control-Max-Age' => '300', 'Cache-Control' => '无缓存,无存储','Request-Id' =>'req_0iY8NIWwT0tAqr','Stripe-Version' =>'2018-07-27','Strict-Transport-Security' =>'max-age=31556926 ; includeSubDomains; preload'), array('error' => array('code' => 'resource_missing', 'doc_url' => 'https://stripe.com/docs/error-codes/resource-missing', 'message' => '没有这样的客户:cus_CHDZD223OmY75y', 'param' => 'id', 'type' => 'invalid_request_error')), array('code' => 'resource_missing', 'doc_url' => 'https://stripe.com/docs/error-codes/resource-missing', ' message' => 'No such customer: cus_CHDZD223OmY75y', 'param' => 'id', 'type' => 'invalid_request_error')) 在 ApiRequestor.php 第 144 行

但我需要捕捉错误并检查它。任何帮助表示赞赏。

目前尝试过

我尝试了以下 try/catch 的变体:

try{
        $StripeCustomer = \Stripe\Customer::retrieve($cust_id);
   } catch (Exception $e){
      return "no customer found";
   }

【问题讨论】:

    标签: php error-handling stripe-payments


    【解决方案1】:

    您的代码看起来通常是正确的,您确实应该在检索调用周围使用 try/catch 块。我建议查看从 Stripe 返回的响应代码,以确定确切的错误是否表明客户不存在,或者它是否是另一种类型的错误。这是一个例子:

    try{
      $customer = \Stripe\Customer::retrieve("cus_xxx");
    }catch(\Stripe\Error\InvalidRequest $e){
      $body = $e->getJsonBody();
      $err  = $body['error'];
      if($err['code'] == "resource_missing"){
        print("customer does not exist!");
      }
    }
    

    您可以在docs 中阅读有关处理 Stripe 错误的更多信息。

    【讨论】:

    • 就是这样! \Stripe\Error\InvalidRequest $e 行改变了一切
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多