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]
Instance Method Summary collapse
- #active? ⇒ Boolean
- #canceled? ⇒ Boolean
- #cancelled? ⇒ Boolean
- #has_incomplete_payment? ⇒ Boolean
- #incomplete? ⇒ Boolean
- #latest_payment ⇒ Object
- #no_prorate ⇒ Object
- #on_trial? ⇒ Boolean
- #past_due? ⇒ Boolean
- #payment_processor ⇒ Object
- #payment_processor_for(name) ⇒ Object
- #processor_subscription(options = {}) ⇒ Object
- #resume ⇒ Object
- #skip_trial ⇒ Object
- #swap(plan) ⇒ Object
- #swap_and_invoice(plan) ⇒ Object
Methods inherited from ApplicationRecord
Instance Method Details
#active? ⇒ Boolean
81 82 83 |
# File 'app/models/pay/subscription.rb', line 81 def active? ["trialing", "active"].include?(status) && (ends_at.nil? || on_grace_period? || on_trial?) end |
#canceled? ⇒ Boolean
73 74 75 |
# File 'app/models/pay/subscription.rb', line 73 def canceled? ends_at? end |
#cancelled? ⇒ Boolean
77 78 79 |
# File 'app/models/pay/subscription.rb', line 77 def cancelled? canceled? end |
#has_incomplete_payment? ⇒ Boolean
93 94 95 |
# File 'app/models/pay/subscription.rb', line 93 def has_incomplete_payment? past_due? || incomplete? end |
#incomplete? ⇒ Boolean
89 90 91 |
# File 'app/models/pay/subscription.rb', line 89 def incomplete? status == "incomplete" end |
#latest_payment ⇒ Object
117 118 119 120 |
# File 'app/models/pay/subscription.rb', line 117 def latest_payment return unless stripe? processor_subscription(expand: ["latest_invoice.payment_intent"]).latest_invoice.payment_intent end |
#no_prorate ⇒ Object
61 62 63 |
# File 'app/models/pay/subscription.rb', line 61 def no_prorate self.prorate = false end |
#on_trial? ⇒ Boolean
69 70 71 |
# File 'app/models/pay/subscription.rb', line 69 def on_trial? trial_ends_at? && Time.zone.now < trial_ends_at end |
#past_due? ⇒ Boolean
85 86 87 |
# File 'app/models/pay/subscription.rb', line 85 def past_due? status == "past_due" end |
#payment_processor ⇒ Object
46 47 48 |
# File 'app/models/pay/subscription.rb', line 46 def payment_processor @payment_processor ||= payment_processor_for(processor).new(self) end |
#payment_processor_for(name) ⇒ Object
50 51 52 |
# File 'app/models/pay/subscription.rb', line 50 def payment_processor_for(name) "Pay::#{name.to_s.classify}::Subscription".constantize end |
#processor_subscription(options = {}) ⇒ Object
113 114 115 |
# File 'app/models/pay/subscription.rb', line 113 def processor_subscription( = {}) owner.processor_subscription(processor_id, ) end |
#resume ⇒ Object
97 98 99 100 101 |
# File 'app/models/pay/subscription.rb', line 97 def resume payment_processor.resume update(ends_at: nil, status: "active") self end |
#skip_trial ⇒ Object
65 66 67 |
# File 'app/models/pay/subscription.rb', line 65 def skip_trial self.trial_ends_at = nil end |
#swap(plan) ⇒ Object
103 104 105 106 |
# File 'app/models/pay/subscription.rb', line 103 def swap(plan) payment_processor.swap(plan) update(processor_plan: plan, ends_at: nil) end |
#swap_and_invoice(plan) ⇒ Object
108 109 110 111 |
# File 'app/models/pay/subscription.rb', line 108 def swap_and_invoice(plan) swap(plan) owner.invoice!(subscription_id: processor_id) end |