Method: UnionpayApp::Service.sign

Defined in:
lib/unionpay_app/service.rb

.sign(txtAmt, orderId) ⇒ Object

银联支付签名



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/unionpay_app/service.rb', line 10

def self.sign txtAmt, orderId
  union_params = {
    :version => "5.0.0",
    :encoding => "utf-8",
    :certId => UnionpayApp.cert_id,
    :txnType => '01',
    :txnSubType => "01",
    :bizType => "000201",
    :channelType => "08",
    :frontUrl   => UnionpayApp.front_url,
    :backUrl    => UnionpayApp.back_url,
    :accessType => "0",
    :merId      => UnionpayApp.mer_id,
    :orderId => orderId,  #商户订单号
    :txnTime => Time.now.strftime("%Y%m%d%H%M%S"),  #订单发送时间
    :txnAmt  => txtAmt, #以分为单位
    :currencyCode => '156',
    :signMethod => '01',
  }
  data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
  sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
  {time: union_params[:txnTime], sign: union_params.merge(signature: sign)}
end