magpie 0.8.6.2 → 0.8.8

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.
Files changed (71) hide show
  1. data/README.md +86 -11
  2. data/Rakefile +7 -0
  3. data/bin/mag +3 -1
  4. data/lib/apps.rb +43 -0
  5. data/lib/magpie.rb +15 -25
  6. data/lib/magpie/goose.rb +88 -0
  7. data/lib/magpie/mouse.rb +43 -0
  8. data/lib/magpie/rubber.rb +16 -0
  9. data/lib/magpie/server.rb +18 -2
  10. data/lib/magpie/utils.rb +73 -22
  11. data/lib/middles/alipay.rb +3 -13
  12. data/lib/middles/chinabank.rb +3 -13
  13. data/lib/middles/snake.rb +116 -0
  14. data/lib/middles/tenpay.rb +22 -0
  15. data/lib/models/alipay.rb +96 -127
  16. data/lib/models/chinabank.rb +64 -99
  17. data/lib/models/dung.rb +66 -0
  18. data/lib/models/tenpay.rb +89 -0
  19. data/lib/views/fail.html.erb +12 -0
  20. data/lib/views/layouts/app.html.erb +42 -0
  21. data/lib/views/success.html.erb +34 -0
  22. data/magpie.gemspec +46 -4
  23. data/test/helper.rb +6 -1
  24. data/test/partner.yml +3 -0
  25. data/test/test.log +3152 -0
  26. data/test/test_alipay.rb +6 -15
  27. data/test/test_chinabank.rb +13 -5
  28. data/test/test_dung.rb +80 -0
  29. data/test/test_object.rb +18 -0
  30. data/test/test_snake.rb +55 -0
  31. data/test/test_tenpay.rb +144 -0
  32. data/test/test_utils.rb +35 -0
  33. metadata +43 -60
  34. data/doc/AlipayModel.html +0 -2010
  35. data/doc/ChinabankModel.html +0 -1245
  36. data/doc/Magpie.html +0 -255
  37. data/doc/Magpie/Alipay.html +0 -254
  38. data/doc/Magpie/Chinabank.html +0 -252
  39. data/doc/Magpie/Mothlog.html +0 -252
  40. data/doc/Magpie/Server.html +0 -241
  41. data/doc/Magpie/Server/Options.html +0 -245
  42. data/doc/Magpie/Utils.html +0 -85
  43. data/doc/_index.html +0 -186
  44. data/doc/class_list.html +0 -36
  45. data/doc/css/common.css +0 -1
  46. data/doc/css/full_list.css +0 -53
  47. data/doc/css/style.css +0 -307
  48. data/doc/file.COPYING.html +0 -78
  49. data/doc/file.README.html +0 -162
  50. data/doc/file_list.html +0 -38
  51. data/doc/frames.html +0 -13
  52. data/doc/index.html +0 -162
  53. data/doc/js/app.js +0 -202
  54. data/doc/js/full_list.js +0 -149
  55. data/doc/js/jquery.js +0 -154
  56. data/doc/method_list.html +0 -499
  57. data/doc/top-level-namespace.html +0 -90
  58. data/lib/doc/_index.html +0 -72
  59. data/lib/doc/class_list.html +0 -36
  60. data/lib/doc/css/common.css +0 -1
  61. data/lib/doc/css/full_list.css +0 -53
  62. data/lib/doc/css/style.css +0 -307
  63. data/lib/doc/file_list.html +0 -35
  64. data/lib/doc/frames.html +0 -13
  65. data/lib/doc/index.html +0 -72
  66. data/lib/doc/js/app.js +0 -202
  67. data/lib/doc/js/full_list.js +0 -149
  68. data/lib/doc/js/jquery.js +0 -154
  69. data/lib/doc/method_list.html +0 -35
  70. data/lib/doc/top-level-namespace.html +0 -78
  71. data/lib/middles/mothlog.rb +0 -33
@@ -1,13 +1,16 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  $:.unshift(File.dirname(__FILE__))
3
3
  $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
4
+
4
5
  require 'helper'
