jasl_tenpay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3db5c5a416312d0257097bdf7a5ce2b7df126a35
4
+ data.tar.gz: 8f50150489ec0ec3321132e6471af39ab76ba6d9
5
+ SHA512:
6
+ metadata.gz: b17433f76dad459abb443177f1abe878e2666a4045b4fbb18cadec6c85c9087e37f92f57964e7a11396f654dcf1052d1396d494fc68b7d670c39a0ca5417dc06
7
+ data.tar.gz: 100adf34f9cc5b01b084f48b899d0e5f88155de9df08861de5a30c5bfbaa83ee1359bf8014307fe2a7b19f4583d8cefb81d7fc0969f58d0510893b98e7015829
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Jasl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Tenpay'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.libs << 'test'
24
+ t.pattern = 'test/**/*_test.rb'
25
+ t.verbose = false
26
+ end
27
+
28
+ task default: :test
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tenpay do
3
+ # # Task goes here
4
+ # end
data/lib/tenpay.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'tenpay/utils'
2
+ require 'tenpay/sign'
3
+ require 'tenpay/service'
4
+ require 'tenpay/notify'
5
+
6
+ module Tenpay
7
+ class<< self
8
+ attr_accessor :pid, :key
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Tenpay
2
+ class Notify
3
+ GATEWAY = 'https://gw.tenpay.com/gateway/simpleverifynotifyid.xml'
4
+ SUCCESS_STR = '<retcode>0</retcode>'
5
+
6
+ def self.verify?(params)
7
+ if Sign.verify?(params)
8
+ params = {
9
+ 'input_charset' => 'UTF-8',
10
+ 'partner' => Tenpay.pid,
11
+ 'notify_id' => CGI.escape(params[:notify_id].to_s)
12
+ }
13
+
14
+ open("#{GATEWAY}?#{Utils.make_query_string(params)}").read.include? SUCCESS_STR
15
+ else
16
+ false
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+
4
+ module Tenpay
5
+ module Service
6
+ GATEWAY_URL = 'https://gw.tenpay.com/gateway/pay.htm'
7
+
8
+ CREATE_INTERACTIVE_MODE_REQUIRED_OPTIONS = %w(body subject return_url notify_url partner out_trade_no total_fee fee_type spbill_create_ip)
9
+ def self.create_interactive_mode_url(options)
10
+ options = {
11
+ 'input_charset' => 'UTF-8',
12
+ 'partner' => Tenpay.pid,
13
+ 'fee_type' => '1'
14
+ }.merge(Utils.stringify_keys(options))
15
+
16
+ check_required_options(options, CREATE_INTERACTIVE_MODE_REQUIRED_OPTIONS)
17
+
18
+ "#{GATEWAY_URL}?#{Utils.make_query_string(options)}"
19
+ end
20
+
21
+ private
22
+
23
+ def self.check_required_options(options, names)
24
+ names.each do |name|
25
+ warn("Tenpay Warn: missing required option: #{name}") unless options.has_key?(name)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'digest/md5'
2
+
3
+ module Tenpay
4
+ module Sign
5
+ def self.generate(params)
6
+ query = params.sort.map do |key, value|
7
+ "#{key}=#{value}"
8
+ end.join('&')
9
+
10
+ Digest::MD5.hexdigest("#{query}&key=#{Tenpay.key}").upcase
11
+ end
12
+
13
+ def self.verify?(params)
14
+ params = Utils.stringify_keys(params)
15
+ sign = params.delete('sign')
16
+
17
+ generate(params) == sign
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module Tenpay
2
+ module Utils
3
+ def self.stringify_keys(hash)
4
+ new_hash = {}
5
+ hash.each do |key, value|
6
+ new_hash[(key.to_s rescue key) || key] = value
7
+ end
8
+ new_hash
9
+ end
10
+
11
+ def self.make_query_string(options)
12
+ options.merge('sign' => Tenpay::Sign.generate(options)).map do |key, value|
13
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
14
+ end.join('&')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Tenpay
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class Tenpay::NotifyTest < Test::Unit::TestCase
4
+ SUCCESS_RETURN = <<-XML
5
+ <?xml version="1.0" encoding="GBK"?>
6
+ <root>
7
+ <input_charset>UTF-8</input_charset>
8
+ <partner>2000054321</partner>
9
+ <retcode>0</retcode>
10
+ <retmsg />
11
+ <sign>1C58B3A778D9F49F3FA91D30E9A831D2</sign>
12
+ <sign_key_index>1</sign_key_index>
13
+ <sign_type>MD5</sign_type>
14
+ </root>
15
+ XML
16
+
17
+ FAIL_RETURN = <<-XML
18
+ <?xml version="1.0" encoding="GBK"?>
19
+ <root>
20
+ <input_charset>UTF-8</input_charset>
21
+ <partner>2000054321</partner>
22
+ <retcode>88222005</retcode>
23
+ <retmsg />
24
+ <sign>1C58B3A778D9F49F3FA91D30E9A831D2</sign>
25
+ <sign_key_index>1</sign_key_index>
26
+ <sign_type>MD5</sign_type>
27
+ </root>
28
+ XML
29
+
30
+ def setup
31
+ @options = {
32
+ :notify_id => '123456789012345678901234567890',
33
+ :sign => '9E3178005EF2DFF87348E00889765FF0'
34
+ }
35
+ @url = "https://gw.tenpay.com/gateway/simpleverifynotifyid.xml?input_charset=UTF-8&partner=#{Tenpay.pid}&notify_id=123456789012345678901234567890&sign=E040AFBCBC409EA0C62CF0F11CF0BAA4"
36
+ end
37
+
38
+ def test_verify_notify_when_true
39
+ FakeWeb.register_uri(:get, @url, :body => SUCCESS_RETURN)
40
+ assert Tenpay::Notify.verify?(@options)
41
+ end
42
+
43
+ def test_verify_notify_when_false
44
+ FakeWeb.register_uri(:get, @url, :body => FAIL_RETURN)
45
+ assert !Tenpay::Notify.verify?(@options)
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class Tenpay::ServiceTest < Test::Unit::TestCase
4
+ def test_generate_create_interactive_mode_url
5
+ options = {
6
+ :body => 'test good',
7
+ :subject => 'test',
8
+ :return_url => 'http://tenpay.dev/tenpay_return',
9
+ :notify_url => 'http://tenpay.dev/tenpay_notify',
10
+ :out_trade_no => '1',
11
+ :total_fee => 1,
12
+ :spbill_create_ip => '127.0.0.1',
13
+ }
14
+ assert_not_nil Tenpay::Service.create_interactive_mode_url(options)
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class Tenpay::SignTest < Test::Unit::TestCase
4
+ def setup
5
+ @params = {
6
+ :service => 'test',
7
+ :partner => '123'
8
+ }
9
+ @sign = Digest::MD5.hexdigest("partner=123&service=test&key=#{Tenpay.key}").upcase
10
+ end
11
+
12
+ def test_generate_sign
13
+ assert_equal @sign, Tenpay::Sign.generate(@params)
14
+ end
15
+
16
+ def test_verify_sign
17
+ assert Tenpay::Sign.verify?(@params.merge(:sign => @sign))
18
+ end
19
+
20
+ def test_verify_sign_when_fails
21
+ assert !Tenpay::Sign.verify?(@params.merge(:danger => 'danger', :sign => @sign))
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class Tenpay::UtilsTest < Test::Unit::TestCase
4
+ def test_stringify_keys
5
+ hash = { 'a' => 1, :b => 2 }
6
+ assert_equal({ 'a' => 1, 'b' => 2 }.sort, Tenpay::Utils.stringify_keys(hash).sort)
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ require 'tenpay'
3
+ require 'fakeweb'
4
+
5
+ Tenpay.pid = '1900000113'
6
+ Tenpay.key = 'e82573dc7e6136ba414f2e2affbe39fa'
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jasl_tenpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fakeweb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: An unofficial simple tenpay gem
56
+ email:
57
+ - jasl9187@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/tasks/tenpay_tasks.rake
63
+ - lib/tenpay/notify.rb
64
+ - lib/tenpay/service.rb
65
+ - lib/tenpay/sign.rb
66
+ - lib/tenpay/utils.rb
67
+ - lib/tenpay/version.rb
68
+ - lib/tenpay.rb
69
+ - MIT-LICENSE
70
+ - Rakefile
71
+ - test/tenpay/notify_test.rb
72
+ - test/tenpay/service_test.rb
73
+ - test/tenpay/sign_test.rb
74
+ - test/tenpay/utils_test.rb
75
+ - test/test_helper.rb
76
+ homepage: https://github.com/jasl/tenpay
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: An unofficial simple tenpay gem
100
+ test_files:
101
+ - test/tenpay/notify_test.rb
102
+ - test/tenpay/service_test.rb
103
+ - test/tenpay/sign_test.rb
104
+ - test/tenpay/utils_test.rb
105
+ - test/test_helper.rb