lib/pay/paddle/subscription.rb in pay-2.5.0 vs lib/pay/paddle/subscription.rb in pay-2.6.0
- old
+ new
@@ -1,63 +1,74 @@
module Pay
module Paddle
- module Subscription
- extend ActiveSupport::Concern
+ class Subscription
+ attr_reader :pay_subscription
- included do
- store_accessor :data, :paddle_update_url
- store_accessor :data, :paddle_cancel_url
- store_accessor :data, :paddle_paused_from
+ delegate :canceled?,
+ :ends_at,
+ :on_trial?,
+ :owner,
+ :paddle_paused_from,
+ :processor_id,
+ :processor_plan,
+ :processor_subscription,
+ :prorate,
+ :quantity,
+ :quantity?,
+ to: :pay_subscription
+
+ def initialize(pay_subscription)
+ @pay_subscription = pay_subscription
end
- def paddle_cancel
+ def cancel
subscription = processor_subscription
PaddlePay::Subscription::User.cancel(processor_id)
if on_trial?
- update(status: :canceled, ends_at: trial_ends_at)
+ pay_subscription.update(status: :canceled, ends_at: trial_ends_at)
else
- update(status: :canceled, ends_at: Time.zone.parse(subscription.next_payment[:date]))
+ 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
- def paddle_cancel_now!
+ def cancel_now!
PaddlePay::Subscription::User.cancel(processor_id)
- update(status: :canceled, ends_at: Time.zone.now)
+ pay_subscription.update(status: :canceled, ends_at: Time.zone.now)
rescue ::PaddlePay::PaddlePayError => e
raise Pay::Paddle::Error, e
end
- def paddle_on_grace_period?
+ def on_grace_period?
canceled? && Time.zone.now < ends_at || paused? && Time.zone.now < paddle_paused_from
end
- def paddle_paused?
+ def paused?
paddle_paused_from.present?
end
- def paddle_pause
+ def pause
attributes = {pause: true}
response = PaddlePay::Subscription::User.update(processor_id, attributes)
- update(paddle_paused_from: Time.zone.parse(response[:next_payment][:date]))
+ pay_subscription.update(paddle_paused_from: Time.zone.parse(response[:next_payment][:date]))
rescue ::PaddlePay::PaddlePayError => e
raise Pay::Paddle::Error, e
end
- def paddle_resume
+ def resume
unless paused?
raise StandardError, "You can only resume paused subscriptions."
end
attributes = {pause: false}
PaddlePay::Subscription::User.update(processor_id, attributes)
- update(status: :active, paddle_paused_from: nil)
+ pay_subscription.update(status: :active, paddle_paused_from: nil)
rescue ::PaddlePay::PaddlePayError => e
raise Pay::Paddle::Error, e
end
- def paddle_swap(plan)
+ 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