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
45
46
|
# File 'lib/pay/paddle/webhooks/subscription_created.rb', line 5
def call(event)
subscription = Pay.subscription_model.find_by(processor: :paddle, processor_id: event["subscription_id"])
if subscription.nil?
owner = Pay.find_billable(processor: :paddle, processor_id: event["user_id"])
if owner.nil?
owner = Pay::Paddle.owner_from_passthrough(event["passthrough"])
owner&.update!(processor: "paddle", processor_id: event["user_id"])
end
if owner.nil?
Rails.logger.error("[Pay] Unable to find Pay::Billable with owner: '#{event["passthrough"]}'. Searched these models: #{Pay.billable_models.join(", ")}")
return
end
subscription = Pay.subscription_model.new(owner: owner, name: Pay.default_product_name, processor: "paddle", processor_id: event["subscription_id"], status: :active)
end
subscription.quantity = event["quantity"]
subscription.processor_plan = event["subscription_plan_id"]
subscription.paddle_update_url = event["update_url"]
subscription.paddle_cancel_url = event["cancel_url"]
subscription.trial_ends_at = Time.zone.parse(event["next_bill_date"]) if event["status"] == "trialing"
subscription.ends_at = if ["paused", "deleted"].include?(event["status"]) && subscription.on_trial?
subscription.trial_ends_at
elsif ["paused", "deleted"].include?(event["status"])
Time.zone.parse(event["next_bill_date"])
end
subscription.save!
end
|