wx_pay 0.0.1 → 0.0.2

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: 982340d48955dd0cd2d30b9e494f3fd26b6df4dd
4
- data.tar.gz: 2992d5cb8b0c572b85d0bacc25577468f559106a
3
+ metadata.gz: db7946c82422013bede3c2eb795c8f3752e7f738
4
+ data.tar.gz: 77a7c68125f7aafc8e66db14cab4adad35f24ae0
5
5
  SHA512:
6
- metadata.gz: f5222caaabbc3aa43357a71c1392d7ce0c0e987e621c27064d0bc3fcabea91211f0d63b7cc4b0498b03802527904e48559ebf27e327334f4fc24c557cf81b0cd
7
- data.tar.gz: 736c4e4567dd4ac180694cb63a602ab12f576efb28c68e9a96443e587e9341da40d51d831ac68f22593edc9c415be7a696cbf80e74f68a232464ce329acae311
6
+ metadata.gz: 1c71b47e51f0fd640662978e05aa228705885d578f9d76d3f47bbe2158870fa65b0eefc78d6a2be50f5f21c5e3739157ddeb8c8cb8fbce77f896d09d513d2f9f
7
+ data.tar.gz: 6496c0c1678c951d9c081e358283bc92955036826972004371503d906d8cfbd308758786d7c6937234175e11e836529ad7337a11c8b461d89f981e5b5e2ccb90
@@ -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
@@ -8,14 +8,18 @@ module WxPay
8
8
  INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = %i(body out_trade_no total_fee spbill_create_ip notify_url trade_type)
9
9
  def self.invoke_unifiedorder(params)
10
10
  params = {
11
- app_id: WxPay.appid,
11
+ appid: WxPay.appid,
12
12
  mch_id: WxPay.mch_id,
13
13
  nonce_str: SecureRandom.uuid.tr('-', ''),
14
14
  }.merge(params)
15
15
 
16
16
  check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
17
17
 
18
- WxPay::Utils.invoke_remote("#{GATEWAY_URL}/unifiedorder", WxPay::Utils.make_payload(params))
18
+ r = invoke_remote("#{GATEWAY_URL}/unifiedorder", make_payload(params))
19
+
20
+ yield r if block_given?
21
+
22
+ r
19
23
  end
20
24
 
21
25
  private
@@ -25,5 +29,26 @@ module WxPay
25
29
  warn("WxPay Warn: missing required option: #{name}") unless options.has_key?(name)
26
30
  end
27
31
  end
32
+
33
+ def self.make_payload(params)
34
+ "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}<sign>#{WxPay::Sign.generate(params)}</sign></xml>"
35
+ end
36
+
37
+ def self.invoke_remote(url, payload)
38
+ r = RestClient::Request.execute(
39
+ {
40
+ method: :post,
41
+ url: url,
42
+ payload: payload,
43
+ headers: { content_type: 'application/xml' }
44
+ }.merge(WxPay.extra_rest_client_options)
45
+ )
46
+
47
+ if r
48
+ WxPay::Result.new Hash.from_xml(r)
49
+ else
50
+ nil
51
+ end
52
+ end
28
53
  end
29
54
  end
data/lib/wx_pay/sign.rb CHANGED
@@ -11,8 +11,8 @@ module WxPay
11
11
  end
12
12
 
13
13
  def self.verify?(params)
14
- params = Utils.stringify_keys(params)
15
- sign = params.delete('sign')
14
+ params = params.dup
15
+ sign = params.delete('sign') || params.delete(:sign)
16
16
 
17
17
  generate(params) == sign
18
18
  end
@@ -1,3 +1,3 @@
1
1
  module WxPay
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/wx_pay.rb CHANGED
@@ -1,7 +1,6 @@
1
- require 'wx_pay/utils'
1
+ require 'wx_pay/result'
2
2
  require 'wx_pay/sign'
3
3
  require 'wx_pay/service'
