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 )
- LANGUAGES =
Languages that are currently supported by the gem
['FI', 'SE', 'EN']
- ENVIRONMENTS =
Environments that are currently supported by the gem
[:production, :test]
- 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
['NEW', 'DOWNLOADED', 'ALL']
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.
-
#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.
-
#send_request ⇒ Response
Sends request to the bank specified in the attributes.
- #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'.
207 208 209 210 211 212 |
# File 'lib/sepa/client.rb', line 207 def initialize(hash = {}) self.attributes hash self.environment ||= :production self.language ||= 'EN' self.status ||= 'NEW' end |
Instance Attribute Details
#bank ⇒ Symbol
The bank that is used in this client. One of BANKS.
16 17 18 |
# File 'lib/sepa/client.rb', line 16 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.
158 159 160 |
# File 'lib/sepa/client.rb', line 158 def bank_encryption_certificate @bank_encryption_certificate end |
#command ⇒ Symbol
The command that is used with this client. One of AttributeChecks#allowed_commands.
21 22 23 |
# File 'lib/sepa/client.rb', line 21 def command @command end |
#content ⇒ String
The payload in base64 encoded form. Used with upload file command.
28 29 30 |
# File 'lib/sepa/client.rb', line 28 def content @content end |
#customer_id ⇒ String
Customer id got from the bank.
35 36 37 |
# File 'lib/sepa/client.rb', line 35 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.
165 166 167 |
# File 'lib/sepa/client.rb', line 165 def encryption_csr @encryption_csr end |
#encryption_private_key ⇒ String
Own encryption private key. Used to decrypt the response. In "pem" format.
151 152 153 |
# File 'lib/sepa/client.rb', line 151 def encryption_private_key @encryption_private_key end |
#environment ⇒ Symbol
The environment to be used. One of ENVIRONMENTS.
48 49 50 |
# File 'lib/sepa/client.rb', line 48 def environment @environment end |
#file_reference ⇒ String
File reference number used in download_file requests.
55 56 57 |
# File 'lib/sepa/client.rb', line 55 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.
62 63 64 |
# File 'lib/sepa/client.rb', line 62 def file_type @file_type end |
#language ⇒ String
The language to be used in this client. One of LANGUAGES.
67 68 69 |
# File 'lib/sepa/client.rb', line 67 def language @language end |
#own_signing_certificate ⇒ String
Own signing certificate in "pem" format. Embedded in the request
129 130 131 |
# File 'lib/sepa/client.rb', line 129 def own_signing_certificate @own_signing_certificate end |
#pin ⇒ String
The one-time pin got for bank. Used with certificate requests.
79 80 81 |
# File 'lib/sepa/client.rb', line 79 def pin @pin end |
#signing_csr ⇒ String
The signing certificate signing request. Used in certificate requests.
145 146 147 |
# File 'lib/sepa/client.rb', line 145 def signing_csr @signing_csr end |
#signing_private_key ⇒ String
Signing private key which is used to sign the request
100 101 102 |
# File 'lib/sepa/client.rb', line 100 def signing_private_key @signing_private_key end |
#status ⇒ String
Used to filter files in download_file_list request. One of STATUSES.
72 73 74 |
# File 'lib/sepa/client.rb', line 72 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
43 44 45 |
# File 'lib/sepa/client.rb', line 43 def target_id @target_id end |
Instance Method Details
#attributes(hash) ⇒ Object
Sets the attributes given in a hash
236 237 238 239 240 |
# File 'lib/sepa/client.rb', line 236 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.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/sepa/client.rb', line 276 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.
318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/sepa/client.rb', line 318 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
293 294 295 |
# File 'lib/sepa/client.rb', line 293 def initialize_signing_private_key @signing_private_key = rsa_key(@signing_private_key) if @signing_private_key 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.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/sepa/client.rb', line 251 def send_request raise ArgumentError, errors. unless valid? soap = SoapBuilder.new(create_hash).to_xml client = Savon.client(wsdl: wsdl) begin error = nil response = client.call(command, xml: soap) response &&= response.to_xml rescue Savon::Error => e response = nil error = e.http.body end initialize_response(error, response) end |
#wsdl ⇒ String (private)
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/sepa/client.rb', line 299 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 |