Module: WechatPayment::Sign

Defined in:
lib/wechat_payment/sign.rb

Constant Summary collapse

SIGN_TYPE_MD5 =
'MD5'
SIGN_TYPE_HMAC_SHA256 =
'HMAC-SHA256'

Class Method Summary collapse

Class Method Details

.generate(params, sign_type = SIGN_TYPE_MD5) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wechat_payment/sign.rb', line 9

def self.generate(params, sign_type = SIGN_TYPE_MD5)
  key = params.delete(:key)

  new_key = params["key"] #after
  key = params.delete("key") if params["key"] #after

  query = params.sort.map do |k, v|
    "#{k}=#{v}" if v.to_s != ''
  end.compact.join('&')

  string_sign_temp = "#{query}&key=#{key || new_key || WechatPayment.key}" #after

  if sign_type == SIGN_TYPE_MD5
    Digest::MD5.hexdigest(string_sign_temp).upcase
  elsif sign_type == SIGN_TYPE_HMAC_SHA256
    OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, string_sign_temp).upcase
  else
    warn("WxPay Warn: unknown sign_type : #{sign_type}")
  end
end

.verify?(params, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/wechat_payment/sign.rb', line 30

def self.verify?(params, options = {})
  params = params.dup
  params["appid"] = options[:appid] if options[:appid]
  params["mch_id"] = options[:mch_id]  if options[:mch_id]
  params["key"] = options[:key] if options[:key]

  sign = params.delete('sign') || params.delete(:sign)

  generate(params, options[:sign_type] || SIGN_TYPE_MD5) == sign
end