alipay 0.15.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +39 -13
- data/doc/quick_start_cn.md +532 -0
- data/doc/quick_start_en.md +571 -0
- data/doc/rsa_key_cn.md +64 -0
- data/doc/rsa_key_en.md +77 -0
- data/lib/alipay/client.rb +8 -8
- data/lib/alipay/mobile/service.rb +2 -2
- data/lib/alipay/service.rb +10 -10
- data/lib/alipay/version.rb +1 -1
- metadata +6 -2
data/doc/rsa_key_cn.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# 配合支付宝使用RSA密钥
|
2
|
+
|
3
|
+
[English](rsa_key_en.md)
|
4
|
+
|
5
|
+
## 导航
|
6
|
+
* [生成应用密钥](#生成应用密钥)
|
7
|
+
* [验证参数](#验证参数)
|
8
|
+
* [补充格式到支付宝公钥](#补充格式到支付宝公钥)
|
9
|
+
|
10
|
+
### 生成应用密钥
|
11
|
+
#### 在 Ruby 下生成 RSA2 密钥
|
12
|
+
这个会示范在 Ruby 环境下生成RSA2密钥。支付宝推荐使用RSA2密钥来验证。
|
13
|
+
```ruby
|
14
|
+
require 'openssl'
|
15
|
+
|
16
|
+
@app_key = OpenSSL::PKey::RSA.new(2048)
|
17
|
+
```
|
18
|
+
#### 保存密钥
|
19
|
+
你可以使用以下任一方式来保存你的私钥和公钥
|
20
|
+
|
21
|
+
将私钥保存到字符串
|
22
|
+
```ruby
|
23
|
+
app_private_key = @app_key.to_s
|
24
|
+
```
|
25
|
+
|
26
|
+
将私钥保存为证书文件
|
27
|
+
```ruby
|
28
|
+
open 'private_key.pem', 'w' do |io| io.write @app_key.to_pem end
|
29
|
+
```
|
30
|
+
|
31
|
+
将公钥保存到字符串
|
32
|
+
```ruby
|
33
|
+
app_public_key = @app_key.public_key.to_s
|
34
|
+
```
|
35
|
+
将公钥保存为证书文件
|
36
|
+
```ruby
|
37
|
+
open 'public_key.pem', 'w' do |io| io.write @app_key.public_key.to_pem end
|
38
|
+
```
|
39
|
+
|
40
|
+
#### 提取钥匙内容
|
41
|
+
你需要给支付宝提供你所先前生成的公钥内容。但是提供给支付宝之前需要对 Ruby 生成的公钥进行格式清理。清理完后,将清理好的公钥内容提供给支付宝即可。
|
42
|
+
```ruby
|
43
|
+
key_content = app_public_key.gsub(/(-----BEGIN PUBLIC KEY-----)|(-----END PUBLIC KEY-----)|(\n)/, "")
|
44
|
+
puts key_content
|
45
|
+
# => 'MII0ey6QDZLB69i0e5Q0....'
|
46
|
+
```
|
47
|
+
|
48
|
+
### 验证参数
|
49
|
+
当你提交你的应用公钥给支付宝后,有一个可选的步骤是验证你的公钥的有效性。支付宝会提供一个参数让你使用你的私钥签名。把签名结果粘贴到支付宝后,支付宝会使用你上传的公钥来解密验证。
|
50
|
+
```ruby
|
51
|
+
# validate params "a=123"
|
52
|
+
Base64.strict_encode64(@app_key.sign('sha256', "a=123"))
|
53
|
+
# => 'FokDu5uwgmNG2O/cb0QYD....'
|
54
|
+
```
|
55
|
+
|
56
|
+
### 补充格式到支付宝公钥
|
57
|
+
你上传你的公钥后,支付宝会提供他们的公钥给你的应用来验证支付宝回调的内容有效性。但是他们提供公钥不带格式,所以 Ruby 的 OpneSSL 可能无法识别。将格式补充到支付宝所提供的公钥,你可以使用以下运行脚本。
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
pub_key = "MIIBI...HpwIDAQAB"
|
61
|
+
pub_key.scan(/.{64}|.+$/).join("\n").insert(0, "-----BEGIN PUBLIC KEY-----\n").insert(-1, "\n-----END PUBLIC KEY-----\n")
|
62
|
+
# => "-----BEGIN PUBLIC KEY-----\nMIIBI...\n-----END PUBLIC KEY-----\n"
|
63
|
+
```
|
64
|
+
|
data/doc/rsa_key_en.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# RSA Key for Alipay
|
2
|
+
|
3
|
+
[中文](rsa_key_cn.md)
|
4
|
+
|
5
|
+
## Table of Contents
|
6
|
+
|
7
|
+
* [Generate Application Key](#generate-application-key)
|
8
|
+
* [Generate RSA2 Keypair in Ruby](#generate-rsa2-keypair-in-ruby)
|
9
|
+
* [Saving Key](#saving-key)
|
10
|
+
* [Extract Key Content](#extract-key-content)
|
11
|
+
* [Signing Parameters](#signing-parameters)
|
12
|
+
* [Formatting the Public Key from Alipay](#formatting-the-public-key-from-alipay)
|
13
|
+
|
14
|
+
### Generate Application Key
|
15
|
+
#### Generate RSA2 Keypair in Ruby
|
16
|
+
This example creates a 2048 bits RSA2 key. It is recommended by Alipay that
|
17
|
+
you use a RSA2 key.
|
18
|
+
```ruby
|
19
|
+
require 'openssl'
|
20
|
+
|
21
|
+
@app_key = OpenSSL::PKey::RSA.new(2048)
|
22
|
+
```
|
23
|
+
#### Saving Key
|
24
|
+
You can save your private and public key as any of two formats. As long as it can be loaded into the program.
|
25
|
+
|
26
|
+
Saving Private Key to String
|
27
|
+
```ruby
|
28
|
+
app_private_key = @app_key.to_s
|
29
|
+
```
|
30
|
+
|
31
|
+
Saving Private Key to File
|
32
|
+
```ruby
|
33
|
+
open 'private_key.pem', 'w' do |io| io.write @app_key.to_pem end
|
34
|
+
```
|
35
|
+
|
36
|
+
Saving Public Key to String
|
37
|
+
```ruby
|
38
|
+
app_public_key = @app_key.public_key.to_s
|
39
|
+
```
|
40
|
+
|
41
|
+
Saving Public Key to File
|
42
|
+
```ruby
|
43
|
+
open 'public_key.pem', 'w' do |io| io.write @app_key.public_key.to_pem end
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Extract Key Content
|
47
|
+
You will need to submit the application public key that you just created
|
48
|
+
to Alipay. However, you will need to strip the header, footer, and new line
|
49
|
+
characters from the key and just submit the key content to Alipay.
|
50
|
+
```ruby
|
51
|
+
key_content = app_public_key.gsub(/(-----BEGIN PUBLIC KEY-----)|(-----END PUBLIC KEY-----)|(\n)/, "")
|
52
|
+
puts key_content
|
53
|
+
# => 'MII0ey6QDZLB69i0e5Q0....'
|
54
|
+
```
|
55
|
+
|
56
|
+
### Signing Parameters
|
57
|
+
After you submit your application's public key to Alipay. There is an optional
|
58
|
+
step to validate the public key that you just uploaded by signing a parameter
|
59
|
+
provided by Alipay.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# validate params "a=123"
|
63
|
+
Base64.strict_encode64(@app_key.sign('sha256', "a=123"))
|
64
|
+
# => 'FokDu5uwgmNG2O/cb0QYD....'
|
65
|
+
```
|
66
|
+
|
67
|
+
### Formatting the Public Key from Alipay
|
68
|
+
The public key from Alipay does not contain any formatting. Ruby's OpenSSL
|
69
|
+
library cannot import/read the public key without proper formatting. To add
|
70
|
+
formatting back, run the following script.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
pub_key = "MIIBI...HpwIDAQAB"
|
74
|
+
pub_key.scan(/.{64}|.+$/).join("\n").insert(0, "-----BEGIN PUBLIC KEY-----\n").insert(-1, "\n-----END PUBLIC KEY-----\n")
|
75
|
+
# => "-----BEGIN PUBLIC KEY-----\nMIIBI...\n-----END PUBLIC KEY-----\n"
|
76
|
+
```
|
77
|
+
|
data/lib/alipay/client.rb
CHANGED
@@ -44,13 +44,13 @@ module Alipay
|
|
44
44
|
# Example:
|
45
45
|
#
|
46
46
|
# alipay_client.sdk_execute(
|
47
|
-
# method: 'alipay.trade.
|
47
|
+
# method: 'alipay.trade.app.pay',
|
48
48
|
# biz_content: {
|
49
49
|
# out_trade_no: '20160401000000',
|
50
50
|
# product_code: 'QUICK_MSECURITY_PAY',
|
51
51
|
# total_amount: '0.01',
|
52
52
|
# subject: 'test'
|
53
|
-
# }.to_json,
|
53
|
+
# }.to_json(ascii_only: true),
|
54
54
|
# timestamp: '2016-04-01 00:00:00'
|
55
55
|
# )
|
56
56
|
# # => 'app_id=2016000000000000&charset=utf-8&sig....'
|
@@ -64,14 +64,14 @@ module Alipay
|
|
64
64
|
#
|
65
65
|
# Example:
|
66
66
|
#
|
67
|
-
#
|
67
|
+
# alipay_client.page_execute_url(
|
68
68
|
# method: 'alipay.trade.page.pay',
|
69
69
|
# biz_content: {
|
70
70
|
# out_trade_no: '20160401000000',
|
71
71
|
# product_code: 'FAST_INSTANT_TRADE_PAY',
|
72
72
|
# total_amount: '0.01',
|
73
73
|
# subject: 'test'
|
74
|
-
# }.to_json,
|
74
|
+
# }.to_json(ascii_only: true),
|
75
75
|
# timestamp: '2016-04-01 00:00:00'
|
76
76
|
# )
|
77
77
|
# # => 'https://openapi.alipaydev.com/gateway.do?app_id=2016...'
|
@@ -88,14 +88,14 @@ module Alipay
|
|
88
88
|
#
|
89
89
|
# Example:
|
90
90
|
#
|
91
|
-
#
|
91
|
+
# alipay_client.page_execute_form(
|
92
92
|
# method: 'alipay.trade.page.pay',
|
93
93
|
# biz_content: {
|
94
94
|
# out_trade_no: '20160401000000',
|
95
95
|
# product_code: 'FAST_INSTANT_TRADE_PAY',
|
96
96
|
# total_amount: '0.01',
|
97
97
|
# subject: 'test'
|
98
|
-
# }.to_json,
|
98
|
+
# }.to_json(ascii_only: true),
|
99
99
|
# timestamp: '2016-04-01 00:00:00'
|
100
100
|
# )
|
101
101
|
# # => '<form id='alipaysubmit' name='alipaysubmit' action=...'
|
@@ -115,12 +115,12 @@ module Alipay
|
|
115
115
|
#
|
116
116
|
# Example:
|
117
117
|
#
|
118
|
-
#
|
118
|
+
# alipay_client.execute(
|
119
119
|
# method: 'alipay.data.dataservice.bill.downloadurl.query',
|
120
120
|
# biz_content: {
|
121
121
|
# bill_type: 'trade',
|
122
122
|
# bill_date: '2016-04-01'
|
123
|
-
# }.to_json
|
123
|
+
# }.to_json(ascii_only: true)
|
124
124
|
# )
|
125
125
|
# # => '{ "alipay_data_dataservice_bill_downloadurl_query_response":{...'
|
126
126
|
def execute(params)
|
@@ -12,8 +12,8 @@ module Alipay
|
|
12
12
|
params = {
|
13
13
|
'service' => 'mobile.securitypay.pay',
|
14
14
|
'_input_charset' => 'utf-8',
|
15
|
-
'partner' => options[:pid] || Alipay.pid,
|
16
|
-
'seller_id' => options[:pid] || Alipay.pid,
|
15
|
+
'partner' => options[:partner] || options[:pid] || Alipay.pid,
|
16
|
+
'seller_id' => options[:seller_id] || options[:pid] || Alipay.pid,
|
17
17
|
'payment_type' => '1'
|
18
18
|
}.merge(params)
|
19
19
|
|
data/lib/alipay/service.rb
CHANGED
@@ -10,8 +10,8 @@ module Alipay
|
|
10
10
|
params = {
|
11
11
|
'service' => 'create_partner_trade_by_buyer',
|
12
12
|
'_input_charset' => 'utf-8',
|
13
|
-
'partner' => options[:pid] || Alipay.pid,
|
14
|
-
'seller_id' => options[:pid] || Alipay.pid,
|
13
|
+
'partner' => options[:partner] || options[:pid] || Alipay.pid,
|
14
|
+
'seller_id' => options[:seller_id] || options[:pid] || Alipay.pid,
|
15
15
|
'payment_type' => '1'
|
16
16
|
}.merge(params)
|
17
17
|
|
@@ -26,8 +26,8 @@ module Alipay
|
|
26
26
|
params = {
|
27
27
|
'service' => 'trade_create_by_buyer',
|
28
28
|
'_input_charset' => 'utf-8',
|
29
|
-
'partner' => options[:pid] || Alipay.pid,
|
30
|
-
'seller_id' => options[:pid] || Alipay.pid,
|
29
|
+
'partner' => options[:partner] || options[:pid] || Alipay.pid,
|
30
|
+
'seller_id' => options[:seller_id] || options[:pid] || Alipay.pid,
|
31
31
|
'payment_type' => '1'
|
32
32
|
}.merge(params)
|
33
33
|
|
@@ -46,8 +46,8 @@ module Alipay
|
|
46
46
|
params = {
|
47
47
|
'service' => 'create_direct_pay_by_user',
|
48
48
|
'_input_charset' => 'utf-8',
|
49
|
-
'partner' => options[:pid] || Alipay.pid,
|
50
|
-
'seller_id' => options[:pid] || Alipay.pid,
|
49
|
+
'partner' => options[:partner] || options[:pid] || Alipay.pid,
|
50
|
+
'seller_id' => options[:seller_id] || options[:pid] || Alipay.pid,
|
51
51
|
'payment_type' => '1'
|
52
52
|
}.merge(params)
|
53
53
|
|
@@ -62,8 +62,8 @@ module Alipay
|
|
62
62
|
params = {
|
63
63
|
'service' => 'alipay.wap.create.direct.pay.by.user',
|
64
64
|
'_input_charset' => 'utf-8',
|
65
|
-
'partner' => options[:pid] || Alipay.pid,
|
66
|
-
'seller_id' => options[:pid] || Alipay.pid,
|
65
|
+
'partner' => options[:partner] || options[:pid] || Alipay.pid,
|
66
|
+
'seller_id' => options[:seller_id] || options[:pid] || Alipay.pid,
|
67
67
|
'payment_type' => '1'
|
68
68
|
}.merge(params)
|
69
69
|
|
@@ -202,8 +202,8 @@ module Alipay
|
|
202
202
|
params = {
|
203
203
|
'service' => 'create_forex_trade_wap',
|
204
204
|
'_input_charset' => 'utf-8',
|
205
|
-
'partner' => options[:pid] || Alipay.pid,
|
206
|
-
'seller_id' => options[:pid] || Alipay.pid
|
205
|
+
'partner' => options[:partner] || options[:pid] || Alipay.pid,
|
206
|
+
'seller_id' => options[:seller_id] || options[:pid] || Alipay.pid,
|
207
207
|
}.merge(params)
|
208
208
|
|
209
209
|
request_uri(params, options).to_s
|
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.15.
|
4
|
+
version: 0.15.1
|
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: 2018-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,6 +84,10 @@ files:
|
|
84
84
|
- bin/console
|
85
85
|
- config.yml.example
|
86
86
|
- doc/legacy_api.md
|
87
|
+
- doc/quick_start_cn.md
|
88
|
+
- doc/quick_start_en.md
|
89
|
+
- doc/rsa_key_cn.md
|
90
|
+
- doc/rsa_key_en.md
|
87
91
|
- lib/alipay.rb
|
88
92
|
- lib/alipay/client.rb
|
89
93
|
- lib/alipay/mobile/service.rb
|