4
- require 'wx_pay/notify'
5
4
 
6
5
  module WxPay
7
6
  class<< self
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'active_support/core_ext/hash/conversions'
1
2
  require 'minitest/autorun'
2
3
  require 'wx_pay'
3
4
  require 'fakeweb'
@@ -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
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class WxPay::SignTest < MiniTest::Unit::TestCase
3
+ class WxPay::SignTest < MiniTest::Test
4
4
  def setup
5
5
  @params = {
6
6
  appid: 'wxd930ea5d5a258f4f',
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.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-09 00:00:00.000000000 Z
11
+ date: 2014-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -104,14 +104,13 @@ files:
104
104
  - MIT-LICENSE
105
105
  - Rakefile
106
106
  - lib/wx_pay.rb
107
- - lib/wx_pay/notify.rb
107
+ - lib/wx_pay/result.rb
108
108
  - lib/wx_pay/service.rb
109
109
  - lib/wx_pay/sign.rb
110
- - lib/wx_pay/utils.rb
111
110
  - lib/wx_pay/version.rb
112
111
  - test/test_helper.rb
112
+ - test/wx_pay/result_test.rb
113
113
  - test/wx_pay/sign_test.rb
114
- - test/wx_pay/utils_test.rb
115
114
  homepage: https://github.com/jasl/wx_pay
116
115
  licenses:
117
116
  - MIT
@@ -138,5 +137,5 @@ specification_version: 4
138
137
  summary: An unofficial simple wechat pay gem
139
138
  test_files:
140
139
  - test/test_helper.rb
140
+ - test/wx_pay/result_test.rb
141
141
  - test/wx_pay/sign_test.rb
142
- - test/wx_pay/utils_test.rb
data/lib/wx_pay/notify.rb DELETED
@@ -1,18 +0,0 @@
1
- module WxPay
2
- module Notify
3
- GATEWAY = 'https://gw.tenpay.com/gateway/simpleverifynotifyid.xml'
4
- SUCCESS_STR = '<retcode>0</retcode>'
5
-
6
- def self.verify?(params)
7
- return false unless Sign.verify?(params)
8
-
9
- params = {
10
- 'input_charset' => 'UTF-8',
11
- 'partner' => WxPay.appid,
12
- 'notify_id' => CGI.escape(params[:notify_id].to_s)
13
- }
14
-
15
- open("#{GATEWAY}?#{Utils.make_query_string(params)}").read.include?(SUCCESS_STR)
16
- end
17
- end
18
- end
data/lib/wx_pay/utils.rb DELETED
@@ -1,27 +0,0 @@
1
- module WxPay
2
- module Utils
3
- def self.stringify_keys(hash)
4
- new_hash = {}
5
- hash.each do |key, value|
6
- new_hash[(key.to_s rescue key) || key] = value
7
- end
8
- new_hash
9
- end
10
-
11
- def self.make_payload(params)
12
- "<xml>#{params.map {|k, v| "<#{k}>#{v}</#{k}>"}.join}<sign>#{WxPay::Sign.generate(params)}</sign></xml>"
13
- end
14
-
15
- def self.invoke_remote(url, payload)
16
- Hash.from_xml(
17
- RestClient::Request.execute(
18
- {
19
- method: :post,
20
- url: url,
21
- payload: payload,
22
- headers: {content_type: 'application/xml'}
23
- }.merge(WxPay.extra_rest_client_options))
24
- )
25
- end
26
- end
27
- end
@@ -1,8 +0,0 @@
1
- require 'test_helper'
2
-
3
- class WxPay::UtilsTest < MiniTest::Unit::TestCase
4
- def test_stringify_keys
5
- hash = { 'a' => 1, :b => 2 }
6
- assert_equal({ 'a' => 1, 'b' => 2 }.sort, WxPay::Utils.stringify_keys(hash).sort)
7
- end
8
- end