Class: PayPal::SDK::REST::DataTypes::WebhookEvent

Inherits:
Base show all
Includes:
RequestDataType
Defined in:
lib/paypal-sdk/rest/data_types.rb

Constant Summary

Constants inherited from Core::API::DataTypes::Base

Core::API::DataTypes::Base::ContentKey, Core::API::DataTypes::Base::HashOptions

Instance Attribute Summary

Attributes inherited from Base

#error, #header, #request_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestDataType

#api, api, #error=, included

Methods included from SetAPI

#client_id=, #client_secret=, #set_config, #token=

Methods inherited from Base

#http_header, #merge!, #raise_error!, raise_on_api_error, #success?

Methods inherited from Core::API::DataTypes::Base

add_attribute, add_member, array_of, #convert_array, #convert_object, define_alias_methods, #hash_key, #initialize, #member_names, members, #members, #merge!, object_of, #set, #skip_value?, snakecase, #to_hash, #value_to_hash

Methods included from Core::Logging

#log_event, #logger, logger, logger=

Constructor Details

This class inherits a constructor from PayPal::SDK::Core::API::DataTypes::Base

Class Method Details

.all(options = {}) ⇒ Object


1614
1615
1616
1617
# 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) ⇒ Object

Raises:

  • (ArgumentError)

1608
1609
1610
1611
1612
# 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) ⇒ Object


1604
1605
1606
# 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) ⇒ Object


1532
1533
1534
1535
# 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_chainObject


1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
# 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) ⇒ Object


1548
1549
1550
1551
1552
# 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) ⇒ Object


1589
1590
1591
1592
1593
1594
1595
1596
# 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_membersObject


1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
# 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

.search(page_size, start_time, end_time) ⇒ Object


1598
1599
1600
1601
1602
# File 'lib/paypal-sdk/rest/data_types.rb', line 1598

def search(page_size, start_time, end_time)
  path = "v1/notifications/webhooks-events"
  options = { :page_size => page_size, :start_time => start_time, :end_time => end_time }
  WebhookEventList.new(api.get(path, options))
end

.verify(transmission_id, timestamp, webhook_id, event_body, cert_url, sig, algo = 'sha256') ⇒ Object


1584
1585
1586
1587
# 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) ⇒ Object


1578
1579
1580
1581
1582
# 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) ⇒ Object


1574
1575
1576
# 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) ⇒ Object


1554
1555
1556
1557
1558
# 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) ⇒ Object


1570
1571
1572
# 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) ⇒ Object


1560
1561
1562
1563
1564
1565
1566
1567
1568
# 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

Instance Method Details

#get_resourceObject


1518
1519
1520
1521
1522
1523
1524
1525
1526
# 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

#resendObject


1513
1514
1515
1516
# 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