Class: Billy::Webhook

Inherits:
ApplicationRecord show all
Defined in:
app/models/billy/webhook.rb

Instance Method Summary collapse

Instance Method Details

#process!Object



15
16
17
18
19
20
21
# File 'app/models/billy/webhook.rb', line 15

def process!
  return false if processed_at
  
  Billy::Webhooks.instrument type: "#{processor}.#{event_type}", event: rehydrated_event

  update(processed_at: Time.current)
end

#rehydrated_eventObject

Events have already been verified by the webhook, so we just store the raw data Then we can rehydrate as webhook objects for each payment processor



25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/billy/webhook.rb', line 25

def rehydrated_event
  to_recursive_ostruct(event)
  # case kind
  # when "paddle"
  #   to_recursive_ostruct(event)
  # when "lemon_squeezy"
  #   to_recursive_ostruct(event)
  # else
  #   event
  # end
end

#to_recursive_ostruct(hash) ⇒ Object



37
38
39
40
41
42
# File 'app/models/billy/webhook.rb', line 37

def to_recursive_ostruct(hash)
  result = hash.each_with_object({}) do |(key, val), memo|
    memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
  end
  OpenStruct.new(result)
end