Class: Iyzipay::IyzipayResourceV2

Inherits:
IyzipayResource show all
Defined in:
lib/iyzipay/iyzipay_resource_v2.rb

Constant Summary collapse

AUTHORIZATION_HEADER_STRING =
'IYZWSv2'

Constants inherited from IyzipayResource

Iyzipay::IyzipayResource::AUTHORIZATION_HEADER_NAME, Iyzipay::IyzipayResource::RANDOM_CHARS, Iyzipay::IyzipayResource::RANDOM_HEADER_NAME, Iyzipay::IyzipayResource::RANDOM_STRING_SIZE

Instance Method Summary collapse

Methods inherited from IyzipayResource

#format_header_string, #get_plain_http_header, #json_decode, #random_string, #to_pki_string

Instance Method Details

#calculate_hash(pki_string, random_header_value, options, uri) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/iyzipay/iyzipay_resource_v2.rb', line 28

def calculate_hash(pki_string, random_header_value, options, uri)
  data = "#{random_header_value}#{uri}#{pki_string}"
  signature = OpenSSL::HMAC.hexdigest('SHA256', options.secret_key, data)
  authorization_params = [
    'apiKey:' + options.api_key,
    'randomKey:' + random_header_value,
    'signature:' + signature
  ]
  Base64.strict_encode64(authorization_params.join('&'))
end

#get_http_header(options, uri, pki_string = nil, authorize_request = true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/iyzipay/iyzipay_resource_v2.rb', line 7

def get_http_header(options, uri, pki_string = nil, authorize_request = true)
  header = {
    accept: 'application/json',
    'content-type': 'application/json'
  }
  if authorize_request
    random_header_value = random_string(RANDOM_STRING_SIZE)
    authorization = prepare_authorization_string(pki_string, random_header_value, options, uri).to_s
    header[:Authorization] = authorization
    header[:'x-iyzi-rnd'] = random_header_value.to_s
    header[:'x-iyzi-client-version'] = 'iyzipay-ruby-1.0.45'
  end

  header
end

#prepare_authorization_string(pki_string, random_header_value, options, uri) ⇒ Object



23
24
25
26
# File 'lib/iyzipay/iyzipay_resource_v2.rb', line 23

def prepare_authorization_string(pki_string, random_header_value, options, uri)
  hash_digest = calculate_hash(pki_string, random_header_value, options, uri)
  "#{AUTHORIZATION_HEADER_STRING} #{hash_digest}"
end