agideo_weixin_pay 0.0.6

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: 4e81d7cf27b295d0d16a2aa5ab1d103c06283a52
4
+ data.tar.gz: 0b8c78a260be5445577c1dec2ec5bb5f30d240e6
5
+ SHA512:
6
+ metadata.gz: 7c344e21ff3c16bba8add25aeed6cc5ce7edb58d8e0d03d40fe766e2ab8337eccdba3edaf4ad34bf9c4c417b34063edcaf836d80c87653672d2e61e1b7d7cb61
7
+ data.tar.gz: 34a888d9d81e56418018c7cb643e3d42f8039fa477fa63eda06fd1769cbd211524b8afcb2510fce800355bbc23cd9309b228d294c4af794adbfa73e565bbe3c7
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weixin_pay.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jim
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,157 @@
1
+ # WeixinPay
2
+
3
+ A simple Wechat pay ruby gem, without unnecessary magic or wrapper.
4
+ copied from [alipay](https://github.com/chloerei/alipay) .
5
+
6
+ Please read official document first: <https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN>.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'weixin_pay', :git => 'https://github.com/agideo/weixin_pay'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```sh
19
+ $ bundle
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Config
25
+
26
+ Create `config/initializers/weixin_pay.rb` and put following configurations into it.
27
+
28
+ ```ruby
29
+ # required
30
+ WeixinPay.appid = 'YOUR_APPID'
31
+ WeixinPay.key = 'YOUR_KEY'
32
+ WeixinPay.mch_id = 'YOUR_MCH_ID'
33
+ ```
34
+
35
+ Note: You should create your APIKEY (Link to [微信商户平台](https://pay.weixin.qq.com/index.php/home/login)) first if you haven't, and pay attention that **the length of the APIKEY should be 32**.
36
+
37
+ ### APIs
38
+
39
+ **TODO check required fields**
40
+
41
+ #### micropay (提交刷卡支付)
42
+
43
+ WeixinPay supports REST.
44
+
45
+ ```ruby
46
+ #order_code build 32bit uuid
47
+ order_code = SecureRandom.uuid.tr('-', '')
48
+
49
+ WeixinPay::Pay.micrpay({
50
+ attach: "微信刷卡支付",
51
+ body: "购买商品",
52
+ device_info: '88888888',
53
+ out_trade_no: order_code,
54
+ spbill_create_ip: '127.0.0.1',
55
+ total_fee: 1,
56
+ auth_code: 'xxxxxxxxxxxxxxxxxxx' #scan by scaner from weixin QR (扫描枪扫描微信二维码所获得)
57
+ })
58
+ ```
59
+
60
+ `WeixinPay::Pay.micrpay params` will create an payment request and return a WeixinPay::Result instance(subclass of Hash) contains parsed result.
61
+
62
+ The result would be like this.
63
+
64
+ ```ruby
65
+ result = WeixinPay::Pay.micrpay({
66
+ ...
67
+ ...
68
+ })
69
+
70
+ result.raw
71
+ # => {
72
+ # "return_code"=>"SUCCESS",
73
+ # "return_msg"=>"OK",
74
+ # "appid"=>"wx0bbf1b0caa980033",
75
+ # "mch_id"=>"13036914",
76
+ # "device_info"=>"88888888",
77
+ # "nonce_str"=>"lyAFYWYrbn33Y065",
78
+ # "sign"=>"9EC971A06B1CD5B74DDAABA961DD90F",
79
+ # "result_code"=>"SUCCESS",
80
+ # "openid"=>"3Ea4HjqNk5BMEG6Ww6FVqO1tt1bI",
81
+ # "is_subscribe"=>"Y",
82
+ # "trade_type"=>"MICROPAY",
83
+ # "bank_type"=>"CMB_DEBIT",
84
+ # "total_fee"=>"1",
85
+ # "fee_type"=>"CNY",
86
+ # "transaction_id"=>"1004840644201510151204462655",
87
+ # "out_trade_no"=>"e4bcf8b3f6b54c70ad784a73b254a556",
88
+ # "attach"=>"微信刷卡支付", "time_end"=>"20151015133846",
89
+ # "cash_fee"=>"1",
90
+ # "cash_fee_type"=>""
91
+ }
92
+ ```
93
+
94
+ Return true if both `return_code` and `result_code` equal `SUCCESS`
95
+
96
+ ```ruby
97
+ result.success? # => true
98
+ ```
99
+
100
+ #### orderquery (查询订单)
101
+
102
+ WeixinPay supports REST.
103
+
104
+ ```ruby
105
+ WeixinPay::Pay.orderquery({
106
+ out_trade_no: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
107
+ })
108
+ ```
109
+
110
+ `WeixinPay::Pay.orderquery params` will create an payment request and return a WeixinPay::Result instance(subclass of Hash) contains parsed result.
111
+
112
+ The result would be like this.
113
+
114
+ ```ruby
115
+ result = WeixinPay::Pay.orderquery({
116
+ ...
117
+ ...
118
+ })
119
+
120
+ result.raw
121
+ {
122
+ "success":true,
123
+ "paid":true,
124
+ "out_trade_no":"b6c603e17874453fb513f7b46a5706e0",
125
+ "trade_state":"SUCCESS",
126
+ "errorobj":{
127
+ "result_code":"SUCCESS",
128
+ "err_code":null,
129
+ "err_code_des":null,
130
+ "return_msg":"OK"
131
+ }
132
+ }
133
+ ```
134
+
135
+ Details link to Weixin API doc [提交刷卡支付API](https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1)
136
+
137
+ ## TODO
138
+
139
+ Write test for WeixinPay gem
140
+
141
+ ## Contributing
142
+
143
+ Bug report or pull request are welcome.
144
+
145
+ ### Make a pull request
146
+
147
+ - 1. Fork it Fork it ( https://github.com/agideo/weixin_pay )
148
+ - 2. Create your feature branch (`git checkout -b my-new-feature`)
149
+ - 3. Commit your changes (`git commit -am 'Add some feature'`)
150
+ - 4. Push to the branch (`git push origin my-new-feature`)
151
+ - 5. Create new Pull Request
152
+
153
+ Please write unit test with your code if necessary.
154
+
155
+ ## License
156
+
157
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/weixin_pay.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "weixin_pay/version"
2
+ require "weixin_pay/sign"
3
+ require "weixin_pay/pay"
4
+ require "weixin_pay/result"
5
+
6
+ module WeixinPay
7
+ class << self
8
+ attr_accessor :appid, :mch_id, :key
9
+
10
+ def extra_rest_client_options
11
+ {}
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,103 @@
1
+ require 'rest_client'
2
+ require 'active_support/core_ext/hash/conversions'
3
+
4
+ module WeixinPay
5
+ module Pay
6
+ API_BASE_URL = "https://api.mch.weixin.qq.com/pay"
7
+ API_MICRPAY_URL = "#{API_BASE_URL}/micropay"
8
+ API_ORDERQUERY_URL = "#{API_BASE_URL}/orderquery"
9
+ INVOKE_MICRPAY_REQUIRED_FIELDS = %w(appid mch_id nonce_str sign body out_trade_no total_fee spbill_create_ip auth_code)
10
+
11
+ def self.micrpay(params={})
12
+ params = {
13
+ appid: WeixinPay.appid,
14
+ mch_id: WeixinPay.mch_id,
15
+ nonce_str: SecureRandom.uuid.tr('-', ''),
16
+ time_expire: Time.now.since(1.minutes).strftime("%Y%m%d%H%M%S")
17
+ }.merge(params)
18
+
19
+ # TODO check_required_params(params, INVOKE_MICRPAY_REQUIRED_FIELDS)
20
+
21
+ remote_params = params.merge(sign: WeixinPay::Sign.generate(params))
22
+ xml = make_xml(remote_params)
23
+ post_pay(API_MICRPAY_URL, xml)
24
+ end
25
+
26
+ def self.orderquery(params={})
27
+ params = {
28
+ appid: WeixinPay.appid,
29
+ mch_id: WeixinPay.mch_id,
30
+ nonce_str: SecureRandom.uuid.tr('-', '')
31
+ }.merge(params)
32
+
33
+ # TODO check_required_params(params, INVOKE_MICRPAY_REQUIRED_FIELDS)
34
+
35
+ remote_params = params.merge(sign: WeixinPay::Sign.generate(params))
36
+ xml = make_xml(remote_params)
37
+ post_pay(API_ORDERQUERY_URL, xml)
38
+ end
39
+
40
+ private
41
+
42
+ def self.post_pay(url, xml)
43
+ Rails.logger.info url
44
+ Rails.logger.info xml
45
+ result = RestClient::Request.execute({
46
+ method: :post,
47
+ url: url,
48
+ payload: xml,
49
+ headers: { content_type: 'application/xml' }
50
+ }.merge(WeixinPay.extra_rest_client_options))
51
+
52
+ if result
53
+ WeixinPay::Result.new Hash.from_xml(result)
54
+ else
55
+ nil
56
+ end
57
+ end
58
+
59
+ # 提交刷卡支付
60
+ # <xml>
61
+ # <appid>wx2421b1c4370ec43b</appid>
62
+ # <attach>订单额外描述</attach>
63
+ # <auth_code>120269300684844649</auth_code>
64
+ # <body>刷卡支付测试</body>
65
+ # <device_info>1000</device_info>
66
+ # <goods_tag></goods_tag>
67
+ # <mch_id>10000100</mch_id>
68
+ # <nonce_str>8aaee146b1dee7cec9100add9b96cbe2</nonce_str>
69
+ # <out_trade_no>1415757673</out_trade_no>
70
+ # <spbill_create_ip>14.17.22.52</spbill_create_ip>
71
+ # <time_expire></time_expire>
72
+ # <total_fee>1</total_fee>
73
+ # <sign>C29DB7DB1FD4136B84AE35604756362C</sign>
74
+ # </xml>
75
+ # 查询订单
76
+ # <xml>
77
+ # <appid>wx2421b1c4370ec43b</appid>
78
+ # <mch_id>10000100</mch_id>
79
+ # <nonce_str>ec2316275641faa3aacf3cc599e8730f</nonce_str>
80
+ # <transaction_id>1008450740201411110005820873</transaction_id>
81
+ # <out_trade_no>2008450740201411110005820874</out_trade_no>
82
+ # <sign>FDD167FAA73459FD921B144BAF4F4CA2</sign>
83
+ # </xml>
84
+ def self.make_xml(params)
85
+ xml_body = params.map do |k, v|
86
+ "<#{k}>#{v}</#{k}>"
87
+ end.join("\n")
88
+ xml = <<-xml_str
89
+ <xml>
90
+ #{xml_body}
91
+ </xml>
92
+ xml_str
93
+ xml
94
+ end
95
+
96
+ def self.check_required_params(params, names)
97
+ names.each do |name|
98
+ warn("WeixinPay Warn: missing required option: #{name}") unless params.has_key?(name)
99
+ end
100
+ end
101
+ end
102
+ end
103
+
@@ -0,0 +1,24 @@
1
+ module WeixinPay
2
+ class Result < ::Hash
3
+ SUCCESS_FLAG = 'SUCCESS'.freeze
4
+
5
+ def initialize(result)
6
+ super
7
+
8
+ if result['xml'].class == Hash
9
+ @raw = result['xml']
10
+ result['xml'].each_pair do |k, v|
11
+ self[k] = v
12
+ end
13
+ end
14
+ end
15
+
16
+ def raw
17
+ @raw
18
+ end
19
+
20
+ def success?
21
+ self['return_code'] == SUCCESS_FLAG && self['result_code'] == SUCCESS_FLAG
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'digest/md5'
2
+
3
+ module WeixinPay
4
+ module Sign
5
+ def self.generate(params)
6
+ key = WeixinPay.key
7
+ query_str = params.sort.map do |k, v|
8
+ "#{k}=#{v}"
9
+ end.join('&')
10
+ Rails.logger.info query_str
11
+
12
+ Rails.logger.info "#{query_str}&key=#{key}"
13
+ Digest::MD5.hexdigest("#{query_str}&key=#{key}").upcase
14
+ end
15
+
16
+ def self.verify?(params)
17
+ params = params.dup
18
+ sign = params.delete('sign') || params.delete(:sign)
19
+
20
+ generate(params) == sign
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module WeixinPay
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'weixin_pay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "agideo_weixin_pay"
8
+ spec.version = WeixinPay::VERSION
9
+ spec.authors = ["Jim"]
10
+ spec.email = ["jim.jin2006@gmail.com"]
11
+ spec.summary = %q{ Write a short summary. Required.}
12
+ spec.description = %q{ Write a longer description. Optional.}
13
+ spec.homepage = ""
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_runtime_dependency "rest-client", '>= 1.6.7'
22
+ spec.add_runtime_dependency 'mime-types'
23
+ spec.add_runtime_dependency 'json', "~> 1.8"
24
+
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agideo_weixin_pay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Jim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-14 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.6.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.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
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: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
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
+ description: " Write a longer description. Optional."
84
+ email:
85
+ - jim.jin2006@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/weixin_pay.rb
96
+ - lib/weixin_pay/pay.rb
97
+ - lib/weixin_pay/result.rb
98
+ - lib/weixin_pay/sign.rb
99
+ - lib/weixin_pay/version.rb
100
+ - weixin_pay.gemspec
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Write a short summary. Required.
125
+ test_files: []