如何在Stripe中确定客户付款方式?含默认付款方式设置异常
Stripe多付款方式选择及默认付款方式设置问题
一、多付款方式的选择逻辑
Stripe确实没有全局的"默认付款方式"概念,但你可以通过两种方式处理:
- 自己在系统中记录用户指定的默认付款方式:用户注册或后续管理时,让他们选择一个默认付款方式并存在你的数据库里,生成账单时直接调用这个ID。
- 利用Stripe的付款方式返回规则:查询客户的付款方式列表时,Stripe会按添加时间倒序返回(最新添加的在前),你可以默认选第一个;或者筛选出
card类型中状态为active的付款方式,优先使用信用卡类选项。
二、设置客户默认付款方式的正确方法
你之前的代码是创建发票时指定DefaultPaymentMethod,这只会影响当前发票的付款方式,不会修改客户的全局默认设置。要修改客户的invoice_settings.default_payment_method,需要调用Customer的Update接口:
CustomerService customerService = new(); CustomerUpdateOptions customerUpdateOptions = new() { InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = session.SetupIntent.PaymentMethodId } }; customer = await customerService.UpdateAsync(customer.Id, customerUpdateOptions); // 后续查询客户并展开默认付款方式 CustomerGetOptions customerGetOptions = new() { Expand = ["invoice_settings.default_payment_method"], }; customer = await customerService.GetAsync(customer.Id, customerGetOptions);
这样修改后,客户的InvoiceSettings.DefaultPaymentMethodId就会被正确赋值,后续生成发票时如果不单独指定付款方式,会自动使用这个默认值。
三、补充说明
- 如果只是针对某张发票临时指定付款方式,你之前的代码是有效的,但不会改变客户的全局默认设置。
- 确保
session.SetupIntent.PaymentMethodId对应的付款方式已关联到该客户(创建SetupIntent时需指定Customer参数),否则无法设置为客户的默认付款方式。
内容的提问来源于stack exchange,提问作者Jonathan Wood
相关产品推荐
相关产品推荐

