Class: Iyzipay::IyzipayResource

Inherits:
Object
  • Object
show all
Defined in:
lib/iyzipay/iyzipay_resource.rb

Constant Summary collapse

AUTHORIZATION_HEADER_NAME =
'Authorization'
RANDOM_HEADER_NAME =
'x-iyzi-rnd'
AUTHORIZATION_HEADER_STRING =
'IYZWS %s:%s'
RANDOM_STRING_SIZE =
8
RANDOM_CHARS =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

Instance Method Summary collapse

Instance Method Details

#calculate_hash(pki_string, random_header_value, options) ⇒ Object



38
39
40
# File 'lib/iyzipay/iyzipay_resource.rb', line 38

def calculate_hash(pki_string, random_header_value, options)
  Digest::SHA1.base64digest("#{options.api_key}#{random_header_value}#{options.secret_key}#{pki_string}")
end

#format_header_string(*args) ⇒ Object



42
43
44
# File 'lib/iyzipay/iyzipay_resource.rb', line 42

def format_header_string(*args)
  sprintf(AUTHORIZATION_HEADER_STRING, *args)
end

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



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/iyzipay/iyzipay_resource.rb', line 10

def get_http_header(pki_string = nil, options = nil, authorize_request = true)
  header = {:accept => 'application/json',
            :'content-type' => 'application/json'}

  if authorize_request
    random_header_value = random_string(RANDOM_STRING_SIZE)
    header[:'Authorization'] = "#{prepare_authorization_string(pki_string, random_header_value, options)}"
    header[:'x-iyzi-rnd'] = "#{random_header_value}"
    header[:'x-iyzi-client-version'] = 'iyzipay-ruby-1.0.36'
  end

  header
end

#get_plain_http_headerObject



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

def get_plain_http_header
  get_http_header(nil, false)
end

#json_decode(response, raw_result) ⇒ Object



33
34
35
36
# File 'lib/iyzipay/iyzipay_resource.rb', line 33

def json_decode(response, raw_result)
  json_result = JSON::parse(raw_result)
  response.from_json(json_result)
end

#prepare_authorization_string(pki_string, random_header_value, options) ⇒ Object



28
29
30
31
# File 'lib/iyzipay/iyzipay_resource.rb', line 28

def prepare_authorization_string(pki_string, random_header_value, options)
  hash_digest = calculate_hash(pki_string, random_header_value, options)
  format_header_string(options.api_key, hash_digest)
end

#random_string(string_length) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/iyzipay/iyzipay_resource.rb', line 46

def random_string(string_length)
  random_string = ''
  string_length.times do
    random_string << RANDOM_CHARS.split('').sample
  end
  random_string
end

#to_pki_string(request) ⇒ Object



54
55
56
57
58
# File 'lib/iyzipay/iyzipay_resource.rb', line 54

def to_pki_string(request)
  PkiBuilder.new.append(:locale, request[:locale]).
      append(:conversationId, request[:conversationId]).
      get_request_string
end