lib/oschadbank/response.rb in oschadbank-0.1.0 vs lib/oschadbank/response.rb in oschadbank-0.1.1

- old
+ new

@@ -1,13 +1,15 @@ module Oschadbank class Response include Constants - def initialize(params) + def initialize(client, params) + @client = client @params = params check_required! + check_signature! end def request_type TR_TYPE.invert[@params['TRTYPE'].to_s] end @@ -65,13 +67,22 @@ end private def check_required! - required_params = ['Order', 'Result', 'RC', 'TRTYPE'] + required_params = %w(Order Result RC TRTYPE P_SIGN) required_params.each do |param| raise ParamRequred.new(param) if @params[param].to_s.empty? end + end + + def check_signature! + params = @params.dup + signature = params.delete('P_SIGN') + + valid_signature = MacBuilder.new(:response, @client.mac_key, params).build + + raise InvalidSignature unless signature == valid_signature end def result_code @params['Result'].to_s end