显示条纹信用卡的最后4位数字

xps*_*15z 3 ruby-on-rails stripe-payments

我试图弄清楚如何使用Stripe显示用户默认卡的最后4位数.我提出的代码不会显示它.

有没有人知道如何解决我的问题?

控制器:

  def last4
  customer = Stripe::Customer.retrieve(user.subscription.stripe_customer_token)
  card = customer.cards.retrieve(user.subscription.stripe_card_id)
  charge = Stripe::Charge.retrieve(user.subscription.stripe_charge_id)
  self.last_4_digits = charge.card.last4
  end
Run Code Online (Sandbox Code Playgroud)

Soj*_*ose 5

条纹客户将有一个default_source字段,它将为您提供卡ID.使用卡ID搜索sources in sources属性以获取卡对象,这将为您提供last4属性.

@stripe_customer = Stripe::Customer.retrieve("stripe customer id")
default_card_id = @stripe_customer.default_source
@default_card = @stripe_customer.sources[:data].find {|x| x[:id] == default_card_id }
@default_card.last4
Run Code Online (Sandbox Code Playgroud)