Class: AmazonPay::ClientHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_pay/client_helper.rb

Overview

All the helper files

Class Method Summary collapse

Class Method Details

.fetch_api_endpoint_base_url(config_args: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/amazon_pay/client_helper.rb', line 18

def self.fetch_api_endpoint_base_url(config_args: {})
  if config_args[:overrideServiceUrl]&.length&.positive?
    config_args[:overrideServiceUrl]
  else
    region = config_args[:region].downcase.to_sym
    region_map = AmazonPay::CONSTANTS[:REGION_MAP][region].to_sym

    AmazonPay::CONSTANTS[:API_ENDPOINTS][region_map]
  end
end

.fetch_parameters_as_string(request_params: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/amazon_pay/client_helper.rb', line 48

def self.fetch_parameters_as_string(request_params: nil)
  return '' if request_params.nil?

  query_params = []
  request_params.map do |key, value|
    query_params << "#{key}=#{CGI.escape(value)}"
  end
  query_params.join('&')
end

.fetch_query_string(request_params: nil) ⇒ Object



42
43
44
45
46
# File 'lib/amazon_pay/client_helper.rb', line 42

def self.fetch_query_string(request_params: nil)
  return "?#{fetch_parameters_as_string(request_params: request_params)}" if request_params

  ''
end

.fetch_timestampObject



14
15
16
# File 'lib/amazon_pay/client_helper.rb', line 14

def self.fetch_timestamp
  Time.now.utc.iso8601
end

.invoke_api(config_args: {}, api_options: {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/amazon_pay/client_helper.rb', line 29

def self.invoke_api(config_args: {}, api_options: {})
  options = {
    method: api_options[:method],
    json: false,
    headers: api_options[:headers],
    url: "https://#{fetch_api_endpoint_base_url(config_args: config_args)}/#{api_options[:url_fragment]}"\
    "#{fetch_query_string(request_params: api_options[:query_params])}",
    body: api_options[:payload]
  }

  retry_logic(options: options, count: 1)
end

.prepare_options(config_args: {}, options: {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/amazon_pay/client_helper.rb', line 58

def self.prepare_options(config_args: {}, options: {})
  options[:headers] ||= {}

  # if user doesn't pass in a string, assume it's a JS Hash and convert it to a JSON string
  unless options[:payload].is_a?(String) || options[:payload].nil?
    options[:payload] = JSON.generate(options[:payload])
  end

  options[:url_fragment] = if config_args[:sandbox] == true || config_args[:sandbox] == 'true'
                             "sandbox/#{AmazonPay::CONSTANTS[:API_VERSION]}/#{options[:url_fragment]}"
                           else
                             "live/#{AmazonPay::CONSTANTS[:API_VERSION]}/#{options[:url_fragment]}"
                           end

  options
end

.retry_logic(options: {}, count: 0) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/amazon_pay/client_helper.rb', line 82

def self.retry_logic(options: {}, count: 0)
  response = send_request(options: options, count: count)
  return response if count > AmazonPay::CONSTANTS[:RETRIES]

  return response unless response.nil?

  count += 1
  retry_logic(options: options, count: count)
end

.send_request(options: {}, count: 0) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/amazon_pay/client_helper.rb', line 92

def self.send_request(options: {}, count: 0)
  delay_time = count == 1 ? 0 : (2**(count - 1)).seconds
  sleep(delay_time)
  begin
    uri = URI(options[:url])

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER

    case options[:method].to_sym
    when :GET
      response = http.get(uri, options[:headers])
    when :DELETE
      response = http.delete(uri.request_uri, options[:headers])
    when :POST
      request = Net::HTTP::Post.new(uri.path, options[:headers])
      request.body = options[:body]
      response = http.request(request)
    when :PATCH
      request = Net::HTTP::Patch.new(uri.path, options[:headers])
      request.body = options[:body]
      response = http.request(request)
    end
    JSON.parse(response.body)
  rescue StandardError
    nil
  end
end

.sign(private_key: nil, data: nil) ⇒ Object



75
76
77
78
79
80
# File 'lib/amazon_pay/client_helper.rb', line 75

def self.sign(private_key: nil, data: nil)
  sign_hash = OpenSSL::Digest.new('SHA256')
  priv = OpenSSL::PKey::RSA.new(private_key)
  signature = priv.sign_pss(sign_hash, data, salt_length: 20, mgf1_hash: 'SHA256')
  Base64.strict_encode64(signature)
end

.sign_headers(config_args: {}, options: {}) ⇒ Object

Expected options:

options[:method]
options[:url_fragment]
options[:payload]
options[:headers]


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/amazon_pay/client_helper.rb', line 128

def self.sign_headers(config_args: {}, options: {})
  headers = options[:headers] || {}
  headers['x-amz-pay-region'] = 'us'
  headers['x-amz-pay-host'] = fetch_api_endpoint_base_url(config_args: config_args)
  headers['x-amz-pay-date'] = fetch_timestamp
  headers['content-type'] = 'application/json'
  headers['accept'] = 'application/json'
  headers[
    'user-agent'
  ] = "amazon-pay-api-sdk-ruby/#{AmazonPay::CONSTANTS[:SDK_VERSION]} (Ruby/#{RUBY_VERSION}; #{RUBY_PLATFORM})"

  lower_case_sorted_header_keys = headers.keys.sort.map(&:downcase)
  signed_headers = lower_case_sorted_header_keys.join(';')

  payload = options[:payload]

  if payload.nil? || options[:url_fragment].include?('/account-management/'\
    "#{AmazonPay::CONSTANTS[:API_VERSION]}/accounts")
    payload = '' # do not sign payload for payment critical data APIs
  end

  canonical_request =
    options[:method] + "\n/" +
    options[:url_fragment] + "\n" +
    fetch_parameters_as_string(request_params: options[:query_params]) +
    "\n"

  lower_case_sorted_header_keys.each do |item|
    str = item.downcase + ':' + headers[item] + "\n"

    canonical_request += str
  end

  canonical_request += "\n" + signed_headers + "\n" +
                       OpenSSL::Digest::SHA256.hexdigest(payload)

  string_to_sign =
    AmazonPay::CONSTANTS[:AMAZON_SIGNATURE_ALGORITHM] +
    "\n" +
    OpenSSL::Digest::SHA256.hexdigest(canonical_request)

  signature = sign(private_key: config_args[:private_key], data: string_to_sign)

  headers['authorization'] = "#{AmazonPay::CONSTANTS[:AMAZON_SIGNATURE_ALGORITHM]} "\
  "PublicKeyId=#{config_args[:public_key_id]}, SignedHeaders=#{signed_headers}, Signature=#{signature}"

  headers
end

.sign_payload(config_args: nil, payload: nil) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/amazon_pay/client_helper.rb', line 177

def self.sign_payload(config_args: nil, payload: nil)
  # if user doesn't pass in a string, assume it's a JS Hash and convert it to a JSON string
  payload = JSON.generate(payload) unless payload.is_a?(String)
  string_to_sign = AmazonPay::CONSTANTS[:AMAZON_SIGNATURE_ALGORITHM] + "\n" +
                   OpenSSL::Digest::SHA256.hexdigest(payload)
  sign(private_key: config_args[:private_key], data: string_to_sign)
end