wx_pay 0.15.0 → 0.16.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
  SHA1:
3
- metadata.gz: c7716b9c31fb5cbf24db64ca0aad7c228ab1dc1f
4
- data.tar.gz: ec0a6c208012f09829ff8e2b37238a25b5c07659
3
+ metadata.gz: dda7cad282f587b4eb9db5afb01d0f55e9b6ae8f
4
+ data.tar.gz: a8bf70ef43b11678faf05d70496b962ed9df262a
5
5
  SHA512:
6
- metadata.gz: 4edecc4552ee556a99620920fb3126d09f7ae3d478c7544e6981827375edd35ed8c452037ee9616dcd5bcede35626f434f3e0e41a3e416dda1e95583b2794e94
7
- data.tar.gz: 4ac03cae8b014baadad894fe6ef26f528e1f27c41ec12d35414a787f4471fbcb04436d2736616e191d38c5875595bf30f682eb91d927276fe3e1fae2bbb497b6
6
+ metadata.gz: 9339a3e811c8a4358766f4e79917f92c000718617de351e116f751d92834144f4b9d363b82a72ca0e92b218cb905687dede973254597f8acd5449c5317ef63e0
7
+ data.tar.gz: 7808f6392ddc96ce95874b1de710121a866e631032ef8bb8b2abade3a0e1c936275eb46d9623c7d8ad404c8e0f9ba875e45171cbb7b7c65976093c76e50fae94
@@ -6,9 +6,11 @@ require 'openssl'
6
6
  module WxPay
7
7
  @extra_rest_client_options = {}
8
8
  @debug_mode = true
9
+ @sandbox_mode = false
9
10
 
10
11
  class<< self
11
12
  attr_accessor :appid, :mch_id, :key, :appsecret, :extra_rest_client_options, :debug_mode
13
+ attr_accessor :sandbox_mode, :sandbox_key, :sandbox_mch_id
12
14
  attr_reader :apiclient_cert, :apiclient_key
13
15
 
14
16
  def set_apiclient_by_pkcs12(str, pass)
@@ -30,5 +32,9 @@ module WxPay
30
32
  def debug_mode?
31
33
  @debug_mode
32
34
  end
35
+
36
+ def sandbox_mode?
37
+ @sandbox_mode
38
+ end
33
39
  end
34
40
  end
@@ -6,7 +6,8 @@ require 'active_support/core_ext/hash/conversions'
6
6
 
7
7
  module WxPay
8
8
  module Service
9
- GATEWAY_URL = 'https://api.mch.weixin.qq.com'
9
+ GATEWAY_URL = 'https://api.mch.weixin.qq.com'.freeze
10
+ SANDBOX_GATEWAY_URL = 'https://api.mch.weixin.qq.com/sandboxnew'.freeze
10
11
 
11
12
  def self.generate_authorize_url(redirect_uri, state = nil)
12
13
  state ||= SecureRandom.hex 16
@@ -25,6 +26,16 @@ module WxPay
25
26
  ), quirks_mode: true)
26
27
  end
27
28
 
29
+ def self.get_sandbox_signkey
30
+ params = {
31
+ mch_id: WxPay.mch_id,
32
+ nonce_str: SecureRandom.uuid.tr('-', '')
33
+ }
34
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/getsignkey", xmlify_payload(params))))
35
+ yield r if block_given?
36
+ r
37
+ end
38
+
28
39
  def self.authenticate_from_weapp(js_code, options = {})
29
40
  options = WxPay.extra_rest_client_options.merge(options)
30
41
  url = "https://api.weixin.qq.com/sns/jscode2session?appid=#{WxPay.appid}&secret=#{WxPay.appsecret}&js_code=#{js_code}&grant_type=authorization_code"
@@ -36,7 +47,7 @@ module WxPay
36
47
  }.merge(options)
37
48
  ), quirks_mode: true)
38
49
  end
39
-
50
+
40
51
  INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = [:body, :out_trade_no, :total_fee, :spbill_create_ip, :notify_url, :trade_type]
41
52
  def self.invoke_unifiedorder(params, options = {})
42
53
  params = {
@@ -48,7 +59,7 @@ module WxPay
48
59
 
49
60
  check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
50
61
 
51
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/pay/unifiedorder", make_payload(params), options)))
62
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/unifiedorder", make_payload(params), options)))
52
63
 
53
64
  yield r if block_given?
54
65
 
@@ -66,7 +77,7 @@ module WxPay
66
77
 
67
78
  check_required_options(params, INVOKE_CLOSEORDER_REQUIRED_FIELDS)
68
79
 
69
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/pay/closeorder", make_payload(params), options)))
80
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/closeorder", make_payload(params), options)))
70
81
 
71
82
  yield r if block_given?
72
83
 
@@ -127,7 +138,7 @@ module WxPay
127
138
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
128
139
  }.merge(options)
129
140
 
130
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/secapi/pay/refund", make_payload(params), options)))
141
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/secapi/pay/refund", make_payload(params), options)))
131
142
 
132
143
  yield r if block_given?
133
144
 
@@ -144,7 +155,7 @@ module WxPay
144
155
 
145
156
  check_required_options(params, ORDER_QUERY_REQUIRED_FIELDS)
146
157
 
147
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/pay/refundquery", make_payload(params), options)))
158
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/refundquery", make_payload(params), options)))
148
159
 
149
160
  yield r if block_given?
150
161
 
@@ -167,7 +178,7 @@ module WxPay
167
178
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
168
179
  }.merge(options)
169
180
 
170
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/promotion/transfers", make_payload(params), options)))
181
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/mmpaymkttransfers/promotion/transfers", make_payload(params), options)))
171
182
 
172
183
  yield r if block_given?
173
184
 
