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

如何判断Stripe Payment Intent或Session是否已退款?

判断Stripe Payment Intent或Checkout Session是否已退款

一、判断Payment Intent是否退款

Stripe的Payment Intent本身提供了几个直接可用来判断退款状态的字段,不用依赖status字段:

  • 查看amount_refunded字段:只要这个值大于0,就说明该Payment Intent有过退款操作
  • 检查关联Charge的refunded状态:在charges.data数组中,每个charge对象的refunded布尔值为true时,代表该笔charge已退款
  • 查看refunds.data数组:如果这个数组长度大于0,说明存在退款记录

Node.js示例代码:

const stripe = require('stripe')('你的密钥');

async function hasPaymentIntentRefunded(paymentIntentId) {
  const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
  
  // 三种判断方式取其一即可
  return (
    paymentIntent.amount_refunded > 0 ||
    paymentIntent.charges.data.some(charge => charge.refunded) ||
    paymentIntent.refunds.data.length > 0
  );
}

二、判断Checkout Session是否退款

Checkout Session本身没有直接的退款状态字段,需要通过它关联的Payment Intent来判断:

  1. 先查询Session对象,获取payment_intent字段对应的ID
  2. 通过这个ID查询对应的Payment Intent,再用上面的方法判断退款状态

Node.js示例代码:

async function hasSessionRefunded(sessionId) {
  const session = await stripe.checkout.sessions.retrieve(sessionId);
  
  // 若Session未关联Payment Intent(比如使用了其他付款方式),需单独处理
  if (!session.payment_intent) return false;
  
  const paymentIntent = await stripe.paymentIntents.retrieve(session.payment_intent);
  return paymentIntent.amount_refunded > 0;
}

注意:如果Session使用的是订阅(Subscription)相关的付款,需要查询订阅关联的Invoice和Charge,再判断退款状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:55:21