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

如何从Stripe的checkout.session.completed事件获取支付风险评分?

获取Stripe支付风险评分的解决方案

你遇到的问题是因为checkout.session.completed事件本身并不包含支付风险评估数据——风险评分属于PaymentIntent对象的属性,而非Checkout Session。以下两种方法可以帮你获取并存储风险评分:

方法1:从Checkout Session关联的PaymentIntent中拉取数据

在checkout.session.completed的Webhook处理逻辑中,你可以从事件对象里拿到payment_intent字段(即PaymentIntent的ID),然后调用Stripe API获取该PaymentIntent的完整详情,从中提取风险相关字段:

// Node.js示例
const stripe = require('stripe')('你的API密钥');

// 处理checkout.session.completed事件
app.post('/webhook', async (req, res) => {
  const event = req.body;

  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;
    const paymentIntentId = session.payment_intent;

    // 调用API获取PaymentIntent详情
    const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
    
    // 提取风险评分和相关信息
    const riskScore = paymentIntent.risk_score;
    const riskEvaluation = paymentIntent.risk_evaluation;

    // 这里将riskScore等数据存入你的数据库
    saveToDatabase(riskScore, riskEvaluation);
  }

  res.json({ received: true });
});

方法2:直接订阅payment_intent.succeeded事件

如果不想额外调用API,你可以在Stripe控制台中新增订阅payment_intent.succeeded事件的Webhook。这个事件的Payload里直接包含PaymentIntent的完整数据,其中就有risk_score和risk_evaluation字段,你可以直接从事件对象中提取并存库:

// Node.js示例:处理payment_intent.succeeded事件
app.post('/webhook', async (req, res) => {
  const event = req.body;

  if (event.type === 'payment_intent.succeeded') {
    const paymentIntent = event.data.object;
    
    // 直接提取风险评分
    const riskScore = paymentIntent.risk_score;
    const riskEvaluation = paymentIntent.risk_evaluation;

    // 存入数据库
    saveToDatabase(riskScore, riskEvaluation);
  }

  res.json({ received: true });
});

注意事项

  • 确保你的Stripe API密钥拥有读取PaymentIntent的权限(默认密钥已具备该权限)。
  • 处理Webhook时要验证事件签名,避免伪造请求。
  • risk_score取值范围为0-100,数值越高代表支付风险越大;risk_evaluation包含更详细的风险评估细节(如风险原因)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 11:20:53