Class: Pay::PaddleClassic::Subscription
- Inherits:
-
Object
- Object
- Pay::PaddleClassic::Subscription
- Defined in:
- lib/pay/paddle_classic/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
Paddle subscriptions are canceled immediately, however we still want to give the user access to the end of the period they paid for.
- #cancel_now!(**options) ⇒ Object
- #change_quantity(quantity, **options) ⇒ Object
-
#initialize(pay_subscription) ⇒ Subscription
constructor
A new instance of Subscription.
-
#on_grace_period? ⇒ Boolean
A subscription could be set to cancel or pause in the future It is considered on grace period until the cancel or pause time begins.
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #resumable? ⇒ Boolean
- #resume ⇒ Object
-
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription.
- #subscription(**options) ⇒ Object
- #swap(plan, **options) ⇒ Object
Constructor Details
#initialize(pay_subscription) ⇒ Subscription
Returns a new instance of Subscription.
71 72 73 |
# File 'lib/pay/paddle_classic/subscription.rb', line 71 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_classic/subscription.rb', line 4 def pay_subscription @pay_subscription end |
Class Method Details
.sync(subscription_id, object: nil, name: Pay.default_product_name) ⇒ Object
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 65 66 67 68 69 |
# File 'lib/pay/paddle_classic/subscription.rb', line 25 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 ||= PaddleClassic.client.users.list(subscription_id: subscription_id).data.try(:first) pay_customer = Pay::Customer.find_by(processor: :paddle_classic, 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::PaddleClassic.owner_from_passthrough(object.passthrough) pay_customer = owner&.set_payment_processor(:paddle_classic, 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 || 1, status: object.state || object.status } case attributes[:status] when "trialing" attributes[:trial_ends_at] = Time.zone.parse(object.next_bill_date) attributes[:ends_at] = nil when "active", "past_due" attributes[:trial_ends_at] = nil attributes[:ends_at] = nil when "paused", "deleted" # If paused or delete while on trial, set ends_at to match 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
Paddle subscriptions are canceled immediately, however we still want to give the user access to the end of the period they paid for
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pay/paddle_classic/subscription.rb', line 82 def cancel(**) return if canceled? ends_at = if on_trial? trial_ends_at elsif paused? pause_starts_at else Time.parse(processor_subscription.next_payment.date) end PaddleClassic.client.users.cancel(subscription_id: processor_id) pay_subscription.update( status: (ends_at.future? ? :active : :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 ::Paddle::Error => e raise Pay::PaddleClassic::Error, e end |
#cancel_now!(**options) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/pay/paddle_classic/subscription.rb', line 105 def cancel_now!(**) return if canceled? PaddleClassic.client.users.cancel(subscription_id: 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 ::Paddle::Error => e raise Pay::PaddleClassic::Error, e end |
#change_quantity(quantity, **options) ⇒ Object
117 118 119 |
# File 'lib/pay/paddle_classic/subscription.rb', line 117 def change_quantity(quantity, **) raise NotImplementedError, "Paddle does not support setting quantity on subscriptions" end |
#on_grace_period? ⇒ Boolean
A subscription could be set to cancel or pause in the future It is considered on grace period until the cancel or pause time begins
123 124 125 |
# File 'lib/pay/paddle_classic/subscription.rb', line 123 def on_grace_period? (canceled? && Time.current < ends_at) || (paused? && pause_starts_at? && Time.current < pause_starts_at) end |
#pause ⇒ Object
131 132 133 134 135 136 |
# File 'lib/pay/paddle_classic/subscription.rb', line 131 def pause response = PaddleClassic.client.users.pause(subscription_id: processor_id) pay_subscription.update(status: :paused, pause_starts_at: Time.zone.parse(response.dig(:next_payment, :date))) rescue ::Paddle::Error => e raise Pay::PaddleClassic::Error, e end |
#paused? ⇒ Boolean
127 128 129 |
# File 'lib/pay/paddle_classic/subscription.rb', line 127 def paused? pay_subscription.status == "paused" end |
#resumable? ⇒ Boolean
138 139 140 |
# File 'lib/pay/paddle_classic/subscription.rb', line 138 def resumable? paused? end |
#resume ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/pay/paddle_classic/subscription.rb', line 142 def resume unless resumable? raise StandardError, "You can only resume paused subscriptions." end PaddleClassic.client.users.unpause(subscription_id: processor_id) pay_subscription.update(status: :active, pause_starts_at: nil) rescue ::Paddle::Error => e raise Pay::PaddleClassic::Error, e end |
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription
166 167 |
# File 'lib/pay/paddle_classic/subscription.rb', line 166 def retry_failed_payment end |
#subscription(**options) ⇒ Object
75 76 77 78 79 |
# File 'lib/pay/paddle_classic/subscription.rb', line 75 def subscription(**) PaddleClassic.client.users.list(subscription_id: processor_id).data.try(:first) rescue ::Paddle::Error => e raise Pay::PaddleClassic::Error, e end |
#swap(plan, **options) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/pay/paddle_classic/subscription.rb', line 153 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? PaddleClassic.client.users.update(subscription_id: processor_id, **attributes) pay_subscription.update(processor_plan: plan, ends_at: nil, status: :active) rescue ::Paddle::Error => e raise Pay::PaddleClassic::Error, e end |