Class: Pay::Subscription
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Pay::Subscription
show all
- Defined in:
- app/models/pay/subscription.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.find_by_processor_and_id(processor, processor_id) ⇒ Object
46
47
48
|
# File 'app/models/pay/subscription.rb', line 46
def self.find_by_processor_and_id(processor, processor_id)
joins(:customer).find_by(processor_id: processor_id, pay_customers: {processor: processor})
end
|
Instance Method Details
#active? ⇒ Boolean
If you cancel during a trial, you should still retain access until the end of the trial
Otherwise a subscription is active unless it has ended or is currently paused
Check the subscription status so we don't accidentally consider "incomplete", "unpaid", or other statuses as active
89
90
91
92
|
# File 'app/models/pay/subscription.rb', line 89
def active?
["trialing", "active"].include?(status) &&
(!(canceled? || paused?) || on_trial? || on_grace_period?)
end
|
#canceled? ⇒ Boolean
70
71
72
|
# File 'app/models/pay/subscription.rb', line 70
def canceled?
ends_at?
end
|
#cancelled? ⇒ Boolean
74
75
76
|
# File 'app/models/pay/subscription.rb', line 74
def cancelled?
canceled?
end
|
#ended? ⇒ Boolean
78
79
80
|
# File 'app/models/pay/subscription.rb', line 78
def ended?
ends_at? && ends_at <= Time.current
end
|
#generic_trial? ⇒ Boolean
55
56
57
|
# File 'app/models/pay/subscription.rb', line 55
def generic_trial?
fake_processor? && trial_ends_at?
end
|
#has_incomplete_payment? ⇒ Boolean
106
107
108
|
# File 'app/models/pay/subscription.rb', line 106
def has_incomplete_payment?
past_due? || incomplete?
end
|
#incomplete? ⇒ Boolean
102
103
104
|
# File 'app/models/pay/subscription.rb', line 102
def incomplete?
status == "incomplete"
end
|
#on_grace_period? ⇒ Boolean
82
83
84
|
# File 'app/models/pay/subscription.rb', line 82
def on_grace_period?
ends_at? && ends_at > Time.current
end
|
#on_trial? ⇒ Boolean
Does not include the last second of the trial
60
61
62
63
|
# File 'app/models/pay/subscription.rb', line 60
def on_trial?
return false if ended?
trial_ends_at? && trial_ends_at > Time.current
end
|
#past_due? ⇒ Boolean
94
95
96
|
# File 'app/models/pay/subscription.rb', line 94
def past_due?
status == "past_due"
end
|
#swap_and_invoice(plan) ⇒ Object
110
111
112
113
|
# File 'app/models/pay/subscription.rb', line 110
def swap_and_invoice(plan)
swap(plan)
customer.invoice!(subscription: processor_id)
end
|
#sync!(**options) ⇒ Object
50
51
52
53
|
# File 'app/models/pay/subscription.rb', line 50
def sync!(**options)
self.class.sync(processor_id, **options)
reload
end
|
#trial_ended? ⇒ Boolean
65
66
67
68
|
# File 'app/models/pay/subscription.rb', line 65
def trial_ended?
return true if ended?
trial_ends_at? && trial_ends_at <= Time.current
end
|
#unpaid? ⇒ Boolean
98
99
100
|
# File 'app/models/pay/subscription.rb', line 98
def unpaid?
status == "unpaid"
end
|