unionpay_app 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5a55cbcaae7b52d07dc33c0d124c2210c13516d
4
- data.tar.gz: 168a8a8128e34a304671aae79bebd0b9c678fde5
3
+ metadata.gz: b10b89d7204a3daba715e575b9881b05c495d273
4
+ data.tar.gz: a8d9d47546eedf17656b9c54c52ca384ba04862e
5
5
  SHA512:
6
- metadata.gz: df0d6e827780b8bb96b633d8602b0d76f9c44f47446a3b5daf7737147679bbc24c71295606e999a462af235173b406a20ccd8da1ec0c2c200b0b0b94a7b6ccc5
7
- data.tar.gz: ac49e9a1385de89b9d36be14526ad2f4e9a0e13570f44d287d3867cc4871ee2d76dedd80040437f0af2ff48cdf58c95c07cec938f882cd41354ead0b5c3160b9
6
+ metadata.gz: 11e6cc7e82ead671a331135f01403f05a3c8ba0723bd6a476f3bae9fde4fbae22f4862c249f4cbe7c8536ef977efbb1f87722704d8d0bc1d347d7e5fb5c66dfc
7
+ data.tar.gz: 6f22486904d9f620e7ac03c63cc99546c3e751325a887cb24d77cf73fd407f349762ea5cea6e69735fc8278aa9d8c92ca25786ba403cee9f056a6ad4e76387d7
data/README.md CHANGED
@@ -1,18 +1,23 @@
1
- <<<<<<< HEAD
1
+
2
2
  该gem是一个银联手机控件支付接口
3
+ =====
3
4
 
4
5
  Installation
6
+ ------
5
7
 
6
- Add this line to your application's Gemfile:
7
-
8
- gem 'unionpay_app', '~> 0.6.0'
9
- or development version
10
-
8
+ ######Add this line to your application's Gemfile:
9
+ ```ruby
10
+ gem 'unionpay_app', '~> 0.7.0'
11
+ ```
12
+ ######or development version
13
+ ```ruby
11
14
  $ bundle
12
- Usage
15
+ ```
16
+ ######Usage
13
17
 
14
18
  Config
15
-
19
+ ------
20
+ ```ruby
16
21
  UnionpayApp.front_url = Settings.union.frontUrl
17
22
  UnionpayApp.back_url = Settings.union.backUrl
18
23
  UnionpayApp.mer_id = Settings.union.merId
@@ -20,30 +25,30 @@ UnionpayApp.uri = Settings.union.uri
20
25
  UnionpayApp.private_key = Settings.union.private_key
21
26
  UnionpayApp.cer = Settings.union.cer
22
27
  UnionpayApp.cert_id = Settings.union.certId
28
+ ```
23
29
  Generate payment url for web
30
+ ------
31
+
32
+ * 1. 银联签名 返回hash {time: "交易时间", sign: "post给银联的hash"}
33
+ ```ruby
34
+ UnionpayApp::Service.sign txtAmt, orderId
35
+ #如下使用
36
+ options = UnionpayApp::Service.sign("100", "20150415122801272")
37
+ ```
24
38
 
25
- 1. #银联签名 返回hash {time: "交易时间", sign: "post给银联的hash"}
26
- UnionpayApp::Service.sign txtAmt, orderId
27
- params = UnionpayApp::Service.sign("100", "20150415122801272")
28
- 2. #发送给银联post请求 换取tn值
29
- UnionpayApp::Service.post UnionpayApp::Service.sign(txtAmt, orderId)
30
- UnionpayApp::Service.post(params)
31
- 3. #银联验签:当请求银联获取tn后,或者请求银联查询交易后,或者当交易完成后,银联通过backUrl、frontUrl返回你结果时要进行验签
32
- UnionpayApp::Service.verify params
33
- 4. #查询交易是否成功 传入参数order_id
34
- UnionpayApp::Service.query order_id, UnionpayApp::Service.sign(txtAmt, orderId)[:time]
35
- UnionpayApp::Service.query "20150415122801272", params[:time]
36
- =======
37
- 该gem是一个银联手机控件支付接口
38
- 1. #银联签名 返回hash {time: "交易时间", sign: "post给银联的hash"}
39
- UnionpayApp::Service.sign txtAmt, orderId
40
- params = UnionpayApp::Service.sign("100", "20150415122801272")
41
- 2. #发送给银联post请求 换取tn值
42
- UnionpayApp::Service.post UnionpayApp::Service.sign(txtAmt, orderId)[:sign]
43
- UnionpayApp::Service.post(params)
44
- 3. #银联验签:当请求银联获取tn后,或者请求银联查询交易后,或者当交易完成后,银联通过backUrl、frontUrl返回你结果时要进行验签
45
- UnionpayApp::Service.verify params
46
- 4. #查询交易是否成功 传入参数order_id
47
- UnionpayApp::Service.query order_id, UnionpayApp::Service.sign(txtAmt, orderId)[:time]
48
- UnionpayApp::Service.query "20150415122801272", params[:time]
49
- >>>>>>> 760bcdda0389701cee33cbedba2d424b7ce36d7d
39
+ * 2. 发送给银联post请求 换取tn值
40
+ ```ruby
41
+ UnionpayApp::Service.post UnionpayApp::Service.sign(txtAmt, orderId)
42
+ #如下使用 会返回tn值
43
+ UnionpayApp::Service.post(options)
44
+ ```
45
+ * 3.银联验签:当请求银联获取tn后,或者请求银联查询交易后,或者当交易完成后,银联通过backUrl、frontUrl返回你结果时要进行验签
46
+ ```ruby
47
+ UnionpayApp::Service.verify options
48
+ ```
49
+ * 4. 查询交易是否成功 传入参数order_id
50
+ ```ruby
51
+ UnionpayApp::Service.query order_id, UnionpayApp::Service.sign(txtAmt, orderId)[:time]
52
+ # 不要再做一次签名 取之前的options
53
+ UnionpayApp::Service.query "20150415122801272", options[:time]
54
+ ```
@@ -6,92 +6,92 @@ require 'typhoeus'
6
6
 
