1 | <?php |
||
65 | class AuthnetJsonResponse |
||
66 | { |
||
67 | /** |
||
68 | * @const Indicates the status code of an approved transaction |
||
69 | */ |
||
70 | public const STATUS_APPROVED = 1; |
||
71 | |||
72 | /** |
||
73 | * @const Indicates the status code of an declined transaction |
||
74 | */ |
||
75 | public const STATUS_DECLINED = 2; |
||
76 | |||
77 | /** |
||
78 | * @const Indicates the status code of an transaction which has encountered an error |
||
79 | */ |
||
80 | public const STATUS_ERROR = 3; |
||
81 | |||
82 | /** |
||
83 | * @const Indicates the status code of a transaction held for review |
||
84 | */ |
||
85 | public const STATUS_HELD = 4; |
||
86 | |||
87 | /** |
||
88 | * @const Indicates the status code of a transaction held for review |
||
89 | */ |
||
90 | public const STATUS_PAYPAL_NEED_CONSENT = 5; |
||
91 | |||
92 | /** |
||
93 | * @var object SimpleXML object representing the API response |
||
94 | */ |
||
95 | private $response; |
||
96 | |||
97 | /** |
||
98 | * @var string JSON string that is the response sent by Authorize.Net |
||
99 | */ |
||
100 | private $responseJson; |
||
101 | |||
102 | /** |
||
103 | * @var object TransactionResponse |
||
104 | */ |
||
105 | private $transactionInfo; |
||
106 | |||
107 | /** |
||
108 | * Creates the response object with the response json returned from the API call |
||
109 | * |
||
110 | * @param string $responseJson Response from Authorize.Net |
||
111 | * @throws AuthnetInvalidJsonException |
||
112 | */ |
||
113 | 3 | public function __construct(string $responseJson) |
|
125 | |||
126 | /** |
||
127 | * Outputs the response JSON in a human readable format |
||
128 | * |
||
129 | * @return string HTML table containing debugging information |
||
130 | */ |
||
131 | 1 | public function __toString() |
|
143 | |||
144 | /** |
||
145 | * Gets a response variable from the API response |
||
146 | * |
||
147 | * @param string $var unused |
||
148 | * @return string requested variable from the API call response |
||
149 | */ |
||
150 | 1 | public function __get(string $var) |
|
154 | |||
155 | /** |
||
156 | * Checks if the API call is not in an error state |
||
157 | * |
||
158 | * @return bool Whether the transaction was in an successful state |
||
159 | */ |
||
160 | 2 | public function isSuccessful() : bool |
|
164 | |||
165 | /** |
||
166 | * Checks if the API is reporting an error with the API call |
||
167 | * |
||
168 | * @return bool Whether the transaction was in an error state |
||
169 | */ |
||
170 | 2 | public function isError() : bool |
|
174 | |||
175 | /** |
||
176 | * Checks if a transaction was approved |
||
177 | * |
||
178 | * @return bool true if the transaction is approved |
||
179 | */ |
||
180 | 1 | public function isApproved() : bool |
|
184 | |||
185 | /** |
||
186 | * Checks if a transaction was completed using a prepaid card |
||
187 | * |
||
188 | * @return bool true if the transaction was completed using a prepaid card |
||
189 | */ |
||
190 | 1 | public function isPrePaidCard() : bool |
|
194 | |||
195 | /** |
||
196 | * Checks if a transaction was declined |
||
197 | * |
||
198 | * @return bool true if the transaction is declined |
||
199 | */ |
||
200 | 1 | public function isDeclined() : bool |
|
204 | |||
205 | /** |
||
206 | * Check to see if the ResponseCode matches the expected value |
||
207 | * |
||
208 | * @param int $status |
||
209 | * @return bool Check to see if the ResponseCode matches the expected value |
||
210 | */ |
||
211 | 2 | protected function checkTransactionStatus(int $status) : bool |
|
220 | |||
221 | /** |
||
222 | * Gets the transaction response field for AIM and CIM transactions. |
||
223 | * |
||
224 | * @param mixed $field Name or key of the transaction field to be retrieved |
||
225 | * @return string Transaction field to be retrieved |
||
226 | * @throws AuthnetTransactionResponseCallException |
||
227 | */ |
||
228 | 2 | public function getTransactionResponseField($field) : string |
|
229 | { |
||
230 | 2 | if ($this->transactionInfo instanceof TransactionResponse) { |
|
231 | 1 | return $this->transactionInfo->getTransactionResponseField($field); |
|
232 | } |
||
233 | 1 | throw new AuthnetTransactionResponseCallException('This API call does not have any transaction response data'); |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Gets the transaction response from Authorize.Net in JSON format for logging purposes |
||
238 | * |
||
239 | * @return string transaction response from Authorize.Net in JSON format |
||
240 | */ |
||
241 | 1 | public function getRawResponse() : string |
|
245 | |||
246 | /** |
||
247 | * An alias of self::getErrorText() |
||
248 | * |
||
249 | * @return string Error response from Authorize.Net |
||
250 | */ |
||
251 | 1 | public function getErrorMessage() : string |
|
255 | |||
256 | /** |
||
257 | * If an error has occurred, returns the error message |
||
258 | * |
||
259 | * @return string Error response from Authorize.Net |
||
260 | */ |
||
261 | 2 | public function getErrorText() : string |
|
265 | |||
266 | /** |
||
267 | * If an error has occurred, returns the error message |
||
268 | * |
||
269 | * @return string Error response from Authorize.Net |
||
270 | */ |
||
271 | 2 | public function getErrorCode() : string |
|
275 | |||
276 | /** |
||
277 | * @param string $type Whether to get the error code or text |
||
278 | * @return string |
||
279 | */ |
||
280 | 2 | private function getError(string $type) : string |
|
292 | } |
||
293 |