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 +4 -4
- data/README.md +38 -33
- data/lib/unionpay_app/service.rb +78 -78
- data/lib/unionpay_app/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b10b89d7204a3daba715e575b9881b05c495d273
|
4
|
+
data.tar.gz: a8d9d47546eedf17656b9c54c52ca384ba04862e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11e6cc7e82ead671a331135f01403f05a3c8ba0723bd6a476f3bae9fde4fbae22f4862c249f4cbe7c8536ef977efbb1f87722704d8d0bc1d347d7e5fb5c66dfc
|
7
|
+
data.tar.gz: 6f22486904d9f620e7ac03c63cc99546c3e751325a887cb24d77cf73fd407f349762ea5cea6e69735fc8278aa9d8c92ca25786ba403cee9f056a6ad4e76387d7
|
data/README.md
CHANGED
@@ -1,18 +1,23 @@
|
|
1
|
-
|
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.
|
9
|
-
|
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
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
3
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
+
```
|
data/lib/unionpay_app/service.rb
CHANGED
@@ -6,92 +6,92 @@ require 'typhoeus'
|
|
6
6
|
|
7
7
|
module UnionpayApp
|
8
8
|
module Service
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
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
|
-
|
38
|
+
tn = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['tn']
|
39
39
|
else
|
40
|
-
|
40
|
+
tn = ""
|
41
41
|
end
|
42
|
-
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
data/lib/unionpay_app/version.rb
CHANGED
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.
|
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-
|
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.
|
89
|
+
rubygems_version: 2.4.6
|
90
90
|
signing_key:
|
91
91
|
specification_version: 4
|
92
92
|
summary: An unofficial simple unionpay_app gem
|