Class: Effective::Customer
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Customer
- Defined in:
- app/models/effective/customer.rb
Instance Attribute Summary collapse
-
#stripe_customer ⇒ Object
Returns the value of attribute stripe_customer.
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #create_stripe_customer! ⇒ Object
- #email ⇒ Object
- #invoices ⇒ Object
- #past_due? ⇒ Boolean
- #payment_status ⇒ Object
- #to_s ⇒ Object
- #token_required? ⇒ Boolean
- #upcoming_invoice ⇒ Object
Instance Attribute Details
#stripe_customer ⇒ Object
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
Instance Method Details
#active? ⇒ Boolean
77 78 79 |
# File 'app/models/effective/customer.rb', line 77 def active? subscriptions.present? && subscriptions.all? { |subscription| subscription.active? } end |
#create_stripe_customer! ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/effective/customer.rb', line 36 def create_stripe_customer! return if stripe_customer.present? raise('expected a user') unless user.present? Rails.logger.info "[STRIPE] create customer: #{user.email}" self.stripe_customer = EffectiveOrders.with_stripe { ::Stripe::Customer.create(email: user.email, description: user.to_s, metadata: { user_id: user.id }) } self.stripe_customer_id = stripe_customer.id save! end |
#email ⇒ Object
32 33 34 |
# File 'app/models/effective/customer.rb', line 32 def email user.email if user end |
#invoices ⇒ Object
55 56 57 58 59 60 |
# File 'app/models/effective/customer.rb', line 55 def invoices @invoices ||= if stripe_customer_id.present? Rails.logger.info "[STRIPE] list invoices: #{stripe_customer_id}" EffectiveOrders.with_stripe { ::Stripe::Invoice.list(customer: stripe_customer_id) rescue nil } end end |
#past_due? ⇒ Boolean
73 74 75 |
# File 'app/models/effective/customer.rb', line 73 def past_due? subscriptions.any? { |subscription| subscription.past_due? } end |
#payment_status ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/effective/customer.rb', line 81 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_s ⇒ Object
28 29 30 |
# File 'app/models/effective/customer.rb', line 28 def to_s user.to_s.presence || 'New Customer' end |
#token_required? ⇒ Boolean
69 70 71 |
# File 'app/models/effective/customer.rb', line 69 def token_required? active_card.blank? || past_due? end |
#upcoming_invoice ⇒ Object
62 63 64 65 66 67 |
# File 'app/models/effective/customer.rb', line 62 def upcoming_invoice @upcoming_invoice ||= if stripe_customer_id.present? Rails.logger.info "[STRIPE] get upcoming invoice: #{stripe_customer_id}" EffectiveOrders.with_stripe { ::Stripe::Invoice.upcoming(customer: stripe_customer_id) rescue nil } end end |