Class: Pay::Customer
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Pay::Customer
- Defined in:
- app/models/pay/customer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #customer_name ⇒ Object
- #deleted? ⇒ Boolean
- #has_incomplete_payment? ⇒ Boolean
- #on_generic_trial? ⇒ Boolean
- #on_trial?(name: Pay.default_product_name, plan: nil) ⇒ Boolean
- #on_trial_or_subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean
- #pay_processor ⇒ Object
- #subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean
- #subscription(name: Pay.default_product_name) ⇒ Object
- #update_payment_method(payment_method_id) ⇒ Object
Class Method Details
.processor_for(name) ⇒ Object
26 27 28 |
# File 'app/models/pay/customer.rb', line 26 def self.processor_for(name) "Pay::#{name.to_s.classify}::Billable".constantize end |
Instance Method Details
#active? ⇒ Boolean
71 72 73 |
# File 'app/models/pay/customer.rb', line 71 def active? deleted_at.nil? end |
#customer_name ⇒ Object
67 68 69 |
# File 'app/models/pay/customer.rb', line 67 def customer_name owner.respond_to?(:name) ? owner.name : [owner.try(:first_name), owner.try(:last_name)].compact.join(" ") end |
#deleted? ⇒ Boolean
75 76 77 |
# File 'app/models/pay/customer.rb', line 75 def deleted? deleted_at.present? end |
#has_incomplete_payment? ⇒ Boolean
63 64 65 |
# File 'app/models/pay/customer.rb', line 63 def has_incomplete_payment? subscriptions.active.incomplete.any? end |
#on_generic_trial? ⇒ Boolean
79 80 81 82 83 84 85 86 87 |
# File 'app/models/pay/customer.rb', line 79 def on_generic_trial? return false unless fake_processor? subscription = subscriptions.active.last return false unless subscription # If these match, consider it a generic trial subscription.trial_ends_at == subscription.ends_at end |
#on_trial?(name: Pay.default_product_name, plan: nil) ⇒ Boolean
51 52 53 54 55 56 |
# File 'app/models/pay/customer.rb', line 51 def on_trial?(name: Pay.default_product_name, plan: nil) sub = subscription(name: name) return sub&.on_trial? if plan.nil? sub&.on_trial? && sub.processor_plan == plan end |
#on_trial_or_subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean
58 59 60 61 |
# File 'app/models/pay/customer.rb', line 58 def on_trial_or_subscribed?(name: Pay.default_product_name, processor_plan: nil) on_trial?(name: name, plan: processor_plan) || subscribed?(name: name, processor_plan: processor_plan) end |
#pay_processor ⇒ Object
30 31 32 |
# File 'app/models/pay/customer.rb', line 30 def pay_processor @pay_processor ||= self.class.processor_for(processor).new(self) end |
#subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean
42 43 44 45 46 47 48 49 |
# File 'app/models/pay/customer.rb', line 42 def subscribed?(name: Pay.default_product_name, processor_plan: nil) subscription = subscription(name: name) return false if subscription.nil? return subscription.active? if processor_plan.nil? subscription.active? && subscription.processor_plan == processor_plan end |
#subscription(name: Pay.default_product_name) ⇒ Object
38 39 40 |
# File 'app/models/pay/customer.rb', line 38 def subscription(name: Pay.default_product_name) subscriptions.loaded? ? subscriptions.reverse.detect { |s| s.name == name } : subscriptions.for_name(name).last end |
#update_payment_method(payment_method_id) ⇒ Object
34 35 36 |
# File 'app/models/pay/customer.rb', line 34 def update_payment_method(payment_method_id) add_payment_method(payment_method_id, default: true) end |