wx_pay 0.0.1

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: 982340d48955dd0cd2d30b9e494f3fd26b6df4dd
4
+ data.tar.gz: 2992d5cb8b0c572b85d0bacc25577468f559106a
5
+ SHA512:
6
+ metadata.gz: f5222caaabbc3aa43357a71c1392d7ce0c0e987e621c27064d0bc3fcabea91211f0d63b7cc4b0498b03802527904e48559ebf27e327334f4fc24c557cf81b0cd
7
+ data.tar.gz: 736c4e4567dd4ac180694cb63a602ab12f576efb28c68e9a96443e587e9341da40d51d831ac68f22593edc9c415be7a696cbf80e74f68a232464ce329acae311
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 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,18 @@
1
+ module WxPay
2
+ module Notify
3
+ GATEWAY = 'https://gw.tenpay.com/gateway/simpleverifynotifyid.xml'
4
+ SUCCESS_STR = '<retcode>0</retcode>'
5
+
6
+ def self.verify?(params)
7
+ return false unless Sign.verify?(params)
8
+
9
+ params = {
10
+ 'input_charset' => 'UTF-8',
11
+ 'partner' => WxPay.appid,
12
+ 'notify_id' => CGI.escape(params[:notify_id].to_s)
13
+ }
14
+
15
+ open("#{GATEWAY}?#{Utils.make_query_string(params)}").read.include?(SUCCESS_STR)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+
4
+ module WxPay
5
+ module Service
6
+ GATEWAY_URL = 'https://api.mch.weixin.qq.com/pay'
7
+
8
+ INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = %i(body out_trade_no total_fee spbill_create_ip notify_url trade_type)
9
+ def self.invoke_unifiedorder(params)
10
+ params = {
11
+ app_id: WxPay.appid,
12
+ mch_id: WxPay.mch_id,
13
+ nonce_str: SecureRandom.uuid.tr('-', ''),
14
+ }.merge(params)
15
+
16
+ check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
17
+
18
+ WxPay::Utils.invoke_remote("#{GATEWAY_URL}/unifiedorder", WxPay::Utils.make_payload(params))
19
+ end
20
+
21
+ private
22
+
23
+ def self.check_required_options(options, names)
24
+ names.each do |name|
25
+ warn("WxPay 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 WxPay
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=#{WxPay.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,27 @@
1
+ module WxPay
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_payload(params)
12
+ "<xml>#{params.map {|k, v| "<#{k}>#{v}</#{k}>"}.join}<sign>#{WxPay::Sign.generate(params)}</sign></xml>"
13
+ end
14
+
15
+ def self.invoke_remote(url, payload)
16
+ Hash.from_xml(
17
+ RestClient::Request.execute(
18
+ {
19
+ method: :post,
20
+ url: url,
21
+ payload: payload,
22
+ headers: {content_type: 'application/xml'}
23
+ }.merge(WxPay.extra_rest_client_options))
24
+ )
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module WxPay
2
+ VERSION = "0.0.1"
3
+ end
data/lib/wx_pay.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'wx_pay/utils'
2
+ require 'wx_pay/sign'
3
+ require 'wx_pay/service'
4
+ require 'wx_pay/notify'
5
+
6
+ module WxPay
7
+ class<< self
8
+ attr_accessor :appid, :mch_id, :key
9
+
10
+ def extra_rest_client_options=(options)
11
+ @rest_client_options = options
12
+ end
13
+
14
+ def extra_rest_client_options
15
+ @rest_client_options || {}
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'minitest/autorun'
2
+ require 'wx_pay'
3
+ require 'fakeweb'
4
+
5
+ WxPay.appid = 'wxd930ea5d5a258f4f'
6
+ WxPay.key = '8934e7d15453e97507ef794cf7b0519d'
7
+ WxPay.mch_id = '1900000109'
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class WxPay::SignTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @params = {
6
+ appid: 'wxd930ea5d5a258f4f',
7
+ auth_code: 123456,
8
+ body: 'test',
9
+ device_info: 123,
10
+ mch_id: 1900000109,
11
+ nonce_str: '960f228109051b9969f76c82bde183ac',
12
+ out_trade_no: '1400755861',
13
+ spbill_create_ip: '127.0.0.1',
14
+ total_fee: 1
15
+ }
16
+
17
+ @sign = '729A68AC3DE268DBD9ADE442382E7B24'
18
+ end
19
+
20
+ def test_generate_sign
21
+ assert_equal @sign, WxPay::Sign.generate(@params)
22
+ end
23
+
24
+ def test_verify_sign
25
+ assert WxPay::Sign.verify?(@params.merge(:sign => @sign))
26
+ end
27
+
28
+ def test_verify_sign_when_fails
29
+ assert !WxPay::Sign.verify?(@params.merge(:danger => 'danger', :sign => @sign))
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class WxPay::UtilsTest < MiniTest::Unit::TestCase
4
+ def test_stringify_keys
5
+ hash = { 'a' => 1, :b => 2 }
6
+ assert_equal({ 'a' => 1, 'b' => 2 }.sort, WxPay::Utils.stringify_keys(hash).sort)
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wx_pay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakeweb
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5'
97
+ description: An unofficial simple wechat pay gem
98
+ email:
99
+ - jasl9187@hotmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - MIT-LICENSE
105
+ - Rakefile
106
+ - lib/wx_pay.rb
107
+ - lib/wx_pay/notify.rb
108
+ - lib/wx_pay/service.rb
109
+ - lib/wx_pay/sign.rb
110
+ - lib/wx_pay/utils.rb
111
+ - lib/wx_pay/version.rb
112
+ - test/test_helper.rb
113
+ - test/wx_pay/sign_test.rb
114
+ - test/wx_pay/utils_test.rb
115
+ homepage: https://github.com/jasl/wx_pay
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.2.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: An unofficial simple wechat pay gem
139
+ test_files:
140
+ - test/test_helper.rb
141
+ - test/wx_pay/sign_test.rb
142
+ - test/wx_pay/utils_test.rb