class PayPal::SDK::REST::DataTypes::WebhookEvent

Public Class Methods

all(options = {}) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1614
def all(options = {})
  path = "v1/notifications/webhooks-events"
  WebhookEventList.new(api.get(path, options))
end
find(resource_id) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1608
def find(resource_id)
  raise ArgumentError.new("webhook_event_id required") if resource_id.to_s.strip.empty?
  path = "v1/notifications/webhooks-events/#{resource_id}"
  self.new(api.get(path))
end
get(webhook_event_id) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1604
def get(webhook_event_id)
  WebhookEvent.find(webhook_event_id)
end
get_cert(cert_url) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1532
def get_cert(cert_url)
  data = Net::HTTP.get_response(URI.parse(cert_url))
  cert = OpenSSL::X509::Certificate.new data.body
end
get_cert_chain() click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1537
def get_cert_chain()
  root_cert = File.expand_path(File.join(File.dirname(__FILE__), '../../../data/DigiCertHighAssuranceEVRootCA.pem'))
  intermediate_cert = File.expand_path(File.join(File.dirname(__FILE__), '../../../data/DigiCertSHA2ExtendedValidationServerCA.pem'))

  cert_store = OpenSSL::X509::Store.new
  cert_store.add_file(root_cert)
  cert_store.add_file(intermediate_cert)

  cert_store
end
get_expected_sig(transmission_id, timestamp, webhook_id, event_body) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1548
def get_expected_sig(transmission_id, timestamp, webhook_id, event_body)
  utf8_encoded_event_body = event_body.force_encoding("UTF-8")
  crc = Zlib::crc32(utf8_encoded_event_body).to_s
  transmission_id + "|" + timestamp + "|" + webhook_id + "|" + crc
end
get_resource_class(name) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1589
def get_resource_class(name)
  class_array = PayPal::SDK::REST.constants.select {|c| Class === PayPal::SDK::REST.const_get(c)}
  class_array.each do |classname|
    if (classname.to_s.downcase == name.downcase)
      return classname
    end
  end
end
load_members() click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1501
def self.load_members
  object_of :id, String
  object_of :create_time, String
  object_of :resource_type, String
  object_of :event_version, String
  object_of :event_type, String
  object_of :summary, String
  object_of :status, String
  object_of :resource, Hash
  array_of  :links, Links
end
verify(transmission_id, timestamp, webhook_id, event_body, cert_url, sig, algo='sha256') click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1584
def verify(transmission_id, timestamp, webhook_id, event_body, cert_url, sig, algo='sha256')
  cert = get_cert(cert_url)
  verify_signature(transmission_id, timestamp, webhook_id, event_body, cert, sig, algo) && verify_cert(cert)
end
verify_cert(cert) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1578
def verify_cert(cert)
  cert_store = get_cert_chain()

  verify_cert_chain(cert_store, cert) && verify_common_name(cert) && verify_expiration(cert)
end
verify_cert_chain(cert_store, cert) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1574
def verify_cert_chain(cert_store, cert)
  cert_store.verify(cert)
end
verify_common_name(cert) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1554
def verify_common_name(cert)
  common_name = cert.subject.to_a.select{|name, _, _| name == 'CN' }.first[1]

  common_name.start_with?("messageverificationcerts.") && common_name.end_with?(".paypal.com")
end
verify_expiration(cert) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1570
def verify_expiration(cert)
  cert.not_after >= Time.now
end
verify_signature(transmission_id, timestamp, webhook_id, event_body, cert, actual_sig_encoded, algo) click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1560
def verify_signature(transmission_id, timestamp, webhook_id, event_body, cert, actual_sig_encoded, algo)
  expected_sig = get_expected_sig(transmission_id, timestamp, webhook_id, event_body)

  digest = OpenSSL::Digest.new(algo)
  digest.update(expected_sig)
  actual_sig = Base64.decode64(actual_sig_encoded).force_encoding('UTF-8')

  cert.public_key.verify(digest, actual_sig, expected_sig)
end

Public Instance Methods

get_resource() click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1518
def get_resource()
  webhook_resource_type = self.resource_type
  resource_class = self.class.get_resource_class(webhook_resource_type)
  if resource_class
    return Object::const_get(resource_class).new(self.resource)
  else
    return self.resource
  end
end
resend() click to toggle source
# File lib/paypal-sdk/rest/data_types.rb, line 1513
def resend()
  path = "v1/notifications/webhooks-events/#{self.id}/resend"
  WebhookEvent.new(api.post(path))
end