@@ -190,7 +201,7 @@ module WxPay
190
201
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
191
202
  }.merge(options)
192
203
 
193
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/gettransferinfo", make_payload(params), options)))
204
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/mmpaymkttransfers/gettransferinfo", make_payload(params), options)))
194
205
 
195
206
  yield r if block_given?
196
207
 
@@ -213,7 +224,7 @@ module WxPay
213
224
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
214
225
  }.merge(options)
215
226
 
216
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/secapi/pay/reverse", make_payload(params), options)))
227
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/secapi/pay/reverse", make_payload(params), options)))
217
228
 
218
229
  yield r if block_given?
219
230
 
@@ -236,7 +247,7 @@ module WxPay
236
247
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
237
248
  }.merge(options)
238
249
 
239
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/pay/micropay", make_payload(params), options)))
250
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/micropay", make_payload(params), options)))
240
251
 
241
252
  yield r if block_given?
242
253
 
@@ -252,7 +263,7 @@ module WxPay
252
263
  }.merge(params)
253
264
 
254
265
 
255
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/pay/orderquery", make_payload(params), options)))
266
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/pay/orderquery", make_payload(params), options)))
256
267
  check_required_options(params, ORDER_QUERY_REQUIRED_FIELDS)
257
268
 
258
269
  yield r if block_given?
@@ -270,7 +281,7 @@ module WxPay
270
281
 
271
282
  check_required_options(params, DOWNLOAD_BILL_REQUIRED_FIELDS)
272
283
 
273
- r = invoke_remote("#{GATEWAY_URL}/pay/downloadbill", make_payload(params), options)
284
+ r = invoke_remote("/pay/downloadbill", make_payload(params), options)
274
285
 
275
286
  yield r if block_given?
276
287
 
@@ -292,7 +303,7 @@ module WxPay
292
303
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
293
304
  }.merge(options)
294
305
 
295
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/sendgroupredpack", make_payload(params), options)))
306
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/mmpaymkttransfers/sendgroupredpack", make_payload(params), options)))
296
307
 
297
308
  yield r if block_given?
298
309
 
@@ -314,7 +325,7 @@ module WxPay
314
325
  verify_ssl: OpenSSL::SSL::VERIFY_NONE
315
326
  }.merge(options)
316
327
 
317
- r = WxPay::Result.new(Hash.from_xml(invoke_remote("#{GATEWAY_URL}/mmpaymkttransfers/sendredpack", make_payload(params), options)))
328
+ r = WxPay::Result.new(Hash.from_xml(invoke_remote("/mmpaymkttransfers/sendredpack", make_payload(params), options)))
318
329
 
319
330
  yield r if block_given?
320
331
 
@@ -324,6 +335,11 @@ module WxPay
324
335
  class << self
325
336
  private
326
337
 
338
+ def get_gateway_url
339
+ return SANDBOX_GATEWAY_URL if WxPay.sandbox_mode?
340
+ GATEWAY_URL
341
+ end
342
+
327
343
  def check_required_options(options, names)
328
344
  return unless WxPay.debug_mode?
329
345
  names.each do |name|
@@ -331,19 +347,36 @@ module WxPay
331
347
  end
332
348
  end
333
349
 
334
- def make_payload(params)
350
+ def xmlify_payload(params)
335
351
  sign = WxPay::Sign.generate(params)
336
352
  params.delete(:key) if params[:key]
337
353
  "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{sign}</sign></xml>"
338
354
  end
339
355
 
356
+ def make_payload(params)
357
+ if WxPay.sandbox_mode?
358
+ r = get_sandbox_signkey
359
+ if r['return_code'] == WxPay::Result::SUCCESS_FLAG
360
+ params = params.merge({
361
+ :mch_id => r['mch_id'] || WxPay.mch_id,
362
+ :key => r['sandbox_signkey']
363
+ })
364
+ WxPay.sandbox_key = r['sandbox_signkey']
365
+ WxPay.sandbox_mch_id = r['mch_id']
366
+ else
367
+ warn("WxPay Warn: fetch sandbox sign key failed #{r['return_msg']}")
368
+ end
369
+ end
370
+ xmlify_payload(params)
371
+ end
372
+
340
373
  def invoke_remote(url, payload, options = {})
341
374
  options = WxPay.extra_rest_client_options.merge(options)
342
375
 
343
376
  RestClient::Request.execute(
344
377
  {
345
378
  method: :post,
346
- url: url,
379
+ url: "#{get_gateway_url}#{url}",
347
380
  payload: payload,
348
381
  headers: { content_type: 'application/xml' }
349
382
  }.merge(options)
@@ -12,8 +12,13 @@ module WxPay
12
12
  Digest::MD5.hexdigest("#{query}&key=#{key || WxPay.key}").upcase
13
13
  end
14
14
 
15
- def self.verify?(params)
15
+ def self.verify?(params, options = {})
16
16
  params = params.dup
17
+ params = params.merge(options)
18
+ if WxPay.sandbox_mode?
19
+ params = params.merge({:mch_id => WxPay.sandbox_mch_id, :key => WxPay.sandbox_key})
20
+ end
21
+
17
22
  sign = params.delete('sign') || params.delete(:sign)
18
23
 
19
24
  generate(params) == sign
@@ -1,3 +1,3 @@
1
1
  module WxPay
2
- VERSION = '0.15.0'
2
+ VERSION = '0.16.0'
3
3
  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.15.0
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-13 00:00:00.000000000 Z
11
+ date: 2017-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
19
+ version: 2.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
26
+ version: 2.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  requirements: []
134
134
  rubyforge_project:
135
- rubygems_version: 2.6.11
135
+ rubygems_version: 2.6.13
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: An unofficial simple wechat pay gem