Module: Stripeable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/stripeable.rb

Instance Method Summary collapse

Instance Method Details

#card_expirationObject

TODO: Redo this to accommodate banks?



102
103
104
105
106
107
108
109
110
# File 'app/models/concerns/stripeable.rb', line 102

def card_expiration # TODO: Redo this to accommodate banks?
  if self.stripe_customer_id? && self.stripe_card_token?
    Rails.cache.fetch("#{cache_key}/card_expiration", expires_in: 7.days) do
      exp_month = Stripe::Customer.retrieve(self.stripe_customer_id).sources.data.first.exp_month
      exp_year = Stripe::Customer.retrieve(self.stripe_customer_id).sources.data.first.exp_year
      "#{exp_month}/#{exp_year}"
    end
  end
end

#create_stripe_customer_idObject



9
10
11
12
13
14
15
# File 'app/models/concerns/stripeable.rb', line 9

def create_stripe_customer_id
  customer = Stripe::Customer.create(
    :description => self.name
    #:email => @agency.email
  )
  self.update_attributes(stripe_customer_id: customer.id)
end

#has_bank_account?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/concerns/stripeable.rb', line 56

def has_bank_account?
  if self.stripe_customer_id?
    if Stripe::Customer.retrieve(self.stripe_customer_id).sources.data.first.object == "bank_account"
      true
    else
      false
    end
  else
    false
  end
end

#has_card?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/stripeable.rb', line 44

def has_card?
  if self.stripe_customer_id?
    if Stripe::Customer.retrieve(self.stripe_customer_id).sources.data.first.object == "card"
      true
    else
      false
    end
  else
    false
  end
end

#has_source?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/concerns/stripeable.rb', line 32

def has_source?
  if self.stripe_customer_id?
    if Stripe::Customer.retrieve(self.stripe_customer_id).sources.any?
      true
    else
      false
    end
  else
    false
  end
end

#is_billable?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'app/models/concerns/stripeable.rb', line 68

def is_billable?
  if self.stripe_customer_id? && self.has_source?
    true
  else
    false
  end
end

#payment_last4Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/concerns/stripeable.rb', line 89

def payment_last4
  if self.is_billable?
    Rails.cache.fetch("#{cache_key}/payment_last4", expires_in: 7.days) do
      if self.has_card?
        last4 = Stripe::Customer.retrieve(self.stripe_customer_id).sources.data.first.last4
        "#{last4}"
      elsif self.has_bank_account?
        # Something...
      end
    end
  end
end

#payment_nameObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/concerns/stripeable.rb', line 76

def payment_name
  if self.is_billable?
    Rails.cache.fetch("#{cache_key}/payment_name", expires_in: 7.days) do
      if self.has_card?
        name = Stripe::Customer.retrieve(self.stripe_customer_id).sources.data.first.brand
        "#{name}"
      elsif self.has_bank_account?
        # Something...
      end
    end
  end
end

#update_stripeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/concerns/stripeable.rb', line 17

def update_stripe
  # TODO: Update this method to work w/ bank accounts
  if self.saved_change_to_stripe_card_token? && self.stripe_card_token? && self.stripe_customer_id?
    token = self.stripe_card_token
    customer = Stripe::Customer.retrieve(self.stripe_customer_id)
    customer.sources.create(source: self.stripe_card_token)
    customer.save
  elsif saved_change_to_remove_card? && remove_card && stripe_customer_id
    customer = Stripe::Customer.retrieve(self.stripe_customer_id)
    customer.sources.retrieve(customer.sources.data.first.id).delete
    customer.save
    self.update_attributes(remove_card: nil, stripe_card_token: nil)
  end
end