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
|
# File 'lib/pay/stripe/webhooks/subscription_created.rb', line 5
def call(event)
object = event.data.object
subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.id)
if subscription.nil?
owner = Pay.find_billable(processor: :stripe, processor_id: object.customer)
if owner.nil?
Rails.logger.error("[Pay] Unable to find Pay::Billable with processor: :stripe and processor_id: '#{object.customer}'. Searched these models: #{Pay.billable_models.join(", ")}")
return
end
subscription = Pay.subscription_model.new(name: Pay.default_product_name, owner: owner, processor: :stripe, processor_id: object.id)
end
subscription.quantity = object.quantity
subscription.processor_plan = object.plan.id
subscription.trial_ends_at = Time.at(object.trial_end) if object.trial_end.present?
subscription.ends_at = if object.cancel_at_period_end && subscription.on_trial?
subscription.trial_ends_at
elsif object.cancel_at_period_end
Time.at(object.current_period_end)
end
subscription.save!
end
|