Class: Pay::Customer

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

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  deleted_at.nil?
end

#customer_nameObject



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

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)


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

def deleted?
  deleted_at.present?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)


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

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

#on_generic_trial?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
# File 'app/models/pay/customer.rb', line 74

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)


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

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)


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

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



86
87
88
# File 'app/models/pay/customer.rb', line 86

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)


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

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



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

def subscription(name: Pay.default_product_name)
  subscriptions.order(created_at: :desc).for_name(name).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