|
1
|
1 |
|
module AuthorizeNet |
|
2
|
|
|
# The core, xml response class. You shouldn't instantiate this one. |
|
3
|
|
|
# Instead you should use AuthorizeNet::ARB::Response. |
|
4
|
1 |
|
class XmlResponse < AuthorizeNet::Response |
|
5
|
|
|
# DO NOT USE. Instantiate AuthorizeNet::ARB::Response or AuthorizeNet::CIM::Response instead. |
|
6
|
1 |
|
def initialize(raw_response, transaction) |
|
7
|
11 |
|
@raw_response = raw_response |
|
8
|
11 |
|
@transaction = transaction |
|
9
|
11 |
|
unless connection_failure? |
|
10
|
11 |
|
begin |
|
11
|
11 |
|
xml = Nokogiri::XML(@raw_response.body) do |config| |
|
12
|
|
|
# confirm noent is the right flag |
|
13
|
11 |
|
config.recover.noent.nonet |
|
14
|
|
|
end |
|
15
|
11 |
|
@root = xml.children[0] |
|
16
|
11 |
|
@result_code = node_content_unless_nil(@root.at_css('messages resultCode')) |
|
17
|
11 |
|
@message_code = node_content_unless_nil(@root.at_css('messages message code')) |
|
18
|
11 |
|
@message_text = node_content_unless_nil(@root.at_css('messages message text')) |
|
19
|
11 |
|
@reference_id = node_content_unless_nil(@root.at_css('refId')) |
|
20
|
|
|
rescue StandardError |
|
21
|
|
|
@raw_response = $ERROR_INFO |
|
22
|
|
|
end |
|
23
|
|
|
end |
|
24
|
|
|
end |
|
25
|
|
|
|
|
26
|
|
|
# Check to see if the response indicated success. Success is defined as a 200 OK response with a resultCode |
|
27
|
|
|
# of 'Ok'. |
|
28
|
1 |
|
def success? |
|
29
|
12 |
|
!connection_failure? && @result_code == 'Ok' |
|
30
|
|
|
end |
|
31
|
|
|
|
|
32
|
|
|
# Returns true if we failed to open a connection to the gateway or got back a non-200 OK HTTP response. |
|
33
|
1 |
|
def connection_failure? |
|
34
|
34 |
|
!@raw_response.is_a?(Net::HTTPOK) |
|
35
|
|
|
end |
|
36
|
|
|
|
|
37
|
|
|
# Returns the underlying Net::HTTPResponse object. This has the original response body along with |
|
38
|
|
|
# headers and such. Note that if an exception is generated while making the request (which happens |
|
39
|
|
|
# if there is no internet connection for example), you will get the exception object here instead of |
|
40
|
|
|
# a Net::HTTPResponse object. |
|
41
|
1 |
|
def raw |
|
42
|
|
|
@raw_response |
|
43
|
|
|
end |
|
44
|
|
|
|
|
45
|
|
|
# Returns a deep-copy of the XML object received from the payment gateway. Or nil if there was no XML payload. |
|
46
|
1 |
|
def xml |
|
47
|
|
|
@root.dup unless @root.nil? |
|
48
|
|
|
end |
|
49
|
|
|
|
|
50
|
|
|
# Returns the resultCode from the XML response. resultCode will be either 'Ok' or 'Error'. |
|
51
|
1 |
|
attr_reader :result_code |
|
52
|
|
|
|
|
53
|
|
|
# Returns the messageCode from the XML response. This is a code indicating the details of an error |
|
54
|
|
|
# or success. |
|
55
|
1 |
|
attr_reader :message_code |
|
56
|
|
|
|
|
57
|
|
|
# Returns the messageText from the XML response. This is a text description of the message_code. |
|
58
|
1 |
|
attr_reader :message_text |
|
59
|
|
|
|
|
60
|
|
|
# Alias for result_code. |
|
61
|
1 |
|
def response_code |
|
62
|
|
|
result_code |
|
63
|
|
|
end |
|
64
|
|
|
|
|
65
|
|
|
# Alias for message_code. |
|
66
|
1 |
|
def response_reason_code |
|
67
|
|
|
message_code |
|
68
|
|
|
end |
|
69
|
|
|
|
|
70
|
|
|
# Alias for message_text. |
|
71
|
1 |
|
def response_reason_text |
|
72
|
|
|
message_text |
|
73
|
|
|
end |
|
74
|
|
|
|
|
75
|
|
|
# Returns the refId from the response if there is one. Otherwise returns nil. |
|
76
|
1 |
|
attr_reader :reference_id |
|
77
|
|
|
|
|
78
|
|
|
#:enddoc: |
|
79
|
1 |
|
protected |
|
80
|
|
|
|
|
81
|
1 |
|
def node_content_unless_nil(node) |
|
82
|
77 |
|
if node.nil? |
|
83
|
|
|
nil |
|
84
|
|
|
else |
|
85
|
53 |
|
node.content |
|
86
|
|
|
end |
|
87
|
|
|
end |
|
88
|
|
|
|
|
89
|
1 |
|
def node_child_content_unless_nil(node) |
|
90
|
|
|
if node.nil? |
|
91
|
|
|
nil |
|
92
|
|
|
else |
|
93
|
|
|
node.children.collect(&:content) unless node.children.empty? |
|
94
|
|
|
end |
|
95
|
|
|
end |
|
96
|
|
|
|
|
97
|
|
|
# Transforms a block of XML into a model Object defined by entity_desc. |
|
98
|
1 |
|
def build_entity(xml, entity_desc) |
|
99
|
65 |
|
args = {} |
|
100
|
65 |
|
entity_desc.node_structure.each do |node_desc| |
|
101
|
1456 |
|
node_name = (node_desc.keys.reject { |k| k.to_s[0..0] == '_' }).first |
|
102
|
613 |
|
args.merge!(handle_node_type(xml, node_desc, node_name, args, '')) |
|
103
|
|
|
end |
|
104
|
|
|
|
|
105
|
65 |
|
return nil if args.empty? |
|
106
|
|
|
|
|
107
|
33 |
|
if entity_desc.arg_mapping.nil? |
|
108
|
30 |
|
return entity_desc.entity_class.new(args) |
|
109
|
|
|
else |
|
110
|
3 |
|
args_list = [] |
|
111
|
3 |
|
entity_desc.arg_mapping.each do |arg| |
|
112
|
8 |
|
args_list <<= args[arg] |
|
113
|
8 |
|
args.delete(arg) |
|
114
|
|
|
end |
|
115
|
3 |
|
args_list <<= args |
|
116
|
3 |
|
return entity_desc.entity_class.new(*args_list) |
|
117
|
|
|
end |
|
118
|
|
|
end |
|
119
|
|
|
|
|
120
|
|
|
# Parses an XML fragment into an internal representation. |
|
121
|
1 |
|
def handle_node_type(xml, node_desc, node_name, args, base_name) |
|
122
|
643 |
|
case node_desc[node_name] |
|
123
|
|
|
when Symbol |
|
124
|
559 |
|
node = xml.at_css(base_name + node_name.to_s) |
|
125
|
559 |
|
unless node.nil? |
|
126
|
181 |
|
content = node.content |
|
127
|
181 |
|
case node_desc[:_converter] |
|
128
|
|
|
when Method, Proc |
|
129
|
|
|
content = node_desc[:_converter].call(content) |
|
130
|
|
|
when Symbol |
|
131
|
73 |
|
content = send(node_desc[:_converter], content) |
|
132
|
|
|
end |
|
133
|
181 |
|
args[node_desc[node_name]] = content unless content.nil? |
|
134
|
|
|
end |
|
135
|
|
|
when AuthorizeNet::EntityDescription |
|
136
|
60 |
|
if node_desc[:_multivalue].nil? |
|
137
|
42 |
|
entity = build_entity(xml.css(base_name + node_name.to_s), node_desc[node_name]) |
|
138
|
42 |
|
args[node_desc[:_value]] = entity unless entity.nil? |
|
139
|
|
|
else |
|
140
|
18 |
|
xml.css(base_name + node_name.to_s).each do |node| |
|
|
|
|
|
|
141
|
7 |
|
entity = build_entity(node, node_desc[node_name]) |
|
142
|
7 |
|
args[node_desc[:_multivalue]] = args[node_desc[:_multivalue]].to_a + entity.to_a unless entity.nil? |
|
143
|
|
|
end |
|
144
|
|
|
end |
|
145
|
|
|
when Array |
|
146
|
24 |
|
node_desc[node_name].each do |inner_node| |
|
147
|
90 |
|
inner_node_name = (inner_node.keys.reject { |k| k.to_s[0..0] == '_' }).first |
|
148
|
30 |
|
args.merge!(handle_node_type(xml, inner_node, inner_node_name, args, node_name.to_s + ' ')) |
|
149
|
|
|
end |
|
150
|
|
|
end |
|
151
|
643 |
|
args |
|
152
|
|
|
end |
|
153
|
|
|
end |
|
154
|
|
|
end |
|
155
|
|
|
|