jyt_pay 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +16 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/doc/JytPay/Api/AuthCard.html +375 -0
- data/doc/JytPay/Api/QueryBalance.html +299 -0
- data/doc/JytPay/Api/QueryOrder.html +372 -0
- data/doc/JytPay/Api/QuickDraw.html +433 -0
- data/doc/JytPay/Api/QuickPay.html +435 -0
- data/doc/JytPay/Api.html +117 -0
- data/doc/JytPay/Client.html +323 -0
- data/doc/JytPay/Encrypt/Des.html +258 -0
- data/doc/JytPay/Encrypt/Rsa.html +232 -0
- data/doc/JytPay/Encrypt.html +127 -0
- data/doc/JytPay/Http/RetCode.html +391 -0
- data/doc/JytPay/Http.html +242 -0
- data/doc/JytPay/Sign/Rsa.html +178 -0
- data/doc/JytPay/Sign.html +115 -0
- data/doc/JytPay/Utils.html +358 -0
- data/doc/JytPay/Xml/MyPrecious.html +220 -0
- data/doc/JytPay/Xml.html +210 -0
- data/doc/JytPay.html +140 -0
- data/doc/_index.html +317 -0
- data/doc/class_list.html +51 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +58 -0
- data/doc/css/style.css +492 -0
- data/doc/file.README.html +150 -0
- data/doc/file_list.html +56 -0
- data/doc/frames.html +17 -0
- data/doc/index.html +150 -0
- data/doc/js/app.js +248 -0
- data/doc/js/full_list.js +216 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +203 -0
- data/doc/top-level-namespace.html +110 -0
- data/jyt_pay.gemspec +34 -0
- data/lib/jyt_pay/api/auth_card.rb +60 -0
- data/lib/jyt_pay/api/query_balance.rb +50 -0
- data/lib/jyt_pay/api/query_order.rb +71 -0
- data/lib/jyt_pay/api/quick_draw.rb +75 -0
- data/lib/jyt_pay/api/quick_pay.rb +76 -0
- data/lib/jyt_pay/client.rb +37 -0
- data/lib/jyt_pay/encrypt/des.rb +30 -0
- data/lib/jyt_pay/encrypt/encrypt.rb +7 -0
- data/lib/jyt_pay/encrypt/rsa.rb +20 -0
- data/lib/jyt_pay/http/communicate.rb +77 -0
- data/lib/jyt_pay/http/ret_code.rb +60 -0
- data/lib/jyt_pay/sign/rsa.rb +14 -0
- data/lib/jyt_pay/utils.rb +42 -0
- data/lib/jyt_pay/version.rb +3 -0
- data/lib/jyt_pay/xml/xml.rb +74 -0
- data/lib/jyt_pay.rb +32 -0
- metadata +166 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# 认证银行卡
|
3
|
+
module JytPay
|
4
|
+
module Api
|
5
|
+
module AuthCard
|
6
|
+
|
7
|
+
AUTH_CARD_TRAN_CODE = "TR4003"
|
8
|
+
|
9
|
+
# 四要素认证
|
10
|
+
#
|
11
|
+
# @param flow_id [ String ] 订单号
|
12
|
+
# @param card_id [ String ] 银行卡号
|
13
|
+
# @param true_name [ String ] 真实姓名
|
14
|
+
# @param identity_id [ String ] 身份证
|
15
|
+
# @param phone [ String ] 银行卡预留手机号
|
16
|
+
#
|
17
|
+
# @return [ Hash ] 结果集
|
18
|
+
# * :result [String] 是否成功,`F`, `S`
|
19
|
+
# * :bank_name [String] 成功后会返回该卡在 jyt 系统内的中文名
|
20
|
+
# * :msg [String] 结果说明
|
21
|
+
# * :ret_code [String] 结果 CODE
|
22
|
+
# * :flow_id [String] 订单号
|
23
|
+
# * :request_body [String] 请求报文
|
24
|
+
# * :response_body [String] 响应报文
|
25
|
+
#
|
26
|
+
def auth_card(flow_id, card_id, true_name, identity_id, phone)
|
27
|
+
|
28
|
+
params = {
|
29
|
+
bank_card_no: card_id,
|
30
|
+
id_num: identity_id,
|
31
|
+
id_name: true_name,
|
32
|
+
terminal_type: '01',
|
33
|
+
bank_card_type: 'D',
|
34
|
+
phone_no: phone,
|
35
|
+
}
|
36
|
+
|
37
|
+
xml_str = JytPay::Xml.generate(@merchant_id, AUTH_CARD_TRAN_CODE,
|
38
|
+
params, flow_id)
|
39
|
+
|
40
|
+
response = Http.post(@merchant_id, @uris[:auth],
|
41
|
+
AUTH_CARD_TRAN_CODE, xml_str,
|
42
|
+
@rsa_private_key, @rsa_jyt_public_key)
|
43
|
+
|
44
|
+
res = {
|
45
|
+
result: 'S0000000' == response[:head][:resp_code] ? 'S' : 'F',
|
46
|
+
bank_name: response[:body][:bank_name],
|
47
|
+
msg: response[:head][:resp_desc],
|
48
|
+
ret_code: response[:head][:resp_code],
|
49
|
+
flow_id: flow_id,
|
50
|
+
vendor_order_id: nil, # jyt 不会返回他们的订单号
|
51
|
+
request_body: xml_str,
|
52
|
+
response_body: response[:xml_str],
|
53
|
+
}
|
54
|
+
|
55
|
+
res
|
56
|
+
end
|
57
|
+
|
58
|
+
end # module QuickPay
|
59
|
+
end # module Api
|
60
|
+
end # module
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# 商户余额查询
|
3
|
+
module JytPay
|
4
|
+
module Api
|
5
|
+
module QueryBalance
|
6
|
+
|
7
|
+
QUERY_BALANCE_TRAN_CODE = "TC2020"
|
8
|
+
|
9
|
+
# 查询商户当前余额
|
10
|
+
#
|
11
|
+
# @param flow_id [ String ] 订单号
|
12
|
+
#
|
13
|
+
# @return [ Hash ] 结果集
|
14
|
+
# * :result [String] 是否成功,`F`, `S`, `P`
|
15
|
+
# * :msg [String] 结果说明
|
16
|
+
# * :balance [Float] 余额
|
17
|
+
#
|
18
|
+
def query_balance(flow_id)
|
19
|
+
params = {
|
20
|
+
mer_viral_acct: @mer_viral_acct,
|
21
|
+
}
|
22
|
+
|
23
|
+
xml_str = JytPay::Xml.generate(@merchant_id, QUERY_BALANCE_TRAN_CODE,
|
24
|
+
params, flow_id)
|
25
|
+
|
26
|
+
response = Http.post(@merchant_id, @uris[:query_balance],
|
27
|
+
QUERY_BALANCE_TRAN_CODE, xml_str,
|
28
|
+
@rsa_private_key, @rsa_jyt_public_key)
|
29
|
+
|
30
|
+
res = {
|
31
|
+
result: 'F',
|
32
|
+
msg: response[:head][:resp_desc],
|
33
|
+
balance: 0,
|
34
|
+
}
|
35
|
+
|
36
|
+
case response[:head][:resp_code]
|
37
|
+
when 'S0000000'
|
38
|
+
res = {
|
39
|
+
result: 'S',
|
40
|
+
msg: response[:head][:resp_desc],
|
41
|
+
balance: response[:body][:balance].to_f,
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
res
|
46
|
+
end
|
47
|
+
|
48
|
+
end # module QueryBalance
|
49
|
+
end # module Api
|
50
|
+
end # module
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# 查询代付代收订单
|
3
|
+
module JytPay
|
4
|
+
module Api
|
5
|
+
module QueryOrder
|
6
|
+
|
7
|
+
QUERY_PAY_TRAN_CODE = "TC2001"
|
8
|
+
QUERY_DRAW_TRAN_CODE = "TC2002"
|
9
|
+
|
10
|
+
# 查询代付代收订单
|
11
|
+
#
|
12
|
+
# @param flow_id [ String ] 订单号
|
13
|
+
# @param ori_flow_id [ String ] 原业务订单号
|
14
|
+
# @param tran_type [ String ] 原业务类型:`:pay` or `:draw`
|
15
|
+
#
|
16
|
+
# @return [ Hash ] 结果集
|
17
|
+
# * :result [String] 原业务是否成功,`F`, `S`, `P`
|
18
|
+
# * :msg [String] 结果说明
|
19
|
+
# * :ret_code [String] 结果 CODE
|
20
|
+
# * :flow_id [String] 订单号
|
21
|
+
# * :request_body [String] 请求报文
|
22
|
+
# * :response_body [String] 响应报文
|
23
|
+
#
|
24
|
+
def query_order(flow_id, ori_flow_id, tran_type)
|
25
|
+
case tran_type.to_sym
|
26
|
+
when :pay
|
27
|
+
server_uri = @uris[:pay]
|
28
|
+
tran_code = QUERY_PAY_TRAN_CODE
|
29
|
+
when :draw
|
30
|
+
server_uri = @uris[:draw]
|
31
|
+
tran_code = QUERY_DRAW_TRAN_CODE
|
32
|
+
else
|
33
|
+
raise 'unknow query tran code'
|
34
|
+
end
|
35
|
+
|
36
|
+
params = {
|
37
|
+
ori_tran_flowid: ori_flow_id
|
38
|
+
}
|
39
|
+
|
40
|
+
xml_str = JytPay::Xml.generate(@merchant_id, tran_code,
|
41
|
+
params, flow_id)
|
42
|
+
|
43
|
+
response = Http.post(@merchant_id, server_uri,
|
44
|
+
tran_code, xml_str,
|
45
|
+
@rsa_private_key, @rsa_jyt_public_key)
|
46
|
+
|
47
|
+
res = {
|
48
|
+
result: 'P', # 默认 pending
|
49
|
+
msg: response[:body][:tran_resp_desc] || response[:head][:resp_desc],
|
50
|
+
ret_code: response[:body][:tran_resp_code] || response[:head][:resp_code],
|
51
|
+
flow_id: flow_id,
|
52
|
+
vendor_order_id: nil, # jyt 不会返回他们的订单号
|
53
|
+
request_body: xml_str,
|
54
|
+
response_body: response[:xml_str],
|
55
|
+
}
|
56
|
+
|
57
|
+
return res if response[:body][:tran_resp_code].nil?
|
58
|
+
|
59
|
+
res[:result] = case tran_type.to_sym
|
60
|
+
when :pay
|
61
|
+
Http::RetCode.pay_result(response[:body][:tran_resp_code], response[:body][:tran_state])
|
62
|
+
when :draw
|
63
|
+
Http::RetCode.draw_result(response[:body][:tran_resp_code], response[:body][:tran_state])
|
64
|
+
end
|
65
|
+
|
66
|
+
res
|
67
|
+
end
|
68
|
+
|
69
|
+
end # module QueryOrder
|
70
|
+
end # module Api
|
71
|
+
end # module
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# 单笔同步代付
|
3
|
+
module JytPay
|
4
|
+
module Api
|
5
|
+
module QuickDraw
|
6
|
+
|
7
|
+
QUICK_DRAW_TRAN_CODE = "TC1002"
|
8
|
+
|
9
|
+
# 单笔同步代付
|
10
|
+
#
|
11
|
+
# @param flow_id [ String ] 订单号
|
12
|
+
# @param money [ Float ] 代付金额(精确到2位)
|
13
|
+
# @param bank_business_name [ String ] 银行卡的中文名(在银行卡认证后有返回)
|
14
|
+
# @param card_id [ String ] 银行卡号
|
15
|
+
# @param true_name [ String ] 真实姓名
|
16
|
+
# @param identity_id [ String ] 身份证
|
17
|
+
# @param phone [ String ] 银行卡预留手机号
|
18
|
+
#
|
19
|
+
# @return [ Hash ] 结果集
|
20
|
+
# * :result [String] 是否成功,`F`, `S`, `P`
|
21
|
+
# * :msg [String] 结果说明
|
22
|
+
# * :ret_code [String] 结果 CODE
|
23
|
+
# * :flow_id [String] 订单号
|
24
|
+
# * :request_body [String] 请求报文
|
25
|
+
# * :response_body [String] 响应报文
|
26
|
+
#
|
27
|
+
def quick_draw(flow_id, money,
|
28
|
+
bank_business_name, card_id,
|
29
|
+
true_name, identity_id, province, city)
|
30
|
+
|
31
|
+
params = {
|
32
|
+
mer_viral_acct: "",
|
33
|
+
agrt_no: "",
|
34
|
+
bank_name: bank_business_name,
|
35
|
+
account_no: card_id,
|
36
|
+
account_name: true_name,
|
37
|
+
account_type: "00", #对私
|
38
|
+
branch_bank_province: province,
|
39
|
+
branch_bank_city: city,
|
40
|
+
branch_bank_name: "",
|
41
|
+
tran_amt: money, #两位小数
|
42
|
+
currency: "CNY",
|
43
|
+
bsn_code: "09400", #14900其他费用
|
44
|
+
cert_type: "01",
|
45
|
+
cert_no: identity_id,
|
46
|
+
mobile: "",
|
47
|
+
remark: "",
|
48
|
+
reserve: "",
|
49
|
+
}
|
50
|
+
|
51
|
+
xml_str = JytPay::Xml.generate(@merchant_id, QUICK_DRAW_TRAN_CODE,
|
52
|
+
params, flow_id)
|
53
|
+
|
54
|
+
response = Http.post(@merchant_id, @uris[:draw],
|
55
|
+
QUICK_DRAW_TRAN_CODE, xml_str,
|
56
|
+
@rsa_private_key, @rsa_jyt_public_key)
|
57
|
+
|
58
|
+
res = {
|
59
|
+
result: 'P', # 默认 pending
|
60
|
+
msg: response[:head][:resp_desc],
|
61
|
+
ret_code: response[:head][:resp_code],
|
62
|
+
flow_id: flow_id,
|
63
|
+
vendor_order_id: nil, # jyt 不会返回他们的订单号
|
64
|
+
request_body: xml_str,
|
65
|
+
response_body: response[:xml_str],
|
66
|
+
}
|
67
|
+
|
68
|
+
res[:result] = Http::RetCode.draw_result(response[:head][:resp_code], response[:body][:tran_state])
|
69
|
+
|
70
|
+
res
|
71
|
+
end
|
72
|
+
|
73
|
+
end # module QuickDraw
|
74
|
+
end # module Api
|
75
|
+
end # module
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# 单笔同步代扣
|
3
|
+
module JytPay
|
4
|
+
module Api
|
5
|
+
module QuickPay
|
6
|
+
|
7
|
+
QUICK_PAY_TRAN_CODE = "TC1001"
|
8
|
+
|
9
|
+
# 单笔同步代付
|
10
|
+
#
|
11
|
+
# @param flow_id [ String ] 订单号
|
12
|
+
# @param money [ Float ] 代付金额(精确到2位)
|
13
|
+
# @param bank_business_name [ String ] 银行卡的中文名(在银行卡认证后有返回)
|
14
|
+
# @param card_id [ String ] 银行卡号
|
15
|
+
# @param true_name [ String ] 真实姓名
|
16
|
+
# @param identity_id [ String ] 身份证
|
17
|
+
# @param phone [ String ] 银行卡预留手机号
|
18
|
+
#
|
19
|
+
# @return [ Hash ] 结果集
|
20
|
+
# * :result [String] 是否成功,`F`, `S`, `P`
|
21
|
+
# * :msg [String] 结果说明
|
22
|
+
# * :ret_code [String] 结果 CODE
|
23
|
+
# * :flow_id [String] 订单号
|
24
|
+
# * :request_body [String] 请求报文
|
25
|
+
# * :response_body [String] 响应报文
|
26
|
+
#
|
27
|
+
def quick_pay(flow_id, money,
|
28
|
+
bank_business_name, card_id,
|
29
|
+
true_name, identity_id, phone)
|
30
|
+
|
31
|
+
params = {
|
32
|
+
mer_viral_acct: "",
|
33
|
+
agrt_no: "",
|
34
|
+
bank_name: bank_business_name,
|
35
|
+
account_no: card_id,
|
36
|
+
account_name: true_name,
|
37
|
+
account_type: "00", #对私
|
38
|
+
branch_bank_province: "",
|
39
|
+
branch_bank_city: "",
|
40
|
+
branch_bank_name: "",
|
41
|
+
tran_amt: money, #两位小数
|
42
|
+
currency: "CNY",
|
43
|
+
bsn_code: "11203", #14900其他费用
|
44
|
+
cert_type: "01",
|
45
|
+
cert_no: identity_id,
|
46
|
+
mobile: phone,
|
47
|
+
remark: "",
|
48
|
+
reserve: "",
|
49
|
+
}
|
50
|
+
|
51
|
+
xml_str = JytPay::Xml.generate(@merchant_id, QUICK_PAY_TRAN_CODE,
|
52
|
+
params, flow_id)
|
53
|
+
|
54
|
+
response = Http.post(@merchant_id, @uris[:pay],
|
55
|
+
QUICK_PAY_TRAN_CODE, xml_str,
|
56
|
+
@rsa_private_key, @rsa_jyt_public_key)
|
57
|
+
|
58
|
+
res = {
|
59
|
+
result: 'P', # 默认 pending
|
60
|
+
msg: response[:head][:resp_desc],
|
61
|
+
ret_code: response[:head][:resp_code],
|
62
|
+
flow_id: flow_id,
|
63
|
+
vendor_order_id: nil, # jyt 不会返回他们的订单号
|
64
|
+
request_body: xml_str,
|
65
|
+
response_body: response[:xml_str],
|
66
|
+
}
|
67
|
+
|
68
|
+
res[:result] = Http::RetCode.pay_result(response[:head][:resp_code],
|
69
|
+
response[:body][:tran_state])
|
70
|
+
|
71
|
+
res
|
72
|
+
end
|
73
|
+
|
74
|
+
end # module QuickPay
|
75
|
+
end # module Api
|
76
|
+
end # module
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# 客户端
|
4
|
+
|
5
|
+
module JytPay
|
6
|
+
class Client
|
7
|
+
|
8
|
+
# 代付代扣的 api
|
9
|
+
include JytPay::Api::QuickPay
|
10
|
+
include JytPay::Api::QuickDraw
|
11
|
+
include JytPay::Api::QueryOrder
|
12
|
+
# 通用 api
|
13
|
+
include JytPay::Api::QueryBalance
|
14
|
+
# 认证银行卡 api
|
15
|
+
include JytPay::Api::AuthCard
|
16
|
+
|
17
|
+
def initialize(options_arg)
|
18
|
+
options = Utils.symbolize_keys(options_arg)
|
19
|
+
|
20
|
+
@uris = {
|
21
|
+
pay: URI(options[:pay_url]),
|
22
|
+
draw: URI(options[:draw_url]),
|
23
|
+
auth: URI(options[:auth_url]),
|
24
|
+
query_balance: URI(options[:query_balance_url]),
|
25
|
+
}
|
26
|
+
|
27
|
+
@merchant_id = options[:merchant_id]
|
28
|
+
@mer_viral_acct = options[:mer_viral_acct]
|
29
|
+
@private_key = options[:private_key]
|
30
|
+
@jyt_public_key = options[:jyt_public_key]
|
31
|
+
|
32
|
+
@rsa_private_key = OpenSSL::PKey::RSA.new(@private_key)
|
33
|
+
@rsa_jyt_public_key = OpenSSL::PKey::RSA.new(@jyt_public_key)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module JytPay
|
4
|
+
module Encrypt
|
5
|
+
module Des
|
6
|
+
|
7
|
+
ALG = 'DES-CBC'
|
8
|
+
|
9
|
+
def self.encrypt(str, key_arg = Encrypt::DES_KEY)
|
10
|
+
des = OpenSSL::Cipher::Cipher.new(ALG)
|
11
|
+
des.key = key_arg
|
12
|
+
des.iv = key_arg
|
13
|
+
des.encrypt
|
14
|
+
cipher = des.update(str) + des.final
|
15
|
+
cipher.unpack('H*')[0]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.decrypt(xml_enc, key_str)
|
19
|
+
xml_str = [xml_enc].pack 'H*'
|
20
|
+
|
21
|
+
des = OpenSSL::Cipher::Cipher.new(ALG)
|
22
|
+
des.decrypt
|
23
|
+
des.key = key_str
|
24
|
+
des.iv = key_str
|
25
|
+
des.update(xml_str) + des.final
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module JytPay
|
4
|
+
module Encrypt
|
5
|
+
module Rsa
|
6
|
+
|
7
|
+
def self.encrypt(rsa_public_key, key_arg = Encrypt::DES_KEY)
|
8
|
+
data = [key_arg].pack 'H*'
|
9
|
+
kenc = rsa_public_key.public_encrypt key_arg
|
10
|
+
kenc.unpack('H*')[0]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.decrypt(key_enc, rsa_private_key)
|
14
|
+
ec = [key_enc].pack 'H*'
|
15
|
+
rsa_private_key.private_decrypt(ec)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module JytPay
|
3
|
+
module Http
|
4
|
+
|
5
|
+
def self.post(merchant_id, server_uri,
|
6
|
+
tran_code, xml_str,
|
7
|
+
rsa_private_key, rsa_jyt_public_key)
|
8
|
+
body = {
|
9
|
+
merchant_id: merchant_id,
|
10
|
+
xml_enc: Encrypt::Des.encrypt(xml_str),
|
11
|
+
key_enc: Encrypt::Rsa.encrypt(rsa_jyt_public_key),
|
12
|
+
sign: Sign::Rsa.sign(rsa_private_key, xml_str)
|
13
|
+
}
|
14
|
+
|
15
|
+
puts "\n[#{tran_code}][#{server_uri}] 发送原始 xml 为:\n#{xml_str}\n"
|
16
|
+
|
17
|
+
result = {}
|
18
|
+
|
19
|
+
begin
|
20
|
+
response = Net::HTTP.post_form(server_uri, body)
|
21
|
+
|
22
|
+
if response.is_a?(Net::HTTPSuccess) # 返回 200 才处理
|
23
|
+
result = unpack_body(response.body, rsa_private_key)
|
24
|
+
else
|
25
|
+
# 非 200 请求
|
26
|
+
result = Http::RetCode.general_error_response(response.code)
|
27
|
+
end
|
28
|
+
rescue Net::ReadTimeout # 请求超时
|
29
|
+
result = Http::RetCode.general_error_response(0)
|
30
|
+
end
|
31
|
+
|
32
|
+
puts "\n[#{tran_code}][#{server_uri}] 返回结果为:\n#{result}\n\n"
|
33
|
+
|
34
|
+
result
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def self.unpack_body(response_body, rsa_private_key)
|
40
|
+
# 把返回的字符串,按键值对组成 hash
|
41
|
+
response_result = {}
|
42
|
+
response_body.split('&').each{ |key_value_str|
|
43
|
+
k, v = key_value_str.split('=')
|
44
|
+
response_result[k.to_sym] = v
|
45
|
+
}
|
46
|
+
|
47
|
+
# 解密
|
48
|
+
key_str = Encrypt::Rsa.decrypt(response_result[:key_enc], rsa_private_key)
|
49
|
+
xml_str = Encrypt::Des.decrypt(response_result[:xml_enc], key_str)
|
50
|
+
|
51
|
+
# TODO(tony): 好像没有验签啊?
|
52
|
+
doc = ::REXML::Document.new xml_str
|
53
|
+
|
54
|
+
result = {
|
55
|
+
head: {},
|
56
|
+
body: {},
|
57
|
+
xml_str: xml_str,
|
58
|
+
}
|
59
|
+
|
60
|
+
root = doc.root
|
61
|
+
if root.elements["head"]
|
62
|
+
root.elements["head"].elements.each { |element|
|
63
|
+
result[:head][element.name.to_sym] = element.text
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
if root.elements["body"]
|
68
|
+
root.elements["body"].elements.each { |element|
|
69
|
+
result[:body][element.name.to_sym] = element.text
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
result
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module JytPay
|
3
|
+
module Http
|
4
|
+
|
5
|
+
module RetCode
|
6
|
+
# 默认的返回值
|
7
|
+
def self.general_error_response(http_status)
|
8
|
+
result = {
|
9
|
+
head: {
|
10
|
+
resp_code: 'S0000000',
|
11
|
+
resp_desc: "网络错误( http 状态码:#{http_status})"
|
12
|
+
},
|
13
|
+
body: {
|
14
|
+
tran_state: '03',
|
15
|
+
},
|
16
|
+
xml_str: '',
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
# 支付的结果
|
21
|
+
def self.pay_result(resp_code, tran_state)
|
22
|
+
case resp_code
|
23
|
+
when 'S0000000'
|
24
|
+
case tran_state
|
25
|
+
when '01'
|
26
|
+
return 'S'
|
27
|
+
when '03'
|
28
|
+
return 'F'
|
29
|
+
else
|
30
|
+
return 'P'
|
31
|
+
end
|
32
|
+
when 'E0000000' # jyt 处理中
|
33
|
+
return 'P'
|
34
|
+
else
|
35
|
+
return 'F'
|
36
|
+
end
|
37
|
+
end # self.pay_result
|
38
|
+
|
39
|
+
# 提现的结果(目前和支付一样逻辑,但是不知道以后会不会改)
|
40
|
+
def self.draw_result(resp_code, tran_state)
|
41
|
+
case resp_code
|
42
|
+
when 'S0000000'
|
43
|
+
case tran_state
|
44
|
+
when '01'
|
45
|
+
return 'S'
|
46
|
+
when '03'
|
47
|
+
return 'F'
|
48
|
+
else
|
49
|
+
return 'P'
|
50
|
+
end
|
51
|
+
when 'E0000000' # jyt 处理中
|
52
|
+
return 'P'
|
53
|
+
else
|
54
|
+
return 'F'
|
55
|
+
end
|
56
|
+
end # self.pay_result
|
57
|
+
|
58
|
+
end
|
59
|
+
end # Http
|
60
|
+
end # JytPay
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# 通用工具类方法
|
4
|
+
|
5
|
+
module JytPay
|
6
|
+
module Utils
|
7
|
+
|
8
|
+
# 把 hash 中的 key,都转化为 symbol 类型
|
9
|
+
#
|
10
|
+
# @param hash [Hash] 需要更改的 hash
|
11
|
+
# @return [Hash] 更改后的 hash
|
12
|
+
def self.symbolize_keys(hash)
|
13
|
+
new_hash = {}
|
14
|
+
hash.each do |key, value|
|
15
|
+
new_hash[(key.to_sym rescue key) || key] = value
|
16
|
+
end
|
17
|
+
new_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
# 通过时间,返回唯一一个24位flow id(支持分布)
|
21
|
+
# 同一秒,同一台机器,同一个进程,最多可以产生 16777214 个不一样的订单号
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# JytPay::Utils.gen_flow_id
|
25
|
+
#
|
26
|
+
# @param [ Integer ] 时间(默认是 now)
|
27
|
+
#
|
28
|
+
# @return [ String ] flow id
|
29
|
+
#
|
30
|
+
def self.gen_flow_id(time=Time.now)
|
31
|
+
machine_id = Digest::MD5.digest(::Mac.addr).unpack("N")[0]
|
32
|
+
process_id = Process.pid % 0xFFFF
|
33
|
+
|
34
|
+
@counter ||= 0
|
35
|
+
@counter += 1
|
36
|
+
count = (@counter) % 0xFFFFFF
|
37
|
+
|
38
|
+
return [ time.to_i, machine_id, process_id, count << 8 ].pack("N NX lXX NX").unpack("H*")[0].force_encoding('UTF-8')
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|