weixin_pay 0.2.0

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjliOTNlYjgwNThlMjM3NTE1NDEwZTc3N2FhM2VkZjBiOTJhN2NmMA==
5
+ data.tar.gz: !binary |-
6
+ M2E3MTA0N2U3OGY4ZThkN2M2Y2I3YzNmZWFhYmQyNDVhNWYzZDQ3NQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YTFjZGYyM2M5ZjIxZGFiMTZhYzI5Y2YzYjA3NzI5NDAzNGZmYTBmNmFjNmNj
10
+ N2E1M2MzNTY3ZTBmZDljYmE0YzE3YzQ2YmYxOGQ2Nzg4YWQzNzUyMjIzYmNh
11
+ ZDUxYmY2ODYzYjI1NWI3MmU2YjFiNjFhNTc4ZmZiMjkyOTg5ZTg=
12
+ data.tar.gz: !binary |-
13
+ ZTlhNzMzYmRlNDI1MzU2NDY0MGJhOTIyODg3MWQyMDhjYzQ3M2NiZmNjNzc2
14
+ MjA2MTRkYWU1NmVhNDUxYTQ0OTdjYjYwNjlkZjZlNzk4ZTQ4ZTdlYWM3NjE4
15
+ OWQ0YmU4OGQ0NWVmZmNmZWI4NDI4OTkwZmM4YzgyNjBhNWVhZGQ=
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
data/lib/weixin_pay.rb ADDED
@@ -0,0 +1,21 @@
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
18
+ @apiclient_cert ||= OpenSSL::PKCS12.new(WxPay.apiclient_cert_path, WxPay.mch_id)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module WxPay
2
+ class Result < ::Hash
3
+ SUCCESS_FLAG = 'SUCCESS'.freeze
4
+
5
+ def initialize(result)
6
+ super
7
+
8
+ if result['xml'].class == Hash
9
+ result['xml'].each_pair do |k, v|
10
+ self[k] = v
11
+ end
12
+ end
13
+ end
14
+
15
+ def success?
16
+ self['return_code'] == SUCCESS_FLAG && self['result_code'] == SUCCESS_FLAG
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,101 @@
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'
7
+
8
+ INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = [: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
+ nonce_str: SecureRandom.uuid.tr('-', '')
14
+ }.merge(params)
15
+
16
+ check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
17
+
18
+ r = invoke_remote("#{GATEWAY_URL}/pay/unifiedorder", make_payload(params))
19
+
20
+ yield r if block_given?
21
+
22
+ r
23
+ end
24
+
25
+ GENERATE_APP_PAY_REQ_REQUIRED_FIELDS = [:prepayid, :noncestr]
26
+ def self.generate_app_pay_req(params)
27
+ params = {
28
+ appid: WxPay.appid,
29
+ partnerid: WxPay.mch_id,
30
+ package: 'Sign=WXPay',
31
+ timestamp: Time.now.to_i.to_s
32
+ }.merge(params)
33
+
34
+ check_required_options(params, GENERATE_APP_PAY_REQ_REQUIRED_FIELDS)
35
+
36
+ params[:sign] = WxPay::Sign.generate(params)
37
+
38
+ params
39
+ end
40
+
41
+ INVOKE_REFUND_REQUIRED_FIELDS = [:transaction_id, :out_trade_no, :out_refund_no, :total_fee, :refund_fee]
42
+ def self.invoke_refund(params)
43
+ params = {
44
+ appid: WxPay.appid,
45
+ mch_id: WxPay.mch_id,
46
+ nonce_str: SecureRandom.uuid.tr('-', ''),
47
+ op_user_id: WxPay.mch_id
48
+ }.merge(params)
49
+
50
+ check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
51
+
52
+ # 微信退款需要双向证书
53
+ # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
54
+ # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
55
+
56
+ WxPay.extra_rest_client_options = {
57
+ ssl_client_cert: WxPay.apiclient_cert.certificate,
58
+ ssl_client_key: WxPay.apiclient_cert.key,
59
+ verify_ssl: OpenSSL::SSL::VERIFY_NONE
60
+ }
61
+
62
+ r = invoke_remote "#{GATEWAY_URL}/secapi/pay/refund", make_payload(params)
63
+
64
+ yield(r) if block_given?
65
+
66
+ r
67
+ end
68
+
69
+
70
+ private
71
+
72
+ def self.check_required_options(options, names)
73
+ names.each do |name|
74
+ warn("WxPay Warn: missing required option: #{name}") unless options.has_key?(name)
75
+ end
76
+ end
77
+
78
+ def self.make_payload(params)
79
+ sign = WxPay::Sign.generate(params)
80
+ params.delete(:key) if params[:key]
81
+ "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
82
+ end
83
+
84
+ def self.invoke_remote(url, payload)
85
+ r = RestClient::Request.execute(
86
+ {
87
+ method: :post,
88
+ url: url,
89
+ payload: payload,
90
+ headers: { content_type: 'application/xml' }
91
+ }.merge(WxPay.extra_rest_client_options)
92
+ )
93
+
94
+ if r
95
+ WxPay::Result.new Hash.from_xml(r)
96
+ else
97
+ nil
98
+ end
99
+ end
100
+ end
101
+ 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.0"
3
+ 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,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weixin_pay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-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: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
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/weixin_pay.rb
107
+ - lib/wx_pay/result.rb
108
+ - lib/wx_pay/service.rb
109
+ - lib/wx_pay/sign.rb
110
+ - lib/wx_pay/version.rb
111
+ - test/test_helper.rb
112
+ - test/wx_pay/result_test.rb
113
+ - test/wx_pay/service_test.rb
114
+ - test/wx_pay/sign_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.4.8
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/result_test.rb
142
+ - test/wx_pay/service_test.rb
143
+ - test/wx_pay/sign_test.rb