Sha256: 41993d88e38e083e37e68e58ed034a0d3f3932612d28b6a009d5e636a9e24028

Contents?: true

Size: 1.71 KB

Versions: 1

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 ParamRequired.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

1 entries across 1 versions & 1 rubygems

Version Path
oschadbank-0.1.10 lib/oschadbank/response.rb