ralipay 0.0.2 → 0.0.3
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/lib/ralipay/common.rb +7 -3
- data/lib/ralipay/notify.rb +42 -0
- data/lib/ralipay/service.rb +1 -1
- data/lib/ralipay/version.rb +1 -1
- data/lib/ralipay.rb +62 -2
- data/test/test_ralipay.rb +53 -3
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8106fe0016c5c10983d383d25986faaa9f95caa0
|
|
4
|
+
data.tar.gz: f0f4a072239c608e9d9cd4fee85b47ba250caf24
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fdb04223b1a5b7e4d962e982e5ca093c12b5260ae7527acd99e1f38cbddb68431e7af883bb800c895331fc7007646659b24af495f18426328a2a8e87c4ccab5d
|
|
7
|
+
data.tar.gz: c6495e7562763e3e8b72e74468e550caad2373e932c8356adc92b3e990ed69ae587f0466ca5fba7eb47431de7067cd63f5689fe7d1ee2bab16f7aca6e6bd99da
|
data/lib/ralipay/common.rb
CHANGED
|
@@ -18,9 +18,13 @@ module Ralipay::Common
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
#把数组所有元素,排序后按照“参数=参数值”的模式用“&”字符拼接成字符串
|
|
21
|
-
def self.create_link_string hash
|
|
21
|
+
def self.create_link_string hash, sort = true
|
|
22
22
|
result_string = ''
|
|
23
|
-
|
|
23
|
+
#是否排序
|
|
24
|
+
if sort
|
|
25
|
+
hash = hash.sort
|
|
26
|
+
end
|
|
27
|
+
|
|
24
28
|
hash.each{|key,value|
|
|
25
29
|
result_string += (key.to_s + '=' + value.to_s + '&')
|
|
26
30
|
}
|
|
@@ -45,7 +49,7 @@ module Ralipay::Common
|
|
|
45
49
|
|
|
46
50
|
#MD5签名
|
|
47
51
|
def self.md5_sign(for_sign_string)
|
|
48
|
-
|
|
52
|
+
#@todo md5签名方法
|
|
49
53
|
end
|
|
50
54
|
|
|
51
55
|
#验签
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'ralipay/common'
|
|
2
|
+
require 'uri'
|
|
3
|
+
require 'cgi'
|
|
4
|
+
require 'nokogiri'
|
|
5
|
+
|
|
6
|
+
include Ralipay::Common
|
|
7
|
+
|
|
8
|
+
class Notify
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
#对return_url的认证,以hash symbol方式输入get数组
|
|
15
|
+
def return_verify? gets
|
|
16
|
+
#@todo 入参合法性验证
|
|
17
|
+
in_hash = Ralipay::Common::para_filter gets
|
|
18
|
+
sort_hash = in_hash.sort
|
|
19
|
+
sign = CGI::unescape gets[:sign]
|
|
20
|
+
|
|
21
|
+
for_sign_string = Ralipay::Common::create_link_string(sort_hash)
|
|
22
|
+
Ralipay::Common::verify?(for_sign_string, sign)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
#对notify_url的认证,以hash symbol方式输入post数组
|
|
26
|
+
def notify_verify? posts
|
|
27
|
+
#@todo 入参合法性验证
|
|
28
|
+
#此处为固定顺序,支付宝Notify返回消息通知比较特殊,这里不需要升序排列
|
|
29
|
+
notify_hash = {
|
|
30
|
+
:service => posts[:service],
|
|
31
|
+
:v => posts[:v],
|
|
32
|
+
:sec_id => posts[:sec_id],
|
|
33
|
+
:notify_data => posts[:notify_data]
|
|
34
|
+
}
|
|
35
|
+
#解密notify_data
|
|
36
|
+
notify_hash[:notify_data] = Ralipay::Common::decrypt notify_hash[:notify_data]
|
|
37
|
+
sign = posts[:sign]
|
|
38
|
+
for_sign_string = Ralipay::Common::create_link_string(notify_hash,false)
|
|
39
|
+
Ralipay::Common::verify?(for_sign_string, sign)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
data/lib/ralipay/service.rb
CHANGED
|
@@ -71,7 +71,7 @@ class Service
|
|
|
71
71
|
#生成签名
|
|
72
72
|
@@my_sign = Ralipay::Common::build_sign sort_array
|
|
73
73
|
#创建POST请求数据串
|
|
74
|
-
@@req_data = Ralipay::Common::create_link_string(
|
|
74
|
+
@@req_data = Ralipay::Common::create_link_string(sort_array).to_s \
|
|
75
75
|
+ '&sign=' \
|
|
76
76
|
+ CGI::escape(@@my_sign)
|
|
77
77
|
#请求支付宝接口
|
data/lib/ralipay/version.rb
CHANGED
data/lib/ralipay.rb
CHANGED
|
@@ -3,8 +3,10 @@ module Ralipay
|
|
|
3
3
|
require 'ralipay/version'
|
|
4
4
|
require 'ralipay/common'
|
|
5
5
|
require 'ralipay/service'
|
|
6
|
+
require 'ralipay/notify'
|
|
6
7
|
require 'json'
|
|
7
8
|
require 'date'
|
|
9
|
+
require 'nokogiri'
|
|
8
10
|
|
|
9
11
|
include Ralipay::Common
|
|
10
12
|
|
|
@@ -41,6 +43,7 @@ module Ralipay
|
|
|
41
43
|
$global_configs = $global_configs.merge configs
|
|
42
44
|
end
|
|
43
45
|
|
|
46
|
+
#生成wap支付地址
|
|
44
47
|
def generate_wap_pay_url
|
|
45
48
|
params = {
|
|
46
49
|
:_input_charset => $input_charset_gbk,
|
|
@@ -104,8 +107,65 @@ module Ralipay
|
|
|
104
107
|
}
|
|
105
108
|
|
|
106
109
|
#调用alipay_Wap_Auth_AuthAndExecute接口方法,生成支付地址
|
|
107
|
-
|
|
108
|
-
|
|
110
|
+
Service.new.alipay_wap_auth_and_execute(req_hash)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
#同步回调验证,支付后跳转,前端GET方式获得参数,传入hash symbol,该方法只返回bool
|
|
114
|
+
def callback_verify? gets
|
|
115
|
+
(Notify.new.return_verify? gets) && (gets[:result] == 'success')
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
#同步回调验证,支付后跳转,前端GET方式获得参数,传入hash symbol,该方法返回支付状态,并安全的返回回调参数hash,失败返回false
|
|
119
|
+
def callback_verify gets
|
|
120
|
+
if (Notify.new.return_verify? gets) && (gets[:result] == 'success')
|
|
121
|
+
{
|
|
122
|
+
:out_trade_no => gets[:out_trade_no], #外部交易号
|
|
123
|
+
:trade_no => gets[:trade_no] #支付宝交易号
|
|
124
|
+
}
|
|
125
|
+
else
|
|
126
|
+
false #交易失败/验证失败返回false
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
#异步回调验证,支付宝主动通知,前端POST xml方式获得参数,该方法只返回bool
|
|
131
|
+
#成功请自行向支付宝打印纯文本success
|
|
132
|
+
#如验签失败或未输出success支付宝会24小时根据策略重发总共7次,需考虑重复通知的情况
|
|
133
|
+
def notify_verify? posts
|
|
134
|
+
if Notify.new.notify_verify? posts
|
|
135
|
+
#解密并解析返回参数的xml
|
|
136
|
+
xml = Ralipay::Common::decrypt posts[:notify_data]
|
|
137
|
+
doc = Nokogiri::XML xml
|
|
138
|
+
status = doc.xpath('/notify/trade_status').text
|
|
139
|
+
#获得可信的交易状态
|
|
140
|
+
status == 'TRADE_FINISHED' ? true : false
|
|
141
|
+
else
|
|
142
|
+
false
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
#异步回调验证,支付宝主动通知,前端POST xml方式获得参数,该方法返回支付状态,并安全的返回回调参数hash,失败返回false
|
|
147
|
+
#成功请自行向支付宝打印纯文本success
|
|
148
|
+
#如验签失败或未输出success支付宝会24小时根据策略重发总共7次,需考虑重复通知的情况
|
|
149
|
+
def notify_verify posts
|
|
150
|
+
if Notify.new.notify_verify? posts
|
|
151
|
+
#解密并解析返回参数的xml
|
|
152
|
+
xml = Ralipay::Common::decrypt posts[:notify_data]
|
|
153
|
+
doc = Nokogiri::XML xml
|
|
154
|
+
status = doc.xpath('/notify/trade_status').text
|
|
155
|
+
#获得可信的交易状态
|
|
156
|
+
if status == 'TRADE_FINISHED'
|
|
157
|
+
{
|
|
158
|
+
:out_trade_no => doc.xpath('/notify/out_trade_no').text,
|
|
159
|
+
:subject => doc.xpath('/notify/subject').text,
|
|
160
|
+
:price => doc.xpath('/notify/price').text,
|
|
161
|
+
:trade_no => doc.xpath('/notify/trade_no').text
|
|
162
|
+
}
|
|
163
|
+
else
|
|
164
|
+
false
|
|
165
|
+
end
|
|
166
|
+
else
|
|
167
|
+
false
|
|
168
|
+
end
|
|
109
169
|
end
|
|
110
170
|
|
|
111
171
|
end
|
data/test/test_ralipay.rb
CHANGED
|
@@ -10,9 +10,9 @@ class TestRalipay < Test::Unit::TestCase
|
|
|
10
10
|
:rsa_private_key_path => '/Users/ZhouYT/Desktop/rsa_private_key.pem',
|
|
11
11
|
:rsa_public_key_path => '/Users/ZhouYT/Desktop/alipay_public_key.pem',
|
|
12
12
|
:subject => '测试商品',
|
|
13
|
-
:out_trade_no => '
|
|
14
|
-
:total_fee => '
|
|
15
|
-
:notify_url => 'http://
|
|
13
|
+
:out_trade_no => '1222222233',
|
|
14
|
+
:total_fee => '0.01',
|
|
15
|
+
:notify_url => 'http://180.110.74.118/post.php',
|
|
16
16
|
:merchant_url => 'http://www.iiseeuu.com',
|
|
17
17
|
:call_back_url => 'http://www.iiseeuu.com'
|
|
18
18
|
}
|
|
@@ -20,6 +20,56 @@ class TestRalipay < Test::Unit::TestCase
|
|
|
20
20
|
Ralipay::Payment.new(configs).generate_wap_pay_url
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
def test_callback_verify
|
|
24
|
+
configs = {
|
|
25
|
+
:partner => '2088701817081672',
|
|
26
|
+
:seller_email => 'service@iiseeuu.com',
|
|
27
|
+
:rsa_private_key_path => '/Users/ZhouYT/Desktop/rsa_private_key.pem',
|
|
28
|
+
:rsa_public_key_path => '/Users/ZhouYT/Desktop/alipay_public_key.pem',
|
|
29
|
+
:subject => '测试商品',
|
|
30
|
+
:out_trade_no => '1222222232',
|
|
31
|
+
:total_fee => '1',
|
|
32
|
+
:notify_url => 'http://180.110.74.118/post.php',
|
|
33
|
+
:merchant_url => 'http://www.iiseeuu.com',
|
|
34
|
+
:call_back_url => 'http://www.iiseeuu.com'
|
|
35
|
+
}
|
|
36
|
+
#'out_trade_no=1222222232&request_token=requestToken&result=success&trade_no=2013032325639837&sign=cANAWkI1dgF1WeyRpp%2F0xzfKXXo50JxRyUxcDh6z%2BZbps1YFiWYRTSUmdCx7HT%2BjAG79ebMWEVIf2HmdIYEIzQDJwu2nl0fElqRgcm9%2BT%2B5b75DbtUg9COla1tr34NLpOcM0P0lcq6byNM0wFbdycUIIoH5Z6JKu66B1YuQLNag%3D&sign_type=0001'
|
|
37
|
+
gets = {
|
|
38
|
+
:out_trade_no => '1222222232',
|
|
39
|
+
:request_token => 'requestToken',
|
|
40
|
+
:result => 'success',
|
|
41
|
+
:trade_no => '2013032325639837',
|
|
42
|
+
:sign => 'cANAWkI1dgF1WeyRpp%2F0xzfKXXo50JxRyUxcDh6z%2BZbps1YFiWYRTSUmdCx7HT%2BjAG79ebMWEVIf2HmdIYEIzQDJwu2nl0fElqRgcm9%2BT%2B5b75DbtUg9COla1tr34NLpOcM0P0lcq6byNM0wFbdycUIIoH5Z6JKu66B1YuQLNag%3D',
|
|
43
|
+
:sign_type => '0001'
|
|
44
|
+
}
|
|
45
|
+
assert_equal true,
|
|
46
|
+
Ralipay::Payment.new(configs).callback_verify?(gets)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_notify_verify
|
|
50
|
+
configs = {
|
|
51
|
+
:partner => '2088701817081672',
|
|
52
|
+
:seller_email => 'service@iiseeuu.com',
|
|
53
|
+
:rsa_private_key_path => '/Users/ZhouYT/Desktop/rsa_private_key.pem',
|
|
54
|
+
:rsa_public_key_path => '/Users/ZhouYT/Desktop/alipay_public_key.pem',
|
|
55
|
+
:subject => '测试商品',
|
|
56
|
+
:out_trade_no => '1222222233',
|
|
57
|
+
:total_fee => '1',
|
|
58
|
+
:notify_url => 'http://180.110.74.118/post.php',
|
|
59
|
+
:merchant_url => 'http://www.iiseeuu.com',
|
|
60
|
+
:call_back_url => 'http://www.iiseeuu.com'
|
|
61
|
+
}
|
|
62
|
+
posts = {
|
|
63
|
+
:service => 'alipay.wap.trade.create.direct',
|
|
64
|
+
:sign => 'pCEWVfxBWqvpndkXmCPbd70Tqfo7IG3tP68WmH4wWuUDylb6Rv2RzOghs7m+ANtAx+NyCIuE4KpoonS4qZrc16Qh7/bnwZL2C4FHQJ903HrV0c4n/Pdko0owksnb9VYUGMEppVEBvYPap0bP1GZsbCtI1iuXb2cI1h4vlKJjdGw=',
|
|
65
|
+
:sec_id => '0001',
|
|
66
|
+
:v => '1.0',
|
|
67
|
+
:notify_data => 'Ych9Cg/zaLHsqaBePwoFtxAE7vX0ZvslWCLFTP1AdsxQgvrEcwflAFdbhIHgqsy8AZdRGp7rNeP1Bpn9v+feNlHD96RQit4p1/JHTAOfdoNmQsRaDvBNH9xUlCANXC4zTFDxCZEJN79ppSAzhOu1iMAdUOzim+ZAacxpkMfM4c+YSkpXrEK7kt3Lw4FpkJwwkRAUlYNtrlva94Qo6RZDmo4UpDlUyS1GkqIOihnwxzE4rDO84txzVWtdGDD+Il5ev7nVwnbcsaJzbhl7jgkg8+KzcoIibrn8QJy/xsSckwXZ2pq9Q2d+ufOd/zqpeUvTaD9nNbaH0UYZXDzKPnS91au6zk/2Nqe1d+TsNBeU7muQTM719y7btzfq6tRjs2eNdOfinIYK/MC0lypYtakHrIxpxqyKe1UlpyIVwUauKBkzmJP03x4FJOC7jsbVxI7N2Um/7qlAYCIzLog+SvjW3RX7dhjXAG72qJ93yrTySMJE4yvHEhSFfc2EgjepMzVlCNLwlENrvzkTiqs6rdrKmReKLr2D4QETC5qPL+v3S1iEdlPN4z0KKxp1RWzdwNfLtOceas+lx8oNM2AiQ4O0Y8+YDI/NX7xjckPzwPsEfNWYnwfLYb3MkbmIfhRVozQtsVI4VfxLX35bSQLVtKPdAM4foUIZFh0+HGM+qiSNtZQ2BWjmOUFvIdNyrxMzrRF2+BnuoP+lCkOvQdOnWNoBFpD8aUQrD48AJHQTeyf63bSeJ5Vdif/LPpeJjL+FkP/k+h6mcvigIo1ZcOoF9S2Gg20EgXZWggqR7RLnkroCaNbn/86gZsDQvtwn855n3PRQxosw8a/0F5uFIt/oD5zI7TDFv+p3q9SO3+GYaOwd6OHK3hS11kx2EpzYWw9PaFoANsER9r9buKvQDfcJDkxeN79qmP2zuznVq9OM5X9wDFy2WiFfyYl/swuvZN5e9Q2c0MXj9aeA2H464Am+iFOv1lBWGOlxyVsTQuFjRzRg4FIOp8cv0qOtI/UQPWGbGAy4qys7+sJN/wkboLpdWKLhwgPzto9TM0jN5GZMeA35+2wGcuPAe/SNqhvWM9r8xeCniuSDjesHWWN5bkiYzB4cb0n+E72Yh5WmwJw87ySuXAQWgONxnzgjX0LAGiinoBoufBqmpi/f8vAV3sJFEzkGZxFLSO1Us1ldSi16IqnPTdNtaQAe0N+QYnALM10rmsuGp4/jJatFWFMnmi06AMLcC31who0SD/xItGl6riSkX0yDqWQJzCvZUrrZJoqXJ7XBZoCQscL3Ug2RCKrGba/Ekc10EQINiX99XzYUPVK8xKxO4WW/7T00rZ8B1YjnUaBdWKGdmWmv0itnLx6rK8NWBw=='
|
|
68
|
+
}
|
|
69
|
+
assert_equal true,
|
|
70
|
+
Ralipay::Payment.new(configs).notify_verify?(posts)
|
|
71
|
+
end
|
|
72
|
+
|
|
23
73
|
def test_para_filter
|
|
24
74
|
input_para = {:a => 'abc', :sign_type => 'abc', :c => '', :d => nil}
|
|
25
75
|
assert_equal ({:a => 'abc'}),
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ralipay
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- RaymondChou
|
|
@@ -66,6 +66,7 @@ files:
|
|
|
66
66
|
- Rakefile
|
|
67
67
|
- lib/ralipay.rb
|
|
68
68
|
- lib/ralipay/common.rb
|
|
69
|
+
- lib/ralipay/notify.rb
|
|
69
70
|
- lib/ralipay/service.rb
|
|
70
71
|
- lib/ralipay/version.rb
|
|
71
72
|
- ralipay.gemspec
|