关于Stripe Invoices的off session支付流程配置疑问
一、用Invoice流程实现自动Off-Session扣款
如果你想用Invoice流程自动完成中标后的扣款,同时让客户在Express门户看到收据,得把这些配置做对:
确认客户默认支付方式已授权Off-Session
客户在Express门户保存的支付方式,必须是支持Off-Session支付的(比如卡片已经过3DS验证,或者属于无需验证的场景)。你可以通过customer.default_payment_method拿到默认支付方式ID,再查该支付方式的状态,确保card.three_d_secure_usage.supported为True(如果需要3DS的话)。创建Invoice时指定自动扣款配置
创建Invoice的时候,必须设置collection_method: 'charge_automatically',同时配置支付方式类型和默认支付方式保存规则,示例代码:stripe.Invoice.create( customer='cus_你的客户ID', collection_method='charge_automatically', payment_settings={ 'payment_method_types': ['card'], # 匹配客户的支付方式类型 'save_default_payment_method': 'on_subscription' }, auto_advance=True # 自动把Invoice从draft推进到open状态 )添加明细并Finalize Invoice
给Invoice加完Item后,调用stripe.Invoice.finalize_invoice('in_你的InvoiceID'),Stripe会自动生成Payment Intent并尝试用默认支付方式扣款。如果支付方式没问题,Payment Intent会直接成功,不会停在requires_confirmation。处理验证需求
如果还是出现requires_confirmation,大概率是支付方式需要额外验证(比如3DS)。这时候要监听invoice.payment_action_required的webhook,给客户发通知让他们通过Express门户或者Stripe的托管页面完成验证。
二、手动创建Off-Session Payment Intent并关联Invoice
要是你想手动控制Payment Intent的创建,同时让Invoice在客户门户显示收据,这么做:
创建Draft Invoice并添加明细
先建一个draft状态的Invoice,不要自动生成Payment Intent:invoice = stripe.Invoice.create( customer='cus_你的客户ID', collection_method='send_invoice', auto_advance=False ) stripe.InvoiceItem.create( customer='cus_你的客户ID', invoice=invoice.id, amount=1000, # 金额,单位为分 currency='usd', description='中标服务费' )手动生成Off-Session Payment Intent
创建的时候指定客户、默认支付方式,开启off_session: True并直接确认扣款,同时标记关联的Invoice ID:pi = stripe.PaymentIntent.create( amount=1000, currency='usd', customer='cus_你的客户ID', payment_method='pm_客户默认支付方式ID', off_session=True, confirm=True, metadata={'invoice_id': invoice.id} )更新Invoice状态并关联支付记录
等Payment Intent成功后,把Invoice改成已支付状态,并关联这个Payment Intent:stripe.Invoice.modify( invoice.id, paid=True, payment_intent=pi.id )这样客户在Express门户就能看到这个Invoice的收据,同时支付流程是你手动控制的Off-Session支付。
内容的提问来源于stack exchange,提问作者Ramzizi

