wx_pay 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58376390f703eda446bc3b8c692d11c9aeb001e6fde882515ecb1fce1f490aa4
4
- data.tar.gz: fa597c10df24591e2e62c460b64c4c73a5deb46e41ec4f85bd9a99cd4b05cac5
3
+ metadata.gz: 8cbb707c3e377f06aed3ae84c7d405a1091a45e62585ecebfecfab13f3de7c44
4
+ data.tar.gz: ee75f73c3842d83640437145b71f6438bfe998c55fcf1de668842ab278b560b2
5
5
  SHA512:
6
- metadata.gz: 5815550a0ae5ce7568c6389329f0a4c3f856fa6de917e706f3c856bc4aed6b19fa57bd5477c40e6bf6df7ff228939e7bba05e70945d514dc4f982990fbb09af8
7
- data.tar.gz: 3eb50fdd268f845f2a7e931f37de8672bb8128f23bfb9d99b88c1931db63b5afc85aadbb2c421e6c3d58e4b05a35484a99fcd31280fb750d81539b65ffbb89cb
6
+ metadata.gz: 5e445241334bbff574199d93041717b307a1ee07fdddc7128ac99b483b438773753733f648b9a1422dcbdf4b764ffbe40ed34c7b1b2ade8f18454f0c5bac39fd
7
+ data.tar.gz: 1f0c1b37452a65b5a2a5dcfbbf88e4083c22eca317a1978364baf038407b74892ff99c380b84c4feaa7c427b081696ccebb2beb56bdd6ab4abdf07c656641d2e
@@ -11,7 +11,7 @@ module WxPay
11
11
 
12
12
  class<< self
13
13
  attr_accessor :appid, :mch_id, :key, :appsecret, :extra_rest_client_options, :debug_mode
14
- attr_accessor :sandbox_mode, :sandbox_key, :sandbox_mch_id
14
+ attr_accessor :sandbox_mode, :manual_get_sandbox_key
15
15
  attr_reader :apiclient_cert, :apiclient_key
16
16
 
17
17
  def set_apiclient_by_pkcs12(str, pass)
@@ -37,5 +37,9 @@ module WxPay
37
37
  def sandbox_mode?
38
38
  @sandbox_mode
39
39
  end
40
+
41
+ def manual_get_sandbox_key?
42
+ @manual_get_sandbox_key
43
+ end
40
44
  end
41
45
  end
@@ -17,19 +17,27 @@ module WxPay
17
17
 
18
18
  def self.authenticate(authorization_code, options = {})
19
19
  options = WxPay.extra_rest_client_options.merge(options)
20
- url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{WxPay.appid}&secret=#{WxPay.appsecret}&code=#{authorization_code}&grant_type=authorization_code"
20
+ payload = {
21
+ appid: options.delete(:appid) || WxPay.appid,
22
+ secret: options.delete(:appsecret) || WxPay.appsecret,
23
+ code: authorization_code,
24
+ grant_type: 'authorization_code'
25
+ }
26
+ url = "https://api.weixin.qq.com/sns/oauth2/access_token"
21
27
 
22
28
  ::JSON.parse(RestClient::Request.execute(
23
29
  {
24
30
  method: :get,
31
+ payload: payload,
25
32
  url: url
26
33
  }.merge(options)
27
34
  ), quirks_mode: true)
28
35
  end
29
36
 
30
- def self.get_sandbox_signkey
37
+ def self.get_sandbox_signkey(mch_id = WxPay.mch_id, options = {})
31
38
  params = {
32
- mch_id: WxPay.mch_id,
39
+ mch_id: mch_id,
40
+ key: options.delete(:key) || WxPay.key,
33
41
  nonce_str: SecureRandom.uuid.tr('-', '')
34
42
  }
35
43
  r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/getsignkey", xmlify_payload(params))))
@@ -39,11 +47,18 @@ module WxPay
39
47
 
40
48
  def self.authenticate_from_weapp(js_code, options = {})
41
49
  options = WxPay.extra_rest_client_options.merge(options)
42
- url = "https://api.weixin.qq.com/sns/jscode2session?appid=#{WxPay.appid}&secret=#{WxPay.appsecret}&js_code=#{js_code}&grant_type=authorization_code"
50
+ payload = {
51
+ appid: options.delete(:appid) || WxPay.appid,
52
+ secret: options.delete(:appsecret) || WxPay.appsecret,
53
+ js_code: authorization_code,
54
+ grant_type: 'authorization_code'
55
+ }
56
+ url = "https://api.weixin.qq.com/sns/jscode2session"
43
57
 
