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.



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

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) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/nps/soap_client.rb', line 92

def add_extra_data(params)
  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



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

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
  }
  concatenated_data = concatenated_data+@key
  hashed_string = Digest::MD5.hexdigest(concatenated_data)
  params["psp_SecureHash"] = hashed_string
  return params
end

#setupObject



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

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



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

def soap_call(service, params)
  params = add_extra_data(params)
  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