Class: Pay::Stripe::Subscription
- Inherits:
-
Object
- Object
- Pay::Stripe::Subscription
- Defined in:
- lib/pay/stripe/subscription.rb
Instance Attribute Summary collapse
-
#pay_subscription ⇒ Object
readonly
Returns the value of attribute pay_subscription.
Instance Method Summary collapse
- #cancel ⇒ Object
- #cancel_now! ⇒ Object
-
#initialize(pay_subscription) ⇒ Subscription
constructor
A new instance of Subscription.
- #on_grace_period? ⇒ Boolean
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #resume ⇒ Object
- #swap(plan) ⇒ Object
Constructor Details
#initialize(pay_subscription) ⇒ Subscription
Returns a new instance of Subscription.
22 23 24 |
# File 'lib/pay/stripe/subscription.rb', line 22 def initialize(pay_subscription) @pay_subscription = pay_subscription end |
Instance Attribute Details
#pay_subscription ⇒ Object (readonly)
Returns the value of attribute pay_subscription.
4 5 6 |
# File 'lib/pay/stripe/subscription.rb', line 4 def pay_subscription @pay_subscription end |
Instance Method Details
#cancel ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pay/stripe/subscription.rb', line 26 def cancel subscription = processor_subscription subscription.cancel_at_period_end = true subscription.save new_ends_at = on_trial? ? trial_ends_at : Time.at(subscription.current_period_end) pay_subscription.update(ends_at: new_ends_at) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#cancel_now! ⇒ Object
37 38 39 40 41 42 |
# File 'lib/pay/stripe/subscription.rb', line 37 def cancel_now! processor_subscription.delete pay_subscription.update(ends_at: Time.zone.now, status: :canceled) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#on_grace_period? ⇒ Boolean
44 45 46 |
# File 'lib/pay/stripe/subscription.rb', line 44 def on_grace_period? canceled? && Time.zone.now < ends_at end |
#pause ⇒ Object
52 53 54 |
# File 'lib/pay/stripe/subscription.rb', line 52 def pause raise NotImplementedError, "Stripe does not support pausing subscriptions" end |
#paused? ⇒ Boolean
48 49 50 |
# File 'lib/pay/stripe/subscription.rb', line 48 def paused? false end |
#resume ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pay/stripe/subscription.rb', line 56 def resume unless on_grace_period? raise StandardError, "You can only resume subscriptions within their grace period." end subscription = processor_subscription subscription.plan = processor_plan subscription.trial_end = on_trial? ? trial_ends_at.to_i : "now" subscription.cancel_at_period_end = false subscription.save rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#swap(plan) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pay/stripe/subscription.rb', line 70 def swap(plan) subscription = processor_subscription subscription.cancel_at_period_end = false subscription.plan = plan subscription.proration_behavior = (prorate ? "create_prorations" : "none") subscription.trial_end = on_trial? ? trial_ends_at.to_i : "now" subscription.quantity = quantity if quantity? subscription.save rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |