class YlPay::Utils

Constants

BACK_SIGN_FIELD
NOTIFY_SIGN_FIELD
PARAM_BASE64_FIELD
PARAM_SIGN_FIELD

Public Class Methods

back_sign(params) click to toggle source
# File lib/yl_pay/utils.rb, line 17
def self.back_sign(params)
  hash_params = params_to_hash(params, BACK_SIGN_FIELD)
  sign_uri hash_params
end
notify_sign(params) click to toggle source
# File lib/yl_pay/utils.rb, line 22
def self.notify_sign(params)
  hash_params = params_to_hash(params, NOTIFY_SIGN_FIELD)
  sign_uri hash_params
end
param_base64_field(params) click to toggle source
# File lib/yl_pay/utils.rb, line 41
def self.param_base64_field(params)
  params.collect do |param|
    if PARAM_BASE64_FIELD.include? param[0]
      param[1] = Base64.strict_encode64(param[1])
    end
    param
  end
end
param_sign(params) click to toggle source
# File lib/yl_pay/utils.rb, line 12
def self.param_sign(params)
  hash_params = params_sign_to_hash(params, PARAM_SIGN_FIELD)
  sign_uri hash_params
end
params_sign_to_hash(params, target_params) click to toggle source

针对param_sign hash的键大写,里面获取值参数小写 params[:A: 'b']

# File lib/yl_pay/utils.rb, line 61
def self.params_sign_to_hash(params, target_params)
  hash = {}
  target_params.collect do |k|
    k1 = k.camelize
    hash.merge!("#{k1}": params[k.to_sym])
  end
  hash
end
params_to_hash(params, target_params) click to toggle source

hash的键需要大写, 里面获取值参数也是大写 params['A': 'b']

# File lib/yl_pay/utils.rb, line 51
def self.params_to_hash(params, target_params)
  target_params.collect!(&:camelize)
  hash = {}
  target_params.collect do |k|
    hash.merge!("#{k}": params[k])
  end
  hash
end
sign_uri(hash) click to toggle source
# File lib/yl_pay/utils.rb, line 27
def self.sign_uri(hash)
  string_to_uri(stringify_keys(hash))
end
string_to_uri(params) click to toggle source
# File lib/yl_pay/utils.rb, line 70
def self.string_to_uri(params)
  params.map{|entry| entry * '='} * '&'
end
stringify_keys(hash) click to toggle source
# File lib/yl_pay/utils.rb, line 74
def self.stringify_keys(hash)
  new_hash = {}
  hash.each do |key, value|
    new_hash[(key.to_s rescue key) || key] = value.to_s
  end
  new_hash
end
uri_params(params) click to toggle source

提交参数中包含中文的需要做base64转码, 对url进行urlencode处理

# File lib/yl_pay/utils.rb, line 32
def self.uri_params(params)
  new_params = param_base64_field(params)
  new_params = Hash[new_params]
  new_params[:notify_url] = CGI.escape(new_params[:notify_url]) if new_params.key?(:notify_url)
  new_params[:return_url] = CGI.escape(new_params[:return_url]) if new_params.key?(:return_url)
  param_sign new_params
end