Class: Effective::Customer

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/customer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stripe_customerObject

Returns the value of attribute stripe_customer.



5
6
7
# File 'app/models/effective/customer.rb', line 5

def stripe_customer
  @stripe_customer
end

Class Method Details

.for_user(user) ⇒ Object



22
23
24
# File 'app/models/effective/customer.rb', line 22

def self.for_user(user)
  Effective::Customer.where(user: user).first_or_initialize
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/effective/customer.rb', line 56

def active?
  subscriptions.present? && subscriptions.all? { |subscription| subscription.active? }
end

#emailObject



30
31
32
# File 'app/models/effective/customer.rb', line 30

def email
  user.email if user
end

#past_due?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/effective/customer.rb', line 52

def past_due?
  subscriptions.any? { |subscription| subscription.past_due? }
end

#payment_statusObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/effective/customer.rb', line 60

def payment_status
  if past_due?
    'We ran into an error processing your last payment. Please update or confirm your card details to continue.'
  elsif active?
    "Your payment is in good standing. Thanks so much for your support!"
  elsif active_card.blank?
    'No credit card on file. Please add a card.'
  else
    'Please update or confirm your card details to continue.'
  end.html_safe
end

#to_sObject



26
27
28
# File 'app/models/effective/customer.rb', line 26

def to_s
  user.to_s.presence || 'New Customer'
end

#token_required?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/effective/customer.rb', line 48

def token_required?
  active_card.blank? || past_due?
end

#upcoming_invoiceObject



41
42
43
44
45
46
# File 'app/models/effective/customer.rb', line 41

def upcoming_invoice
  @upcoming_invoice ||= if stripe_customer_id.present?
    Rails.logger.info "[STRIPE] get upcoming invoice: #{stripe_customer_id}"
    ::Stripe::Invoice.upcoming(customer: stripe_customer_id) rescue nil
  end
end