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
# 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"
    to_recursive_ostruct(event)
  when "stripe"
    ::Stripe::Event.construct_from(event)
  else
    event
  end
end

#to_recursive_ostruct(hash) ⇒ Object


29
30
31
32
33
34
# File 'app/models/pay/webhook.rb', line 29

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