Class: Pay::Paddle::Subscription
- Inherits:
-
Object
- Object
- Pay::Paddle::Subscription
- Defined in:
- lib/pay/paddle/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
- #subscription(**options) ⇒ Object
- #swap(plan) ⇒ Object
Constructor Details
#initialize(pay_subscription) ⇒ Subscription
Returns a new instance of Subscription.
23 24 25 |
# File 'lib/pay/paddle/subscription.rb', line 23 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/paddle/subscription.rb', line 4 def pay_subscription @pay_subscription end |
Instance Method Details
#cancel ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pay/paddle/subscription.rb', line 34 def cancel subscription = processor_subscription PaddlePay::Subscription::User.cancel(processor_id) if on_trial? pay_subscription.update(status: :canceled, ends_at: trial_ends_at) else pay_subscription.update(status: :canceled, ends_at: Time.zone.parse(subscription.next_payment[:date])) end rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#cancel_now! ⇒ Object
46 47 48 49 50 51 |
# File 'lib/pay/paddle/subscription.rb', line 46 def cancel_now! PaddlePay::Subscription::User.cancel(processor_id) pay_subscription.update(status: :canceled, ends_at: Time.zone.now) rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#on_grace_period? ⇒ Boolean
53 54 55 |
# File 'lib/pay/paddle/subscription.rb', line 53 def on_grace_period? canceled? && Time.zone.now < ends_at || paused? && Time.zone.now < paddle_paused_from end |
#pause ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/pay/paddle/subscription.rb', line 61 def pause attributes = {pause: true} response = PaddlePay::Subscription::User.update(processor_id, attributes) pay_subscription.update(paddle_paused_from: Time.zone.parse(response[:next_payment][:date])) rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#paused? ⇒ Boolean
57 58 59 |
# File 'lib/pay/paddle/subscription.rb', line 57 def paused? paddle_paused_from.present? end |
#resume ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pay/paddle/subscription.rb', line 69 def resume unless paused? raise StandardError, "You can only resume paused subscriptions." end attributes = {pause: false} PaddlePay::Subscription::User.update(processor_id, attributes) pay_subscription.update(status: :active, paddle_paused_from: nil) rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#subscription(**options) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/pay/paddle/subscription.rb', line 27 def subscription(**) hash = PaddlePay::Subscription::User.list({subscription_id: processor_id}, ).try(:first) OpenStruct.new(hash) rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#swap(plan) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/pay/paddle/subscription.rb', line 81 def swap(plan) attributes = {plan_id: plan, prorate: prorate} attributes[:quantity] = quantity if quantity? PaddlePay::Subscription::User.update(processor_id, attributes) rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |