|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base class for the AuthorizeNet ARB & CIM Responses. |
|
4
|
|
|
* |
|
5
|
|
|
* @package AuthorizeNet |
|
6
|
|
|
* @subpackage AuthorizeNetXML |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base class for the AuthorizeNet ARB & CIM Responses. |
|
11
|
|
|
* |
|
12
|
|
|
* @package AuthorizeNet |
|
13
|
|
|
* @subpackage AuthorizeNetXML |
|
14
|
|
|
*/ |
|
15
|
|
|
class AuthorizeNetXMLResponse |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
public $xml; // Holds a SimpleXML Element with response. |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. Parses the AuthorizeNet response string. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $response The response from the AuthNet server. |
|
24
|
|
|
*/ |
|
25
|
11 |
|
public function __construct($response) |
|
26
|
|
|
{ |
|
27
|
11 |
|
$this->response = $response; |
|
|
|
|
|
|
28
|
11 |
|
if ($response) { |
|
29
|
11 |
|
$this->xml = @simplexml_load_string($response); |
|
30
|
|
|
|
|
31
|
|
|
// Remove namespaces for use with XPath. |
|
32
|
11 |
|
$this->xpath_xml = @simplexml_load_string(preg_replace('/ xmlns:xsi[^>]+/','',$response)); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
11 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Was the transaction successful? |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
9 |
|
public function isOk() |
|
42
|
|
|
{ |
|
43
|
9 |
|
return ($this->getResultCode() == "Ok"); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Run an xpath query on the cleaned XML response |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @return array Returns an array of SimpleXMLElement objects or FALSE in case of an error. |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function xpath($path) |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->xpath_xml->xpath($path); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Was there an error? |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function isError() |
|
63
|
|
|
{ |
|
64
|
|
|
return ($this->getResultCode() == "Error"); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getErrorMessage() |
|
71
|
|
|
{ |
|
72
|
|
|
return "Error: {$this->getResultCode()} |
|
73
|
|
|
Message: {$this->getMessageText()} |
|
74
|
|
|
{$this->getMessageCode()}"; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getRefID() |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->_getElementContents("refId"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
9 |
|
public function getResultCode() |
|
89
|
|
|
{ |
|
90
|
9 |
|
return $this->_getElementContents("resultCode"); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
3 |
|
public function getMessageCode() |
|
97
|
|
|
{ |
|
98
|
3 |
|
return $this->_getElementContents("code"); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function getMessageText() |
|
105
|
|
|
{ |
|
106
|
3 |
|
return $this->_getElementContents("text"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return dynamic |
|
111
|
|
|
*/ |
|
112
|
|
|
public function data(){ |
|
113
|
|
|
return json_decode(json_encode($this->xml)); |
|
114
|
|
|
} |
|
115
|
9 |
|
|
|
116
|
|
|
/** |
|
117
|
9 |
|
* Grabs the contents of a unique element. |
|
118
|
9 |
|
* |
|
119
|
9 |
|
* @param string |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
9 |
|
protected function _getElementContents($elementName) |
|
123
|
9 |
|
{ |
|
124
|
9 |
|
$start = "<$elementName>"; |
|
125
|
|
|
$end = "</$elementName>"; |
|
126
|
|
|
if (strpos($this->response,$start) === false || strpos($this->response,$end) === false) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} else { |
|
129
|
|
|
$start_position = strpos($this->response, $start)+strlen($start); |
|
130
|
|
|
$end_position = strpos($this->response, $end); |
|
131
|
|
|
return substr($this->response, $start_position, $end_position-$start_position); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.