You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe PHP创建Payment Intent时Guzzle异常未被捕获求助

问题分析与解决

你的问题是Guzzle的RequestException未被现有catch块捕获,原因有两点:

  1. 该异常属于GuzzleHttp\Exception命名空间,不在Stripe定义的异常体系内
  2. 你最后的catch (Exception $e)如果代码处于自定义命名空间下,实际捕获的是当前命名空间的Exception类,而非PHP全局的\Exception

修复方案

修改你的异常捕获逻辑,添加对Guzzle异常的捕获,并确保全局异常的正确捕获:

try {
  $intent = $stripe->paymentIntents->create($payload);
  header("Content-Type: application/json");
  print( json_encode($intent->jsonSerialize() ));
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
  http_response_code(429);
} catch (\Stripe\Exception\CardException $e) {
  // Card was declined
  logAlert("Card was declined {$e->getError()->message}");
  http_response_code(402);
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
  http_response_code(400);
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  http_response_code(511);
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
  http_response_code(503);
} catch (\GuzzleHttp\Exception\RequestException $e) {
  // 捕获Guzzle层面的请求异常(如cURL错误)
  error_log("GUZZLE ERROR: {$e->getMessage()}");
  http_response_code(503);
} catch (\Exception $e) {
  error_log("ERROR: {$e->getMessage()}");
  http_response_code(500);
}

关键说明

  • 新增的\GuzzleHttp\Exception\RequestException捕获块专门处理Guzzle底层的请求错误(比如你遇到的cURL错误3),这类错误未被Stripe SDK包装成自身的ApiConnectionException
  • 将最后的catch (Exception $e)改为catch (\Exception $e),确保捕获所有未被前面块处理的全局异常,避免命名空间导致的捕获失效

另外,cURL错误3通常关联URL格式问题,但你提到正常卡片可支付成功,推测是拒付场景下Stripe的返回链路出现临时网络异常,建议检查服务器网络连通性,确保能稳定访问Stripe的API端点。

内容的提问来源于stack exchange,提问作者Frank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:27:20