【发布时间】: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