5
6
 
7
+ Magpie::AlipayModel.class_eval{ set_accounts_kind :alipay, :env => ENV['magpie']}
8
+
6
9
  class AlipayTest < Test::Unit::TestCase
7
10
  include Rack::Test::Methods
8
11
 
9
12
  def app
10
- Magpie::APP
13
+ Magpie::BIRD_APP
11
14
  end
12
15
 
13
16
  def setup
@@ -20,7 +23,7 @@ class AlipayTest < Test::Unit::TestCase
20
23
  get @gateway, @params
21
24
  assert last_response.ok?
22
25
  assert last_response.body.include? "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
23
- assert last_response.headers["Content-type"], "text/xml"
26
+ assert_equal last_response.headers["Content-type"], "text/xml"
24
27
  end
25
28
 
26
29
  def test_return_final_error
@@ -61,12 +64,6 @@ class AlipayTest < Test::Unit::TestCase
61
64
  end
62
65
 
63
66
  def test_validates_numericality
64
- get @gateway, @params.merge("price" => -2.00, "total_fee" => "1000000000.00")
65
- assert last_response.body.include? "<price>should between 0.01~100000000.00</price>"
66
- assert last_response.body.include? "<total_fee>should between 0.01~100000000.00</total_fee>"
67
- get @gateway, @params.merge("price" => 0.00, "total_fee" => "100000000.01")
68
- assert last_response.body.include? "<price>should between 0.01~100000000.00</price>"
69
- assert last_response.body.include? "<total_fee>should between 0.01~100000000.00</total_fee>"
70
67
  get @gateway, @params.merge("quantity" => 0)
71
68
  assert last_response.body.include? "<quantity>should be integer and between 1~999999</quantity>"
72
69
  get @gateway, @params.merge("quantity" => 1.2)
@@ -153,12 +150,6 @@ class AlipayTest < Test::Unit::TestCase
153
150
  assert am.send(:notify_text) != /quantity=/
154
151
  end
155
152
 
156
- def test_send_notify
157
- am = get_am
158
- res = am.send_notify
159
- assert res.is_a? String
160
- end
161
-
162
153
  def test_notify_sign
163
154
  am = get_am
164
155
  raw_h = notify_params
@@ -182,7 +173,7 @@ class AlipayTest < Test::Unit::TestCase
182
173
  private
183
174
 
184
175
  def get_am
