Class: Payrex::Services::WebhooksService
  
  
  
  
    
      Constant Summary
      collapse
    
    
      
        - PATH =
          
        
- "webhooks" 
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods inherited from BaseService
  #initialize, #request
  
    Instance Method Details
    
      
  
  
    #create(payload)  ⇒ Object 
  
  
  
  
    | 
32
33
34
35
36
37
38
39 | # File 'lib/services/webhooks_service.rb', line 32
def create(payload)
  request(
    method: :post,
    object: Payrex::Entities::Webhook,
    path: PATH,
    payload: payload
  )
end | 
 
    
      
  
  
    #delete(id)  ⇒ Object 
  
  
  
  
    | 
87
88
89
90
91
92
93
94 | # File 'lib/services/webhooks_service.rb', line 87
def delete(id)
  request(
    method: :delete,
    object: Payrex::Entities::Deleted,
    path: "#{PATH}/#{id}",
    payload: {}
  )
end | 
 
    
      
  
  
    #disable(id)  ⇒ Object 
  
  
  
  
    | 
78
79
80
81
82
83
84
85 | # File 'lib/services/webhooks_service.rb', line 78
def disable(id)
  request(
    method: :post,
    object: Payrex::Entities::Webhook,
    path: "#{PATH}/#{id}/disable",
    payload: {}
  )
end | 
 
    
      
  
  
    #enable(id)  ⇒ Object 
  
  
  
  
    | 
69
70
71
72
73
74
75
76 | # File 'lib/services/webhooks_service.rb', line 69
def enable(id)
  request(
    method: :post,
    object: Payrex::Entities::Webhook,
    path: "#{PATH}/#{id}/enable",
    payload: {}
  )
end | 
 
    
      
  
  
    #list(payload = {})  ⇒ Object 
  
  
  
  
    | 
50
51
52
53
54
55
56
57
58 | # File 'lib/services/webhooks_service.rb', line 50
def list(payload = {})
  request(
    is_list: true,
    method: :get,
    object: Payrex::Entities::Webhook,
    path: PATH,
    payload: payload
  )
end | 
 
    
      
  
  
    #parse_event(payload:, signature_header:, webhook_secret_key:)  ⇒ Object 
  
  
  
  
    | 
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | # File 'lib/services/webhooks_service.rb', line 6
def parse_event(payload:, signature_header:, webhook_secret_key:)
  raise Payrex::Errors::ValueUnexpectedError.new("The signature must be a string.") if !.is_a?(String)
  
  signature_array = .split(",")
  
  raise Payrex::Errors::ValueUnexpectedError.new("The format of signature #{} is invalid.") if signature_array.length < 3
  
  timestamp = signature_array[0].split("=")[1]
  test_mode_signature = signature_array[1].split("=")[1]
  live_mode_signature = signature_array[2].split("=")[1]
  
  comparison_signature = live_mode_signature || test_mode_signature
  
  hash = OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest.new("sha256"),
    webhook_secret_key,
    "#{timestamp}.#{payload}"
  )
  raise Payrex::Errors::SignatureInvalidError.new("The signature is invalid.") if hash != comparison_signature
  
  api_resource = Payrex::ApiResource.new(JSON.parse(payload))
  
  Payrex::Entities::Event.new(api_resource)
end | 
 
    
      
  
  
    #retrieve(id)  ⇒ Object 
  
  
  
  
    | 
60
61
62
63
64
65
66
67 | # File 'lib/services/webhooks_service.rb', line 60
def retrieve(id)
  request(
    method: :get,
    object: Payrex::Entities::Webhook,
    path: "#{PATH}/#{id}",
    payload: {}
  )
end | 
 
    
      
  
  
    #update(id, payload)  ⇒ Object 
  
  
  
  
    | 
41
42
43
44
45
46
47
48 | # File 'lib/services/webhooks_service.rb', line 41
def update(id, payload)
  request(
    method: :put,
    object: Payrex::Entities::Webhook,
    path: "#{PATH}/#{id}",
    payload: payload
  )
end |