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



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

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  deleted_at.nil?
end

#customer_nameObject



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

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)


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

def deleted?
  deleted_at.present?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)


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

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

#on_generic_trial?Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

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



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

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



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

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)


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

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



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

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

#update_payment_method(payment_method_id) ⇒ Object



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

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