Class: ECpayPayment::PaymentVerifyBase
- Inherits:
-
Object
- Object
- ECpayPayment::PaymentVerifyBase
show all
- Includes:
- ECpayErrorDefinition
- Defined in:
- lib/ecpay_payment/verification.rb
Constant Summary
collapse
- @@param_xml =
Nokogiri::XML(File.open(File.join(File.dirname(__FILE__), 'ECpayPayment.xml')))
Instance Method Summary
collapse
Instance Method Details
#get_all_pattern(apiname) ⇒ Object
125
126
127
128
129
130
131
132
133
|
# File 'lib/ecpay_payment/verification.rb', line 125
def get_all_pattern(apiname)
res = {}
res['Type_idx'] = self.get_param_type(apiname)
res['Int'] = self.get_int_param_pattern(apiname)
res['String'] = self.get_str_param_pattern(apiname)
res['Opt'] = self.get_opt_param_pattern(apiname)
res['DepOpt'] = self.get_depopt_param_pattern(apiname)
return res
end
|
#get_basic_params(apiname) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/ecpay_payment/verification.rb', line 23
def get_basic_params(apiname)
basic_param = []
@@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters/param[@require=\"1\"]").each do |elem|
basic_param.push(elem.attributes['name'].value)
end
return basic_param
end
|
#get_cond_param(apiname) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/ecpay_payment/verification.rb', line 31
def get_cond_param(apiname)
aio_sw_param = []
conditional_param = {}
@@param_xml.xpath("/ECPayPayment/#{apiname}/Config/switchparam/n").each do |elem|
aio_sw_param.push(elem.text)
end
aio_sw_param.each do |pname|
opt_param = {}
node = @@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param[@name=\"#{pname}\"]")
node.xpath('./condparam').each do |elem|
opt = elem.attributes['owner'].value
params = []
elem.xpath('./param[@require="1"]').each do |pa|
params.push(pa.attributes['name'].value)
end
opt_param[opt] = params
end
conditional_param[pname] = opt_param
end
return conditional_param
end
|
#get_depopt_param_pattern(apiname) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/ecpay_payment/verification.rb', line 104
def get_depopt_param_pattern(apiname)
pattern = {}
node = @@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param[@type=\"DepOpt\"]")
node.each do |param_elem|
parent_n_opts = {}
sub_opts = {}
p_name = param_elem.attributes['name'].value
parent_name = param_elem.attributes['main'].value
param_elem.xpath('./mainoption').each do |elem|
k = elem.attributes['name'].value
opt = []
elem.element_children.each{|c|opt.push(c.text)}
sub_opts[k] = opt
end
parent_n_opts[parent_name] = sub_opts
pattern[p_name] = parent_n_opts
end
return pattern
end
|
#get_int_param_pattern(apiname) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/ecpay_payment/verification.rb', line 73
def get_int_param_pattern(apiname)
pattern = {}
node = @@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param[@type=\"Int\"]")
node.each do |param_elem|
mode = param_elem.xpath('./mode').text
mx = param_elem.xpath('./maximum').text
mn = param_elem.xpath('./minimal').text
a = []
[mode, mx, mn].each{|f|a.push(f)}
pattern[param_elem.attributes['name'].value] = a
end
return pattern
end
|
#get_opt_param_pattern(apiname) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/ecpay_payment/verification.rb', line 61
def get_opt_param_pattern(apiname)
pattern = {}
node = @@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param[@type=\"Opt\"]")
node.each do |param_elem|
opt_elems = param_elem.xpath('./option')
opt = []
opt_elems.each{|oe|opt.push(oe.text)}
pattern[param_elem.attributes['name'].value] = opt
end
return pattern
end
|
#get_param_type(apiname) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/ecpay_payment/verification.rb', line 53
def get_param_type(apiname)
param_type = {}
@@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param").each do |elem|
param_type[elem.attributes['name'].value] = elem.attributes['type'].value
end
return param_type
end
|
#get_special_encode_param(apiname) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/ecpay_payment/verification.rb', line 16
def get_special_encode_param(apiname)
ret = []
node = @@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param[@urlencode=\"1\"]")
node.each {|elem| ret.push(elem.attributes['name'].value)}
return ret
end
|
#get_str_param_pattern(apiname) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/ecpay_payment/verification.rb', line 87
def get_str_param_pattern(apiname)
pattern = {}
node = @@param_xml.xpath("/ECPayPayment/#{apiname}/Parameters//param[@type=\"String\"]")
node.each do |param_elem|
p_name = param_elem.attributes['name'].value
pat_elems = param_elem.xpath('./pattern')
pat = pat_elems.text
pattern[p_name] = pat
end
return pattern
end
|
#get_svc_url(apiname, mode) ⇒ Object
11
12
13
14
|
# File 'lib/ecpay_payment/verification.rb', line 11
def get_svc_url(apiname, mode)
url = @@param_xml.xpath("/ECPayPayment/#{apiname}/ServiceAddress/url[@type=\"#{mode}\"]").text
return url
end
|
#verify_param_by_pattern(params, pattern) ⇒ Object
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
|
# File 'lib/ecpay_payment/verification.rb', line 135
def verify_param_by_pattern(params, pattern)
type_index = pattern['Type_idx']
params.keys.each do |p_name|
p_type = type_index[p_name]
patt_container = pattern[p_type]
case
when p_type == 'String'
regex_patt = patt_container[p_name]
mat = /#{regex_patt}/.match(params[p_name])
if mat.nil?
raise ECpayInvalidParam, "Wrong format of param #{p_name} or length exceeded."
end
when p_type == 'Opt'
aval_opt = patt_container[p_name]
mat = aval_opt.include?(params[p_name])
if mat == false
raise ECpayInvalidParam, "Unexpected option of param #{p_name} (#{params[p_name]}). Avaliable option: (#{aval_opt})."
end
when p_type == 'Int'
criteria = patt_container[p_name]
mode = criteria[0]
max = criteria[1].to_i
min = criteria[2].to_i
val = params[p_name].to_i
case
when mode == 'BETWEEN'
if val < min or val > max
raise ECpayInvalidParam, "Value of #{p_name} should be between #{min} and #{max} ."
end
when mode == 'GE'
if val < min
raise ECpayInvalidParam, "Value of #{p_name} should be greater than or equal to #{min}."
end
when mode == 'LE'
if val > max
raise ECpayInvalidParam, "Value of #{p_name} should be less than or equal to #{max}."
end
when mode == 'EXCLUDE'
if val >= min and val <= max
raise ECpayInvalidParam, "Value of #{p_name} can NOT be between #{min} and #{max} .."
end
else
raise "Unexpected integer verification mode for parameter #{p_name}: #{mode}. "
end
when p_type == 'DepOpt'
dep_opt = patt_container[p_name]
parent_param = dep_opt.keys()[0]
all_dep_opt = dep_opt[parent_param]
parent_val = params[parent_param]
aval_opt = all_dep_opt[parent_val]
if aval_opt.nil? and pattern['Opt'][parent_param].include?(parent_val) == false
raise ECpayInvalidParam, "Cannot find avaliable option of [#{p_name}] by related param [#{parent_param}](Value: #{parent_val})."
elsif aval_opt.is_a?(Array)
unless aval_opt.include?(params[p_name])
raise ECpayInvalidParam, "Unexpected option of param #{p_name} (#{params[p_name]}). Avaliable option: (#{aval_opt})."
end
end
else
raise "Unexpected type (#{p_type}) for parameter #{p_name}. "
end
end
end
|