Class: Pay::Lago::Subscription

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

Constant Summary collapse

STATUS_MAP =
{
  "pending" => "active",
  "terminated" => "canceled"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_subscriptionObject (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(**options)
  customer_id = subscription.external_customer_id
  response = Lago.client.subscriptions.destroy(processor_id, options: 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!(**options)
  cancel(options)
end

#change_quantity(_quantity, **_options) ⇒ Object

Raises:



84
85
86
# File 'lib/pay/lago/subscription.rb', line 84

def change_quantity(_quantity, **_options)
  raise Pay::Lago::Error.new("Lago subscriptions do not implement quantity.")
end

#changing_plan?Boolean

Returns:

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/pay/lago/subscription.rb', line 88

def on_grace_period?
  false
end

#pauseObject

Raises:



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

Returns:

  • (Boolean)


92
93
94
# File 'lib/pay/lago/subscription.rb', line 92

def paused?
  false
end

#resumeObject

Raises:



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_paymentObject

Retries the latest invoice for a Past Due subscription

Raises:



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, **options)
  customer_id = subscription.external_customer_id
  attributes = options.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