Tintin81
Tintin81

Reputation: 10207

How to retrieve Stripe subscription and credit card with one request only?

in Stripe version 2020-03-02 I was able to retrieve a subscription and its associated credit_card in one go like this:

stripe_subscription = Stripe::Subscription.retrieve({:id => stripe_subscription_id, :expand => [:customer]})
stripe_customer = stripe_subscription.customer
stripe_credit_card = stripe_customer.sources.data.first

In version 2020-08-27 this seems no longer possible since Stripe won't recognise the sources attribute on customer.

So how can I retrieve a subscription and its credit card with one request only?

Upvotes: 0

Views: 125

Answers (1)

koopajah
koopajah

Reputation: 25552

Since sources on Customer is not included by default, you have to explicitly include it when you expand the related properties. Your code would look like this:

stripe_subscription = Stripe::Subscription.retrieve({
  id: stripe_subscription_id,
  expand: ['customer.sources'],
})
stripe_customer = stripe_subscription.customer
stripe_credit_card = stripe_customer.sources.data.first

The expand feature is quite powerful and lets you expand multiple separate properties or chain expansion like we did above. I recommend reading the detailed documentation that Stripe shipped.

Upvotes: 1

Related Questions