185
- am = AlipayModel.new(:partner => "test123",
176
+ am = Magpie::AlipayModel.new(:partner => "test123",
186
177
  :notify_url => "http://ticket.fantong.com:3000/alipay/notify",
187
178
  :return_url => "http://ticket.fantong.com:3000/alipay/feedback",
188
179
  :sign_type => "MD5",
@@ -3,13 +3,14 @@ $:.unshift(File.dirname(__FILE__))
3
3
  $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
4
4
 
5
5
  require 'helper'
6
- require 'models/chinabank'
7
6
 
7
+
8
+ Magpie::ChinabankModel.class_eval{ set_accounts_kind :chinabank, :env => ENV['magpie']}
8
9
  class ChinabankTest < Test::Unit::TestCase
9
10
  include Rack::Test::Methods
10
11
 
11
12
  def app
12
- Magpie::APP
13
+ Magpie::BIRD_APP
13
14
  end
14
15
 
15
16
  def setup
@@ -49,6 +50,13 @@ class ChinabankTest < Test::Unit::TestCase
49
50
  assert last_response.body.include? "<v_url>is too long (maximum is 200 characters)</v_url>"
50
51
  end
51
52
 
53
+ def test_validates_partner
54
+ post @gateway, @params.merge("v_mid" => "fake123")
55
+ assert last_response.body.include? "<v_mid>商户编号不存在</v_mid>"
56
+ post @gateway, @params.merge("v_mid" => "20000400")
57
+ assert !last_response.body.include?("<v_mid>商户编号不存在</v_mid>")
58
+ end
59
+
52
60
  def test_validates_numericality
53
61
  post @gateway, @params.merge("v_amount" => -1.00)
54
62
  assert last_response.body.include? "<v_amount>format should be Number(6, 2)</v_amount>"
@@ -66,12 +74,12 @@ class ChinabankTest < Test::Unit::TestCase
66
74
  end
67
75
 
68
76
  def test_key
69
- am = ChinabankModel.new(@params)
77
+ am = Magpie::ChinabankModel.new(@params)
70
78
  assert am.key.length > 0
71
79
  end
72
80
 
73
81
  def test_notify_sign
74
- am = ChinabankModel.new(@params)
82
+ am = Magpie::ChinabankModel.new(@params)
75
83
  raw_hash = @params.dup
76
84
  raw_sign = am.send :notify_sign
77
85
  raw_hash.delete("remark2")
@@ -81,7 +89,7 @@ class ChinabankTest < Test::Unit::TestCase
81
89
  end
82
90
 
83
91
  def test_notify
84
- am = ChinabankModel.new(@params)
92
+ am = Magpie::ChinabankModel.new(@params)
85
93
  assert am.notify.has_key?("v_oid")
86
94
  assert am.notify.has_key?("v_pmode")
87
95
  assert am.notify.has_key?("v_pstring")
@@ -0,0 +1,80 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift(File.dirname(__FILE__))
3
+ $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
4
+
5
+ require 'helper'
6
+
7
+ Magpie::AlipayModel.class_eval{ set_accounts_kind :alipay, :env => ENV['magpie']}
8
+ module Magpie
9
+ class DungTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @alipay = AlipayModel.new(:total_fee => 10, :subject => "测试商品", :out_trade_no => "123", :price => "100", :notify_url => "http://dog.com")
13
+ @tenpay = TenpayModel.new(:total_fee => 200, :desc => "测试商品", :transaction_id => "123", :total_fee => "10000", :return_url => "http://dog.com")
14
+ @chinabank = ChinabankModel.new(:v_amount => 100, :v_oid => "123", :v_amount => "100", :v_url => "http://dog.com")
15
+ end
16
+
17
+
18
+ def test_total_fee
19
+ dung = Dung.new(@alipay)
20
+ assert dung.total_fee == @alipay.total_fee
21
+ dung = Dung.new(@tenpay)
22
+ assert_equal dung.total_fee, @tenpay.total_fee.to_f/100
23
+ dung = Dung.new(@chinabank)
24
+ assert_equal dung.total_fee, @chinabank.v_amount
25
+ end
26
+
27
+ def test_subject
28
+ dung = Dung.new(@alipay)
29
+ assert dung.subject == @alipay.subject
30
+ dung = Dung.new(@tenpay)
31
+ assert dung.subject == @tenpay.desc
32
+ dung = Dung.new(@chinabank)
33
+ assert dung.subject.nil?
34
+ end
35
+
36
+ def test_trade_kind
37
+ dung = Dung.new(@alipay)
38
+ assert_equal dung.trade_kind, "即时到帐交易"
39
+ dung = Dung.new(@tenpay)
40
+ assert_equal dung.trade_kind, "即时到帐交易"
41
+ dung = Dung.new(@chinabank)
42
+ assert_equal dung.trade_kind, "即时到帐交易"
43
+ end
44
+
45
+ def test_trade_no
46
+ assert_equal Dung.new(@alipay).trade_no, @alipay.out_trade_no
47
+ assert_equal Dung.new(@tenpay).trade_no, @tenpay.transaction_id
48
+ assert_equal Dung.new(@chinabank).trade_no, @chinabank.v_oid
49
+ end
50
+
51
+ def test_price
52
+ assert_equal Dung.new(@alipay).price, @alipay.price
53
+ assert_equal Dung.new(@tenpay).price, @tenpay.total_fee.to_f/100
54
+ assert_equal Dung.new(@chinabank).price, @chinabank.v_amount
55
+ end
56
+
57
+ def test_notify_url
58
+ assert_equal Dung.new(@alipay).notify_url, @alipay.notify_url
59
+ assert_equal Dung.new(@alipay).notify_url, "http://dog.com"
60
+ assert_equal Dung.new(@tenpay).notify_url, @tenpay.notify_url
61
+ assert_equal Dung.new(@tenpay).notify_url, "http://dog.com"
62
+ assert_equal Dung.new(@chinabank).notify_url, @chinabank.notify_url
63
+ assert_equal Dung.new(@chinabank).notify_url, "http://dog.com"
64
+ end
65
+
66
+ def test_notify_to_query
67
+ assert Dung.new(@alipay).notify_to_query.index("&")
68
+ assert Dung.new(@tenpay).notify_to_query.index("&")
69
+ assert Dung.new(@chinabank).notify_to_query.index("&")
70
+ end
71
+
72
+ def test_dung_kind
73
+ assert Dung.new(@alipay).kind == "alipay"
74
+ assert Dung.new(@tenpay).kind == "tenpay"
75
+ assert Dung.new(@chinabank).kind == "chinabank"
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
3
+
4
+ require 'helper'
5
+
6
+ class ObjectTest < Test::Unit::TestCase
7
+
8
+ def test_blank
9
+ assert " ".blank?
10
+ assert nil.blank?
11
+ assert [].blank?
12
+ assert ({ }.blank?)
13
+ assert !{ :a => 1}.blank?
14
+ assert !" dd".blank?
15
+ assert ![1].blank?
16
+ end
17
+
18
+ end
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift(File.dirname(__FILE__))
3
+ $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
4
+
5
+ require 'helper'
6
+
7
+ Magpie::AlipayModel.class_eval{ set_accounts_kind :alipay, :env => ENV['magpie']}
8
+ Magpie::TenpayModel.class_eval{ set_accounts_kind :tenpay, :env => ENV['magpie']}
9
+ Magpie::ChinabankModel.class_eval{ set_accounts_kind :chinabank, :env => ENV['magpie']}
10
+
11
+ class SnakeTest < Test::Unit::TestCase
12
+ include Rack::Test::Methods
13
+
14
+ def app
15
+ Magpie::SNAKE_APP
16
+ end
17
+
18
+ def test_return_html
19
+ get "/alipay", { }
20
+ assert last_response.ok?
21
+ assert_equal last_response.headers["Content-type"], "text/html"
22
+ end
23
+
24
+ def test_alipay_index
25
+ get "/alipay", { }
26
+ assert last_response.body.include?("请求失败")
27
+ end
28
+
29
+ def test_tenpay_index
30
+ get "/tenpay", { }
31
+ assert last_response.body.include?("请求失败")
32
+ post "/tenpay", { }
33
+ assert last_response.body.include?("请求失败")
34
+ end
35
+
36
+ def test_chinabank_index
37
+ get "/chinabank", { }
38
+ assert last_response.body.include?("请求失败")
39
+ post "/chinabank", { }
40
+ assert last_response.body.include?("请求失败")
41
+ end
42
+
43
+ def test_static_file
44
+ get "/images/errors.gif"
45
+ #assert last_response.ok?
46
+ #assert last_response.body.size > 0
47
+ end
48
+
49
+ def test_order_pay
50
+ post "/order/pay", { "a" => "test"}
51
+ assert last_response.status == 500
52
+ assert last_response.body.include?("500")
53
+ end
54
+
55
+ end
@@ -0,0 +1,144 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift(File.dirname(__FILE__))
3
+ $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
4
+
5
+ require 'helper'
6
+
7
+ Magpie::TenpayModel.class_eval{ set_accounts_kind :tenpay, :env => ENV['magpie']}
8
+
9
+ class TenpayTest < Test::Unit::TestCase
10
+ include Rack::Test::Methods
11
+
12
+ def app
13
+ Magpie::BIRD_APP
14
+ end
15
+
16
+ def setup
17
+ @accounts = YAML.load_file("test/partner.yml")["tenpay"]
18
+ @params = { "cmdno" => "1",
19
+ "date" => "20101025",
20
+ "bargainor_id" => @accounts[0][0],
21
+ "transaction_id" => "#{@accounts[0][0]}2010102512345678910",
22
+ "sp_billno" => "1111",
23
+ "total_fee" => "1200",
24
+ "fee_type" => 1,
25
+ "return_url" => "http://ticket.fantong.com:3000/tenpay/notify_url",
26
+ "attach" => "text",
27
+ "spbill_create_ip" => "127.0.0.1"
28
+ }
29
+ @gateway = "/tenpay/cgi-bin/v3.0/payservice.cgi"
30
+ end
31
+
32
+ def test_return_xml
33
+ get @gateway, @params
34
+ assert last_response.ok?
35
+ assert last_response.body.include? "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
36
+ assert last_response.headers["Content-type"], "text/xml"
37
+ post @gateway, @params
38
+ assert last_response.ok?
39
+ assert last_response.body.include? "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
40
+ assert last_response.headers["Content-type"], "text/xml"
41
+ end
42
+
43
+ def test_validates_prensence
44
+ post @gateway, { }
45
+ %w(cmdno date bank_type desc bargainor_id transaction_id sp_billno total_fee
46
+ fee_type return_url attach spbill_create_ip sign).each do |attr|
47
+ assert last_response.body.include? can_not_blank(attr)
48
+ end
49
+ end
50
+
51
+ def test_validates_length
52
+ post @gateway, @params.merge("sp_billno" => "a" * 30)
53
+ assert last_response.body.include? "<sp_billno>长度错误,应该在28个字符内</sp_billno>"
54
+ post @gateway, { }
55
+ assert !last_response.body.include?("<sp_billno>长度错误,应该在28个字符内</sp_billno>")
56
+ end
57
+
58
+ def test_validates_format
59
+ post @gateway, @params.merge("transaction_id" => "45aabb2008954321")
60
+ assert last_response.body.include?(transaction_id_error_msg)
61
+ post @gateway, { }
62
+ assert !last_response.body.include?(transaction_id_error_msg)
63
+ post @gateway, @params.merge("transaction_id" => "1234567890201010241234567890")
64
+ assert !last_response.body.include?(transaction_id_error_msg)
65
+ post @gateway, @params.merge("total_fee" => "10.2")
66
+ assert last_response.body.include?(total_fee_error_msg)
67
+ post @gateway, @params.merge("total_fee" => "100a")
68
+ assert last_response.body.include?(total_fee_error_msg)
69
+ post @gateway, @params.merge("total_fee" => "998")
70
+ assert !last_response.body.include?(total_fee_error_msg)
71
+ post @gateway, @params.merge("fee_type" => "2")
72
+ assert last_response.body.include?(fee_type_error_msg)
73
+ post @gateway, @params.merge("fee_type" => 1)
74
+ assert !last_response.body.include?(fee_type_error_msg)
75
+ post @gateway, @params.merge("spbill_create_ip" => "187.25.32")
76
+ assert last_response.body.include?(spbill_create_ip_error_msg)
77
+ post @gateway, @params.merge("spbill_create_ip" => "187.25.32.999")
78
+ assert last_response.body.include?(spbill_create_ip_error_msg)
79
+ post @gateway, @params.merge("spbill_create_ip" => "127.0.0.1")
80
+ assert !last_response.body.include?(spbill_create_ip_error_msg)
81
+ end
82
+
83
+ def test_validates_sign
84
+ post @gateway, @params.merge("sign" => "aaaaa")
85
+ assert last_response.body.include?(sign_should_upcase)
86
+ post @gateway, @params.merge("sign" => "AAAAA")
87
+ assert !last_response.body.include?(sign_should_upcase)
88
+ text = %w(cmdno date bargainor_id transaction_id sp_billno total_fee fee_type return_url attach spbill_create_ip ).map {|attr|
89
+ "#{attr}=#{@params[attr]}" unless @params[attr].blank?
90
+ }.join("&")
91
+ invalid_sign = Digest::MD5.hexdigest(text).upcase
92
+ post @gateway, @params.merge("sign" => invalid_sign)
93
+ assert last_response.body.include?("<sign>invalid sign</sign>")
94
+ sign = Digest::MD5.hexdigest(text + "&key=" + @accounts[0][1]).upcase
95
+ post @gateway, @params.merge("sign" => sign)
96
+ assert !last_response.body.include?("<sign>invalid sign</sign>")
97
+ end
98
+
99
+ def test_notify_sign
100
+ am = Magpie::TenpayModel.new(@params)
101
+ assert am.pay_result == "0"
102
+ @params["pay_result"] = am.pay_result
103
+ text = %w(cmdno pay_result date transaction_id sp_billno total_fee fee_type attach).map {|attr|
104
+ "#{attr}=#{@params[attr]}" unless @params[attr].blank?
105
+ }.join("&") + "&key=" + @accounts[0][1]
106
+ sign = Digest::MD5.hexdigest(text).upcase
107
+ assert_equal sign, am.notify_sign.upcase
108
+ end
109
+
110
+ def test_validates_partner
111
+ post @gateway, @params.merge("bargainor_id" => "fake123")
112
+ assert last_response.body.include?("<bargainor_id>商户号不存在</bargainor_id>")
113
+ post @gateway, @params.merge("bargainor_id" => "secret789")
114
+ assert !last_response.body.include?("<bargainor_id>商户编号不存在</bargainor_id>")
115
+ end
116
+
117
+ private
118
+
119
+ def can_not_blank(attr)
120
+ "<#{attr}>can't be blank</#{attr}>"
121
+ end
122
+
123
+ def transaction_id_error_msg
124
+ "<transaction_id>格式错误,transaction_id为28位长的数值,其中前10位为商户网站编号(SPID),由财付通统一分配;之后8位为订单产生的日期,如20050415;最后10位商户需要保证一天内不同的事务(用户订购一次商品或购买一次服务),其ID不相同</transaction_id>"
125
+ end
126
+
127
+ def total_fee_error_msg
128
+ "<total_fee>格式错误,只能为数字,以分为单位,不允许包含任何字符</total_fee>"
129
+ end
130
+
131
+ def fee_type_error_msg
132
+ "<fee_type>目前只支持人民币,请填1</fee_type>"
133
+ end
134
+
135
+ def spbill_create_ip_error_msg
136
+ "<spbill_create_ip>格式错误</spbill_create_ip>"
137
+ end
138
+
139
+ def sign_should_upcase
140
+ "<sign>sign签名必须大写</sign>"
141
+ end
142
+
143
+
144
+ end
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift(File.dirname(__FILE__))
3
+ $:.unshift(File.dirname(__FILE__) + "/.." + "/lib")
4
+
5
+ require 'helper'
6
+
7
+ class Magpie::UtilsTest < Test::Unit::TestCase
8
+ include Magpie::Utils
9
+
10
+
11
+ def test_send_notify_timeout
12
+ url = "bad_url"
13
+ res = send_notify("post", url, "")
14
+ assert res.is_a?(String)
15
+ assert res.include?("请确认#{url}在你的商户系统中可用")
16
+ end
17
+
18
+ def test_send_notify_code
19
+ url = "http://piao.fantong.com/alipay/notify"
20
+ res = send_notify("POST", url, { })
21
+ assert res.include?("fail")
22
+ url = "http://piao.fantong.com/alipay/page_not_exit"
23
+ res = send_notify("post", url, { })
24
+ assert res.include?("请确认#{url}在你的商户系统中可用")
25
+ url = "http://piao.fantong.com/chinabank/feedback"
26
+ res = send_notify("post", url, { })
27
+ assert res.include?("redirected")
28
+ url = "http://piao.fantong.com/tenpay/notify"
29
+ res = send_notify("post", url, { })
30
+ assert res.include?("请确认#{url}在你的商户系统中可用")
31
+ res = send_notify("get", url, "")
32
+ assert !res.include?("请确认#{url}在你的商户系统中可用")
33
+ end
34
+
35
+ end