Stripe生产环境美国银行账户支付失败求助(卡支付正常)
美国银行账户Stripe支付失败排查与解决方案
我在Stripe生产环境开发支付功能,流程是先通过SetupIntent完成账户设置,存储客户Stripe ID和支付方式ID,再用PaymentIntent从已绑定账户扣款。目前卡支付正常,但美国银行账户支付始终失败,相关代码如下:
try: if payment_method_types == "us_bank_account": stripe.PaymentMethod.attach( stripe_payment_id, customer=customer.stripe_customer_id, ) payment = stripe.PaymentIntent.create( payment_method=stripe_payment_id, customer=stripe_customer_id, amount=int(1 * 100), currency='usd', confirm=True, return_url='https://my_domain.com/', off_session=True, payment_method_types=["ach_credit_transfer", "ach_debit", "us_bank_account"] ) else: payment = stripe.PaymentIntent.create( payment_method=stripe_payment_id, customer=stripe_customer_id, amount=int(1 * 100), currency='usd', confirm=True, return_url='https://my_domain.com/', off_session=True ) except Exception as e: print(str(e)) if payment.status == 'succeeded': print("Payment success.") else: print("Payment not success.")
解决方案要点
- 验证账户状态:美国银行账户(ACH)支付必须完成微存款验证才能发起off_session扣款。如果是通过SetupIntent创建的支付方式,要确保
setup_future_usage设为off_session,且用户已完成账户验证步骤,未验证的支付方式无法执行扣款。 - 修正支付方式类型:创建PaymentIntent时,针对美国银行账户只需指定
["ach_debit"]即可。us_bank_account是支付方式的类型,实际扣款用的是ACH Debit,无需包含ach_credit_transfer(该类型是用户主动转账到Stripe,不是商户扣款)。 - 移除重复绑定操作:通过SetupIntent完成账户设置后,支付方式已经自动绑定到客户,无需再调用
stripe.PaymentMethod.attach,重复操作会触发错误。 - 处理异步支付状态:ACH支付并非实时到账,PaymentIntent创建后状态会是
processing而非直接succeeded。不能仅通过即时状态判断支付结果,必须监听Stripe的payment_intent.succeeded等webhook事件来确认最终状态。 - 针对性处理异常:捕获的异常包含具体错误码(如
payment_method_unactivated、requires_action),需根据错误码处理。比如出现requires_action时,需引导用户完成账户验证。
修正后代码示例
try: if payment_method_types == "us_bank_account": payment = stripe.PaymentIntent.create( payment_method=stripe_payment_id, customer=stripe_customer_id, amount=int(1 * 100), currency='usd', confirm=True, return_url='https://my_domain.com/', off_session=True, payment_method_types=["ach_debit"] ) else: payment = stripe.PaymentIntent.create( payment_method=stripe_payment_id, customer=stripe_customer_id, amount=int(1 * 100), currency='usd', confirm=True, return_url='https://my_domain.com/', off_session=True ) except Exception as e: print(f"Error: {str(e)}") # 根据错误码执行对应逻辑,如requires_action时返回前端引导用户验证 # ACH支付需通过webhook确认最终状态,此处仅作即时状态提示 if payment.status == 'succeeded': print("Payment success.") elif payment.status == 'processing': print("Payment is processing, wait for webhook notification.") else: print("Payment not success.")
内容的提问来源于stack exchange,提问作者Shawon Barmon Ovi
相关产品推荐
相关产品推荐

