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
45
46
47
48
49
50
51
|
# File 'app/models/pay/paddle_classic/subscription.rb', line 7
def self.sync(subscription_id, object: nil, name: Pay.default_product_name)
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 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"
attributes[:trial_ends_at] = nil
attributes[:ends_at] = Time.zone.parse(object.next_bill_date)
end
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
|