Class: Pay::Subscription
Constant Summary
collapse
- STATUSES =
%w[incomplete incomplete_expired trialing active past_due canceled unpaid paused]
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
95
96
97
98
|
# File 'app/models/pay/subscription.rb', line 95
def active?
["trialing", "active"].include?(status) &&
(!(canceled? || paused?) || on_trial? || on_grace_period?)
end
|
#canceled? ⇒ Boolean
76
77
78
|
# File 'app/models/pay/subscription.rb', line 76
def canceled?
ends_at?
end
|
#cancelled? ⇒ Boolean
80
81
82
|
# File 'app/models/pay/subscription.rb', line 80
def cancelled?
canceled?
end
|
#ended? ⇒ Boolean
84
85
86
|
# File 'app/models/pay/subscription.rb', line 84
def ended?
ends_at? && ends_at <= Time.current
end
|
#generic_trial? ⇒ Boolean
59
60
61
|
# File 'app/models/pay/subscription.rb', line 59
def generic_trial?
fake_processor? && trial_ends_at?
end
|
#has_incomplete_payment? ⇒ Boolean
112
113
114
|
# File 'app/models/pay/subscription.rb', line 112
def has_incomplete_payment?
past_due? || incomplete?
end
|
#has_trial? ⇒ Boolean
63
64
65
|
# File 'app/models/pay/subscription.rb', line 63
def has_trial?
trial_ends_at?
end
|
#incomplete? ⇒ Boolean
108
109
110
|
# File 'app/models/pay/subscription.rb', line 108
def incomplete?
status == "incomplete"
end
|
#on_grace_period? ⇒ Boolean
88
89
90
|
# File 'app/models/pay/subscription.rb', line 88
def on_grace_period?
ends_at? && ends_at > Time.current
end
|
#on_trial? ⇒ Boolean
Does not include the last second of the trial
68
69
70
|
# File 'app/models/pay/subscription.rb', line 68
def on_trial?
trial_ends_at? && trial_ends_at > Time.current
end
|
#past_due? ⇒ Boolean
100
101
102
|
# File 'app/models/pay/subscription.rb', line 100
def past_due?
status == "past_due"
end
|
#skip_trial ⇒ Object
55
56
57
|
# File 'app/models/pay/subscription.rb', line 55
def skip_trial
self.trial_ends_at = nil
end
|
#swap_and_invoice(plan) ⇒ Object
116
117
118
119
|
# File 'app/models/pay/subscription.rb', line 116
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
72
73
74
|
# File 'app/models/pay/subscription.rb', line 72
def trial_ended?
trial_ends_at? && trial_ends_at <= Time.current
end
|
#unpaid? ⇒ Boolean
104
105
106
|
# File 'app/models/pay/subscription.rb', line 104
def unpaid?
status == "unpaid"
end
|