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)


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

def active?
  deleted_at.nil?
end

#customer_nameObject



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

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)


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

def deleted?
  deleted_at.present?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)


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

def has_incomplete_payment?
  subscriptions.where(status: %w[incomplete past_due]).exists?
end

#on_generic_trial?Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

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

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

Returns:

  • (Boolean)


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

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.



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

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



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

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