Django中使用Stripe批量转账遇余额不足错误求助
解决Stripe转账余额不足问题
核心问题原因
- 测试环境下普通测试卡的收款存在结算延迟,无法立刻用于转账,导致账户可用余额不足。
- 代码中同时创建了
Charge和PaymentIntent,两者未关联,转账直接从账户总余额扣除,而非使用本次收款资金。
具体解决方案
1. 使用指定测试卡确保资金即时到账
测试环境中,Stripe提供专用测试卡4000000000000077,用该卡创建的收款会直接进入可用余额,无结算延迟。替换你的收款代码:
# 方式一:用Charge创建收款 charge = stripe.Charge.create( amount=5000, currency="usd", source='4000000000000077', # 替换为指定测试卡 description="Adding funds to test account" ) # 方式二:用Stripe推荐的PaymentIntent创建收款(更优) payment_intent = stripe.PaymentIntent.create( amount=total_charges, currency="usd", payment_method="pm_card_bypassPending", # 对应该测试卡的预定义支付方式 confirm=True, transfer_group=f"ORDER10_{user.id}", )
2. 统一资金流关联,避免占用账户余额
删除重复的收款逻辑(不要同时用Charge和PaymentIntent),确保转账与收款使用同一个transfer_group。这样Stripe会直接从本次收款的资金中扣除转账金额,无需依赖账户原有余额。
修改后的完整代码示例:
# 创建收款并关联转账组 payment_intent = stripe.PaymentIntent.create( amount=total_charges, currency="usd", payment_method="pm_card_bypassPending", confirm=True, transfer_group=f"ORDER10_{user.id}", ) # 转账给卖家 stripe.Transfer.create( amount=seller_amount, currency='usd', destination='acct_1OrzGLI3hFSHp0X5', transfer_group=f"ORDER10_{user.id}", ) # 转账给推荐人 stripe.Transfer.create( amount=referral_amount, currency='usd', destination="acct_1OrzPRI5g3KKKWKv", transfer_group=f"ORDER10_{user.id}", )
3. 校验转账总金额
确保seller_amount + referral_amount的总和不超过本次收款金额(total_charges),避免因超额转账触发余额不足错误。
内容的提问来源于stack exchange,提问作者Tejinder Singh
相关产品推荐
相关产品推荐

