Class: Pay::Customer

Inherits:
ApplicationRecord show all
Defined in:
app/models/pay/customer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pay_processor_for(name) ⇒ Object


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

def self.pay_processor_for(name)
  "Pay::#{name.to_s.classify}::Billable".constantize
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'app/models/pay/customer.rb', line 69

def active?
  deleted_at.nil?
end

#customer_nameObject


64
65
66
67
# File 'app/models/pay/customer.rb', line 64

def customer_name
  return owner.pay_customer_name if owner.respond_to?(:pay_customer_name) && owner.pay_customer_name.present?
  owner.respond_to?(:name) ? owner.name : [owner.try(:first_name), owner.try(:last_name)].compact.join(" ")
end

#deleted?Boolean

Returns:

  • (Boolean)

73
74
75
# File 'app/models/pay/customer.rb', line 73

def deleted?
  deleted_at.present?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'app/models/pay/customer.rb', line 60

def has_incomplete_payment?
  subscriptions.active.incomplete.any?
end

#on_generic_trial?Boolean

Returns:

  • (Boolean)

77
78
79
80
81
82
83
84
85
# File 'app/models/pay/customer.rb', line 77

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

Returns:

  • (Boolean)

48
49
50
51
52
53
# File 'app/models/pay/customer.rb', line 48

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

Returns:

  • (Boolean)

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

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_processorObject


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

def pay_processor
  return if processor.blank?
  @pay_processor ||= self.class.pay_processor_for(processor).new(self)
end

#subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean

Returns:

  • (Boolean)

43
44
45
46
# File 'app/models/pay/customer.rb', line 43

def subscribed?(name: Pay.default_product_name, processor_plan: nil)
  query = {name: name, processor_plan: processor_plan}.compact
  subscriptions.active.where(query).exists?
end

#subscription(name: Pay.default_product_name) ⇒ Object


39
40
41
# File 'app/models/pay/customer.rb', line 39

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


35
36
37
# File 'app/models/pay/customer.rb', line 35

def update_payment_method(payment_method_id)
  add_payment_method(payment_method_id, default: true)
end