首页 > 解决方案 > 如何仅通过一个请求检索 Stripe 订阅和信用卡?

问题描述

在 Stripe 版本 2020-03-02 中,我能够一次性检索 asubscription及其关联credit_card,如下所示:

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

在 2020-08-27 版本中,这似乎不再可能,因为 Stripe 无法sources识别customer.

那么如何仅通过一个请求检索订阅及其信用卡呢?

标签: stripe-payments

解决方案


由于默认情况下不包含sourceson Customer,因此您必须在展开相关属性时显式包含它。您的代码如下所示:

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

扩展功能非常强大,可以像我们上面那样扩展多个单独的属性或链扩展。我建议阅读Stripe 提供的详细文档。


推荐阅读