Class: Pay::Webhook

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

Instance Method Summary collapse

Instance Method Details

#process!Object



7
8
9
10
11
12
# File 'app/models/pay/webhook.rb', line 7

def process!
  Pay::Webhooks.instrument type: "#{processor}.#{event_type}", event: rehydrated_event

  # Remove after successfully processing
  destroy
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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/pay/webhook.rb', line 16

def rehydrated_event
  case processor
  when "braintree"
    Pay.braintree_gateway.webhook_notification.parse(event["bt_signature"], event["bt_payload"])
  when "paddle_billing"
    to_recursive_ostruct(event["data"])
  when "paddle_classic"
    to_recursive_ostruct(event)
  when "stripe"
    ::Stripe::Event.construct_from(event)
  else
    event
  end
end

#to_recursive_ostruct(obj) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/models/pay/webhook.rb', line 31

def to_recursive_ostruct(obj)
  if obj.is_a?(Hash)
    OpenStruct.new(obj.map { |key, val| [key, to_recursive_ostruct(val)] }.to_h)
  elsif obj.is_a?(Array)
    obj.map { |o| to_recursive_ostruct(o) }
  else # Assumed to be a primitive value
    obj
  end
end