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 ).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.
-
#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.
- #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'.
205 206 207 208 209 210 |
# File 'lib/sepa/client.rb', line 205 def initialize(hash = {}) 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.
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 |
#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
234 235 236 237 238 |
# File 'lib/sepa/client.rb', line 234 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.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/sepa/client.rb', line 274 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.
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/sepa/client.rb', line 316 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
291 292 293 |
# File 'lib/sepa/client.rb', line 291 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.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/sepa/client.rb', line 249 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(soap_command, xml: soap) response &&= response.to_xml rescue Savon::Error => e response = nil error = e.http.body end initialize_response(error, response) end |
#soap_command ⇒ Object (private)
330 331 332 333 334 335 336 |
# File 'lib/sepa/client.rb', line 330 def soap_command if @command == :renew_certificate && [:nordea, :op].include?(@bank) :get_certificate else @command end end |
#wsdl ⇒ String (private)
297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/sepa/client.rb', line 297 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 |