alipay 0.15.2 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46b46f90c6340aeb58cf9de3a967423f390fa0444a63635f0548bace4019a61f
4
- data.tar.gz: b011da754fa7343f08785829906b33014807794184b130a0d413dfee3048e679
3
+ metadata.gz: eaf353d4e52fc0f91a43b6f46e80adf7f9d98b17ab0f1c67be502c3fce821fa3
4
+ data.tar.gz: '0788e20aa4336c4f5c343e6fcd0808394639c490b0ea0dc8eadfc22ad77f2483'
5
5
  SHA512:
6
- metadata.gz: '033712118c048e90a8e45189d4483fcef0c549c68c21182c2c09b3b72cefe9c32ee75b1645b39dc8f54346315a47fd8f17013f02e897f4399f1be6ccf1727f68'
7
- data.tar.gz: ed308742f277d296686254a6801269db4c33ea32fcc2f64b7df6bc778913332050426970638f5797639db9f45c8773d1c7476dcae0ab51fe20bf2e296e42537a
6
+ metadata.gz: 8ed5ec0e18693fcc9832183b9bc47e09b2b32bd12b1d039ec06d42d9c3fa3ce8da9e99c37e600401bb97a4efd1f404c2da2cb25e73209b4fbdb73b37313235b3
7
+ data.tar.gz: 9381151fbccf366f5eeeda5f93a6ec9bb190918f11e39f0f11b4de6953531f40bf60711b103d56ab729ad235bea82e635c3a2efabc5e2b4dceabc6c658e72530
@@ -1,5 +1,9 @@
1
1
  ## master
2
2
 
3
+ ## v0.16.0 (2020-05-15)
4
+
5
+ - Support certificate signature, thanks @moohao #92
6
+
3
7
  ## v0.15.2 (2019-08-02)
4
8
 
5
9
  - page_execute_form invalid-signature charset, thanks @lingceng #90
@@ -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
+ ```
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Alipay
2
- VERSION = "0.15.2"
2
+ VERSION = "0.16.0"
3
3
  end
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.15.2
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: 2019-08-02 00:00:00.000000000 Z
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.3
126
+ rubygems_version: 3.0.6
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: An unofficial simple alipay gem