Module: JwAlipay::Sign

Included in:
Helper, Response
Defined in:
lib/jw_alipay/sign.rb

Overview

TODO 暂时只支持MD5方式,RSA后续添加

Instance Method Summary collapse

Instance Method Details

#_md5_sign(params, sort = true) ⇒ Object



17
18
19
# File 'lib/jw_alipay/sign.rb', line 17

def _md5_sign(params, sort = true)
  Digest::MD5.hexdigest(_sign_string(params, sort) + KEY)
end

#_query_string(params) ⇒ Object



32
33
34
# File 'lib/jw_alipay/sign.rb', line 32

def _query_string(params)
  params.collect{ |k, v| "#{k}=#{v}" }.join("&")
end

#_sign_string(params, sort) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/jw_alipay/sign.rb', line 21

def _sign_string(params, sort)
  params = _sort_params(params, sort)
  params.stringify_keys.collect do |s|
    unless s[0] == :notify_id
      "#{s[0]}=#{CGI.unescape(s[1])}"
    else
      "#{s[0]}=#{s[1]}"
    end
  end.join("&")
end

#_sort_params(params, sort) ⇒ Object

排序 sort true 排序 sort false 不排序 sort Array 按数组顺序排序



40
41
42
43
44
45
46
47
48
49
# File 'lib/jw_alipay/sign.rb', line 40

def _sort_params(params, sort)
  case
    when sort.is_a?(Array)
      then Hash[sort.map{ |key| [key, params[key]] }]
    when true
      then Hash[params.sort]
    else
      params
  end
end

#sign(params, sort = true) ⇒ Object



12
13
14
15
# File 'lib/jw_alipay/sign.rb', line 12

def sign(params, sort = true)
  sign_type, sign = "MD5", _md5_sign(params, sort)
  _query_string(params.merge(:sign => sign))
end

#verify_sign(params, sort = true, sign_type = nil) ⇒ Object



7
8
9
10
# File 'lib/jw_alipay/sign.rb', line 7

def verify_sign(params, sort = true, sign_type = nil)
  sign, sign_type = params.delete(:sign), params.delete(:sign_type) || sign_type || 'MD5'
  _md5_sign(params, sort) == sign.downcase
end