44
58
  ::JSON.parse(RestClient::Request.execute(
45
59
  {
46
60
  method: :get,
61
+ payload: payload,
47
62
  url: url
48
63
  }.merge(options)
49
64
  ), quirks_mode: true)
@@ -152,6 +167,7 @@ module WxPay
152
167
  params = {
153
168
  appid: options.delete(:appid) || WxPay.appid,
154
169
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
170
+ key: options.delete(:key) || WxPay.key,
155
171
  nonce_str: SecureRandom.uuid.tr('-', '')
156
172
  }.merge(params)
157
173
 
@@ -286,6 +302,7 @@ module WxPay
286
302
  params = {
287
303
  appid: options.delete(:appid) || WxPay.appid,
288
304
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
305
+ key: options.delete(:key) || WxPay.key,
289
306
  nonce_str: SecureRandom.uuid.tr('-', '')
290
307
  }.merge(params)
291
308
 
@@ -309,6 +326,7 @@ module WxPay
309
326
  params = {
310
327
  appid: options.delete(:appid) || WxPay.appid,
311
328
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
329
+ key: options.delete(:key) || WxPay.key,
312
330
  nonce_str: SecureRandom.uuid.tr('-', '')
313
331
  }.merge(params)
314
332
 
@@ -332,6 +350,7 @@ module WxPay
332
350
  params = {
333
351
  appid: options.delete(:appid) || WxPay.appid,
334
352
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
353
+ key: options.delete(:key) || WxPay.key,
335
354
  nonce_str: SecureRandom.uuid.tr('-', '')
336
355
  }.merge(params)
337
356
 
@@ -349,6 +368,7 @@ module WxPay
349
368
  params = {
350
369
  appid: options.delete(:appid) || WxPay.appid,
351
370
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
371
+ key: options.delete(:key) || WxPay.key,
352
372
  nonce_str: SecureRandom.uuid.tr('-', ''),
353
373
  }.merge(params)
354
374
 
@@ -389,6 +409,7 @@ module WxPay
389
409
  params = {
390
410
  wxappid: options.delete(:appid) || WxPay.appid,
391
411
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
412
+ key: options.delete(:key) || WxPay.key,
392
413
  nonce_str: SecureRandom.uuid.tr('-', '')
393
414
  }.merge(params)
394
415
 
@@ -411,6 +432,7 @@ module WxPay
411
432
  params = {
412
433
  wxappid: options.delete(:appid) || WxPay.appid,
413
434
  mch_id: options.delete(:mch_id) || WxPay.mch_id,
435
+ key: options.delete(:key) || WxPay.key,
414
436
  nonce_str: SecureRandom.uuid.tr('-', '')
415
437
  }.merge(params)
416
438
 
@@ -428,6 +450,31 @@ module WxPay
428
450
 
429
451
  r
430
452
  end
453
+
454
+ # 用于商户对已发放的红包进行查询红包的具体信息,可支持普通红包和裂变包。
455
+ GETHBINFO_FIELDS = [:mch_billno, :bill_type]
456
+ def self.gethbinfo(params, options = {})
457
+ params = {
458
+ appid: options.delete(:appid) || WxPay.appid,
459
+ mch_id: options.delete(:mch_id) || WxPay.mch_id,
460
+ nonce_str: SecureRandom.uuid.tr('-', ''),
461
+ key: options.delete(:key) || WxPay.key
462
+ }.merge(params)
463
+
464
+ check_required_options(params, GETHBINFO_FIELDS)
465
+
466
+ options = {
467
+ ssl_client_cert: options.delete(:apiclient_cert) || WxPay.apiclient_cert,
468
+ ssl_client_key: options.delete(:apiclient_key) || WxPay.apiclient_key,
469
+ verify_ssl: OpenSSL::SSL::VERIFY_NONE
470
+ }.merge(options)
471
+
472
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/mmpaymkttransfers/gethbinfo", make_payload(params), options)))
473
+
474
+ yield r if block_given?
475
+
476
+ r
477
+ end
431
478
 
432
479
  class << self
433
480
  private
@@ -446,23 +493,23 @@ module WxPay
446
493
 
