Class: Payola::UpdateCard

Inherits:
Object
  • Object
show all
Defined in:
app/services/payola/update_card.rb

Class Method Summary collapse

Class Method Details

.call(subscription, token) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/payola/update_card.rb', line 3

def self.call(subscription, token)
  secret_key = Payola.secret_key_for_sale(subscription)
  begin
    customer = Stripe::Customer.retrieve(subscription.stripe_customer_id, secret_key)

    customer.source = token
    customer.save

    customer = Stripe::Customer.retrieve(subscription.stripe_customer_id, secret_key)
    card = customer.sources.retrieve(customer.default_source, secret_key)

    subscription.update_attributes(
      card_type: card.brand,
      card_last4: card.last4,
      card_expiration: Date.parse("#{card.exp_year}/#{card.exp_month}/1")
    )
    subscription.save!
  rescue RuntimeError, Stripe::StripeError => e
    subscription.errors[:base] << e.message
  end

  subscription
end