7
7
  module UnionpayApp
8
8
  module Service
9
- #银联支付签名
10
- def self.sign txtAmt, orderId
11
- union_params = {
12
- :version => "5.0.0",
13
- :encoding => "utf-8",
14
- :certId => UnionpayApp.cert_id,
15
- :txnType => '01',
16
- :txnSubType => "01",
17
- :bizType => "000201",
18
- :channelType => "08",
19
- :frontUrl => UnionpayApp.front_url,
20
- :backUrl => UnionpayApp.back_url,
21
- :accessType => "0",
22
- :merId => UnionpayApp.mer_id,
23
- :orderId => orderId, #商户订单号
24
- :txnTime => Time.now.strftime("%Y%m%d%H%M%S"), #订单发送时间
25
- :txnAmt => txtAmt, #以分为单位
26
- :currencyCode => '156',
27
- :signMethod => '01',
28
- }
29
- data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
30
- sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
31
- {time: union_params[:txnTime], sign: union_params.merge(signature: sign)}
32
- end
9
+ #银联支付签名
10
+ def self.sign txtAmt, orderId
11
+ union_params = {
12
+ :version => "5.0.0",
13
+ :encoding => "utf-8",
14
+ :certId => UnionpayApp.cert_id,
15
+ :txnType => '01',
16
+ :txnSubType => "01",
17
+ :bizType => "000201",
18
+ :channelType => "08",
19
+ :frontUrl => UnionpayApp.front_url,
20
+ :backUrl => UnionpayApp.back_url,
21
+ :accessType => "0",
22
+ :merId => UnionpayApp.mer_id,
23
+ :orderId => orderId, #商户订单号
24
+ :txnTime => Time.now.strftime("%Y%m%d%H%M%S"), #订单发送时间
25
+ :txnAmt => txtAmt, #以分为单位
26
+ :currencyCode => '156',
27
+ :signMethod => '01',
28
+ }
29
+ data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
30
+ sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
31
+ {time: union_params[:txnTime], sign: union_params.merge(signature: sign)}
32
+ end
33
33
 
34
- def self.post union_params
35
- request = Typhoeus::Request.new(UnionpayApp.uri, method: :post, params: union_params[:sign], ssl_verifypeer: false, headers: {'Content-Type' =>'application/x-www-form-urlencoded'} )
34
+ def self.post union_params
35
+ request = Typhoeus::Request.new(UnionpayApp.uri, method: :post, params: union_params[:sign], ssl_verifypeer: false, headers: {'Content-Type' =>'application/x-www-form-urlencoded'} )
36
36
  request.run
37
37
  if request.response.success?
38
- tn = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['tn']
38
+ tn = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['tn']
39
39
  else
40
- tn = ""
40
+ tn = ""
41
41
  end
42
- end
42
+ end
43
43
 
