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



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

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/pay/customer.rb', line 79

def active?
  deleted_at.nil?
end

#customer_nameObject



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

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)


83
84
85
# File 'app/models/pay/customer.rb', line 83

def deleted?
  deleted_at.present?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)


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

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

#on_generic_trial?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# File 'app/models/pay/customer.rb', line 87

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)


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

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)


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

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



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

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

#retry_past_due_subscriptions!(status: [:past_due]) ⇒ Object

Attempts to pay all past_due subscription invoices to bring them back to active state Pass in ‘statuses: []` if you would like to only include specific subscription statuses



99
100
101
# File 'app/models/pay/customer.rb', line 99

def retry_past_due_subscriptions!(status: [:past_due])
  subscriptions.where(status: Array.wrap(status)).each(&:pay_open_invoices)
end

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

Returns:

  • (Boolean)


53
54
55
56
# File 'app/models/pay/customer.rb', line 53

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



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

def subscription(name: Pay.default_product_name)
  subscriptions.order(created_at: :desc).for_name(name).first
end

#update_payment_method(payment_method_id) ⇒ Object



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

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