Class: Sepa::Client
- Inherits:
-
Object
- Object
- Sepa::Client
- Includes:
- ActiveModel::Validations, AttributeChecks, ErrorMessages, Utilities
- Defined in:
- lib/sepa/client.rb
Overview
Handles parameter validation, key initialization, SoapBuilder initialization, communicating with the bank and Response initialization.
Constant Summary collapse
- BANKS =
The list of banks that are currently supported by this gem
%i( danske nordea op samlink ).freeze
- LANGUAGES =
Languages that are currently supported by the gem
%w(FI SE EN).freeze
- ENVIRONMENTS =
Environments that are currently supported by the gem
[:production, :test].freeze
- STATUSES =
Statuses that can be given to download file list command. When NEW is given, only those files that have not yet been downloaded will be listed. DOWNLOADED will list only downloaded files and ALL will list every file
%w(NEW DOWNLOADED ALL).freeze
Constants included from ErrorMessages
ErrorMessages::CONTENT_ERROR_MESSAGE, ErrorMessages::CUSTOMER_ID_ERROR_MESSAGE, ErrorMessages::DECRYPTION_ERROR_MESSAGE, ErrorMessages::ENCRYPTION_CERT_ERROR_MESSAGE, ErrorMessages::ENCRYPTION_CERT_REQUEST_ERROR_MESSAGE, ErrorMessages::ENCRYPTION_PRIVATE_KEY_ERROR_MESSAGE, ErrorMessages::ENVIRONMENT_ERROR_MESSAGE, ErrorMessages::FILE_REFERENCE_ERROR_MESSAGE, ErrorMessages::FILE_TYPE_ERROR_MESSAGE, ErrorMessages::HASH_ERROR_MESSAGE, ErrorMessages::NOT_OK_RESPONSE_CODE_ERROR_MESSAGE, ErrorMessages::PIN_ERROR_MESSAGE, ErrorMessages::SIGNATURE_ERROR_MESSAGE, ErrorMessages::SIGNING_CERT_REQUEST_ERROR_MESSAGE, ErrorMessages::STATUS_ERROR_MESSAGE, ErrorMessages::TARGET_ID_ERROR_MESSAGE
Instance Attribute Summary collapse
-
#bank ⇒ Symbol
The bank that is used in this client.
-
#bank_encryption_certificate ⇒ String
Bank's encryption certificate.
-
#command ⇒ Symbol
The command that is used with this client.
-
#content ⇒ String
The payload in base64 encoded form.
-
#customer_id ⇒ String
Customer id got from the bank.
-
#encryption_csr ⇒ String
Encryption certificate signing request.
-
#encryption_private_key ⇒ String
Own encryption private key.
-
#environment ⇒ Symbol
The environment to be used.
-
#file_reference ⇒ String
File reference number used in download_file requests.
-
#file_type ⇒ String
The file type of the file that is about to be uploaded or downloaded.
-
#language ⇒ String
The language to be used in this client.
-
#own_signing_certificate ⇒ String
Own signing certificate in "pem" format.
-
#pin ⇒ String
The one-time pin got for bank.
-
#savon_options ⇒ Hash
Options to be passed directly to the underlying savon client.
-
#signing_csr ⇒ String
The signing certificate signing request.
-
#signing_private_key ⇒ String
Signing private key which is used to sign the request.
-
#status ⇒ String
Used to filter files in download_file_list request.
-
#target_id ⇒ String
A file categorization id used by Nordea.
Instance Method Summary collapse
-
#attributes(hash) ⇒ Object
Sets the attributes given in a hash.
-
#create_hash ⇒ Hash
private
Creates a hash of all instance variables and their values.
-
#initialize(hash = {}) ⇒ Client
constructor
Initializes the class.
-
#initialize_response(error, response) ⇒ Response
private
Initializes Response as correct class for a bank.
-
#initialize_signing_private_key ⇒ OpenSSL::PKey::RSA
private
Converts the #signing_private_key from String to OpenSSL::PKey::RSA.
- #savon_globals ⇒ Object private
- #savon_locals ⇒ Object private
-
#send_request ⇒ Response
Sends request to the bank specified in the attributes.
- #soap_command ⇒ Object private
- #wsdl ⇒ String private
Methods included from AttributeChecks
#allowed_commands, #check_command, #check_content, #check_customer_id, #check_encryption_cert_request, #check_encryption_certificate, #check_encryption_private_key, #check_environment, #check_file_reference, #check_file_type, #check_keys, #check_pin, #check_presence_and_length, #check_signing_csr, #check_status, #check_target_id
Methods included from Utilities
#calculate_digest, #canonicalize_exclusively, #canonicalized_node, #cert_request_valid?, #check_validity_against_schema, #csr_to_binary, #decode, #encode, #extract_cert, #format_cert, #format_cert_request, #hmac, #iso_time, #load_body_template, #process_cert_value, #rsa_key, #set_node_id, #validate_signature, #verify_certificate_against_root_certificate, #x509_certificate, #xml_doc
Constructor Details
#initialize(hash = {}) ⇒ Client
Initializes the class. An optional hash of attributes can be given. Environment is set to production if not given, language to 'EN' and status to 'NEW'.
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/sepa/client.rb', line 221 def initialize(hash = {}) attributes(hash) self.environment ||= :production self.language ||= 'EN' self.status ||= 'NEW' self. ||= { globals: {}, locals: {}, } end |
Instance Attribute Details
#bank ⇒ Symbol
The bank that is used in this client. One of BANKS.
14 15 16 |
# File 'lib/sepa/client.rb', line 14 def bank @bank end |
#bank_encryption_certificate ⇒ String
Bank's encryption certificate. The request is encrypted with this so that the bank can decrypt the request with their private key. In "pem" format.
156 157 158 |
# File 'lib/sepa/client.rb', line 156 def bank_encryption_certificate @bank_encryption_certificate end |
#command ⇒ Symbol
The command that is used with this client. One of AttributeChecks#allowed_commands.
19 20 21 |
# File 'lib/sepa/client.rb', line 19 def command @command end |
#content ⇒ String
The payload in base64 encoded form. Used with upload file command.
26 27 28 |
# File 'lib/sepa/client.rb', line 26 def content @content end |
#customer_id ⇒ String
Customer id got from the bank.
33 34 35 |
# File 'lib/sepa/client.rb', line 33 def customer_id @customer_id end |
#encryption_csr ⇒ String
Encryption certificate signing request. This needs to be generated and is then sent to the bank to be signed.
163 164 165 |
# File 'lib/sepa/client.rb', line 163 def encryption_csr @encryption_csr end |
#encryption_private_key ⇒ String
Own encryption private key. Used to decrypt the response. In "pem" format.
149 150 151 |
# File 'lib/sepa/client.rb', line 149 def encryption_private_key @encryption_private_key end |
#environment ⇒ Symbol
The environment to be used. One of ENVIRONMENTS.
46 47 48 |
# File 'lib/sepa/client.rb', line 46 def environment @environment end |
#file_reference ⇒ String
File reference number used in download_file requests.
53 54 55 |
# File 'lib/sepa/client.rb', line 53 def file_reference @file_reference end |
#file_type ⇒ String
The file type of the file that is about to be uploaded or downloaded. These vary by bank.
60 61 62 |
# File 'lib/sepa/client.rb', line 60 def file_type @file_type end |
#language ⇒ String
The language to be used in this client. One of LANGUAGES.
65 66 67 |
# File 'lib/sepa/client.rb', line 65 def language @language end |
#own_signing_certificate ⇒ String
Own signing certificate in "pem" format. Embedded in the request
127 128 129 |
# File 'lib/sepa/client.rb', line 127 def own_signing_certificate @own_signing_certificate end |
#pin ⇒ String
The one-time pin got for bank. Used with certificate requests.
77 78 79 |
# File 'lib/sepa/client.rb', line 77 def pin @pin end |
#savon_options ⇒ Hash
Options to be passed directly to the underlying savon client. Format is as follows:
{
globals: {
ssl_verify_mode: :none,
...,
},
locals: {
xml: "
178 179 180 |
# File 'lib/sepa/client.rb', line 178 def @savon_options end |
#signing_csr ⇒ String
The signing certificate signing request. Used in certificate requests.
143 144 145 |
# File 'lib/sepa/client.rb', line 143 def signing_csr @signing_csr end |
#signing_private_key ⇒ String
Signing private key which is used to sign the request
98 99 100 |
# File 'lib/sepa/client.rb', line 98 def signing_private_key @signing_private_key end |
#status ⇒ String
Used to filter files in download_file_list request. One of STATUSES.
70 71 72 |
# File 'lib/sepa/client.rb', line 70 def status @status end |
#target_id ⇒ String
A file categorization id used by Nordea. Can be retrieved with get_user_info request. Not used with Danske Bank
41 42 43 |
# File 'lib/sepa/client.rb', line 41 def target_id @target_id end |
Instance Method Details
#attributes(hash) ⇒ Object
Sets the attributes given in a hash
254 255 256 257 258 |
# File 'lib/sepa/client.rb', line 254 def attributes(hash) hash.each do |name, value| send("#{name}=", value) end end |
#create_hash ⇒ Hash (private)
Creates a hash of all instance variables and their values. Before the actual hash is created, the #signing_private_key is converted to OpenSSL::PKey::RSA using #initialize_signing_private_key method.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/sepa/client.rb', line 293 def create_hash initialize_signing_private_key iv = {} # Create hash of all instance variables instance_variables.map do |name| key = name[1..-1].to_sym value = instance_variable_get(name) iv[key] = value end iv end |
#initialize_response(error, response) ⇒ Response (private)
Initializes Response as correct class for a bank. Also converts possible #encryption_private_key from String to OpenSSL::PKey::RSA.
335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/sepa/client.rb', line 335 def initialize_response(error, response) = { command: command, environment: environment, error: error, response: response, } if encryption_private_key && !encryption_private_key.empty? [:encryption_private_key] = rsa_key(encryption_private_key) end "Sepa::#{bank.capitalize}Response".constantize.new() end |
#initialize_signing_private_key ⇒ OpenSSL::PKey::RSA (private)
Converts the #signing_private_key from String to OpenSSL::PKey::RSA
310 311 312 |
# File 'lib/sepa/client.rb', line 310 def initialize_signing_private_key @signing_private_key = rsa_key(@signing_private_key) if @signing_private_key end |
#savon_globals ⇒ Object (private)
357 358 359 |
# File 'lib/sepa/client.rb', line 357 def savon_globals { wsdl: wsdl }.merge([:globals] || {}) end |
#savon_locals ⇒ Object (private)
361 362 363 |
# File 'lib/sepa/client.rb', line 361 def savon_locals { xml: SoapBuilder.new(create_hash).to_xml }.merge([:locals] || {}) end |
#send_request ⇒ Response
Sends request to the bank specified in the attributes. First a new SoapBuilder class is initialized with a hash of the parameters given to the client with the #create_hash method. After this, a Savon client is initialized with a WSDL file got from #wsdl. After this, the Savon client makes the actual call to the server with the #command and the constructed SoapBuilder. After the call, the xml is extracted from the Savon response and the response is then checked for any Savon::Error errors. After this a Response is initialized using the #initialize_response method with the xml response and possible errors.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/sepa/client.rb', line 269 def send_request raise ArgumentError, errors. unless valid? client = Savon.client(savon_globals) begin error = nil response = client.call(soap_command, savon_locals) response &&= response.to_xml rescue Savon::Error => e response = nil error = e.http.body end initialize_response(error, response) end |
#soap_command ⇒ Object (private)
349 350 351 352 353 354 355 |
# File 'lib/sepa/client.rb', line 349 def soap_command if @command == :renew_certificate && [:nordea, :op, :samlink].include?(@bank) :get_certificate else @command end end |
#wsdl ⇒ String (private)
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/sepa/client.rb', line 316 def wsdl file = if STANDARD_COMMANDS.include?(command) "wsdl_#{bank}" else "wsdl_#{bank}_cert" end path = "#{WSDL_PATH}/#{file}" path2 = "#{path}_#{environment}.xml" File.exist?(path2) ? path2 : "#{path}.xml" end |