Class: Pay::Customer

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

Instance Method Summary collapse

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

#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



89
90
91
# File 'app/models/pay/customer.rb', line 89

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)


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

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

#subscription(name: Pay.default_product_name) ⇒ Object

Returns the active or paused subscription for the given name, falling back to the most recently created subscription when none are active.



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

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

#update_payment_method(payment_method_id) ⇒ Object



33
34
35
# File 'app/models/pay/customer.rb', line 33

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