如何通过Stripe API查询一次性付款是否使用优惠券?
查询Stripe一次性付款的优惠券信息
对于非订阅场景的一次性付款,Stripe的Payment对象本身确实不会直接包含折扣或优惠券详情,你需要通过关联的Charge或PaymentIntent对象来获取相关信息,具体操作如下:
方法1:通过关联的Charge对象查询
- 调用
Retrieve Payment接口获取目标付款对象,从中提取charges.data[0].id(即关联的Charge ID)。 - 调用
Retrieve Charge接口获取Charge对象,通过以下字段判断优惠券使用情况:discount字段:若存在则包含优惠券的coupon子对象,可获取优惠券ID、名称、折扣类型等信息。amount为未抵扣的原价,amount_captured是实际收取金额,两者差值即为优惠券抵扣额。
示例代码:
# 获取Payment对象 payment = stripe.Payment.retrieve("pay_xxx") # 提取关联的Charge ID charge_id = payment.charges.data[0].id # 获取Charge对象 charge = stripe.Charge.retrieve(charge_id) # 检查优惠券信息 if charge.discount: coupon_info = charge.discount.coupon print(f"使用优惠券:{coupon_info.name}(ID:{coupon_info.id}),原价:{charge.amount},实付:{charge.amount_captured}") else: print("未使用优惠券")
方法2:通过关联的PaymentIntent对象查询
如果一次性付款是通过PaymentIntent创建的,可直接查询该对象:
- 从Payment对象中提取
payment_intent字段(即PaymentIntent ID)。 - 调用
Retrieve PaymentIntent接口,查看discount字段获取优惠券详情,同时amount为原价,amount_received为实付金额。
示例代码:
# 获取Payment对象 payment = stripe.Payment.retrieve("pay_xxx") # 提取关联的PaymentIntent ID pi_id = payment.payment_intent # 获取PaymentIntent对象 pi = stripe.PaymentIntent.retrieve(pi_id) # 检查优惠券信息 if pi.discount: coupon = pi.discount.coupon print(f"优惠券ID:{coupon.id},折扣类型:{coupon.discount_type},抵扣金额:{pi.amount - pi.amount_received}") else: print("未使用优惠券")
注意事项
- 仅当付款确实应用了优惠券时,
discount字段才会出现在返回结果中。 - 金额字段单位为对应货币的最小单位(如美元为分、人民币为分),需自行转换为实际货币单位。
内容的提问来源于stack exchange,提问作者Jonas
相关产品推荐
相关产品推荐

