Joshua Ohana
Joshua Ohana

Reputation: 6121

C# Stripe create customer with default payment method not working

In my workflow I am creating a new customer with a valid payment method and want to set that as default (to apply to subscription billing)

Based on the docs it seems if I'm using PaymentsIntent I need to set DefaultPaymentMethod inside InvoiceSettings and IntelliSense matches everything up as expected. https://stripe.com/docs/api/customers/create#create_customer-invoice_settings-default_payment_method

When I actually create the options object (not even making the Create call) I get the below error

{System.NullReferenceException: Object reference not set to an instance of an object. at ... [offending code below]

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

If I remove the InvoiceSettings param everything works perfectly, except their payment method is not set as default. I have also tried with null in DefaultPaymentMethod, same error.

How can I, during customer creation, set their payment method as default?

Upvotes: 0

Views: 972

Answers (1)

cjav_dev
cjav_dev

Reputation: 3105

You'll need to initialize an instance of CustomerInvoiceSettingsOptions to pass as the InvoiceSettings which you can do with the following:

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

Upvotes: 1

Related Questions