Class: Subscription
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Subscription
- Defined in:
- lib/subscription.rb
Instance Method Summary collapse
- #active? ⇒ Boolean
- #cancel ⇒ Object
- #cancel_now ⇒ Object
- #cancelled? ⇒ Boolean
- #decrement_quantity(count = 1) ⇒ Object
- #increment_and_invoice(count = 1) ⇒ Object
- #increment_quantity(count = 1) ⇒ Object
- #mark_as_cancelled ⇒ Object
- #on_grace_period? ⇒ Boolean
- #on_trial? ⇒ Boolean
- #stripe_subscription ⇒ Object
- #swap(plan, *args) ⇒ Object
- #update_quantity(quantity) ⇒ Object
- #valid ⇒ Object
Instance Method Details
#active? ⇒ Boolean
8 9 10 |
# File 'lib/subscription.rb', line 8 def active? ends_at.nil? || on_grace_period? end |
#cancel ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/subscription.rb', line 89 def cancel subscription = stripe_subscription subscription.delete(at_period_end: true) if on_trial? self.ends_at = trial_ends_at else self.ends_at = current_period_end.to_time end self.save self end |
#cancel_now ⇒ Object
103 104 105 106 107 108 |
# File 'lib/subscription.rb', line 103 def cancel_now subscription = stripe_subscription subscription.delete mark_as_cancelled self end |
#cancelled? ⇒ Boolean
12 13 14 |
# File 'lib/subscription.rb', line 12 def cancelled? !ends_at.nil? end |
#decrement_quantity(count = 1) ⇒ Object
43 44 45 46 47 |
# File 'lib/subscription.rb', line 43 def decrement_quantity(count = 1) quantity = [1, (self.quantity - count)].max update_quantity(quantity) self end |
#increment_and_invoice(count = 1) ⇒ Object
37 38 39 40 41 |
# File 'lib/subscription.rb', line 37 def increment_and_invoice(count = 1) increment_quantity(count) user.invoice self end |
#increment_quantity(count = 1) ⇒ Object
32 33 34 35 |
# File 'lib/subscription.rb', line 32 def increment_quantity(count = 1) update_quantity(quantity + count) self end |
#mark_as_cancelled ⇒ Object
110 111 112 113 |
# File 'lib/subscription.rb', line 110 def mark_as_cancelled self.ends_at = DateTime.now self.save end |
#on_grace_period? ⇒ Boolean
24 25 26 27 28 29 30 |
# File 'lib/subscription.rb', line 24 def on_grace_period? if ends_at.nil? return false else DateTime.now < ends_at.to_datetime end end |
#on_trial? ⇒ Boolean
16 17 18 19 20 21 22 |
# File 'lib/subscription.rb', line 16 def on_trial? if trial_ends_at.nil? false else DateTime.now < trial_ends_at.to_datetime end end |
#stripe_subscription ⇒ Object
115 116 117 |
# File 'lib/subscription.rb', line 115 def stripe_subscription user.as_stripe_customer.subscriptions.retrieve(stripe_id) end |
#swap(plan, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/subscription.rb', line 58 def swap(plan, *args) subscription = stripe_subscription subscription.plan = plan subscription.prorate = false = args[0] if subscription.prorate = [:prorate] || false anchor = [:billing_cycle_anchor] || nil subscription.billing_cycle_anchor = anchor if anchor end if on_trial? subscription.trial_end = trial_ends_at.to_time else subscription.trial_end = 'now' end if quantity subscription.quantity = quantity end subscription.save user.invoice self.stripe_plan = plan self.ends_at = nil self.save self end |
#update_quantity(quantity) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/subscription.rb', line 49 def update_quantity(quantity) subscription = stripe_subscription subscription.quantity = quantity subscription.save self.quantity = quantity self.save self end |
#valid ⇒ Object
4 5 6 |
# File 'lib/subscription.rb', line 4 def valid active? || on_trial? || on_grace_period? end |