Class: Pay::LemonSqueezy::Subscription
Constant Summary
Constants inherited
from Subscription
Subscription::STATUSES
Class Method Summary
collapse
Instance Method Summary
collapse
#active?, #canceled?, #cancelled?, #ended?, find_by_processor_and_id, #generic_trial?, #has_incomplete_payment?, #has_trial?, #incomplete?, #no_prorate, #on_trial?, #past_due?, #skip_trial, #swap_and_invoice, #sync!, #trial_ended?, #unpaid?
Class Method Details
.sync(subscription_id, object: nil, name: Pay.default_product_name) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 4
def self.sync(subscription_id, object: nil, name: Pay.default_product_name)
object ||= ::LemonSqueezy::Subscription.retrieve(id: subscription_id)
pay_customer = Pay::Customer.find_by(processor: :lemon_squeezy, processor_id: object.customer_id)
return unless pay_customer
attributes = {
current_period_end: object.renews_at,
ends_at: (object.ends_at ? Time.parse(object.ends_at) : nil),
pause_starts_at: (object.pause&.resumes_at ? Time.parse(object.pause.resumes_at) : nil),
status: object.status,
processor_plan: object.first_subscription_item.price_id,
quantity: object.first_subscription_item.quantity,
created_at: (object.created_at ? Time.parse(object.created_at) : nil),
updated_at: (object.updated_at ? Time.parse(object.updated_at) : nil)
}
case attributes[:status]
when "cancelled"
Pay::PaymentMethod.where(customer_id: object.customer_id).destroy_all
when "on_trial"
attributes[:trial_ends_at] = Time.parse(object.trial_ends_at)
when "paused"
when "active", "past_due"
attributes[:trial_ends_at] = nil
attributes[:pause_starts_at] = nil
attributes[:ends_at] = nil
end
if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.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.id))
end
end
|
Instance Method Details
#api_record(**options) ⇒ Object
46
47
48
49
50
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 46
def api_record(**options)
@api_record ||= ::LemonSqueezy::Subscription.retrieve(id: processor_id)
rescue ::LemonSqueezy::Error => e
raise Pay::LemonSqueezy::Error, e
end
|
#cancel(**options) ⇒ Object
60
61
62
63
64
65
66
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 60
def cancel(**options)
return if canceled?
response = ::LemonSqueezy::Subscription.cancel(id: processor_id)
update(status: response.status, ends_at: response.ends_at)
rescue ::LemonSqueezy::Error => e
raise Pay::LemonSqueezy::Error, e
end
|
#cancel_now!(**options) ⇒ Object
68
69
70
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 68
def cancel_now!(**options)
raise Pay::Error, "Lemon Squeezy does not support cancelling immediately through the API."
end
|
#change_quantity(quantity, **options) ⇒ Object
72
73
74
75
76
77
78
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 72
def change_quantity(quantity, **options)
subscription_item = api_record.first_subscription_item
::LemonSqueezy::SubscriptionItem.update(id: subscription_item.id, quantity: quantity)
update(quantity: quantity)
rescue ::LemonSqueezy::Error => e
raise Pay::LemonSqueezy::Error, e
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
82
83
84
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 82
def on_grace_period?
(canceled? && Time.current < ends_at) || (paused? && pause_starts_at? && Time.current < pause_starts_at)
end
|
#pause(**options) ⇒ Object
90
91
92
93
94
95
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 90
def pause(**options)
response = ::LemonSqueezy::Subscription.pause(id: processor_id, **options)
update!(status: :paused, pause_starts_at: response.pause&.resumes_at)
rescue ::LemonSqueezy::Error => e
raise Pay::LemonSqueezy::Error, e
end
|
#paused? ⇒ Boolean
86
87
88
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 86
def paused?
status == "paused"
end
|
#portal_url ⇒ Object
52
53
54
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 52
def portal_url
api_record.urls.customer_portal
end
|
#resumable? ⇒ Boolean
97
98
99
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 97
def resumable?
paused? || canceled?
end
|
#resume ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 101
def resume
unless resumable?
raise Error, "You can only resume paused or cancelled subscriptions"
end
if paused? && pause_starts_at? && Time.current < pause_starts_at
::LemonSqueezy::Subscription.unpause(id: processor_id)
else
::LemonSqueezy::Subscription.uncancel(id: processor_id)
end
update(ends_at: nil, status: :active, pause_starts_at: nil)
rescue ::LemonSqueezy::Error => e
raise Pay::LemonSqueezy::Error, e
end
|
#swap(plan, **options) ⇒ Object
Lemon Squeezy requires both the Product ID and Variant ID. The Variant ID will be saved as the processor_plan
119
120
121
122
123
124
125
126
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 119
def swap(plan, **options)
raise Error, "A plan_id is required to swap a subscription" unless plan
raise Error, "A variant_id is required to swap a subscription" unless options[:variant_id]
::LemonSqueezy::Subscription.change_plan id: processor_id, plan_id: plan, variant_id: options[:variant_id]
update(processor_plan: options[:variant_id], ends_at: nil, status: :active)
end
|
#update_url ⇒ Object
56
57
58
|
# File 'app/models/pay/lemon_squeezy/subscription.rb', line 56
def update_url
api_record.urls.update_payment_method
end
|