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
|
|
|
/** |
17
|
|
|
* Adapter for the Authorize.Net JSON API |
18
|
|
|
* |
19
|
|
|
* @package AuthnetJSON |
20
|
|
|
* @author John Conde <[email protected]> |
21
|
|
|
* @copyright John Conde <[email protected]> |
22
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
23
|
|
|
* @link https://github.com/stymiee/authnetjson |
24
|
|
|
* @see https://developer.authorize.net/api/reference/ |
25
|
|
|
* |
26
|
|
|
* @property object $messages |
27
|
|
|
* @property string $directResponse |
28
|
|
|
* @property string $validationDirectResponseList |
29
|
|
|
* @property object $transactionResponse |
30
|
|
|
* |
31
|
|
|
* @method null createTransactionRequest(array $array) process a payment |
|
|
|
|
32
|
|
|
* @method null sendCustomerTransactionReceiptRequest(array $array) get a list of unsettled transactions |
|
|
|
|
33
|
|
|
* @method null ARBCancelSubscriptionRequest(array $array) cancel a subscription |
|
|
|
|
34
|
|
|
* @method null ARBCreateSubscriptionRequest(array $array) create a subscription |
|
|
|
|
35
|
|
|
* @method null ARBGetSubscriptionStatusRequest(array $array) get a subscription's status |
|
|
|
|
36
|
|
|
* @method null ARBUpdateSubscriptionRequest(array $array) update a subscription |
|
|
|
|
37
|
|
|
* @method null createCustomerPaymentProfileRequest(array $array) create a payment profile |
|
|
|
|
38
|
|
|
* @method null createCustomerProfileRequest(array $array) create a customer profile |
|
|
|
|
39
|
|
|
* @method null createCustomerProfileTransactionRequest_authCapture(array $array) process an Authorization and Capture transaction (Sale) |
|
|
|
|
40
|
|
|
* @method null createCustomerProfileTransactionRequest_authOnly(array $array) process an Authorization Only transaction |
|
|
|
|
41
|
|
|
* @method null createCustomerProfileTransactionRequest_captureOnly(array $array) process a Capture Only transaction |
|
|
|
|
42
|
|
|
* @method null createCustomerProfileTransactionRequest_priorAuthCapture(array $array) process a Prior Authorization Capture transaction |
|
|
|
|
43
|
|
|
* @method null createCustomerProfileTransactionRequest_refund(array $array) process a Refund (credit) |
|
|
|
|
44
|
|
|
* @method null createCustomerProfileTransactionRequest_void(array $array) void a transaction |
|
|
|
|
45
|
|
|
* @method null createCustomerShippingAddressRequest(array $array) create a shipping profile |
|
|
|
|
46
|
|
|
* @method null deleteCustomerPaymentProfileRequest(array $array) delete a payment profile |
|
|
|
|
47
|
|
|
* @method null deleteCustomerProfileRequest(array $array) delete a customer profile |
|
|
|
|
48
|
|
|
* @method null deleteCustomerShippingAddressRequest(array $array) delete a shipping profile |
|
|
|
|
49
|
|
|
* @method null getCustomerPaymentProfileRequest(array $array) retrieve a payment profile |
|
|
|
|
50
|
|
|
* @method null getCustomerProfileIdsRequest(array $array) retrieve a list of profile IDs |
|
|
|
|
51
|
|
|
* @method null getCustomerProfileRequest(array $array) retrieve a customer profile |
|
|
|
|
52
|
|
|
* @method null getCustomerShippingAddressRequest(array $array) retrieve a shipping address |
|
|
|
|
53
|
|
|
* @method null getHostedProfilePageRequest(array $array) retrieve a hosted payment page token |
|
|
|
|
54
|
|
|
* @method null updateCustomerPaymentProfileRequest(array $array) update a customer profile |
|
|
|
|
55
|
|
|
* @method null updateCustomerProfileRequest(array $array) update a customer profile |
|
|
|
|
56
|
|
|
* @method null updateCustomerShippingAddressRequest(array $array) update a shipping address |
|
|
|
|
57
|
|
|
* @method null updateSplitTenderGroupRequest(array $array) update a split tender transaction |
|
|
|
|
58
|
|
|
* @method null validateCustomerPaymentProfileRequest(array $array) validate a payment profile |
|
|
|
|
59
|
|
|
* @method null getBatchStatisticsRequest(array $array) get a summary of a settled batch |
|
|
|
|
60
|
|
|
* @method null getSettledBatchListRequest(array $array) get a list of settled batches |
|
|
|
|
61
|
|
|
* @method null getTransactionDetailsRequest(array $array) get the details of a transaction |
|
|
|
|
62
|
|
|
* @method null getTransactionListRequest(array $array) get a list of transaction in a batch |
|
|
|
|
63
|
|
|
* @method null getUnsettledTransactionListRequest(array $array) get a list of unsettled transactions |
|
|
|
|
64
|
|
|
*/ |
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
|
|
|
* @var array TransactionResponse |
109
|
|
|
*/ |
110
|
|
|
private $transactionInfoArray; |
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Creates the response object with the response json returned from the API call |
114
|
|
|
* |
115
|
|
|
* @param string $responseJson Response from Authorize.Net |
|
|
|
|
116
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
117
|
|
|
*/ |
118
|
4 |
|
public function __construct(string $responseJson) |
|
|
|
|
119
|
|
|
{ |
|
|
|
|
120
|
4 |
|
$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson); |
121
|
4 |
|
if (($this->response = json_decode($this->responseJson, false)) === null) { |
|
|
|
|
122
|
1 |
|
throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
if ($this->directResponse || $this->validationDirectResponseList || isset($this->response->validationDirectResponse)) { |
|
|
|
|
126
|
3 |
|
$response = $this->directResponse ?: $this->validationDirectResponseList ?: $this->response->validationDirectResponse; |
|
|
|
|
127
|
3 |
|
if (is_array($response)) { |
128
|
|
|
$this->transactionInfoArray = array_map(static function ($r) { |
|
|
|
|
129
|
1 |
|
return new TransactionResponse($r); |
130
|
1 |
|
}, $response); |
|
|
|
|
131
|
|
|
} else { |
132
|
2 |
|
$this->transactionInfo = new TransactionResponse($response); |
|
|
|
|
133
|
2 |
|
$this->transactionInfoArray = [$this->transactionInfo]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
3 |
|
} |
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Outputs the response JSON in a human readable format |
140
|
|
|
* |
141
|
|
|
* @return string HTML table containing debugging information |
142
|
|
|
*/ |
143
|
1 |
|
public function __toString() |
144
|
|
|
{ |
|
|
|
|
145
|
1 |
|
$output = '<table id="authnet-response">'."\n"; |
|
|
|
|
146
|
1 |
|
$output .= '<caption>Authorize.Net Response</caption>'."\n"; |
|
|
|
|
147
|
1 |
|
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>'."\n"; |
|
|
|
|
148
|
1 |
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
|
|
|
149
|
1 |
|
$output .= $this->responseJson."\n"; |
|
|
|
|
150
|
1 |
|
$output .= '</pre></td></tr>'."\n"; |
|
|
|
|
151
|
1 |
|
$output .= '</table>'; |
152
|
|
|
|
153
|
1 |
|
return $output; |
154
|
|
|
} |
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Gets a response variable from the API response |
158
|
|
|
* |
159
|
|
|
* @param string $var unused |
|
|
|
|
160
|
|
|
* @return string requested variable from the API call response |
|
|
|
|
161
|
|
|
*/ |
162
|
2 |
|
public function __get(string $var) |
163
|
|
|
{ |
|
|
|
|
164
|
2 |
|
return $this->response->{$var} ?? null; |
|
|
|
|
165
|
|
|
} |
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Checks if the API call is not in an error state |
169
|
|
|
* |
170
|
|
|
* @return bool Whether the transaction was in an successful state |
|
|
|
|
171
|
|
|
*/ |
172
|
2 |
|
public function isSuccessful(): bool |
173
|
|
|
{ |
|
|
|
|
174
|
2 |
|
return strtolower($this->messages->resultCode) === 'ok'; |
175
|
|
|
} |
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Checks if the API is reporting an error with the API call |
179
|
|
|
* |
180
|
|
|
* @return bool Whether the transaction was in an error state |
|
|
|
|
181
|
|
|
*/ |
182
|
2 |
|
public function isError(): bool |
183
|
|
|
{ |
|
|
|
|
184
|
2 |
|
return strtolower($this->messages->resultCode) === 'error'; |
185
|
|
|
} |
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Checks if a transaction was approved |
189
|
|
|
* |
190
|
|
|
* @return bool true if the transaction is approved |
|
|
|
|
191
|
|
|
*/ |
192
|
1 |
|
public function isApproved(): bool |
193
|
|
|
{ |
|
|
|
|
194
|
1 |
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_APPROVED); |
|
|
|
|
195
|
|
|
} |
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Checks if a transaction was completed using a prepaid card |
199
|
|
|
* |
200
|
|
|
* @return bool true if the transaction was completed using a prepaid card |
|
|
|
|
201
|
|
|
*/ |
202
|
1 |
|
public function isPrePaidCard(): bool |
203
|
|
|
{ |
|
|
|
|
204
|
1 |
|
return isset($this->transactionResponse->prePaidCard); |
205
|
|
|
} |
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Checks if a transaction was declined |
209
|
|
|
* |
210
|
|
|
* @return bool true if the transaction is declined |
|
|
|
|
211
|
|
|
*/ |
212
|
1 |
|
public function isDeclined(): bool |
213
|
|
|
{ |
|
|
|
|
214
|
1 |
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_DECLINED); |
|
|
|
|
215
|
|
|
} |
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Check to see if the ResponseCode matches the expected value |
219
|
|
|
* |
220
|
|
|
* @param int $status |
|
|
|
|
221
|
|
|
* @return bool Check to see if the ResponseCode matches the expected value |
|
|
|
|
222
|
|
|
*/ |
223
|
2 |
|
protected function checkTransactionStatus(int $status): bool |
224
|
|
|
{ |
|
|
|
|
225
|
2 |
|
if ($this->transactionInfo instanceof TransactionResponse) { |
226
|
1 |
|
$match = (int) $this->transactionInfo->getTransactionResponseField('ResponseCode') === $status; |
|
|
|
|
227
|
|
|
} else { |
228
|
1 |
|
$match = (int) $this->transactionResponse->responseCode === $status; |
|
|
|
|
229
|
|
|
} |
|
|
|
|
230
|
2 |
|
return $match; |
231
|
|
|
} |
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Gets the transaction response field for AIM and CIM transactions. |
235
|
|
|
* |
236
|
|
|
* @param mixed $field Name or key of the transaction field to be retrieved |
|
|
|
|
237
|
|
|
* @return string Transaction field to be retrieved |
|
|
|
|
238
|
|
|
* @throws AuthnetTransactionResponseCallException |
|
|
|
|
239
|
|
|
*/ |
240
|
2 |
|
public function getTransactionResponseField($field): string |
241
|
|
|
{ |
|
|
|
|
242
|
2 |
|
if ($this->transactionInfo instanceof TransactionResponse) { |
243
|
1 |
|
return $this->transactionInfo->getTransactionResponseField($field); |
|
|
|
|
244
|
|
|
} |
|
|
|
|
245
|
1 |
|
throw new AuthnetTransactionResponseCallException('This API call does not have any transaction response data'); |
|
|
|
|
246
|
|
|
} |
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns the results of a test charge for each payment account provided when created a customer profile |
|
|
|
|
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
1 |
|
public function getTransactionResponses(): array |
254
|
|
|
{ |
|
|
|
|
255
|
1 |
|
return $this->transactionInfoArray; |
256
|
|
|
} |
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Gets the transaction response from Authorize.Net in JSON format for logging purposes |
260
|
|
|
* |
261
|
|
|
* @return string transaction response from Authorize.Net in JSON format |
262
|
|
|
*/ |
263
|
1 |
|
public function getRawResponse(): string |
264
|
|
|
{ |
|
|
|
|
265
|
1 |
|
return $this->responseJson; |
266
|
|
|
} |
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* An alias of self::getErrorText() |
270
|
|
|
* |
271
|
|
|
* @return string Error response from Authorize.Net |
272
|
|
|
*/ |
273
|
1 |
|
public function getErrorMessage(): string |
274
|
|
|
{ |
|
|
|
|
275
|
1 |
|
return $this->getErrorText(); |
276
|
|
|
} |
|
|
|
|
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* If an error has occurred, returns the error message |
280
|
|
|
* |
281
|
|
|
* @return string Error response from Authorize.Net |
282
|
|
|
*/ |
283
|
2 |
|
public function getErrorText(): string |
284
|
|
|
{ |
|
|
|
|
285
|
2 |
|
return $this->getError('text'); |
286
|
|
|
} |
|
|
|
|
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* If an error has occurred, returns the error message |
290
|
|
|
* |
291
|
|
|
* @return string Error response from Authorize.Net |
292
|
|
|
*/ |
293
|
2 |
|
public function getErrorCode(): string |
294
|
|
|
{ |
|
|
|
|
295
|
2 |
|
return $this->getError('code'); |
296
|
|
|
} |
|
|
|
|
297
|
|
|
|
298
|
|
|
/** |
|
|
|
|
299
|
|
|
* @param string $type Whether to get the error code or text |
|
|
|
|
300
|
|
|
* @return string |
|
|
|
|
301
|
|
|
*/ |
302
|
2 |
|
private function getError(string $type): string |
|
|
|
|
303
|
|
|
{ |
|
|
|
|
304
|
2 |
|
$msg = ''; |
305
|
2 |
|
if ($this->isError()) { |
306
|
2 |
|
$prop = sprintf('error%s', ucfirst($type)); |
307
|
2 |
|
$msg = $this->messages->message[0]->{$type}; |
|
|
|
|
308
|
2 |
|
if (@$this->transactionResponse->errors[0]->{$prop}) { |
|
|
|
|
309
|
1 |
|
$msg = $this->transactionResponse->errors[0]->{$prop}; |
310
|
|
|
} |
311
|
|
|
} |
|
|
|
|
312
|
2 |
|
return $msg; |
313
|
|
|
} |
|
|
|
|
314
|
|
|
} |
|
|
|
|
315
|
|
|
|