This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
service.rb
212 lines (178 loc) · 7.96 KB
/
service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require 'rest_client'
require 'active_support/core_ext/hash/conversions'
module JdPay
module Service
UNIORDER_URL = 'https://paygate.jd.com/service/uniorder'
QUERY_URL = 'https://paygate.jd.com/service/query'
REFUND_URL = 'https://paygate.jd.com/service/refund'
H5_PAY_URL = 'https://h5pay.jd.com/jdpay/saveOrder'
PC_PAY_URL = 'https://wepay.jd.com/jdpay/saveOrder'
REVOKE_URL = 'https://paygate.jd.com/service/revoke'
QRCODE_PAY_URL = 'https://paygate.jd.com/service/fkmPay'
USER_RELATION_URL = 'https://paygate.jd.com/service/getUserRelation'
CANCEL_USER_URL = 'https://paygate.jd.com/service/cancelUserRelation'
WEB_PAY_BASE_URL = 'https://wepay.jd.com/jdpay/payCashier'
H5_PAY_BASE_URL = 'https://h5pay.jd.com/jdpay/payCashier'
class << self
# the difference between pc and h5 is just request url
def pc_pay(params, options = {})
web_pay(params, PC_PAY_URL, options)
end
def h5_pay(params, options = {})
web_pay(params, H5_PAY_URL, options)
end
WEB_PAY_REQUIRED_FIELDS = [:tradeNum, :tradeName, :amount, :orderType, :notifyUrl, :callbackUrl, :userId]
def web_pay(params, url, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
currency: "CNY"
}.merge(params)
check_required_options(params, WEB_PAY_REQUIRED_FIELDS)
sign = JdPay::Sign.rsa_encrypt(JdPay::Util.to_query(params), options)
skip_encrypt_params = %i(version merchant)
params.each do |k, v|
params[k] = skip_encrypt_params.include?(k) ? v : JdPay::Des.encrypt_3des(v)
end
params[:sign] = sign
options[:need_redirect_url] ? get_redirect_url(url, params) : JdPay::Util.build_pay_form(url, params)
end
UNIORDER_REQUIRED_FIELDS = [:tradeNum, :tradeName, :amount, :orderType, :notifyUrl, :userId]
def uniorder(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
currency: "CNY"
}.merge(params)
check_required_options(params, UNIORDER_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(UNIORDER_URL, make_payload(params), options)), options)
end
QRCODE_REQUIRED_FIELDS = [:tradeNum, :tradeName, :amount, :device, :token]
def qrcode_pay(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
currency: "CNY"
}.merge(params)
check_required_options(params, QRCODE_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(QRCODE_PAY_URL, make_payload(params), options)), options)
end
USER_RELATION_REQUIRED_FIELDS = [:userId]
def user_relation(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
}.merge(params)
check_required_options(params, USER_RELATION_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(USER_RELATION_URL, make_payload(params), options)), options)
end
def cancel_user(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
}.merge(params)
check_required_options(params, USER_RELATION_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(CANCEL_USER_URL, make_payload(params), options)), options)
end
REFUND_REQUIRED_FIELDS = [:tradeNum, :oTradeNum, :amount, :notifyUrl]
def refund(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
currency: "CNY"
}.merge(params)
check_required_options(params, REFUND_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(REFUND_URL, make_payload(params), options)), options)
end
QUERY_REQUIRED_FIELDS = [:tradeNum, :tradeType]
def query(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
tradeType: '0'
}.merge(params)
check_required_options(params, QUERY_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(QUERY_URL, make_payload(params), options)), options)
end
REVOKE_REQUIRED_FIELDS = [:tradeNum, :oTradeNum, :amount]
def revoke(params, options = {})
params = {
version: "V2.0",
merchant: options[:mch_id] || JdPay.mch_id,
tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
currency: "CNY"
}.merge(params)
check_required_options(params, REVOKE_REQUIRED_FIELDS)
params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)
JdPay::Result.new(Hash.from_xml(invoke_remote(REVOKE_URL, make_payload(params), options)), options)
end
def notify_verify(xml_str, options = {})
JdPay::Result.new(Hash.from_xml(xml_str), options)
end
def verify_redirection(params, options = {})
params = params.dup
sign = params.delete('sign')
decrypted_and_dropped = JdPay::Util.decrypt_and_drop_empty(params)
decrypted, dropped = decrypted_and_dropped[0], decrypted_and_dropped[1]
params = JdPay::Util.stringify_keys(dropped)
string = JdPay::Util.to_query(params)
unless Digest::SHA256.hexdigest(string) == JdPay::Sign.rsa_decrypt(sign, options)
raise JdPay::Error::InvalidRedirection, "decrypted:#{decrypted};dropped:#{dropped}"
end
decrypted
end
private
def check_required_options(options, names)
return unless JdPay.debug_mode?
names.each do |name|
warn("JdPay Warn: missing required option: #{name}") unless options.has_key?(name)
end
end
def make_payload(params, options = {})
request_hash = {
"version" => "V2.0",
"merchant" => options[:mch_id] || JdPay.mch_id,
"encrypt" => Base64.strict_encode64(JdPay::Des.encrypt_3des JdPay::Util.to_xml(params))
}
JdPay::Util.to_xml(request_hash)
end
def invoke_remote(url, payload, options = {})
options = JdPay.extra_rest_client_options.merge(options)
RestClient::Request.execute(
{
method: :post,
url: url,
payload: payload,
headers: { content_type: 'application/xml' }
}.merge(options)
)
end
def get_redirect_url(url, payload)
base_url = WEB_PAY_BASE_URL
base_url = H5_PAY_BASE_URL if url == H5_PAY_URL
url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
req = Net::HTTP::Post.new(url.to_s)
req.form_data = payload
req['Content-Type'] = 'application/x-www-form-urlencoded'
resp = http.request(req)
# This is really a bad design by JDPay.
# Examples:
# For h5pay: https://h5pay.jd.com/jdpay/payCashier?tradeNum=xxx&orderId=xxx&key=xxx
# For webpay: payCashier?tradeNum=xxx&ourTradeNum=xxx&key=xxx
resp['location'].include?(base_url) ? resp['location'] : "#{base_url}?#{resp['location'].split('?')[1]}"
end
end
end
end