wx_pay_api 0.2.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: 440110a4f048c0b831207aca7f41b72abdb1bb62
4
+ data.tar.gz: 7a11dcff841ff9f8f930e4f72f9c549de02e8cbf
5
+ SHA512:
6
+ metadata.gz: dec65a29f8482c7d2778c8b1c16f8490449357c87f03a26bd720019de85d60ade38bca7c270374d38f2c11ecfe5fd0bf1b5c115e58821787e2de47ebd1f5d728
7
+ data.tar.gz: fc1d9940e7db669552cdac6c82376fefb1599e3bb078b19ac115288ab9ddea0ac71c37b7f98640061b72e8b5541ae0fd2af00e311aec6f655f7088274edd2e99
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
+ class Result < ::Hash
3
+ SUCCESS_FLAG = 'SUCCESS'.freeze
4
+
5
+ def initialize(result)
6
+ super
7
+ if result['xml'].class == Hash
8
+ result['xml'].each_pair do |k, v|
9
+ self[k] = v
10
+ end
11
+ end
12
+ end
13
+
14
+ def success?
15
+ self['return_code'] == SUCCESS_FLAG && self['result_code'] == SUCCESS_FLAG
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,144 @@
1
+ require 'rest_client'
2
+ require 'active_support/core_ext/hash/conversions'
3
+
4
+ module WxPay
5
+ module Service
6
+ GATEWAY_URL = 'https://api.mch.weixin.qq.com'.freeze
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
+ appid: WxPay.appid,
12
+ mch_id: WxPay.mch_id
13
+ }.merge(params)
14
+ check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
15
+ r = invoke_remote("#{GATEWAY_URL}/pay/unifiedorder", params)
16
+ yield r if block_given?
17
+ r
18
+ end
19
+
20
+ GENERATE_APP_PAY_REQ_REQUIRED_FIELDS = %i(prepayid noncestr)
21
+ def self.generate_app_pay_req(params)
22
+ params = {
23
+ appid: WxPay.appid,
24
+ partnerid: WxPay.mch_id,
25
+ package: 'Sign=WXPay',
26
+ timestamp: Time.now.to_i.to_s
27
+ }.merge(params)
28
+ check_required_options(params, GENERATE_APP_PAY_REQ_REQUIRED_FIELDS)
29
+ params[:sign] = WxPay::Sign.generate(params)
30
+ params
31
+ end
32
+
33
+ INVOKE_REFUND_REQUIRED_FIELDS = %i(transaction_id out_trade_no out_refund_no total_fee refund_fee cert_path)
34
+ def self.invoke_refund(params)
35
+ params = {
36
+ appid: WxPay.appid,
37
+ mch_id: WxPay.mch_id,
38
+ op_user_id: WxPay.mch_id
39
+ }.merge(params)
40
+ check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
41
+ r = invoke_remote("#{GATEWAY_URL}/secapi/pay/refund", params, true)
42
+ yield(r) if block_given?
43
+ r
44
+ end
45
+
46
+ # out_trade_no
47
+ def self.close_order(params)
48
+ params = {
49
+ appid: WxPay.appid,
50
+ mch_id: WxPay.mch_id
51
+ }.merge(params)
52
+ r = invoke_remote("#{GATEWAY_URL}/pay/closeorder", params)
53
+ yield(r) if block_given?
54
+ r
55
+ end
56
+
57
+ # 查询订单
58
+ # transaction_id 与 out_trade_no 二选一
59
+ def self.query_order(params)
60
+ params = {
61
+ appid: WxPay.appid,
62
+ mch_id: WxPay.mch_id
63
+ }.merge(params)
64
+ r = invoke_remote("#{GATEWAY_URL}/pay/orderquery", params)
65
+ yield(r) if block_given?
66
+ r
67
+ end
68
+
69
+ # 发送红包
70
+ # https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
71
+ INVOKE_REDPACK_FIELDS = %i(mch_billno mch_id wxappid send_name re_openid total_amount total_num wishing client_ip act_name remark key cert_path)
72
+ def self.send_redpack(params)
73
+ check_required_options(params, INVOKE_REDPACK_FIELDS)
74
+ r = invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/sendredpack", params, true)
75
+ yield(r) if block_given?
76
+ r
77
+ end
78
+
79
+ # 查询红包
80
+ # https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo
81
+ INVOKE_QUERY_REDPACK_FIELDS = %i(mch_billno mch_id appid bill_type key cert_path key)
82
+ def self.query_redpack(params)
83
+ check_required_options(params, INVOKE_QUERY_REDPACK_FIELDS)
84
+ r = invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/gethbinfo", params, true)
85
+ yield(r) if block_given?
86
+ r
87
+ end
88
+
89
+ # 发送裂变红包
90
+ # https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack
91
+ INVOKE_GROUPREDPACK_FIELDS = %i(mch_billno mch_id wxappid send_name re_openid total_amount total_num wishing act_name remark key amt_type cert_path)
92
+ def self.send_groupredpack(params)
93
+ check_required_options(params, INVOKE_GROUPREDPACK_FIELDS)
94
+ r = invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/sendgroupredpack", params, true)
95
+ yield(r) if block_given?
96
+ r
97
+ end
98
+
99
+ private
100
+
101
+ def self.check_required_options(options, names)
102
+ names.each do |name|
103
+ warn("WxPay Warn: missing required option: #{name}") unless options.has_key?(name)
104
+ end
105
+ end
106
+
107
+ def self.make_payload(params)
108
+ sign = WxPay::Sign.generate(params)
109
+ params.delete(:key) if params[:key]
110
+ "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
111
+ end
112
+
113
+ def self.invoke_remote(url, params, need_cert=false)
114
+ apply_apiclient_cert(params) if need_cert
115
+ params.merge!(nonce_str: SecureRandom.hex)
116
+ payload = make_payload(params)
117
+ r = RestClient::Request.execute(
118
+ {
119
+ method: :post,
120
+ url: url,
121
+ payload: payload,
122
+ headers: { content_type: 'application/xml' }
123
+ }.merge(WxPay.extra_rest_client_options)
124
+ )
125
+ if r
126
+ WxPay::Result.new Hash.from_xml(r)
127
+ else
128
+ nil
129
+ end
130
+ end
131
+
132
+ # 微信退款需要双向证书
133
+ # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
134
+ # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
135
+ def self.apply_apiclient_cert(params)
136
+ apiclient_cert = WxPay.apiclient_cert(params.delete(:cert_path), params[:mch_id])
137
+ WxPay.extra_rest_client_options = {
138
+ ssl_client_cert: apiclient_cert.certificate,
139
+ ssl_client_key: apiclient_cert.key,
140
+ verify_ssl: OpenSSL::SSL::VERIFY_NONE
141
+ }
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,22 @@
1
+ require 'digest/md5'
2
+
3
+ module WxPay
4
+ module Sign
5
+ def self.generate(params)
6
+ key = params.delete(:key)
7
+
8
+ query = params.sort.map do |key, value|
9
+ "#{key}=#{value}"
10
+ end.join('&')
11
+
12
+ Digest::MD5.hexdigest("#{query}&key=#{key || WxPay.key}").upcase
13
+ end
14
+
15
+ def self.verify?(params)
16
+ params = params.dup
17
+ sign = params.delete('sign') || params.delete(:sign)
18
+
19
+ generate(params) == sign
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module WxPay
2
+ VERSION = "0.2.1"
3
+ end
data/lib/wx_pay.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'wx_pay/result'
2
+ require 'wx_pay/sign'
3
+ require 'wx_pay/service'
4
+
5
+ module WxPay
6
+ class<< self
7
+ attr_accessor :appid, :mch_id, :key, :apiclient_cert_path
8
+
9
+ def extra_rest_client_options=(options)
10
+ @rest_client_options = options
11
+ end
12
+
13
+ def extra_rest_client_options
14
+ @rest_client_options || {}
15
+ end
16
+
17
+ def apiclient_cert(cert_path, mch_id)
18
+ OpenSSL::PKCS12.new(File.read(cert_path), mch_id)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_support/core_ext/hash/conversions'
2
+ require 'minitest/autorun'
3
+ require 'wx_pay'
4
+ require 'fakeweb'
5
+
6
+ WxPay.appid = 'wxd930ea5d5a258f4f'
7
+ WxPay.key = '8934e7d15453e97507ef794cf7b0519d'
8
+ WxPay.mch_id = '1900000109'
9
+ WxPay.apiclient_cert_path = '/path/to/your/cert/file.p12'
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class WxPay::ResultTest < MiniTest::Test
4
+ def test_success_method_with_true
5
+ r = WxPay::Result.new(
6
+ Hash.from_xml(
7
+ <<-XML
8
+ <xml>
9
+ <return_code>SUCCESS</return_code>
10
+ <result_code>SUCCESS</result_code>
11
+ </xml>
12
+ XML
13
+ ))
14
+
15
+ assert_equal r.success?, true
16
+ end
17
+
18
+ def test_success_method_with_false
19
+ r = WxPay::Result.new(
20
+ Hash.from_xml(
21
+ <<-XML
22
+ <xml>
23
+ </xml>
24
+ XML
25
+ ))
26
+
27
+ assert_equal r.success?, false
28
+ end
29
+ end
@@ -0,0 +1,70 @@
1
+
2
+ class ServiceTest < MiniTest::Test
3
+
4
+ # TODO why put the params of refun in setup method
5
+ def setup
6
+ @params = {
7
+ transaction_id: '1217752501201407033233368018',
8
+ op_user_id: '10000100',
9
+ out_refund_no: '1415701182',
10
+ out_trade_no: '1415757673',
11
+ refund_fee: 1,
12
+ total_fee: 1
13
+ }
14
+
15
+ @apiclient_cert = Minitest::Mock.new
16
+ @apiclient_cert.expect(:certificate, 'certificate')
17
+ @apiclient_cert.expect(:key, 'key')
18
+ end
19
+
20
+ def test_invoke_refund
21
+
22
+
23
+ response_body = <<-EOF
24
+ <xml>
25
+ <return_code><![CDATA[SUCCESS]]></return_code>
26
+ <return_msg><![CDATA[OK]]></return_msg>
27
+ <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
28
+ <mch_id><![CDATA[10000100]]></mch_id>
29
+ <nonce_str><![CDATA[NfsMFbUFpdbEhPXP]]></nonce_str>
30
+ <sign><![CDATA[B7274EB9F8925EB93100DD2085FA56C0]]></sign>
31
+ <result_code><![CDATA[SUCCESS]]></result_code>
32
+ <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
33
+ <out_trade_no><![CDATA[1415757673]]></out_trade_no>
34
+ <out_refund_no><![CDATA[1415701182]]></out_refund_no>
35
+ <refund_id><![CDATA[2008450740201411110000174436]]></refund_id>
36
+ <refund_channel><![CDATA[]]></refund_channel>
37
+ <refund_fee>1</refund_fee>
38
+ <coupon_refund_fee>0</coupon_refund_fee>
39
+ </xml>
40
+ EOF
41
+
42
+ FakeWeb.register_uri(
43
+ :post,
44
+ %r|https://api\.mch\.weixin\.qq\.com*|,
45
+ body: response_body
46
+ )
47
+
48
+ WxPay.stub :apiclient_cert, @apiclient_cert do
49
+ r = WxPay::Service.invoke_refund(@params)
50
+ assert_equal r.success?, true
51
+ end
52
+ end
53
+
54
+ def test_accept_multiple_app_id_when_invoke
55
+ params = {
56
+ body: '测试商品',
57
+ out_trade_no: 'test003',
58
+ total_fee: 1,
59
+ spbill_create_ip: '127.0.0.1',
60
+ notify_url: 'http://making.dev/notify',
61
+ trade_type: 'JSAPI',
62
+ openid: 'OPENID',
63
+ app_id: 'app_id',
64
+ mch_id: 'mch_id',
65
+ key: 'key'
66
+ }
67
+ xml_str = '<xml><body>测试商品</body><out_trade_no>test003</out_trade_no><total_fee>1</total_fee><spbill_create_ip>127.0.0.1</spbill_create_ip><notify_url>http://making.dev/notify</notify_url><trade_type>JSAPI</trade_type><openid>OPENID</openid><app_id>app_id</app_id><mch_id>mch_id</mch_id><sign>172A2D487A37D13FDE32B874BA823DD6</sign></xml>'
68
+ assert_equal xml_str, WxPay::Service.send(:make_payload, params)
69
+ end
70
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class WxPay::SignTest < MiniTest::Test
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
+
32
+ def test_accept_pars_key_to_generate_sign
33
+ @params.merge!(key: "key")
34
+
35
+ assert_equal "1454C32E885B8D9E4A05E976D1C45B88", WxPay::Sign.generate(@params)
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wx_pay_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Jasl
8
+ - lanrion
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-12-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '3.2'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '3.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10'
70
+ - !ruby/object:Gem::Dependency
71
+ name: fakeweb
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1'
84
+ - !ruby/object:Gem::Dependency
85
+ name: minitest
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '5'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '5'
98
+ description: An unofficial simple wechat pay gem
99
+ email:
100
+ - jasl9187@hotmail.com
101
+ - huaitao-deng@foxmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - MIT-LICENSE
107
+ - Rakefile
108
+ - lib/wx_pay.rb
109
+ - lib/wx_pay/result.rb
110
+ - lib/wx_pay/service.rb
111
+ - lib/wx_pay/sign.rb
112
+ - lib/wx_pay/version.rb
113
+ - test/test_helper.rb
114
+ - test/wx_pay/result_test.rb
115
+ - test/wx_pay/service_test.rb
116
+ - test/wx_pay/sign_test.rb
117
+ homepage: https://github.com/lanrion/wx_pay_api
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.5.1
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: An unofficial simple wechat pay gem
141
+ test_files:
142
+ - test/test_helper.rb
143
+ - test/wx_pay/result_test.rb
144
+ - test/wx_pay/service_test.rb
145
+ - test/wx_pay/sign_test.rb