Class: Pay::Lago::Subscription
- Inherits:
-
Object
- Object
- Pay::Lago::Subscription
- Defined in:
- lib/pay/lago/subscription.rb
Constant Summary collapse
- STATUS_MAP =
{ "pending" => "active", "terminated" => "canceled" }
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
- #change_quantity(_quantity, **_options) ⇒ Object
- #changing_plan? ⇒ Boolean
-
#initialize(pay_subscription) ⇒ Subscription
constructor
A new instance of Subscription.
- #on_grace_period? ⇒ Boolean
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #resume ⇒ Object
-
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription.
- #subscription(reload: false) ⇒ Object
- #swap(plan, **options) ⇒ Object
Constructor Details
#initialize(pay_subscription) ⇒ Subscription
Returns a new instance of Subscription.
63 64 65 |
# File 'lib/pay/lago/subscription.rb', line 63 def initialize(pay_subscription) @pay_subscription = pay_subscription end |
Instance Attribute Details
#pay_subscription ⇒ Object (readonly)
Returns the value of attribute pay_subscription.
8 9 10 |
# File 'lib/pay/lago/subscription.rb', line 8 def pay_subscription @pay_subscription end |
Class Method Details
.sync(customer_id, subscription_id, object: nil, try: 0, retries: 1) ⇒ 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 |
# File 'lib/pay/lago/subscription.rb', line 25 def self.sync(customer_id, subscription_id, object: nil, try: 0, retries: 1) object ||= Pay::Lago.client.subscriptions.get(subscription_id) pay_customer = Pay::Customer.find_by(processor: :lago, processor_id: customer_id) if pay_customer.blank? Rails.logger.debug "Pay::Customer #{object.customer} is not in the database while syncing Lago Subscription #{object.lago_id}" return end attrs = { name: object.name.present? ? object.name : Pay.default_plan_name, processor_id: object.external_id, processor_plan: object.plan_code, ends_at: object.cancelled_at || object.terminated_at, quantity: 1, status: STATUS_MAP.fetch(object.status, object.status), data: Lago.openstruct_to_h(object) } # Update or create the charge if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: subscription_id)) pay_subscription.with_lock do pay_subscription.update!(attrs) end pay_subscription else pay_customer.subscriptions.create!(attrs) end rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique try += 1 if try <= retries sleep 0.1 retry else raise end end |
Instance Method Details
#cancel(**options) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/pay/lago/subscription.rb', line 72 def cancel(**) customer_id = subscription.external_customer_id response = Lago.client.subscriptions.destroy(processor_id, options: ) self.class.sync(customer_id, processor_id, object: response) rescue ::Lago::Api::HttpError => e raise Pay::Lago::Error, e end |
#cancel_now!(**options) ⇒ Object
80 81 82 |
# File 'lib/pay/lago/subscription.rb', line 80 def cancel_now!(**) cancel() end |
#change_quantity(_quantity, **_options) ⇒ Object
84 85 86 |
# File 'lib/pay/lago/subscription.rb', line 84 def change_quantity(_quantity, **) raise Pay::Lago::Error.new("Lago subscriptions do not implement quantity.") end |
#changing_plan? ⇒ Boolean
96 97 98 99 |
# File 'lib/pay/lago/subscription.rb', line 96 def changing_plan? return false unless subscription.downgrade_plan_date subscription.downgrade_plan_date > Time.now end |
#on_grace_period? ⇒ Boolean
88 89 90 |
# File 'lib/pay/lago/subscription.rb', line 88 def on_grace_period? false end |
#pause ⇒ Object
101 102 103 |
# File 'lib/pay/lago/subscription.rb', line 101 def pause raise Pay::Lago::Error.new("Lago subscriptions cannot be paused.") end |
#paused? ⇒ Boolean
92 93 94 |
# File 'lib/pay/lago/subscription.rb', line 92 def paused? false end |
#resume ⇒ Object
105 106 107 |
# File 'lib/pay/lago/subscription.rb', line 105 def resume raise Pay::Lago::Error.new("Lago subscriptions cannot be paused.") end |
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription
122 123 124 |
# File 'lib/pay/lago/subscription.rb', line 122 def retry_failed_payment raise Pay::Lago::Error.new("Lago subscriptions cannot be retried.") end |
#subscription(reload: false) ⇒ Object
67 68 69 70 |
# File 'lib/pay/lago/subscription.rb', line 67 def subscription(reload: false) @lago_subscription = nil if reload @lago_subscription ||= Pay::Lago.client.subscriptions.get(processor_id) end |
#swap(plan, **options) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pay/lago/subscription.rb', line 109 def swap(plan, **) customer_id = subscription.external_customer_id attributes = .merge( external_customer_id: customer_id, external_id: processor_id, plan_code: plan.to_s ) new_subscription = Lago.client.subscriptions.create(attributes) self.class.sync(customer_id, processor_id, object: new_subscription) rescue ::Lago::Api::HttpError => e raise Pay::Lago::Error, e end |