447
494
  def xmlify_payload(params, sign_type = WxPay::Sign::SIGN_TYPE_MD5)
448
495
  sign = WxPay::Sign.generate(params, sign_type)
449
- params.delete(:key) if params[:key]
450
- "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
496
+ "<xml>#{params.except(:key).sort.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
451
497
  end
452
498
 
453
499
  def make_payload(params, sign_type = WxPay::Sign::SIGN_TYPE_MD5)
454
- if WxPay.sandbox_mode?
500
+ # TODO: Move this out
501
+ if WxPay.sandbox_mode? && !WxPay.manual_get_sandbox_key?
455
502
  r = get_sandbox_signkey
456
503
  if r['return_code'] == WxPay::Result::SUCCESS_FLAG
457
- params = params.merge({
458
- :mch_id => r['mch_id'] || WxPay.mch_id,
459
- :key => r['sandbox_signkey']
460
- })
461
- WxPay.sandbox_key = r['sandbox_signkey']
504
+ params = params.merge(
505
+ mch_id: r['mch_id'] || WxPay.mch_id,
506
+ key: r['sandbox_signkey']
507
+ )
462
508
  else
463
509
  warn("WxPay Warn: fetch sandbox sign key failed #{r['return_msg']}")
464
510
  end
465
511
  end
512
+
466
513
  xmlify_payload(params, sign_type)
467
514
  end
468
515
 
@@ -9,11 +9,14 @@ module WxPay
9
9
  def self.generate(params, sign_type = SIGN_TYPE_MD5)
10
10
  key = params.delete(:key)
11
11
 
12
+ new_key = params["key"] #after
13
+ key = params.delete("key") if params["key"] #after
14
+
12
15
  query = params.sort.map do |k, v|
13
16
  "#{k}=#{v}" if v.to_s != ''
14
17
  end.compact.join('&')
15
18
 
16
- string_sign_temp = "#{query}&key=#{key || WxPay.key}"
19
+ string_sign_temp = "#{query}&key=#{key || new_key || WxPay.key}" #after
17
20
 
18
21
  if sign_type == SIGN_TYPE_MD5
19
22
  Digest::MD5.hexdigest(string_sign_temp).upcase
@@ -25,14 +28,14 @@ module WxPay
25
28
  end
26
29
 
27
30
  def self.verify?(params, options = {})
31
+ return true if WxPay.sandbox_mode?
32
+
28
33
  params = params.dup
29
- params = params.merge(options)
30
- if WxPay.sandbox_mode?
31
- params = params.merge(:key => WxPay.sandbox_key)
32
- end
34
+ params["appid"] = options[:appid] if options[:appid]
35
+ params["mch_id"] = options[:mch_id] if options[:mch_id]
36
+ params["key"] = options[:key] if options[:key]
33
37
 
34
38
  sign = params.delete('sign') || params.delete(:sign)
35
-
36
39
  generate(params) == sign
37
40
  end
38
41
  end
@@ -1,3 +1,3 @@
1
1
  module WxPay
2
- VERSION = '0.18.0'.freeze
2
+ VERSION = '0.19.0'.freeze
3
3
  end
@@ -48,7 +48,7 @@ class ServiceTest < MiniTest::Test
48
48
  mch_id: 'mch_id',
49
49
  key: 'key'
50
50
  }
51
- 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>'
51
+ xml_str = '<xml><app_id>app_id</app_id><body>测试商品</body><mch_id>mch_id</mch_id><notify_url>http://making.dev/notify</notify_url><openid>OPENID</openid><out_trade_no>test003</out_trade_no><spbill_create_ip>127.0.0.1</spbill_create_ip><total_fee>1</total_fee><trade_type>JSAPI</trade_type><sign>172A2D487A37D13FDE32B874BA823DD6</sign></xml>'
52
52
  assert_equal xml_str, WxPay::Service.send(:make_payload, params)
53
53
  end
54
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wx_pay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-19 00:00:00.000000000 Z
11
+ date: 2019-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
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
41
  - !ruby/object:Gem::Dependency
56
42
  name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -131,8 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
117
  - !ruby/object:Gem::Version
132
118
  version: '0'
133
119
  requirements: []
134
- rubyforge_project:
135
- rubygems_version: 2.7.6
120
+ rubygems_version: 3.0.2
136
121
  signing_key:
137
122
  specification_version: 4
138
123
  summary: An unofficial simple wechat pay gem