Class: Pay::Subscription
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Pay::Subscription
- Defined in:
- app/models/pay/subscription.rb
Constant Summary collapse
- STATUSES =
%w[incomplete incomplete_expired trialing active past_due canceled unpaid paused]
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.
- #canceled? ⇒ Boolean
- #cancelled? ⇒ Boolean
- #change_quantity(quantity, **options) ⇒ Object
- #ended? ⇒ Boolean
- #generic_trial? ⇒ Boolean
- #has_incomplete_payment? ⇒ Boolean
- #has_trial? ⇒ Boolean
- #incomplete? ⇒ Boolean
- #latest_payment ⇒ Object
- #no_prorate ⇒ Object
-
#on_trial? ⇒ Boolean
Does not include the last second of the trial.
- #past_due? ⇒ Boolean
- #payment_processor ⇒ Object
- #processor_subscription(**options) ⇒ Object
- #resume ⇒ Object
- #skip_trial ⇒ Object
- #swap(plan, **options) ⇒ Object
- #swap_and_invoice(plan) ⇒ Object
- #sync!(**options) ⇒ Object
- #trial_ended? ⇒ Boolean
- #unpaid? ⇒ Boolean
Class Method Details
.find_by_processor_and_id(processor, processor_id) ⇒ Object
53 54 55 |
# File 'app/models/pay/subscription.rb', line 53 def self.find_by_processor_and_id(processor, processor_id) joins(:customer).find_by(processor_id: processor_id, pay_customers: {processor: processor}) end |
.pay_processor_for(name) ⇒ Object
57 58 59 |
# File 'app/models/pay/subscription.rb', line 57 def self.pay_processor_for(name) "Pay::#{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
110 111 112 113 |
# File 'app/models/pay/subscription.rb', line 110 def active? ["trialing", "active", "canceled"].include?(status) && (!(canceled? || paused?) || on_trial? || on_grace_period?) end |
#canceled? ⇒ Boolean
95 96 97 |
# File 'app/models/pay/subscription.rb', line 95 def canceled? ends_at? end |
#cancelled? ⇒ Boolean
99 100 101 |
# File 'app/models/pay/subscription.rb', line 99 def cancelled? canceled? end |
#change_quantity(quantity, **options) ⇒ Object
131 132 133 134 |
# File 'app/models/pay/subscription.rb', line 131 def change_quantity(quantity, **) payment_processor.change_quantity(quantity, **) update(quantity: quantity) end |
#ended? ⇒ Boolean
103 104 105 |
# File 'app/models/pay/subscription.rb', line 103 def ended? ends_at? && ends_at <= Time.current end |
#generic_trial? ⇒ Boolean
78 79 80 |
# File 'app/models/pay/subscription.rb', line 78 def generic_trial? fake_processor? && trial_ends_at? end |
#has_incomplete_payment? ⇒ Boolean
127 128 129 |
# File 'app/models/pay/subscription.rb', line 127 def has_incomplete_payment? past_due? || incomplete? end |
#has_trial? ⇒ Boolean
82 83 84 |
# File 'app/models/pay/subscription.rb', line 82 def has_trial? trial_ends_at? end |
#incomplete? ⇒ Boolean
123 124 125 |
# File 'app/models/pay/subscription.rb', line 123 def incomplete? status == "incomplete" end |
#latest_payment ⇒ Object
156 157 158 |
# File 'app/models/pay/subscription.rb', line 156 def latest_payment processor_subscription(expand: ["latest_invoice.payment_intent"]).latest_invoice.payment_intent end |
#no_prorate ⇒ Object
70 71 72 |
# File 'app/models/pay/subscription.rb', line 70 def no_prorate self.prorate = false end |
#on_trial? ⇒ Boolean
Does not include the last second of the trial
87 88 89 |
# File 'app/models/pay/subscription.rb', line 87 def on_trial? trial_ends_at? && trial_ends_at > Time.current end |
#past_due? ⇒ Boolean
115 116 117 |
# File 'app/models/pay/subscription.rb', line 115 def past_due? status == "past_due" end |
#payment_processor ⇒ Object
61 62 63 |
# File 'app/models/pay/subscription.rb', line 61 def payment_processor @payment_processor ||= self.class.pay_processor_for(customer.processor).new(self) end |
#processor_subscription(**options) ⇒ Object
152 153 154 |
# File 'app/models/pay/subscription.rb', line 152 def processor_subscription(**) payment_processor.subscription(**) end |
#resume ⇒ Object
136 137 138 139 140 |
# File 'app/models/pay/subscription.rb', line 136 def resume payment_processor.resume update(ends_at: nil, status: :active) self end |
#skip_trial ⇒ Object
74 75 76 |
# File 'app/models/pay/subscription.rb', line 74 def skip_trial self.trial_ends_at = nil end |
#swap(plan, **options) ⇒ Object
142 143 144 145 |
# File 'app/models/pay/subscription.rb', line 142 def swap(plan, **) raise ArgumentError, "plan must be a string. Got `#{plan.inspect}` instead." unless plan.is_a?(String) payment_processor.swap(plan, **) end |
#swap_and_invoice(plan) ⇒ Object
147 148 149 150 |
# File 'app/models/pay/subscription.rb', line 147 def swap_and_invoice(plan) swap(plan) customer.invoice!(subscription: processor_id) end |
#sync!(**options) ⇒ Object
65 66 67 68 |
# File 'app/models/pay/subscription.rb', line 65 def sync!(**) self.class.pay_processor_for(customer.processor).sync(processor_id, **) reload end |
#trial_ended? ⇒ Boolean
91 92 93 |
# File 'app/models/pay/subscription.rb', line 91 def trial_ended? trial_ends_at? && trial_ends_at <= Time.current end |
#unpaid? ⇒ Boolean
119 120 121 |
# File 'app/models/pay/subscription.rb', line 119 def unpaid? status == "unpaid" end |