Sha256: f61014f74a9723b0064f5607575b8c134c38a6693cd40520393abbe5c2c29b9b

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

module Oschadbank
  class Response
    include Constants

    def initialize(client, params)
      @client = client
      @params = params

      check_required!
      check_signature!
    end

    def request_type
      TR_TYPE.invert[@params['TRTYPE'].to_s]
    end

    def pre_auth?
      request_type == :pre_auth
    end

    def auth?
      request_type == :auth
    end

    def charge?
      request_type == :charge
    end

    def refund?
      request_type == :refund
    end

    def success?
      RESULT_SUCCESS.include?(result_code) && RC_SUCCESS.include?(rc_code)
    end

    def status_code
      rc_code
    end

    def status_message
      "#{result_message} (#{rc_message})"
    end

    def order_id
      @params['Order']
    end

    def currency
      @params['Currency']
    end

    def amount
      @params['Amount'] && @params['Amount'].to_f
    end

    def rrn
      @params['RRN']
    end

    def int_ref
      @params['Int_Ref']
    end

    def auth_code
      @params['AuthCode']
    end

    private

    def check_required!
      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.downcase == valid_signature
    end

    def result_code
      @params['Result'].to_s
    end

    def rc_code
      @params['RC'].to_s
    end

    def result_message
      RESULT_MESSAGES[result_code]
    end

    def rc_message
      RC_MESSAGES[rc_code]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
oschadbank-0.1.9 lib/oschadbank/response.rb
oschadbank-0.1.8 lib/oschadbank/response.rb
oschadbank-0.1.7 lib/oschadbank/response.rb
oschadbank-0.1.6 lib/oschadbank/response.rb
oschadbank-0.1.5 lib/oschadbank/response.rb