Sha256: 2fe293b432c9bab83fb000c7f272959210882fb6389d443df5ddd34acd94289c

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

module Oschadbank
  class ParamsBuilder
    include Constants

    REQUEST_PARAMS = {
      amount: 'AMOUNT',
      org_amount: 'ORG_AMOUNT',
      currency: 'CURRENCY',
      order_id: 'ORDER',
      description: 'DESC',
      rrn: 'RRN',
      int_ref: 'INT_REF',
      back_url: 'BACKREF',
    }.freeze

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

    def build
      result = {
        'TRTYPE' => tr_type,
        'TERMINAL' => @client.terminal_id.to_s,
        'MERCHANT' => @client.merchant_id.to_s,
        'MERCH_NAME' => @client.merchant_name.to_s,
        'MERCH_URL' => @client.merchant_url.to_s,
        'MERCH_GMT' => @client.merchant_gmt.to_s,
        'COUNTRY' => @client.country_code.to_s,
        'EMAIL' => @client.email.to_s,
        'TIMESTAMP' => timestamp,
        'NONCE' => nonce,
      }

      @params.each do |key, value|
        key = REQUEST_PARAMS[key] || key
        value = value.to_s
        value = value.encode('CP1251', 'UTF-8') if key == 'DESC'
        result[key] = value
      end

      result.delete_if { |_k, v| v.empty? }

      result['P_SIGN'] = mac_signature(result)

      result
    end

    private

    def tr_type
      TR_TYPE[@request_type] || raise(InvalidRequestType)
    end

    def timestamp
      Time.now.gmtime.strftime('%Y%m%d%H%M%S')
    end

    # Random HEX value, with length between 8 and 32
    def nonce
      min = 0xFFFFFFFF
      max = 0XFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      value = rand(max - min) + min
      value.to_s(16)
    end

    def mac_signature(params)
      MacBuilder.new(@request_type, @client.mac_key, params).build
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
oschadbank-0.1.5 lib/oschadbank/params_builder.rb
oschadbank-0.1.4 lib/oschadbank/params_builder.rb
oschadbank-0.1.3 lib/oschadbank/params_builder.rb
oschadbank-0.1.2 lib/oschadbank/params_builder.rb
oschadbank-0.1.1 lib/oschadbank/params_builder.rb