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 +4 -4
- data/lib/wx_pay/result.rb +19 -0
- data/lib/wx_pay/service.rb +27 -2
- data/lib/wx_pay/sign.rb +2 -2
- data/lib/wx_pay/version.rb +1 -1
- data/lib/wx_pay.rb +1 -2
- data/test/test_helper.rb +1 -0
- data/test/wx_pay/result_test.rb +29 -0
- data/test/wx_pay/sign_test.rb +1 -1
- metadata +5 -6
- data/lib/wx_pay/notify.rb +0 -18
- data/lib/wx_pay/utils.rb +0 -27
- data/test/wx_pay/utils_test.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db7946c82422013bede3c2eb795c8f3752e7f738
|
4
|
+
data.tar.gz: 77a7c68125f7aafc8e66db14cab4adad35f24ae0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/wx_pay/service.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
data/lib/wx_pay/version.rb
CHANGED
data/lib/wx_pay.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -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
|
data/test/wx_pay/sign_test.rb
CHANGED
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.
|
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-
|
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/
|
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
|
data/test/wx_pay/utils_test.rb
DELETED