Tintin81
Tintin81

Reputation: 10207

How to get default payment method from Stripe invoice?

I am listening to Stripe's invoice.payment_failed webhook with my web app and I would like to get the default_payment_method from each invoice but for some reason it always returns nothing but an empty array!

When I query for a Stripe invoice on the command line and expand on the default_payment_method like this...

Stripe::Invoice.retrieve('in_3K6dIY2KgYRkshw2LAzya63P', :expand => "default_payment_method")

...I also get empty arrays. This surprises me because all my Stripe customers do have a default payment method associated with them.

What am I missing here?

Thanks for any help.

Upvotes: 2

Views: 2542

Answers (1)

karllekko
karllekko

Reputation: 7218

There are three independent places a default payment method can be set. From more specific to less specific they go :

  • invoice.default_payment_method (which you are looking at)
  • subscription.default_payment_method
  • customer.invoice_settings.default_payment_method

Stripe charges the most specific one if it's set. When reading from the API, those values don't inherit from the level above, they can all be set individually, if they are not set explicitly then they are null. So that's why you see it as null on the Invoice level.

Instead you likely want to look at the Subscription object or the Customer object(and can leverage the expand feature for that), depending on how you built your integration and which one it sets.

Overall though, you probably actually want the PaymentMethod used in the invoice payment though? That would be from the last_payment_error.

inv = Stripe::Invoice.retrieve({
   id: 'in_1K8iiKJoUivz182DMzSkuBgp',
   expand: ["customer.invoice_settings.default_payment_method",
       "subscription.default_payment_method",
       "payment_intent"]
  }
)

print("invoice : #{inv.default_payment_method} \n")
print("subscription : #{inv.subscription.default_payment_method} \n")
print("customer : #{inv.customer.invoice_settings.default_payment_method} \n")
print("failed charge : #{inv.payment_intent.last_payment_error.payment_method} \n") 

Upvotes: 5

Related Questions