Rwepay 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d80a5249da144ab650e93c819511ea5817963da3
4
+ data.tar.gz: 34f53b8d5edf4fe3caec6f0a735cdf4f606f40c1
5
+ SHA512:
6
+ metadata.gz: b2b659edbab1a79c1e83069db059a85fb65d9a864aada7aadf37e33305dfdd06379cf37804474581ee34c0aca68157be459031525babd40731546f5d85646f4a
7
+ data.tar.gz: ba3a5103417e85a2f9b8e5ecf4ad80ef9b6db342614b757e462e0e3f57ba1410216204df335654538112f2c0df9fa2a1c4b02b89e953f0f17f5ed68f16ae87c7
data/.gitignore ADDED
@@ -0,0 +1,27 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .idea
20
+ .DS_Store
21
+ Thumbs.db
22
+ .cache
23
+ .project
24
+ .settings
25
+ .tmproj
26
+ *.esproj
27
+ nbproject
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Rwepay.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 RaymondChou
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Rwepay
2
+
3
+ [![Build Status](https://travis-ci.org/RaymondChou/Rwepay.svg?branch=master)](https://travis-ci.org/RaymondChou/Rwepay)
4
+
5
+ 微信支付 Wechat Pay Ruby SDK Gem
6
+
7
+ [Ralipay(支付宝PaymentGem)](https://github.com/RaymondChou/ralipay)姊妹篇,微信支付SDK
8
+
9
+ 示例中的账号信息是微信提供的测试数据,你需要使用自己的账号信息才可以完成测试:)
10
+
11
+ 注:测试期间(未上线状态)你需要在微信商户后台设定的域名下进行测试,测试域名只能在当前公众号会话内测试才有效,并且加好测试微信账号的白名单,否则JSAPI会报access_control:not_allow
12
+
13
+ 具体参见[微信API文档](https://mp.weixin.qq.com/htmledition/res/bussiness-course2/wxpay-payment-api.pdf)
14
+
15
+ 特别要注意的一点,千万不要将微信提供的js demo使用在产品环境,那是非常不安全的。notify回调校验请严格使用notify_verify?方法进行。
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'Rwepay'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install Rwepay
30
+
31
+ ## Usage
32
+
33
+ ### JSPayment
34
+
35
+ - 创建支付请求 [get_brand_request]
36
+
37
+ - 回调验证 [notify_verify?]
38
+
39
+ - 发货通知 [deliver_notify]
40
+
41
+ - 获取订单状态 [get_order_query]
42
+
43
+ - 获取access_token [get_access_token]
44
+
45
+ ### NativePayment
46
+
47
+ TODO
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( http://github.com/RaymondChou/Rwepay/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/Rwepay.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Rwepay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "Rwepay"
8
+ spec.version = Rwepay::VERSION
9
+ spec.authors = ["RaymondChou"]
10
+ spec.email = ["freezestart@gmail.com"]
11
+ spec.summary = "WeChat Pay gem"
12
+ spec.description = "WeChat Pay gem"
13
+ spec.homepage = "https://github.com/RaymondChou/Rwepay"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "faraday"
24
+ end
data/lib/Rwepay.rb ADDED
@@ -0,0 +1,93 @@
1
+ require "Rwepay/version"
2
+ require "Rwepay/common"
3
+ require 'json'
4
+
5
+ module Rwepay
6
+
7
+ class JSPayment
8
+ attr_accessor :configs
9
+ attr_accessor :package_options
10
+ attr_accessor :brand_options
11
+
12
+ def initialize(configs = {})
13
+ @configs = Rwepay::Common.configs_check configs,
14
+ [:app_id, :partner_id, :app_key, :partner_key]
15
+ end
16
+
17
+ def get_brand_request(options = {})
18
+ brand_options = Rwepay::Common.configs_check options,
19
+ [:body, :notify_url, :out_trade_no, :total_fee, :spbill_create_ip]
20
+
21
+ # create package
22
+ brand_options[:key] ||= @configs[:partner_key]
23
+ brand_options[:partner] ||= @configs[:partner_id]
24
+ brand_options[:fee_type] ||= '1'
25
+ brand_options[:bank_type] ||= 'WX'
26
+ brand_options[:input_charset] ||= 'GBK'
27
+
28
+ final_params = Hash.new
29
+ final_params[:appId] = @configs[:app_id]
30
+ final_params[:timeStamp] = Rwepay::Common.get_timestamps
31
+ final_params[:nonceStr] = Rwepay::Common.get_nonce_str
32
+ final_params[:package] = Rwepay::Common.get_package(brand_options)
33
+ final_params[:signType] = 'SHA1'
34
+ final_params[:paySign] = Rwepay::Common.pay_sign(
35
+ :appid => @configs[:app_id],
36
+ :appkey => @configs[:app_key],
37
+ :noncestr => final_params[:nonceStr],
38
+ :package => final_params[:package],
39
+ :timestamp => final_params[:timeStamp],
40
+ )
41
+ final_params.to_json
42
+ end
43
+
44
+ def notify_verify?(params = {})
45
+ params['key'] ||= @configs[:partner_key]
46
+ Rwepay::Common.notify_sign(params) == params['sign'] and params['trade_state'] == '0'
47
+ end
48
+
49
+ def deliver_notify(options = {})
50
+ options = Rwepay::Common.configs_check options,
51
+ [:access_token, :open_id, :trans_id, :out_trade_no, :deliver_timestamp, :deliver_status, :deliver_msg]
52
+
53
+ options[:app_id] = @configs[:app_id]
54
+ options[:app_key] = @configs[:app_key]
55
+
56
+ Rwepay::Common.send_deliver_notify(options, options[:access_token])
57
+ end
58
+
59
+ def get_order_query(options = {})
60
+ options = Rwepay::Common.configs_check options,
61
+ [:access_token, :out_trade_no]
62
+
63
+ options[:app_id] = @configs[:app_id]
64
+ options[:app_key] = @configs[:app_key]
65
+ options[:partner_key] = @configs[:partner_key]
66
+ options[:partner_id] = @configs[:partner_id]
67
+
68
+ Rwepay::Common.get_order_query(options, options[:access_token])
69
+ end
70
+
71
+ # expire 7200 seconds, must be cached!
72
+ def get_access_token(app_secret)
73
+ begin
74
+ response = Faraday.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{@configs[:app_id]}&secret=#{app_secret}")
75
+ response = JSON.parse response.body
76
+ if response['access_token'] != nil
77
+ response['access_token']
78
+ else
79
+ false
80
+ end
81
+ rescue
82
+ false
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+ # @TODO
89
+ class NativePayment
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,199 @@
1
+ module Rwepay::Common
2
+
3
+ require 'digest/sha1'
4
+ require 'digest/md5'
5
+ require 'securerandom'
6
+ require 'uri'
7
+ require 'faraday'
8
+
9
+ def self.configs_check(configs = {}, requires = [])
10
+ requires.each do |require|
11
+ unless configs.include? require
12
+ raise "Rwepay Error, configs required hash symbol :#{require}"
13
+ end
14
+ end
15
+ configs
16
+ end
17
+
18
+ def self.get_nonce_str
19
+ SecureRandom.hex 16
20
+ end
21
+
22
+ def self.create_sign_string(sign_params = {}, sort = true)
23
+ #对原串进行签名,注意这里不要对任何字段进行编码。这里是将参数按照key=value进行字典排序后组成下面的字符串,在这个字符串最后拼接上key=XXXX。由于这里的字段固定,因此只需要按照这个顺序进行排序即可。
24
+
25
+ result_string = ''
26
+ key = sign_params[:key]
27
+ #是否排序
28
+ if sort
29
+ sign_params = sign_params.sort
30
+ end
31
+
32
+ sign_params.each{|key,value|
33
+ result_string += (key.to_s + '=' + value.to_s + '&') if key.to_s != 'key'
34
+ }
35
+
36
+ "#{result_string}key=#{key}"
37
+ end
38
+
39
+ def self.create_pay_sign_string(sign_params = {}, sort = true)
40
+ result_string = ''
41
+ key = sign_params[:key]
42
+ #是否排序
43
+ if sort
44
+ sign_params = sign_params.sort
45
+ end
46
+
47
+ sign_params.each{|key,value|
48
+ result_string += (key.to_s + '=' + value.to_s + '&')
49
+ }
50
+
51
+ result_string[0, result_string.length - 1]
52
+ end
53
+
54
+ def self.md5_sign(for_sign_string)
55
+ Digest::MD5.hexdigest(for_sign_string).upcase
56
+ end
57
+
58
+ def self.sha1_sign(for_sign_string)
59
+ Digest::SHA1.hexdigest(for_sign_string)
60
+ end
61
+
62
+ def self.result_params_filter(sign_params, sort = true)
63
+ result_string = ''
64
+ #是否排序
65
+ if sort
66
+ sign_params = sign_params.sort
67
+ end
68
+
69
+ sign_params.each{|key,value|
70
+ encode_value = URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
71
+ result_string += (key.to_s + '=' + encode_value + '&') if key.to_s != 'key'
72
+ }
73
+ #去掉末尾的&
74
+ result_string = result_string[0, result_string.length - 1]
75
+ return result_string
76
+ end
77
+
78
+ #get_package :bank_type, :body, :fee_type, :input_charset, :notify_url, :out_trade_no, :partner, :spbill_create_ip, :total_fee, :key
79
+ def self.get_package(sign_params = {})
80
+ for_sign_string = create_sign_string sign_params
81
+ md5_signed_string = md5_sign for_sign_string
82
+ result_params = result_params_filter sign_params
83
+
84
+ "#{result_params}&sign=#{md5_signed_string}"
85
+ end
86
+
87
+ #sign_string :appid, :appkey, :noncestr, :package, :timestamp
88
+ def self.pay_sign(sign_params = {})
89
+ for_sign_string = create_pay_sign_string sign_params
90
+ sha1_signed_string = sha1_sign for_sign_string
91
+ sha1_signed_string
92
+ end
93
+
94
+ def self.get_timestamps
95
+ Time.now.to_i.to_s
96
+ end
97
+
98
+ def self.creat_notify_sign_string(params = {})
99
+ key = params['key']
100
+ result_string = ''
101
+ sign_params = params.sort
102
+ sign_params.each do |key, value|
103
+ unless value.nil? or value == '' or key.to_s == 'key' or key.to_s == 'xml' or key.to_s == 'sign' or key.to_s == 'action'or key.to_s == 'controller'
104
+ result_string += (key.to_s + '=' + value.to_s + '&')
105
+ end
106
+ end
107
+
108
+ "#{result_string}key=#{key}"
109
+ end
110
+
111
+ def self.notify_sign(sign_params = {})
112
+ for_sign_string = creat_notify_sign_string sign_params
113
+ md5_signed_string = md5_sign for_sign_string
114
+
115
+ md5_signed_string
116
+ end
117
+
118
+ def self.send_deliver_notify(options = {}, access_token)
119
+ for_sign_data = {
120
+ :appid => options[:app_id],
121
+ :appkey => options[:app_key],
122
+ :openid => options[:open_id],
123
+ :transid => options[:trans_id],
124
+ :out_trade_no => options[:out_trade_no],
125
+ :deliver_timestamp => options[:deliver_timestamp],
126
+ :deliver_status => options[:deliver_status],
127
+ :deliver_msg => options[:deliver_msg]
128
+ }
129
+
130
+ result_string = ''
131
+ sign_params = for_sign_data.sort
132
+
133
+ sign_params.each{|key,value|
134
+ result_string += (key.to_s + '=' + value.to_s + '&')
135
+ }
136
+ result_string = result_string[0, result_string.length - 1]
137
+
138
+ for_sign_data[:app_signature] = sha1_sign result_string
139
+ for_sign_data[:sign_method] = 'sha1'
140
+
141
+ for_sign_data.delete :appkey
142
+
143
+ begin
144
+ conn = Faraday.new(:url => "https://api.weixin.qq.com/pay/delivernotify?access_token=#{access_token}")
145
+ response = conn.post do |req|
146
+ req.body = for_sign_data.to_json
147
+ end
148
+ response = JSON.parse response.body
149
+ if response['errcode'] == 0
150
+ return true, nil
151
+ else
152
+ return false, response
153
+ end
154
+ rescue => err
155
+ return false, err
156
+ end
157
+
158
+ end
159
+
160
+ def self.get_order_query(options = {}, access_token)
161
+ package = "out_trade_no=#{options[:out_trade_no]}&partner=#{options[:partner_id]}"
162
+ md5_package_sign = md5_sign "#{package}&key=#{options[:partner_key]}"
163
+ for_sign_data = {
164
+ :appid => options[:app_id],
165
+ :appkey => options[:app_key],
166
+ :package => "#{package}&sign=#{md5_package_sign}",
167
+ :timestamp => get_timestamps
168
+ }
169
+
170
+ result_string = ''
171
+ sign_params = for_sign_data.sort
172
+
173
+ sign_params.each{|key,value|
174
+ result_string += (key.to_s + '=' + value.to_s + '&')
175
+ }
176
+ result_string = result_string[0, result_string.length - 1]
177
+
178
+ for_sign_data[:app_signature] = sha1_sign result_string
179
+ for_sign_data[:sign_method] = 'sha1'
180
+
181
+ for_sign_data.delete :appkey
182
+
183
+ begin
184
+ conn = Faraday.new(:url => "https://api.weixin.qq.com/pay/orderquery?access_token=#{access_token}")
185
+ response = conn.post do |req|
186
+ req.body = for_sign_data.to_json
187
+ end
188
+ response = JSON.parse response.body
189
+ if response['errcode'] == 0
190
+ return true, response
191
+ else
192
+ return false, response
193
+ end
194
+ rescue => err
195
+ return false, err
196
+ end
197
+ end
198
+
199
+ end
@@ -0,0 +1,3 @@
1
+ module Rwepay
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ require 'test/unit'
3
+ require 'Rwepay'
4
+
5
+ class TestRwepay < Test::Unit::TestCase
6
+
7
+ def test_get_brand_request
8
+ configs = {
9
+ :app_id => 'wxf8b4f85f3a794e77',
10
+ :partner_id => '1900000109',
11
+ :app_key => '2Wozy2aksie1puXUBpWD8oZxiD1DfQuEaiC7KcRATv1Ino3mdopKaPGQQ7TtkNySuAmCaDCrw4xhPY5qKTBl7Fzm0RgR3c0WaVYIXZARsxzHV2x7iwPPzOz94dnwPWSn',
12
+ :partner_key => '8934e7d15453e97507ef794cf7b0519d'
13
+ }
14
+
15
+ options = {
16
+ :body => '测试商品',
17
+ :notify_url => 'http://www.qq.com',
18
+ :out_trade_no => '16642817866003386000',
19
+ :total_fee => '1',
20
+ :spbill_create_ip => '127.0.0.1',
21
+ }
22
+
23
+ payment = Rwepay::JSPayment.new configs
24
+ result_json = payment.get_brand_request(options)
25
+ assert_equal result_json, result_json
26
+ end
27
+
28
+ #def test_notify_verify
29
+ # params = "xxx"
30
+ #
31
+ # configs = {
32
+ # :app_id => 'wxf8b4f85f3a794e77',
33
+ # :partner_id => '1900000109',
34
+ # :app_key => '2Wozy2aksie1puXUBpWD8oZxiD1DfQuEaiC7KcRATv1Ino3mdopKaPGQQ7TtkNySuAmCaDCrw4xhPY5qKTBl7Fzm0RgR3c0WaVYIXZARsxzHV2x7iwPPzOz94dnwPWSn',
35
+ # :partner_key => '8934e7d15453e97507ef794cf7b0519d'
36
+ # }
37
+ #
38
+ # payment = Rwepay::JSPayment.new configs
39
+ # assert_equal true, payment.notify_verify?(params)
40
+ #end
41
+
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Rwepay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - RaymondChou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-29 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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: faraday
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: WeChat Pay gem
56
+ email:
57
+ - freezestart@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - Rwepay.gemspec
68
+ - lib/Rwepay.rb
69
+ - lib/Rwepay/common.rb
70
+ - lib/Rwepay/version.rb
71
+ - test/test_rwepay.rb
72
+ homepage: https://github.com/RaymondChou/Rwepay
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: WeChat Pay gem
96
+ test_files:
97
+ - test/test_rwepay.rb