Class: Billy::Subscription
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Billy::Subscription
- Defined in:
- app/models/billy/subscription.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#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”, “past_due”, or other statuses as active.
- #cancel ⇒ Object
- #cancel_now! ⇒ Object
- #canceled? ⇒ Boolean
- #cancelled? ⇒ Boolean
- #ended? ⇒ Boolean
- #features_enabled? ⇒ Boolean
- #has_incomplete_payment? ⇒ Boolean
-
#has_trial? ⇒ Boolean
def generic_trial? fake_processor? && trial_ends_at? end.
- #incomplete? ⇒ Boolean
-
#make_default! ⇒ Object
Makes this the default Subscription.
- #on_grace_period? ⇒ Boolean
-
#on_trial? ⇒ Boolean
Does not include the last second of the trial.
- #past_due? ⇒ Boolean
- #pause ⇒ Object
- #pause_active? ⇒ Boolean
- #paused? ⇒ Boolean
- #payment_processor ⇒ Object
- #processor_subscription(**options) ⇒ Object
- #resume ⇒ Object
- #sync!(**options) ⇒ Object
-
#trial_ended? ⇒ Boolean
def paused? status == “paused” end.
- #unpaid? ⇒ Boolean
- #will_pause? ⇒ Boolean
Class Method Details
.processor_for(name) ⇒ Object
30 31 32 |
# File 'app/models/billy/subscription.rb', line 30 def self.processor_for(name) "Billy::#{name.to_s.classify}::Subscription".constantize 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”, “past_due”, or other statuses as active
122 123 124 125 126 |
# File 'app/models/billy/subscription.rb', line 122 def active? ["trialing", "active", "canceled", "cancelled"].include?(status) && (!(canceled? || paused?) || on_trial? || on_grace_period?) # on_grace_period? end |
#cancel ⇒ Object
192 193 194 |
# File 'app/models/billy/subscription.rb', line 192 def cancel payment_processor.cancel end |
#cancel_now! ⇒ Object
196 197 198 |
# File 'app/models/billy/subscription.rb', line 196 def cancel_now! payment_processor.cancel_now! end |
#canceled? ⇒ Boolean
80 81 82 |
# File 'app/models/billy/subscription.rb', line 80 def canceled? ends_at? end |
#cancelled? ⇒ Boolean
84 85 86 |
# File 'app/models/billy/subscription.rb', line 84 def cancelled? canceled? end |
#ended? ⇒ Boolean
88 89 90 |
# File 'app/models/billy/subscription.rb', line 88 def ended? ends_at? && ends_at <= Time.current end |
#features_enabled? ⇒ Boolean
57 58 59 |
# File 'app/models/billy/subscription.rb', line 57 def features_enabled? !trial_ended? && !ended? end |
#has_incomplete_payment? ⇒ Boolean
140 141 142 |
# File 'app/models/billy/subscription.rb', line 140 def has_incomplete_payment? past_due? || incomplete? end |
#has_trial? ⇒ Boolean
def generic_trial?
fake_processor? && trial_ends_at?
end
67 68 69 |
# File 'app/models/billy/subscription.rb', line 67 def has_trial? trial_ends_at? end |
#incomplete? ⇒ Boolean
136 137 138 |
# File 'app/models/billy/subscription.rb', line 136 def incomplete? status == "incomplete" end |
#make_default! ⇒ Object
Makes this the default Subscription
49 50 51 52 53 54 |
# File 'app/models/billy/subscription.rb', line 49 def make_default! return if default? account.subscriptions.update_all(default: false) update!(default: true) end |
#on_grace_period? ⇒ Boolean
92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/billy/subscription.rb', line 92 def on_grace_period? # (ends_at? && ends_at > Time.current) || # ((status == "paused") && will_pause?) # ((status == "paused" || pause_behaviour == "void") && will_pause?) if ends_at? ends_at > Time.current elsif status == "paused" will_pause? end end |
#on_trial? ⇒ Boolean
Does not include the last second of the trial
72 73 74 |
# File 'app/models/billy/subscription.rb', line 72 def on_trial? trial_ends_at? && trial_ends_at > Time.current end |
#past_due? ⇒ Boolean
128 129 130 |
# File 'app/models/billy/subscription.rb', line 128 def past_due? status == "past_due" end |
#pause ⇒ Object
184 185 186 |
# File 'app/models/billy/subscription.rb', line 184 def pause payment_processor.pause end |
#pause_active? ⇒ Boolean
107 108 109 110 111 112 113 |
# File 'app/models/billy/subscription.rb', line 107 def pause_active? if status == "paused" # Paddle true elsif pause_behaviour == "void" paused_from.nil? || Time.current.after?(paused_from) end end |
#paused? ⇒ Boolean
115 116 117 |
# File 'app/models/billy/subscription.rb', line 115 def paused? status == "paused" end |
#payment_processor ⇒ Object
34 35 36 |
# File 'app/models/billy/subscription.rb', line 34 def payment_processor @payment_processor ||= self.class.processor_for(account.processor).new(self) end |
#processor_subscription(**options) ⇒ Object
43 44 45 |
# File 'app/models/billy/subscription.rb', line 43 def processor_subscription(**) payment_processor.subscription(**) end |
#resume ⇒ Object
188 189 190 |
# File 'app/models/billy/subscription.rb', line 188 def resume payment_processor.resume end |
#sync!(**options) ⇒ Object
38 39 40 41 |
# File 'app/models/billy/subscription.rb', line 38 def sync!(**) self.class.processor_for(account.processor).sync(processor_id, **) reload end |
#trial_ended? ⇒ Boolean
def paused?
status == "paused"
end
167 168 169 |
# File 'app/models/billy/subscription.rb', line 167 def trial_ended? trial_ends_at? && trial_ends_at <= Time.current end |
#unpaid? ⇒ Boolean
132 133 134 |
# File 'app/models/billy/subscription.rb', line 132 def unpaid? status == "unpaid" end |
#will_pause? ⇒ Boolean
103 104 105 |
# File 'app/models/billy/subscription.rb', line 103 def will_pause? paused_from? && Time.current < paused_from end |