alipay 0.15.2 → 0.16.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/CHANGELOG.md +4 -0
- data/doc/rsa_key_cn.md +41 -0
- data/lib/alipay/client.rb +8 -0
- data/lib/alipay/utils.rb +31 -0
- data/lib/alipay/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaf353d4e52fc0f91a43b6f46e80adf7f9d98b17ab0f1c67be502c3fce821fa3
|
4
|
+
data.tar.gz: '0788e20aa4336c4f5c343e6fcd0808394639c490b0ea0dc8eadfc22ad77f2483'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ed5ec0e18693fcc9832183b9bc47e09b2b32bd12b1d039ec06d42d9c3fa3ce8da9e99c37e600401bb97a4efd1f404c2da2cb25e73209b4fbdb73b37313235b3
|
7
|
+
data.tar.gz: 9381151fbccf366f5eeeda5f93a6ec9bb190918f11e39f0f11b4de6953531f40bf60711b103d56ab729ad235bea82e635c3a2efabc5e2b4dceabc6c658e72530
|
data/CHANGELOG.md
CHANGED
data/doc/rsa_key_cn.md
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
* [生成应用密钥](#生成应用密钥)
|
7
7
|
* [验证参数](#验证参数)
|
8
8
|
* [补充格式到支付宝公钥](#补充格式到支付宝公钥)
|
9
|
+
* [使用证书签名方式](#使用证书签名方式)
|
9
10
|
|
10
11
|
### 生成应用密钥
|
11
12
|
#### 在 Ruby 下生成 RSA2 密钥
|
@@ -62,3 +63,43 @@ pub_key.scan(/.{64}|.+$/).join("\n").insert(0, "-----BEGIN PUBLIC KEY-----\n").i
|
|
62
63
|
# => "-----BEGIN PUBLIC KEY-----\nMIIBI...\n-----END PUBLIC KEY-----\n"
|
63
64
|
```
|
64
65
|
|
66
|
+
# 使用证书签名方式
|
67
|
+
|
68
|
+
## 应用证书配置
|
69
|
+
按照官方文档进行新建应用配置证书签名 https://docs.open.alipay.com/291/twngcd/
|
70
|
+
|
71
|
+
配置完成后,可以得到 `xxx.com_私钥.txt alipayCertPublicKey_RSA2.crt appCertPublicKey_2019082600000001.crt alipayRootCert.crt` 四个文件。
|
72
|
+
|
73
|
+
### 应用私钥补充格式
|
74
|
+
```ruby
|
75
|
+
app_private_key = File.read('xxx.com_私钥.txt')
|
76
|
+
app_private_key = app_private_key.scan(/.{64}|.+$/).join("\n").insert(0, "-----BEGIN RSA PRIVATE KEY-----\n").insert(-1, "\n-----END RSA PRIVATE KEY-----\n")
|
77
|
+
```
|
78
|
+
### 处理应用阿里云公钥
|
79
|
+
```ruby
|
80
|
+
alipay_public_key = File.read('alipayCertPublicKey_RSA2.crt')
|
81
|
+
alipay_public_key = OpenSSL::X509::Certificate.new(alipay_public_key).public_key.to_s
|
82
|
+
```
|
83
|
+
### 得到应用公钥证书sn
|
84
|
+
```ruby
|
85
|
+
app_cert = File.read('appCertPublicKey_2019082600000001.crt')
|
86
|
+
app_cert_sn = Alipay::Utils.get_cert_sn(app_cert)
|
87
|
+
# => "28d1147972121b91734da59aa10f3c16"
|
88
|
+
```
|
89
|
+
### 得到支付宝根证书sn
|
90
|
+
```ruby
|
91
|
+
alipay_root_cert = File.read('alipayRootCert.crt')
|
92
|
+
alipay_root_cert_sn = Alipay::Utils.get_root_cert_sn(alipay_root_cert)
|
93
|
+
# => "28d1147972121b91734da59aa10f3c16_28d1147972121b91734da59aa10f3c16"
|
94
|
+
```
|
95
|
+
### 使用
|
96
|
+
```ruby
|
97
|
+
@alipay_client = Alipay::Client.new(
|
98
|
+
url: API_URL,
|
99
|
+
app_id: APP_ID,
|
100
|
+
app_private_key: app_private_key,
|
101
|
+
alipay_public_key: alipay_public_key,
|
102
|
+
app_cert_sn: app_cert_sn,
|
103
|
+
alipay_root_cert_sn: alipay_root_cert_sn
|
104
|
+
)
|
105
|
+
```
|
data/lib/alipay/client.rb
CHANGED
@@ -37,6 +37,8 @@ module Alipay
|
|
37
37
|
@format = options['format'] || 'json'
|
38
38
|
@charset = options['charset'] || 'UTF-8'
|
39
39
|
@sign_type = options['sign_type'] || 'RSA2'
|
40
|
+
@app_cert_sn = options['app_cert_sn']
|
41
|
+
@alipay_root_cert_sn = options['alipay_root_cert_sn']
|
40
42
|
end
|
41
43
|
|
42
44
|
# Generate a query string that use for APP SDK excute.
|
@@ -183,6 +185,12 @@ module Alipay
|
|
183
185
|
'version' => '1.0',
|
184
186
|
'timestamp' => Time.now.localtime('+08:00').strftime("%Y-%m-%d %H:%M:%S")
|
185
187
|
}.merge(::Alipay::Utils.stringify_keys(params))
|
188
|
+
if !@app_cert_sn.nil? && !@alipay_root_cert_sn.nil?
|
189
|
+
params = params.merge({
|
190
|
+
'app_cert_sn' => @app_cert_sn,
|
191
|
+
'alipay_root_cert_sn' => @alipay_root_cert_sn
|
192
|
+
})
|
193
|
+
end
|
186
194
|
params['sign'] = sign(params)
|
187
195
|
params
|
188
196
|
end
|
data/lib/alipay/utils.rb
CHANGED
@@ -15,5 +15,36 @@ module Alipay
|
|
15
15
|
batch_no = t.strftime('%Y%m%d%H%M%S') + t.nsec.to_s
|
16
16
|
batch_no.ljust(24, rand(10).to_s)
|
17
17
|
end
|
18
|
+
|
19
|
+
# get app_cert_sn
|
20
|
+
def self.get_cert_sn(str, match_algo = false)
|
21
|
+
return nil if str.nil?
|
22
|
+
certificate = OpenSSL::X509::Certificate.new(str)
|
23
|
+
if match_algo
|
24
|
+
begin
|
25
|
+
return unless certificate.public_key.is_a?(OpenSSL::PKey::RSA)
|
26
|
+
rescue => exception
|
27
|
+
return
|
28
|
+
end
|
29
|
+
end
|
30
|
+
issuer_arr = OpenSSL::X509::Name.new(certificate.issuer).to_a
|
31
|
+
issuer = issuer_arr.reverse.map { |item| item[0..1].join('=') }.join(',')
|
32
|
+
serial = OpenSSL::BN.new(certificate.serial).to_s
|
33
|
+
OpenSSL::Digest::MD5.hexdigest(issuer + serial)
|
34
|
+
end
|
35
|
+
|
36
|
+
# get alipay_root_cert_sn
|
37
|
+
def self.get_root_cert_sn(str)
|
38
|
+
return nil if str.nil?
|
39
|
+
arr = str.scan(/-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----/)
|
40
|
+
arr_sn = []
|
41
|
+
arr.each do |item|
|
42
|
+
sn = get_cert_sn(item, true)
|
43
|
+
unless sn.nil?
|
44
|
+
arr_sn.push(sn)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
arr_sn.join('_')
|
48
|
+
end
|
18
49
|
end
|
19
50
|
end
|
data/lib/alipay/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alipay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rei
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
|
-
rubygems_version: 3.0.
|
126
|
+
rubygems_version: 3.0.6
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: An unofficial simple alipay gem
|