44
- #银联支付验签
45
- def self.verify params
46
- if unionpay_get_public_key_by_cert_id params['certId']
47
- public_key = unionpay_get_public_key_by_cert_id params['certId']
48
- signature_str = params['signature']
49
- p = params.reject{|k, v| k == "signature"}.sort.map{|key, value| "#{key}=#{value}" }.join('&')
50
- signature = Base64.decode64(signature_str)
51
- data = Digest::SHA1.hexdigest(p)
52
- key = OpenSSL::PKey::RSA.new public_key
53
- digest = OpenSSL::Digest::SHA256.new
54
- key.verify digest, signature, data
55
- else
56
- false
57
- end
58
- end
44
+ #银联支付验签
45
+ def self.verify params
46
+ if get_public_key_by_cert_id params['certId']
47
+ public_key = get_public_key_by_cert_id params['certId']
48
+ signature_str = params['signature']
49
+ p = params.reject{|k, v| k == "signature"}.sort.map{|key, value| "#{key}=#{value}" }.join('&')
50
+ signature = Base64.decode64(signature_str)
51
+ data = Digest::SHA1.hexdigest(p)
52
+ key = OpenSSL::PKey::RSA.new public_key
53
+ digest = OpenSSL::Digest::SHA256.new
54
+ key.verify digest, signature, data
55
+ else
56
+ false
57
+ end
58
+ end
59
59
 
60
- # 银联支付 根据证书id返回公钥
61
- def self.get_public_key_by_cert_id cert_id
62
- certificate = OpenSSL::X509::Certificate.new(UnionpayApp.cer) #读取cer文件
63
- certificate.serial.to_s == cert_id ? certificate.public_key.to_s : nil #php 返回的直接是cer文件 UnionpayApp.cer
64
- end
60
+ # 银联支付 根据证书id返回公钥
61
+ def get_public_key_by_cert_id cert_id
62
+ certificate = OpenSSL::X509::Certificate.new(UnionpayApp.cer) #读取cer文件
63
+ certificate.serial.to_s == cert_id ? certificate.public_key.to_s : nil #php 返回的直接是cer文件 UnionpayApp.cer
64
+ end
65
65
 
66
- def self.query order_id, txnTime
67
- union_params = {
68
- :version => '5.0.0', #版本号
69
- :encoding => 'utf-8', #编码方式
70
- :certId => UnionpayApp.cert_id, #证书ID
71
- :signMethod => '01', #签名方法
72
- :txnType => '00', #交易类型
73
- :txnSubType => '00', #交易子类
74
- :bizType => '000000', #业务类型
75
- :accessType => '0', #接入类型
76
- :channelType => '07', #渠道类型
77
- :orderId => order_id, #请修改被查询的交易的订单号
78
- :merId => UnionpayApp.mer_id, #商户代码,请修改为自己的商户号
79
- :txnTime => txnTime, #请修改被查询的交易的订单发送时间
80
- }
81
- data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
82
- sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
83
- request = Typhoeus::Request.new(UnionpayApp.query_uri, method: :post, params: union_params.merge(signature: sign), ssl_verifypeer: false, headers: {'Content-Type' =>'application/x-www-form-urlencoded'} )
84
- request.run
85
- if request.response.success?
86
- code = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['origRespCode']
87
- elsif request.response.timed_out?
88
- code = "got a time out"
89
- elsif request.response.code == 0
90
- code = request.response.return_message
91
- else
92
- code = request.response.code.to_s
93
- end
94
- end
66
+ def self.query order_id, txnTime
67
+ union_params = {
68
+ :version => '5.0.0', #版本号
69
+ :encoding => 'utf-8', #编码方式
70
+ :certId => UnionpayApp.cert_id, #证书ID
71
+ :signMethod => '01', #签名方法
72
+ :txnType => '00', #交易类型
73
+ :txnSubType => '00', #交易子类
74
+ :bizType => '000000', #业务类型
75
+ :accessType => '0', #接入类型
76
+ :channelType => '07', #渠道类型
77
+ :orderId => order_id, #请修改被查询的交易的订单号
78
+ :merId => UnionpayApp.mer_id, #商户代码,请修改为自己的商户号
79
+ :txnTime => txnTime, #请修改被查询的交易的订单发送时间
80
+ }
81
+ data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
82
+ sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
83
+ request = Typhoeus::Request.new(UnionpayApp.query_uri, method: :post, params: union_params.merge(signature: sign), ssl_verifypeer: false, headers: {'Content-Type' =>'application/x-www-form-urlencoded'} )
84
+ request.run
85
+ if request.response.success?
86
+ code = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['origRespCode']
87
+ elsif request.response.timed_out?
88
+ code = "got a time out"
89
+ elsif request.response.code == 0
90
+ code = request.response.return_message
91
+ else
92
+ code = request.response.code.to_s
93
+ end
94
+ end
95
95
 
96
96
  end
97
97
  end
@@ -1,3 +1,3 @@
1
1
  module UnionpayApp
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unionpay_app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yohansun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-16 00:00:00.000000000 Z
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  version: '0'
87
87
  requirements: []
88
88
  rubyforge_project:
89
- rubygems_version: 2.2.2
89
+ rubygems_version: 2.4.6
90
90
  signing_key:
91
91
  specification_version: 4
92
92
  summary: An unofficial simple unionpay_app gem