Class: Nps::SoapClient

Inherits:
Object
  • Object
show all
Defined in:
lib/nps/soap_client.rb

Direct Known Subclasses

Sdk

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ SoapClient

Returns a new instance of SoapClient.



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
31
32
# File 'lib/nps/soap_client.rb', line 6

def initialize(conf)
  if conf.logger.nil?
    conf.logger = Logger.new(STDOUT)
  end

  if conf.log_level != Logger::DEBUG
    conf.logger.formatter = NpsFormatter.new
  end
  if conf.log_level == Logger::DEBUG and conf.environment == Nps::Environments::PRODUCTION_ENV
    raise LoggerException
  end  

  @key = conf.key
  @log = true
  @logger = conf.logger
  @wsdl = conf.environment
  @open_timeout = conf.o_timeout.nil? ? 5 : conf.o_timeout
  @read_timeout = conf.r_timeout.nil? ? 60 : conf.r_timeout
  @verify_ssl = conf.verify_ssl.nil? ? true : conf.verify_ssl
  @sanitize = conf.sanitize.nil? ? true : conf.sanitize
  @log_level = conf.log_level ? conf.log_level : nil
  @proxy = conf.proxy_url ? conf.proxy_url : nil
  @proxy_username = conf.proxy_username ? @proxy_username : nil
  @proxy_password = conf.proxy_password ? @proxy_password : nil

  setup
end

Instance Method Details

#add_extra_data(params, service) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/nps/soap_client.rb', line 104

def add_extra_data(params, service)
  services = Services.new()
  if services.is_service_in_services_with_additional_details(service)
    return params
  end
  info = {"SdkInfo" => Nps::Utils::SDK[:language] + ' SDK Version: ' + Nps::Version::VERSION}
  if params.key?("psp_MerchantAdditionalDetails")
    params["psp_MerchantAdditionalDetails"] = params["psp_MerchantAdditionalDetails"].merge(info)
  else
    params["psp_MerchantAdditionalDetails"] = info
  end
  return params
end

#add_secure_hash(params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nps/soap_client.rb', line 79

def add_secure_hash(params)
  concatenated_data = ""
  sorted_hash = params.sort_by{|x,y| x}.to_h
  sorted_hash.each { |key, value|
    if not value.is_a? ::Hash and not value.kind_of?(Array)
      concatenated_data = concatenated_data+value.to_s
    end
  }
  secure_hash = create_hmac_sha256(concatenated_data)
  params["psp_SecureHash"] = secure_hash
  return params
end

#create_hmac_sha256(data) ⇒ Object



96
97
98
# File 'lib/nps/soap_client.rb', line 96

def create_hmac_sha256(data)
  return OpenSSL::HMAC.hexdigest('sha256', @key, data)
end

#create_hmac_sha512(data) ⇒ Object



100
101
102
# File 'lib/nps/soap_client.rb', line 100

def create_hmac_sha512(data)
  return OpenSSL::HMAC.hexdigest('sha512', @key, data)
end

#create_md5_hash(data) ⇒ Object



92
93
94
# File 'lib/nps/soap_client.rb', line 92

def create_md5_hash(data)
  return Digest::MD5.hexdigest(data)
end

#setupObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nps/soap_client.rb', line 34

def setup
  client_config = {
      ssl_verify_mode: :none,
      wsdl: File.join(File.dirname(File.expand_path(__FILE__)), "/wsdl/" + @wsdl),
      logger: @logger,
      open_timeout: @open_timeout,
      read_timeout: @read_timeout,
      pretty_print_xml: true,
      log: @log
  }

  if @log_level
    lvl_config = {
      log_level: :debug
    }
    client_config.merge!(lvl_config)
  end
  
  if @verify_ssl
    ssl_config = {
        ssl_verify_mode: :peer,
        ssl_ca_cert_file: Certifi.where
    }
    client_config.merge!(ssl_config)
  end

  if @proxy
    proxy = {
        proxy: @proxy
    }
    client_config.merge!(proxy)
  end

  if @proxy_username
    proxy_auth = {
      headers: { "Proxy-Authorization" => "Basic #{secret}" }
    }
    client_config.merge!(proxy_auth)
  end


  @client = Savon.client client_config

end

#soap_call(service, params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nps/soap_client.rb', line 118

def soap_call(service, params)
  params = add_extra_data(params, service)
  if @sanitize
    params = Nps::Utils::sanitize(params)
  end
  unless params.has_key? 'psp_ClientSession'
    params = add_secure_hash(params)
  end 
  params = {"Requerimiento" => params}
  begin
    @client.call(service, message: params).body
  rescue TimeoutError
    raise ApiException
  end
end