Module: Bestpay::Utils

Defined in:
lib/bestpay/utils.rb

Class Method Summary collapse

Class Method Details

.build_mac(params, keys) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bestpay/utils.rb', line 9

def self.build_mac(params, keys)
  pairs = []
  keys.each do |k|
    value = params[k]
    if value.nil?
      raise Bestpay::ParameterNotFoundError, "参数 #{k} 不能为空!" unless k == 'CLIENTIP'
    else
      pairs << "#{k}=#{value}"
    end
  end

  Digest::MD5.hexdigest(pairs.join("&")).upcase
end

.check_required_options(options, names) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/bestpay/utils.rb', line 29

def self.check_required_options(options, names)
  names.each do |name|

    unless options.has_key?(name)
      raise Bestpay::ParameterNotFoundError,"参数 #{name} 不能为空!" unless name == 'CLIENTIP'
    end
  end
end

.query_string(options) ⇒ Object



23
24
25
26
27
# File 'lib/bestpay/utils.rb', line 23

def self.query_string(options)
  options.map do |key, value|
    "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
  end.join('&')
end

.stringify_hash(hash) ⇒ Object



38
39
40
# File 'lib/bestpay/utils.rb', line 38

def self.stringify_hash(hash)
  hash.inject({}){|h,(k,v)| h[k.to_s] = v; h} 
end