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

Stripe测试支付触发AxiosError 500错误,请求技术排查

排查Stripe测试支付500错误的原因及解决办法

可能原因与对应解决步骤

1. Stripe密钥配置错误

  • 后端必须使用测试模式的秘钥(sk_test_xxx),绝对不能用前端公钥(pk_test_xxx)或生产环境秘钥。
  • 检查环境变量加载情况:
    • 确认项目安装了dotenv包,且在后端入口文件(如server.js)中添加了require('dotenv').config()。
    • 查看.env文件里的STRIPE_KEY,确保没有多余空格、引号,秘钥字符串完全正确。

2. Stripe Token无效

  • 前端获取的stripeToken必须是Stripe测试环境生成的有效token,比如用测试卡号4242 4242 4242 4242(任意过期时间、CVC)生成的token。
  • 检查前端Stripe初始化代码,确认用的是测试公钥(pk_test_xxx)实例化Stripe对象。

3. 旧版Charges API使用问题

  • 虽然Charges API仍可使用,但Stripe官方已推荐用Payment Intents API处理支付(尤其支持3D验证场景)。如果坚持用Charges API,需确认参数无误:
    • 你代码里的amount用最小货币单位(2000代表20美元)是正确的,无需修改。
    • 核对source参数是否确实是有效的token ID,没有拼写错误。

4. 后端未配置CORS(隐性问题)

  • 如果前端运行端口和后端不同(比如前端3000、后端5000),必须配置CORS中间件:
    • 安装依赖:npm install cors
    • 在后端入口文件添加:
      const cors = require('cors');
      app.use(cors());
      

5. 查看Stripe具体错误信息

  • 目前你的后端只把错误返回前端,但没打印细节。修改后端代码,直接在控制台输出stripeErr,就能看到具体原因(比如密钥无效、token过期等):
    router.post('/payment', (req, res) => {
      stripe.charges.create(
        {
          source: req.body.tokenId,
          amount: req.body.amount,
          currency: "usd",
        },
        (stripeErr, stripeRes) => {
          if (stripeErr) {
            console.log('Stripe错误详情:', stripeErr); // 新增该行打印细节
            res.status(500).json(stripeErr);
          } else {
            return res.status(200).json(stripeRes);
          }
        }
      );
    });
    

额外建议

建议切换到Stripe官方推荐的Payment Intents API,能更好处理支付异常和3D验证流程,示例后端代码如下:

router.post('/payment', async (req, res) => {
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: req.body.amount,
      currency: 'usd',
      payment_method: req.body.tokenId,
      confirm: true,
    });
    res.status(200).json(paymentIntent);
  } catch (err) {
    console.log('Stripe错误:', err);
    res.status(500).json(err);
  }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:40:47