如何通过Stripe API设置订阅续订时发送发票而非自动扣费?
解决Stripe订阅续订时发送发票而非自动扣费的问题
问题说明
Stripe文档提到:
当发票到期时,Stripe会尝试通过两种方式收款:自动扣费已存档的支付方式,或向客户发送发票邮件。
目前自动扣费是默认选项,需要设置年费订阅续订时发送带付款指引的发票邮件,而非自动扣费,但尝试的代码未生效。
业务流程
- 在Stripe后台创建年费订阅产品
- 客户使用银行卡完成第一年费用支付
- 次年订阅续订时,Stripe发送发票邮件引导客户主动付款,不自动扣取费用
核心顾虑
自动扣费不够人性化:年费周期过长,客户可能遗忘订阅,也可能对存储在Stripe的支付数据存在顾虑,容易引发客户不满。
尝试过的无效代码
const session = await stripe.checkout.sessions.create( { customer: stripe_customer, mode: "subscription", payment_method_types: ['card'], line_items: [{ price: priceId, quantity: 1 }], automatic_tax: { enabled: true, }, collection_method: "send_invoice", // 仅影响首次发票,对续订无效 customer_update: { address: 'auto', }, success_url: "http://localhost:5173/payment/success", cancel_url: "http://localhost:5173/payment/canceled", } )
正确解决方案
Checkout的顶级collection_method仅控制首次发票的收款方式,续订发票的行为由订阅本身的配置决定。需要通过subscription_data参数设置订阅的收款规则:
示例代码(Checkout创建订阅)
const session = await stripe.checkout.sessions.create({ customer: stripe_customer, mode: "subscription", payment_method_types: ['card'], line_items: [{ price: priceId, quantity: 1 }], automatic_tax: { enabled: true }, customer_update: { address: 'auto' }, success_url: "http://localhost:5173/payment/success", cancel_url: "http://localhost:5173/payment/canceled", // 关键配置:定义订阅的续订行为 subscription_data: { payment_behavior: 'default_incomplete', payment_settings: { // 不保存客户的支付方式,避免后续自动扣费 save_payment_method: 'off', }, invoice_settings: { // 续订发票采用发送邮件的收款方式 collection_method: 'send_invoice', // 设置发票到期期限,给客户足够付款时间 days_until_due: 30, }, }, });
额外处理(若客户已有保存的支付方式)
如果客户之前已经在Stripe保存了支付方式,需要手动移除默认支付方式,确保续订时不会自动扣费:
await stripe.customers.update(stripe_customer, { invoice_settings: { default_payment_method: null, }, });
原理说明
通过subscription_data.invoice_settings.collection_method设置为send_invoice,可以强制后续所有续订发票都采用邮件发送的方式收款;save_payment_method: 'off'确保首次付款后不存储支付方式,从根源避免自动扣费的可能。
内容的提问来源于stack exchange,提问作者Kvetoslav
相关产品推荐
相关产品推荐

