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.
Class Method Summary collapse
Instance Method Summary collapse
- #cancel(**options) ⇒ Object
- #cancel_now!(**options) ⇒ 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.
66 67 68 |
# File 'lib/pay/paddle/subscription.rb', line 66 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 |
Class Method Details
.sync(subscription_id, object: nil, name: Pay.default_product_name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pay/paddle/subscription.rb', line 23 def self.sync(subscription_id, object: nil, name: Pay.default_product_name) # Passthrough is not return from this API, so we can't use that object ||= OpenStruct.new PaddlePay::Subscription::User.list({subscription_id: subscription_id}).try(:first) pay_customer = Pay::Customer.find_by(processor: :paddle, processor_id: object.user_id) # If passthrough exists (only on webhooks) we can use it to create the Pay::Customer if pay_customer.nil? && object.passthrough owner = Pay::Paddle.owner_from_passthrough(object.passthrough) pay_customer = owner&.set_payment_processor(:paddle, processor_id: object.user_id) end return unless pay_customer attributes = { paddle_cancel_url: object.cancel_url, paddle_update_url: object.update_url, processor_plan: object.plan_id || object.subscription_plan_id, quantity: object.quantity, status: object.state || object.status } # If paused or delete while on trial, set ends_at to match case attributes[:status] when "trialing" attributes[:trial_ends_at] = Time.zone.parse(object.next_bill_date) attributes[:ends_at] = nil when "paused", "deleted" attributes[:trial_ends_at] = nil attributes[:ends_at] = Time.zone.parse(object.next_bill_date) end # Update or create the subscription if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.subscription_id)) pay_subscription.with_lock do pay_subscription.update!(attributes) end pay_subscription else pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: object.subscription_id)) end end |
Instance Method Details
#cancel(**options) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pay/paddle/subscription.rb', line 77 def cancel(**) ends_at = if on_trial? trial_ends_at elsif paused? paddle_paused_from else processor_subscription.next_payment&.fetch(:date) || Time.current end PaddlePay::Subscription::User.cancel(processor_id) pay_subscription.update(status: :canceled, ends_at: ends_at) # Remove payment methods since customer cannot be reused after cancelling Pay::PaymentMethod.where(customer_id: pay_subscription.customer_id).destroy_all rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#cancel_now!(**options) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/pay/paddle/subscription.rb', line 95 def cancel_now!(**) PaddlePay::Subscription::User.cancel(processor_id) pay_subscription.update(status: :canceled, ends_at: Time.current) # Remove payment methods since customer cannot be reused after cancelling Pay::PaymentMethod.where(customer_id: pay_subscription.customer_id).destroy_all rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#on_grace_period? ⇒ Boolean
105 106 107 |
# File 'lib/pay/paddle/subscription.rb', line 105 def on_grace_period? canceled? && Time.current < ends_at || paused? && Time.current < paddle_paused_from end |
#pause ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/pay/paddle/subscription.rb', line 113 def pause attributes = {pause: true} response = PaddlePay::Subscription::User.update(processor_id, attributes) pay_subscription.update(paddle_paused_from: Time.zone.parse(response.dig(:next_payment, :date))) rescue ::PaddlePay::PaddlePayError => e raise Pay::Paddle::Error, e end |
#paused? ⇒ Boolean
109 110 111 |
# File 'lib/pay/paddle/subscription.rb', line 109 def paused? paddle_paused_from.present? end |
#resume ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/pay/paddle/subscription.rb', line 121 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
70 71 72 73 74 75 |
# File 'lib/pay/paddle/subscription.rb', line 70 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
133 134 135 136 137 138 139 140 141 |
# File 'lib/pay/paddle/subscription.rb', line 133 def swap(plan) raise ArgumentError, "plan must be a string" unless plan.is_a?(String) 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 |