1 | <?php |
||
64 | class AuthnetJsonResponse |
||
65 | { |
||
66 | /** |
||
67 | * @const Indicates the status code of an approved transaction |
||
68 | */ |
||
69 | public const STATUS_APPROVED = 1; |
||
70 | |||
71 | /** |
||
72 | * @const Indicates the status code of an declined transaction |
||
73 | */ |
||
74 | public const STATUS_DECLINED = 2; |
||
75 | |||
76 | /** |
||
77 | * @const Indicates the status code of an transaction which has encountered an error |
||
78 | */ |
||
79 | public const STATUS_ERROR = 3; |
||
80 | |||
81 | /** |
||
82 | * @const Indicates the status code of a transaction held for review |
||
83 | */ |
||
84 | public const STATUS_HELD = 4; |
||
85 | |||
86 | /** |
||
87 | * @const Indicates the status code of a transaction held for review |
||
88 | */ |
||
89 | public const STATUS_PAYPAL_NEED_CONSENT = 5; |
||
90 | |||
91 | /** |
||
92 | * @var object SimpleXML object representing the API response |
||
93 | */ |
||
94 | private $response; |
||
95 | |||
96 | /** |
||
97 | * @var string JSON string that is the response sent by Authorize.Net |
||
98 | */ |
||
99 | private $responseJson; |
||
100 | |||
101 | /** |
||
102 | * @var object TransactionResponse |
||
103 | */ |
||
104 | private $transactionInfo; |
||
105 | |||
106 | /** |
||
107 | * Creates the response object with the response json returned from the API call |
||
108 | * |
||
109 | * @param string $responseJson Response from Authorize.Net |
||
110 | * @throws AuthnetInvalidJsonException |
||
111 | */ |
||
112 | public function __construct(string $responseJson) |
||
113 | { |
||
114 | $this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson); |
||
115 | if (($this->response = json_decode($this->responseJson, false)) === null) { |
||
116 | throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
||
117 | } |
||
118 | |||
119 | if ($this->directResponse || $this->validationDirectResponse) { |
||
120 | $dr = $this->directResponse ?: $this->validationDirectResponse; |
||
121 | $this->transactionInfo = new TransactionResponse($dr); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Outputs the response JSON in a human readable format |
||
127 | * |
||
128 | * @return string HTML table containing debugging information |
||
129 | */ |
||
130 | public function __toString() |
||
131 | { |
||
132 | $output = '<table id="authnet-response">'."\n"; |
||
133 | $output .= '<caption>Authorize.Net Response</caption>'."\n"; |
||
134 | $output .= '<tr>'."\n\t\t".'<th colspan="2"><b>Response JSON</b></th>'."\n".'</tr>'."\n"; |
||
135 | $output .= '<tr><td colspan="2"><pre>'."\n"; |
||
136 | $output .= $this->responseJson."\n"; |
||
137 | $output .= '</pre></td></tr>'."\n"; |
||
138 | $output .= '</table>'; |
||
139 | |||
140 | return $output; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Gets a response variable from the API response |
||
145 | * |
||
146 | * @param string $var unused |
||
147 | * @return string requested variable from the API call response |
||
148 | */ |
||
149 | public function __get(string $var) |
||
153 | |||
154 | /** |
||
155 | * Checks if the API call is not in an error state |
||
156 | * |
||
157 | * @return bool Whether the transaction was in an successful state |
||
158 | */ |
||
159 | public function isSuccessful() : bool |
||
163 | |||
164 | /** |
||
165 | * Checks if the API is reporting an error with the API call |
||
166 | * |
||
167 | * @return bool Whether the transaction was in an error state |
||
168 | */ |
||
169 | public function isError() : bool |
||
173 | |||
174 | /** |
||
175 | * Checks if a transaction was approved |
||
176 | * |
||
177 | * @return bool true if the transaction is approved |
||
178 | */ |
||
179 | public function isApproved() : bool |
||
183 | |||
184 | /** |
||
185 | * Checks if a transaction was completed using a prepaid card |
||
186 | * |
||
187 | * @return bool true if the transaction was completed using a prepaid card |
||
188 | */ |
||
189 | public function isPrePaidCard() : bool |
||
193 | |||
194 | /** |
||
195 | * Checks if a transaction was declined |
||
196 | * |
||
197 | * @return bool true if the transaction is declined |
||
198 | */ |
||
199 | public function isDeclined() : bool |
||
203 | |||
204 | /** |
||
205 | * Check to see if the ResponseCode matches the expected value |
||
206 | * |
||
207 | * @param integer $status |
||
208 | * @return bool Check to see if the ResponseCode matches the expected value |
||
209 | */ |
||
210 | protected function checkTransactionStatus(int $status) : bool |
||
219 | |||
220 | /** |
||
221 | * Gets the transaction response field for AIM and CIM transactions. |
||
222 | * |
||
223 | * @param mixed $field Name or key of the transaction field to be retrieved |
||
224 | * @return string Transaction field to be retrieved |
||
225 | * @throws AuthnetTransactionResponseCallException |
||
226 | */ |
||
227 | public function getTransactionResponseField($field) : string |
||
234 | |||
235 | /** |
||
236 | * Gets the transaction response from Authorize.Net in JSON format for logging purposes |
||
237 | * |
||
238 | * @return string transaction response from Authorize.Net in JSON format |
||
239 | */ |
||
240 | public function getRawResponse() : string |
||
244 | |||
245 | /** |
||
246 | * An alias of self::getErrorText() |
||
247 | * |
||
248 | * @return string Error response from Authorize.Net |
||
249 | */ |
||
250 | public function getErrorMessage() : string |
||
254 | |||
255 | /** |
||
256 | * If an error has occurred, returns the error message |
||
257 | * |
||
258 | * @return string Error response from Authorize.Net |
||
259 | */ |
||
260 | public function getErrorText() : string |
||
264 | |||
265 | /** |
||
266 | * If an error has occurred, returns the error message |
||
267 | * |
||
268 | * @return string Error response from Authorize.Net |
||
269 | */ |
||
270 | public function getErrorCode() : string |
||
274 | |||
275 | /** |
||
276 | * @param string $type Whether to get the error code or text |
||
277 | * @return string |
||
278 | */ |
||
279 | private function getError(string $type) : string |
||
291 | } |
||
292 |