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 +4 -4
- data/lib/wx_pay.rb +5 -1
- data/lib/wx_pay/service.rb +59 -12
- data/lib/wx_pay/sign.rb +9 -6
- data/lib/wx_pay/version.rb +1 -1
- data/test/wx_pay/service_test.rb +1 -1
- metadata +3 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cbb707c3e377f06aed3ae84c7d405a1091a45e62585ecebfecfab13f3de7c44
|
4
|
+
data.tar.gz: ee75f73c3842d83640437145b71f6438bfe998c55fcf1de668842ab278b560b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e445241334bbff574199d93041717b307a1ee07fdddc7128ac99b483b438773753733f648b9a1422dcbdf4b764ffbe40ed34c7b1b2ade8f18454f0c5bac39fd
|
7
|
+
data.tar.gz: 1f0c1b37452a65b5a2a5dcfbbf88e4083c22eca317a1978364baf038407b74892ff99c380b84c4feaa7c427b081696ccebb2beb56bdd6ab4abdf07c656641d2e
|
data/lib/wx_pay.rb
CHANGED
@@ -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, :
|
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
|
data/lib/wx_pay/service.rb
CHANGED
@@ -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
|
-
|
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:
|
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
|
-
|
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.
|
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
|
-
|
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
|
-
:
|
459
|
-
:
|
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
|
|
data/lib/wx_pay/sign.rb
CHANGED
@@ -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 =
|
30
|
-
if
|
31
|
-
|
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
|
data/lib/wx_pay/version.rb
CHANGED
data/test/wx_pay/service_test.rb
CHANGED
@@ -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><
|
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.
|
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:
|
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
|
-
|
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
|