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



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

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



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

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
# 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}"
  end

  header
end

#get_plain_http_headerObject



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

def get_plain_http_header
  get_http_header(nil, false)
end

#json_decode(response, raw_result) ⇒ Object



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

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



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

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



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

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



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

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