1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
|
|
|
|
6
|
|
|
* This file is part of the AuthnetJSON package. |
7
|
|
|
* |
8
|
|
|
* (c) John Conde <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Authnetjson; |
15
|
|
|
|
16
|
|
|
use Authnetjson\Exception\AuthnetCannotSetParamsException; |
17
|
|
|
use Authnetjson\Exception\AuthnetInvalidJsonException; |
18
|
|
|
use Authnetjson\Exception\AuthnetTransactionResponseCallException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Adapter for the Authorize.Net JSON API |
22
|
|
|
* |
23
|
|
|
* @package AuthnetJSON |
24
|
|
|
* @author John Conde <[email protected]> |
25
|
|
|
* @copyright John Conde <[email protected]> |
26
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
27
|
|
|
* @link https://github.com/stymiee/authnetjson |
28
|
|
|
* @see https://developer.authorize.net/api/reference/ |
29
|
|
|
* |
30
|
|
|
* @property object $messages |
31
|
|
|
* @property string $directResponse |
32
|
|
|
* @property string $validationDirectResponseList |
33
|
|
|
* @property object $transactionResponse |
34
|
|
|
* |
35
|
|
|
* @method null createTransactionRequest(array $array) |
36
|
|
|
* @method null sendCustomerTransactionReceiptRequest(array $array) |
37
|
|
|
* @method null ARBCancelSubscriptionRequest(array $array) |
38
|
|
|
* @method null ARBCreateSubscriptionRequest(array $array) |
39
|
|
|
* @method null ARBGetSubscriptionStatusRequest(array $array) |
40
|
|
|
* @method null ARBUpdateSubscriptionRequest(array $array) |
41
|
|
|
* @method null createCustomerPaymentProfileRequest(array $array) |
42
|
|
|
* @method null createCustomerProfileRequest(array $array) |
43
|
|
|
* @method null createCustomerProfileTransactionRequest_authCapture(array $array) |
44
|
|
|
* @method null createCustomerProfileTransactionRequest_authOnly(array $array) |
45
|
|
|
* @method null createCustomerProfileTransactionRequest_captureOnly(array $array) |
46
|
|
|
* @method null createCustomerProfileTransactionRequest_priorAuthCapture(array $array) |
47
|
|
|
* @method null createCustomerProfileTransactionRequest_refund(array $array) |
48
|
|
|
* @method null createCustomerProfileTransactionRequest_void(array $array) |
49
|
|
|
* @method null createCustomerShippingAddressRequest(array $array) |
50
|
|
|
* @method null deleteCustomerPaymentProfileRequest(array $array) |
51
|
|
|
* @method null deleteCustomerProfileRequest(array $array) |
52
|
|
|
* @method null deleteCustomerShippingAddressRequest(array $array) |
53
|
|
|
* @method null getCustomerPaymentProfileRequest(array $array) |
54
|
|
|
* @method null getCustomerProfileIdsRequest(array $array) |
55
|
|
|
* @method null getCustomerProfileRequest(array $array) |
56
|
|
|
* @method null getCustomerShippingAddressRequest(array $array) |
57
|
|
|
* @method null getHostedProfilePageRequest(array $array) |
58
|
|
|
* @method null updateCustomerPaymentProfileRequest(array $array) |
59
|
|
|
* @method null updateCustomerProfileRequest(array $array) |
60
|
|
|
* @method null updateCustomerShippingAddressRequest(array $array) |
61
|
|
|
* @method null updateSplitTenderGroupRequest(array $array) |
62
|
|
|
* @method null validateCustomerPaymentProfileRequest(array $array) |
63
|
|
|
* @method null getBatchStatisticsRequest(array $array) |
64
|
|
|
* @method null getSettledBatchListRequest(array $array) |
65
|
|
|
* @method null getTransactionDetailsRequest(array $array) |
66
|
|
|
* @method null getTransactionListRequest(array $array) |
67
|
|
|
* @method null getUnsettledTransactionListRequest(array $array) |
68
|
|
|
*/ |
69
|
|
|
class AuthnetJsonResponse |
70
|
|
|
{ |
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @const Indicates the status code of an approved transaction |
73
|
|
|
*/ |
74
|
|
|
public const STATUS_APPROVED = 1; |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* @const Indicates the status code of a declined transaction |
78
|
|
|
*/ |
79
|
|
|
public const STATUS_DECLINED = 2; |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* @const Indicates the status code of a transaction which has encountered an error |
83
|
|
|
*/ |
84
|
|
|
public const STATUS_ERROR = 3; |
85
|
|
|
|
86
|
|
|
/** |
|
|
|
|
87
|
|
|
* @const Indicates the status code of a transaction held for review |
88
|
|
|
*/ |
89
|
|
|
public const STATUS_HELD = 4; |
90
|
|
|
|
91
|
|
|
/** |
|
|
|
|
92
|
|
|
* @const Indicates the status code of a transaction held for review |
93
|
|
|
*/ |
94
|
|
|
public const STATUS_PAYPAL_NEED_CONSENT = 5; |
95
|
|
|
|
96
|
|
|
/** |
|
|
|
|
97
|
|
|
* @var object SimpleXML object representing the API response |
98
|
|
|
*/ |
99
|
|
|
private $response; |
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
|
|
|
|
102
|
|
|
* @var string JSON string that is the response sent by Authorize.Net |
103
|
|
|
*/ |
104
|
|
|
private $responseJson; |
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
|
|
|
|
107
|
|
|
* @var object TransactionResponse |
108
|
|
|
*/ |
109
|
|
|
private $transactionInfo; |
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
|
|
|
|
112
|
|
|
* @var array TransactionResponse |
113
|
|
|
*/ |
114
|
|
|
private $transactionInfoArray; |
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates the response object with the response json returned from the API call |
118
|
|
|
* |
119
|
|
|
* @param string $responseJson Response from Authorize.Net |
|
|
|
|
120
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
121
|
|
|
*/ |
122
|
4 |
|
public function __construct(string $responseJson) |
|
|
|
|
123
|
|
|
{ |
|
|
|
|
124
|
4 |
|
$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson); |
125
|
4 |
|
if (($this->response = json_decode($this->responseJson, false)) === null) { |
|
|
|
|
126
|
1 |
|
throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
if ($this->directResponse |
130
|
2 |
|
|| $this->validationDirectResponseList |
131
|
3 |
|
|| isset($this->response->validationDirectResponse) |
132
|
|
|
) { |
133
|
3 |
|
$response = $this->directResponse ?: |
|
|
|
|
134
|
2 |
|
$this->validationDirectResponseList ?: |
|
|
|
|
135
|
3 |
|
$this->response->validationDirectResponse; |
136
|
3 |
|
if (is_array($response)) { |
137
|
1 |
|
$this->transactionInfoArray = array_map( |
138
|
|
|
static function ($r) { |
139
|
1 |
|
return new TransactionResponse($r); |
140
|
1 |
|
}, |
141
|
1 |
|
$response |
142
|
|
|
); |
143
|
|
|
} else { |
144
|
2 |
|
$this->transactionInfo = new TransactionResponse($response); |
|
|
|
|
145
|
2 |
|
$this->transactionInfoArray = [$this->transactionInfo]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
3 |
|
} |
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Outputs the response JSON in a human-readable format |
152
|
|
|
* |
153
|
|
|
* @return string HTML table containing debugging information |
154
|
|
|
*/ |
155
|
1 |
|
public function __toString() |
156
|
|
|
{ |
|
|
|
|
157
|
1 |
|
$output = '<table id="authnet-response">' . "\n"; |
|
|
|
|
158
|
1 |
|
$output .= '<caption>Authorize.Net Response</caption>' . "\n"; |
|
|
|
|
159
|
1 |
|
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>' . "\n"; |
|
|
|
|
160
|
1 |
|
$output .= '<tr><td colspan="2"><pre>' . "\n"; |
|
|
|
|
161
|
1 |
|
$output .= $this->responseJson . "\n"; |
|
|
|
|
162
|
1 |
|
$output .= '</pre></td></tr>' . "\n"; |
|
|
|
|
163
|
1 |
|
$output .= '</table>'; |
164
|
|
|
|
165
|
1 |
|
return $output; |
166
|
|
|
} |
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Gets a response variable from the API response |
170
|
|
|
* |
171
|
|
|
* @param string $key Name of the response key to be retrieved if it exists |
|
|
|
|
172
|
|
|
* @return string requested variable from the API call response |
|
|
|
|
173
|
|
|
*/ |
174
|
2 |
|
public function __get(string $key) |
175
|
|
|
{ |
|
|
|
|
176
|
2 |
|
return $this->response->{$key} ?? null; |
|
|
|
|
177
|
|
|
} |
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
|
|
|
|
180
|
|
|
* @throws AuthnetCannotSetParamsException |
|
|
|
|
181
|
|
|
*/ |
|
|
|
|
182
|
1 |
|
public function __set(string $key, $value) |
|
|
|
|
183
|
|
|
{ |
|
|
|
|
184
|
1 |
|
throw new AuthnetCannotSetParamsException(sprintf('You cannot set parameters directly in %s.', __CLASS__)); |
|
|
|
|
185
|
|
|
} |
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Checks if a value exists in the response JSON object |
189
|
|
|
* |
190
|
|
|
* @param string $key Name of the response key to check existence of |
|
|
|
|
191
|
|
|
* @return bool |
|
|
|
|
192
|
|
|
*/ |
193
|
1 |
|
public function __isset(string $key): bool |
194
|
|
|
{ |
|
|
|
|
195
|
1 |
|
return isset($this->response->{$key}); |
196
|
|
|
} |
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Checks if the API call is not in an error state |
200
|
|
|
* |
201
|
|
|
* @return bool Whether the transaction was in a successful state |
|
|
|
|
202
|
|
|
*/ |
203
|
2 |
|
public function isSuccessful(): bool |
204
|
|
|
{ |
|
|
|
|
205
|
2 |
|
return strtolower($this->messages->resultCode) === 'ok'; |
206
|
|
|
} |
|
|
|
|
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Checks if the API is reporting an error with the API call |
210
|
|
|
* |
211
|
|
|
* @return bool Whether the transaction was in an error state |
|
|
|
|
212
|
|
|
*/ |
213
|
2 |
|
public function isError(): bool |
214
|
|
|
{ |
|
|
|
|
215
|
2 |
|
return strtolower($this->messages->resultCode) === 'error'; |
216
|
|
|
} |
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Checks if a transaction was approved |
220
|
|
|
* |
221
|
|
|
* @return bool true if the transaction is approved |
|
|
|
|
222
|
|
|
*/ |
223
|
1 |
|
public function isApproved(): bool |
224
|
|
|
{ |
|
|
|
|
225
|
1 |
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_APPROVED); |
|
|
|
|
226
|
|
|
} |
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Checks if a transaction was completed using a prepaid card |
230
|
|
|
* |
231
|
|
|
* @return bool true if the transaction was completed using a prepaid card |
|
|
|
|
232
|
|
|
*/ |
233
|
1 |
|
public function isPrePaidCard(): bool |
234
|
|
|
{ |
|
|
|
|
235
|
1 |
|
return isset($this->transactionResponse->prePaidCard); |
236
|
|
|
} |
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Checks if a transaction was declined |
240
|
|
|
* |
241
|
|
|
* @return bool true if the transaction is declined |
|
|
|
|
242
|
|
|
*/ |
243
|
1 |
|
public function isDeclined(): bool |
244
|
|
|
{ |
|
|
|
|
245
|
1 |
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_DECLINED); |
|
|
|
|
246
|
|
|
} |
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Check to see if the ResponseCode matches the expected value |
250
|
|
|
* |
251
|
|
|
* @param int $status |
|
|
|
|
252
|
|
|
* @return bool Check to see if the ResponseCode matches the expected value |
|
|
|
|
253
|
|
|
*/ |
254
|
2 |
|
protected function checkTransactionStatus(int $status): bool |
255
|
|
|
{ |
|
|
|
|
256
|
2 |
|
if ($this->transactionInfo instanceof TransactionResponse) { |
257
|
1 |
|
$match = (int)$this->transactionInfo->getTransactionResponseField('ResponseCode') === $status; |
|
|
|
|
258
|
|
|
} else { |
259
|
1 |
|
$match = (int)$this->transactionResponse->responseCode === $status; |
|
|
|
|
260
|
|
|
} |
|
|
|
|
261
|
2 |
|
return $match; |
262
|
|
|
} |
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Gets the transaction response field for AIM and CIM transactions. |
266
|
|
|
* |
267
|
|
|
* @param mixed $field Name or key of the transaction field to be retrieved |
|
|
|
|
268
|
|
|
* @return null|string Transaction field to be retrieved |
|
|
|
|
269
|
|
|
* @throws AuthnetTransactionResponseCallException |
|
|
|
|
270
|
|
|
*/ |
271
|
2 |
|
public function getTransactionResponseField($field): ?string |
272
|
|
|
{ |
|
|
|
|
273
|
2 |
|
if ($this->transactionInfo instanceof TransactionResponse) { |
274
|
1 |
|
return $this->transactionInfo->getTransactionResponseField($field); |
275
|
|
|
} |
|
|
|
|
276
|
1 |
|
throw new AuthnetTransactionResponseCallException('This API call does not have any transaction response data'); |
|
|
|
|
277
|
|
|
} |
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Returns the results of a test charge for each payment account provided when created a customer profile |
|
|
|
|
281
|
|
|
* |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
1 |
|
public function getTransactionResponses(): array |
285
|
|
|
{ |
|
|
|
|
286
|
1 |
|
return $this->transactionInfoArray; |
287
|
|
|
} |
|
|
|
|
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Gets the transaction response from Authorize.Net in JSON format for logging purposes |
291
|
|
|
* |
292
|
|
|
* @return string transaction response from Authorize.Net in JSON format |
293
|
|
|
*/ |
294
|
1 |
|
public function getRawResponse(): string |
295
|
|
|
{ |
|
|
|
|
296
|
1 |
|
return $this->responseJson; |
297
|
|
|
} |
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* An alias of self::getErrorText() |
301
|
|
|
* |
302
|
|
|
* @return string Error response from Authorize.Net |
303
|
|
|
*/ |
304
|
1 |
|
public function getErrorMessage(): string |
305
|
|
|
{ |
|
|
|
|
306
|
1 |
|
return $this->getErrorText(); |
307
|
|
|
} |
|
|
|
|
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* If an error has occurred, returns the error message |
311
|
|
|
* |
312
|
|
|
* @return string Error response from Authorize.Net |
313
|
|
|
*/ |
314
|
2 |
|
public function getErrorText(): string |
315
|
|
|
{ |
|
|
|
|
316
|
2 |
|
return $this->getError('text'); |
317
|
|
|
} |
|
|
|
|
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* If an error has occurred, returns the error message |
321
|
|
|
* |
322
|
|
|
* @return string Error response from Authorize.Net |
323
|
|
|
*/ |
324
|
2 |
|
public function getErrorCode(): string |
325
|
|
|
{ |
|
|
|
|
326
|
2 |
|
return $this->getError('code'); |
327
|
|
|
} |
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
|
|
|
|
330
|
|
|
* @param string $type Whether to get the error code or text |
|
|
|
|
331
|
|
|
* @return string |
|
|
|
|
332
|
|
|
*/ |
333
|
2 |
|
private function getError(string $type): string |
|
|
|
|
334
|
|
|
{ |
|
|
|
|
335
|
2 |
|
$msg = ''; |
336
|
2 |
|
if ($this->isError()) { |
337
|
2 |
|
$prop = sprintf('error%s', ucfirst($type)); |
338
|
2 |
|
$msg = $this->messages->message[0]->{$type}; |
|
|
|
|
339
|
2 |
|
if (@$this->transactionResponse->errors[0]->{$prop}) { |
|
|
|
|
340
|
1 |
|
$msg = $this->transactionResponse->errors[0]->{$prop}; |
341
|
|
|
} |
342
|
|
|
} |
|
|
|
|
343
|
2 |
|
return $msg; |
344
|
|
|
} |
|
|
|
|
345
|
|
|
} |
|
|
|
|
346
|
|
|
|