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.



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

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

  if conf.environment.nil?
    raise MissingEnvironmentException
  end

  if conf.environment == Nps::Environments::CUSTOM_ENV and conf.custom_env_urls.nil?
    raise EmptyCustomUrlsWithCustomEnvException
  end

  if conf.custom_env_urls
    if conf.environment != Nps::Environments::CUSTOM_ENV
      raise WrongConfigurationException
    end

    if conf.custom_env_urls.length > Nps::Utils::MAX_CUSTOM_URLS
      raise MaxCustomUrlsException
    end

    if conf.custom_env_urls.kind_of?(Array) and conf.custom_env_urls.empty?
      raise EmptyCustomUrlsException
    end

    conf.custom_env_urls.each do |url|
      # if url =~ /\A#{URI::regexp}\z/
      #   raise InvalidUrlException.error_message_with_url url
      # end

      unless url_valid? url
        raise InvalidUrlException.error_message_with_url url
      end

      # unless valid_url_1? url
      #   raise InvalidUrlException.error_message_with_url url
      # end
    end
  end

  @key = conf.key
  @log = true
  @logger = conf.logger
  @wsdl = Nps::Environments::get_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
  @custom_env_urls = conf.custom_env_urls ? conf.custom_env_urls : nil

  setup
end

Instance Method Details

#add_extra_data(params, service) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/nps/soap_client.rb', line 152

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nps/soap_client.rb', line 127

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



144
145
146
# File 'lib/nps/soap_client.rb', line 144

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

#create_hmac_sha512(data) ⇒ Object



148
149
150
# File 'lib/nps/soap_client.rb', line 148

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

#create_md5_hash(data) ⇒ Object



140
141
142
# File 'lib/nps/soap_client.rb', line 140

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

#setupObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nps/soap_client.rb', line 85

def setup
  @client_config = {
      ssl_verify_mode: :none,
      logger: @logger,
      wsdl: File.join(File.dirname(File.expand_path(__FILE__)), "/wsdl/" + @wsdl),
      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

end

#soap_call(service, params) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/nps/soap_client.rb', line 166

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}

    if @custom_env_urls
      internal_connection_timeout = @read_timeout
      @custom_env_urls.each do |url|
        begin
          start_time = Time.now
          @client_config.merge!({endpoint: url,
                                 open_timeout: Nps::Utils::CUSTOM_URL_CONNECTION_TIMEOUT,
                                 read_timeout: internal_connection_timeout})
          @client = Savon.client @client_config
          @client.call(service, message: params).body
          break
        rescue SocketError, HTTPClient::ConnectTimeoutError
   if @custom_env_urls.last == url
  	        raise ApiException
   end
   @logger.warn("Could not connect to #{url}")
          finish_time = Time.now
          delta_time = finish_time - start_time
          internal_connection_timeout -= delta_time.ceil
          next
        rescue HTTPClient::ReceiveTimeoutError
          raise ApiException
        rescue StandardError 
          raise 'An unexpected error occurred'
        end
      end
    else
      begin
        @client = Savon.client @client_config
        @client.call(service, message: params).body
      rescue HTTPClient::ReceiveTimeoutError
       raise ApiException
      rescue StandardError 
       raise 'An unexpected error occurred'
     end
   end
end

#url_valid?(url) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/nps/soap_client.rb', line 73

def url_valid?(url)
    url = URI.parse(url) rescue false
    url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
end

#valid_url_1?(url) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/nps/soap_client.rb', line 78

def valid_url_1?(url)
  uri = URI.parse(url)
  uri.is_a?(URI::HTTP) && !uri.host.nil?
rescue URI::InvalidURIError
  false
end