【问题标题】:C# Stripe create customer with default payment method not workingC# Stripe 使用默认付款方式创建客户不起作用
【发布时间】:2019-12-27 01:07:41
【问题描述】:

在我的工作流程中,我正在创建一个具有有效付款方式的新客户,并希望将其设置为默认值(以应用于订阅计费)

根据文档,如果我使用 PaymentsIntent,我似乎需要在 InvoiceSettings 中设置 DefaultPaymentMethod,并且 IntelliSense 会按预期匹配所有内容。 https://stripe.com/docs/api/customers/create#create_customer-invoice_settings-default_payment_method

当我实际创建选项对象时(甚至没有进行 Create 调用),我收到以下错误

{System.NullReferenceException:对象引用未设置为 对象的实例。 at ... [下面有问题的代码]

var customerOptions = new CustomerCreateOptions
{
    Email = user.Email,
    Name = user.FirstName + ' ' + user.LastName,
    PaymentMethod = model.payment_method,
    InvoiceSettings =
    {
        DefaultPaymentMethod = model.payment_method
    }
};

如果我删除 InvoiceSettings 参数,一切正常,除了他们的付款方式没有设置为默认值。我也尝试在 DefaultPaymentMethod 中使用 null,同样的错误。

如何在创建客户期间将其付款方式设置为默认方式?

【问题讨论】:

  • Joshua,你能确认 model.payment_method 是像“pm_xxx”这样的字符串吗?这应该有效。
  • 没错,就是这样的字符串。根据文档相同的要求,它在PaymentMethod = model.payment_method 上面的行中完美地工作,我同意这应该工作:P
  • 您可以编辑问题以包含错误的完整输出吗?你能打印出 user.Email、user.FirstName、user.LastName 和 model.payment_method 来确认这些是我们所期望的吗?
  • 如果对您有帮助,大多数工作时间都有 Stripe 工程师可以在 Freenode 上的#stripe 频道 (webchat.freenode.net/?channel=#stripe) 聊天。
  • @w1zeman1p 我 100% 确定这是 InvoiceSettings 参数,如果我删除一切正常,客户就会被创建并保存他们的付款方式......唯一的问题是他们的卡没有设置作为默认值 - 感谢 freenode 链接

标签: stripe-payments


【解决方案1】:

您需要初始化CustomerInvoiceSettingsOptions 的实例以作为InvoiceSettings 传递,您可以使用以下方法:

var customerOptions = new CustomerCreateOptions
{
    Email = user.Email,
    Name = user.FirstName + ' ' + user.LastName,
    PaymentMethod = model.payment_method,
    InvoiceSettings = new CustomerInvoiceSettingsOptions
    {
        DefaultPaymentMethod = model.payment_method
    }
};

【讨论】:

  • 乐于助人! :D
猜你喜欢
  • 2021-03-29
  • 2019-11-11
  • 2023-02-04
  • 2021-05-15
  • 1970-01-01
  • 2021-02-14
  • 2014-12-28
  • 2019-12-31
  • 2017-07-01
相关资源
最近更新 更多