Class: Pay::Paddle::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/paddle/subscription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_subscriptionObject (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

#cancelObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pay/paddle/subscription.rb', line 27

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



39
40
41
42
43
44
# File 'lib/pay/paddle/subscription.rb', line 39

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/pay/paddle/subscription.rb', line 46

def on_grace_period?
  canceled? && Time.zone.now < ends_at || paused? && Time.zone.now < paddle_paused_from
end

#pauseObject



54
55
56
57
58
59
60
# File 'lib/pay/paddle/subscription.rb', line 54

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/pay/paddle/subscription.rb', line 50

def paused?
  paddle_paused_from.present?
end

#resumeObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pay/paddle/subscription.rb', line 62

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

#swap(plan) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/pay/paddle/subscription.rb', line 74

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