wechatpay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a8cfdad7f4a9a82b59f764fe4a812b409d422dbd
4
+ data.tar.gz: 42bfaefb745498559eb559ec4334b769caa2a15f
5
+ SHA512:
6
+ metadata.gz: f028e51a0e4181458473ee45ae5f008803ade8b34af23881c9001cd0addc4e8114d4acebf6c3bcadc92db89a2862149f3662a247b209f4b65145ba2a57e50315
7
+ data.tar.gz: bfd622f45f8452beee1511f35228665d7e8402afb53276ef2016be78f221c7e09dbc08ee75759d98d45931ac133988cb34dfecb30e8eaaa05712fe4a0fd37a34
@@ -0,0 +1,22 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wechatpay.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Strikingly.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Junchen Xia
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.
@@ -0,0 +1,69 @@
1
+ # Wechatpay
2
+
3
+ A gem to deal with Wechatpay. The Wechatpay official document can be found [here](http://mch.weixin.qq.com/wiki/doc/api/index.html). You will receive the backend document when you get the Wechatpay access.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'wechatpay', '~> 0.0.1'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install wechatpay
18
+
19
+ ## Usage
20
+
21
+ ### Configuration
22
+
23
+ ```ruby
24
+ Wechatpay.configure do |w|
25
+ w.app_id = 'wx123456'
26
+ w.mch_id = 'abcdefg'
27
+ w.payment_key = 'abc123'
28
+ end
29
+ ```
30
+
31
+ The params contains:
32
+
33
+ - app_id: 公众账号ID
34
+ - mch_id: 微信商户账号
35
+ - payment_key: 商户支付秘钥
36
+
37
+ ### Unified Order/统一下单
38
+ ```ruby
39
+ options = {
40
+ body: 'awesome product name'
41
+ out_trade_no: '20150101030320204',
42
+ total_fee: 100, # in cents
43
+ spbill_create_ip: '127.0.0.1',
44
+ notify_url: 'http://example.com/notify',
45
+ trade_type: "JSAPI",
46
+ openid: '123123123'
47
+ }
48
+ response = Wechatpay::Service.unified_order(options)
49
+ prepay_id = response["prepay_id"]
50
+ ```
51
+
52
+ ### Generate payment params
53
+ ```ruby
54
+ params = Wechatpay::Service.pay_params(prepay_id)
55
+ # {
56
+ # timestamp: 0, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
57
+ # nonceStr: '', // 支付签名随机串,不长于 32 位
58
+ # package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
59
+ # signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
60
+ # paySign: '', // 支付签名
61
+ # }
62
+ ```
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/[my-github-username]/wechatpay/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = ['--color']
8
+ t.verbose = false
9
+ end
10
+
11
+ task :default => :spec
12
+
@@ -0,0 +1,24 @@
1
+ require "wechatpay/version"
2
+ require "wechatpay/sign"
3
+ require "wechatpay/utils"
4
+ require "wechatpay/service"
5
+
6
+ module Wechatpay
7
+
8
+ class Config
9
+
10
+ UNIFIED_ORDER_URL = 'https://api.mch.weixin.qq.com/pay/unifiedorder'
11
+
12
+ class << self
13
+ # app_id: 公众账号id, mch_id: 商户号, payment_key: 商户支付秘钥
14
+ attr_accessor :app_id, :mch_id, :payment_key
15
+ end
16
+ end
17
+
18
+ class << self
19
+ def configure
20
+ yield Config if block_given?
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_support/core_ext/hash'
2
+ require 'httparty'
3
+
4
+ module Wechatpay
5
+ module Service
6
+ class << self
7
+ def unified_order(options)
8
+ config = Wechatpay::Config
9
+ options = {
10
+ appid: config.app_id,
11
+ mch_id: config.mch_id,
12
+ nonce_str: Wechatpay::Utils.nonce_str
13
+ }.merge(options)
14
+
15
+ body = Wechatpay::Utils.add_sign_and_generate_xml_body(options)
16
+ response = HTTParty.post(
17
+ Wechatpay::Config::UNIFIED_ORDER_URL,
18
+ :body => body
19
+ )
20
+ result = Hash.from_xml(response.body)["xml"]
21
+ if Wechatpay::Sign.valid?(result)
22
+ result
23
+ else
24
+ nil
25
+ end
26
+ end
27
+
28
+ def pay_params(prepay_id)
29
+ options = {
30
+ appId: Wechatpay::Config.app_id,
31
+ timeStamp: Wechatpay::Utils.timestamp,
32
+ nonceStr: Wechatpay::Utils.nonce_str,
33
+ package: "prepay_id=#{prepay_id}",
34
+ signType: "MD5"
35
+ }
36
+ options[:paySign] = Wechatpay::Sign.md5(options)
37
+ options.delete :appId
38
+ options
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ module Wechatpay
2
+ module Sign
3
+ def self.md5(params)
4
+ str = params.sort.map{ |i| i.join("=") }.join('&')
5
+ str << "&key=#{Wechatpay::Config.payment_key}"
6
+ Digest::MD5.hexdigest(str).upcase
7
+ end
8
+
9
+ def self.valid?(params)
10
+ signature = params.delete("sign")
11
+ md5(params) == signature
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,53 @@
1
+ module Wechatpay
2
+ module Utils
3
+ class << self
4
+ def cdata(hash)
5
+ with_hash(hash) do
6
+ hash.each { |k, v| hash[k] = cdata_cell(v) }
7
+ end
8
+ end
9
+
10
+ def cdata_cell(item)
11
+ if item.is_a? Numeric
12
+ item
13
+ else
14
+ "<![CDATA[#{item}]]>"
15
+ end
16
+ end
17
+
18
+ def to_xml(hash)
19
+ with_hash(hash) do
20
+ str = "<xml>\n"
21
+ hash.each do |k, v|
22
+ str << "<#{k}>#{v}</#{k}>\n"
23
+ end
24
+ str << "</xml>"
25
+ end
26
+ end
27
+
28
+ def add_sign_and_generate_xml_body(options)
29
+ with_hash(options) do
30
+ options[:sign] = Wechatpay::Sign.md5(options)
31
+ options = Wechatpay::Utils.cdata(options)
32
+ Wechatpay::Utils.to_xml(options)
33
+ end
34
+ end
35
+
36
+ def with_hash(hash)
37
+ if hash.is_a? Hash
38
+ yield hash
39
+ else
40
+ raise ArgumentError
41
+ end
42
+ end
43
+
44
+ def timestamp
45
+ Time.now.to_i
46
+ end
47
+
48
+ def nonce_str
49
+ SecureRandom.urlsafe_base64(nil, false)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Wechatpay
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'pry'
2
+ require 'wechatpay'
3
+
4
+
5
+ Wechatpay.configure do |w|
6
+ w.payment_key = 'test_key'
7
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wechatpay::Sign do
4
+ let (:params) {
5
+ {a:1, b:2}
6
+ }
7
+ let (:inversed_params) {
8
+ {b:2, a:1}
9
+ }
10
+ let (:params_string) { "a=1&b=2&key=#{Wechatpay::Config.payment_key}" }
11
+
12
+ describe "#md5" do
13
+ it 'returns the same with different params order' do
14
+ sign1 = Wechatpay::Sign.md5(params)
15
+ sign2 = Wechatpay::Sign.md5(inversed_params)
16
+ expect(sign1).to eq(sign2)
17
+ end
18
+
19
+ it 'signs the params with md5' do
20
+ sign = Wechatpay::Sign.md5(params)
21
+ expect(sign).to eq(Digest::MD5.hexdigest(params_string).upcase)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wechatpay::Utils do
4
+ let (:utils) { Wechatpay::Utils }
5
+ let (:hash) { {a:1, b:'TEST'} }
6
+
7
+ describe '.cdata_cell' do
8
+ it 'doesnt convert number' do
9
+ result = utils.cdata_cell(1)
10
+ expect(result).to eq(1)
11
+ end
12
+
13
+ it 'wrap others' do
14
+ result = utils.cdata_cell("TEST")
15
+ expect(result).to eq('<![CDATA[TEST]]>')
16
+ end
17
+ end
18
+
19
+ describe '.cdata' do
20
+ it 'cdata a hash' do
21
+ result = utils.cdata(hash)
22
+ expect(result).to eq({a: 1, b: '<![CDATA[TEST]]>'})
23
+ end
24
+
25
+ it 'raise error when not a hash' do
26
+ expect { utils.cdata("123") }.to raise_error ArgumentError
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wechatpay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wechatpay"
8
+ spec.version = Wechatpay::VERSION
9
+ spec.authors = ["Junchen Xia"]
10
+ spec.email = ["junchen@striking.ly"]
11
+ spec.summary = %q{A gem to deal with Wechat payment.}
12
+ spec.homepage = "https://github.com/strikingly/wechat-pay"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'activesupport'
21
+ spec.add_dependency 'builder'
22
+ spec.add_dependency 'httparty'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-remote"
30
+ spec.add_development_dependency "pry-nav"
31
+
32
+ spec.add_development_dependency "fakeweb"
33
+ spec.add_development_dependency "awesome_print"
34
+ end
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wechatpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Junchen Xia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: builder
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-remote
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-nav
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: fakeweb
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: awesome_print
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - junchen@striking.ly
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - Gemfile
176
+ - LICENSE
177
+ - LICENSE.txt
178
+ - README.md
179
+ - Rakefile
180
+ - lib/wechatpay.rb
181
+ - lib/wechatpay/service.rb
182
+ - lib/wechatpay/sign.rb
183
+ - lib/wechatpay/utils.rb
184
+ - lib/wechatpay/version.rb
185
+ - spec/spec_helper.rb
186
+ - spec/wechatpay/sign_spec.rb
187
+ - spec/wechatpay/utils_spec.rb
188
+ - wechatpay.gemspec
189
+ homepage: https://github.com/strikingly/wechat-pay
190
+ licenses:
191
+ - MIT
192
+ metadata: {}
193
+ post_install_message:
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubyforge_project:
209
+ rubygems_version: 2.2.2
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: A gem to deal with Wechat payment.
213
+ test_files:
214
+ - spec/spec_helper.rb
215
+ - spec/wechatpay/sign_spec.rb
216
+ - spec/wechatpay/utils_spec.rb