Class: Pay::PaddleBilling::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
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 9
def self.sync(subscription_id, object: nil, name: Pay.default_product_name)
object ||= ::Paddle::Subscription.retrieve(id: subscription_id)
pay_customer = Pay::Customer.find_by(processor: :paddle_billing, processor_id: object.customer_id)
return unless pay_customer
attributes = {
current_period_end: object.current_billing_period&.ends_at,
current_period_start: object.current_billing_period&.starts_at,
ends_at: (object.canceled_at ? Time.parse(object.canceled_at) : nil),
metadata: object.custom_data,
paddle_cancel_url: object.management_urls&.cancel,
paddle_update_url: object.management_urls&.update_payment_method,
pause_starts_at: (object.paused_at ? Time.parse(object.paused_at) : nil),
status: object.status
}
if object.items&.first
item = object.items.first
attributes[:processor_plan] = item.price.id
attributes[:quantity] = item.quantity
end
case attributes[:status]
when "canceled"
Pay::PaymentMethod.where(customer_id: object.customer_id).destroy_all
when "trialing"
attributes[:trial_ends_at] = Time.parse(object.next_billed_at) if object.next_billed_at
when "paused"
attributes[:pause_starts_at] = Time.parse(object.paused_at) if object.paused_at
when "active", "past_due"
attributes[:trial_ends_at] = nil
attributes[:pause_starts_at] = nil
attributes[:ends_at] = nil
end
case object.scheduled_change&.action
when "cancel"
attributes[:ends_at] = Time.parse(object.scheduled_change.effective_at)
when "pause"
attributes[:pause_starts_at] = Time.parse(object.scheduled_change.effective_at)
when "resume"
attributes[:pause_resumes_at] = Time.parse(object.scheduled_change.effective_at)
end
if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: 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: subscription_id))
end
end
|
.sync_from_transaction(transaction_id) ⇒ Object
4
5
6
7
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 4
def self.sync_from_transaction(transaction_id)
transaction = ::Paddle::Transaction.retrieve(id: transaction_id)
sync(transaction.subscription_id) if transaction.subscription_id
end
|
Instance Method Details
#api_record(**options) ⇒ Object
67
68
69
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 67
def api_record(**options)
@api_record ||= ::Paddle::Subscription.retrieve(id: processor_id, **options)
end
|
#cancel(**options) ⇒ Object
If a subscription is paused, cancel immediately Otherwise, cancel at period end
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 78
def cancel(**options)
return if canceled?
response = ::Paddle::Subscription.cancel(
id: processor_id,
effective_from: options.fetch(:effective_from, (paused? ? "immediately" : "next_billing_period"))
)
update(
status: response.status,
ends_at: response.scheduled_change&.effective_at || Time.current
)
rescue ::Paddle::Error => e
raise Pay::PaddleBilling::Error, e
end
|
#cancel_now!(**options) ⇒ Object
93
94
95
96
97
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 93
def cancel_now!(**options)
cancel(options.merge(effective_from: "immediately"))
rescue ::Paddle::Error => e
raise Pay::PaddleBilling::Error, e
end
|
#change_quantity(quantity, **options) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 99
def change_quantity(quantity, **options)
items = [{
price_id: processor_plan,
quantity: quantity
}]
::Paddle::Subscription.update(id: processor_id, items: items, proration_billing_mode: "prorated_immediately")
update(quantity: quantity)
rescue ::Paddle::Error => e
raise Pay::PaddleBilling::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
113
114
115
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 113
def on_grace_period?
(canceled? && Time.current < ends_at) || (paused? && pause_starts_at? && Time.current < pause_starts_at)
end
|
#pause ⇒ Object
121
122
123
124
125
126
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 121
def pause
response = ::Paddle::Subscription.pause(id: processor_id)
update!(status: :paused, pause_starts_at: response.scheduled_change.effective_at)
rescue ::Paddle::Error => e
raise Pay::PaddleBilling::Error, e
end
|
#paused? ⇒ Boolean
117
118
119
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 117
def paused?
status == "paused"
end
|
#payment_method_transaction ⇒ Object
Get a transaction to update payment method
72
73
74
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 72
def payment_method_transaction
::Paddle::Subscription.get_transaction(id: processor_id)
end
|
#resumable? ⇒ Boolean
128
129
130
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 128
def resumable?
paused?
end
|
#resume ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 132
def resume
unless resumable?
raise StandardError, "You can only resume paused subscriptions."
end
if paused? && pause_starts_at? && Time.current < pause_starts_at
::Paddle::Subscription.update(id: processor_id, scheduled_change: nil)
else
::Paddle::Subscription.resume(id: processor_id, effective_from: "immediately")
end
update(ends_at: nil, status: :active, pause_starts_at: nil)
rescue ::Paddle::Error => e
raise Pay::PaddleBilling::Error, e
end
|
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription
161
162
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 161
def retry_failed_payment
end
|
#swap(plan, **options) ⇒ Object
150
151
152
153
154
155
156
157
158
|
# File 'app/models/pay/paddle_billing/subscription.rb', line 150
def swap(plan, **options)
items = [{
price_id: plan,
quantity: quantity || 1
}]
::Paddle::Subscription.update(id: processor_id, items: items, proration_billing_mode: "prorated_immediately")
update(processor_plan: plan, ends_at: nil, status: :active)
end
|