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 JohnConde\Authnet; |
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 $validationDirectResponse |
|
|
|
|
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
|
|
|
* 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) |
|
|
|
|
114
|
|
|
{ |
|
|
|
|
115
|
3 |
|
$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson); |
116
|
3 |
|
if (($this->response = json_decode($this->responseJson, false)) === null) { |
|
|
|
|
117
|
1 |
|
throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
if ($this->directResponse || $this->validationDirectResponse) { |
121
|
2 |
|
$dr = $this->directResponse ?: $this->validationDirectResponse; |
|
|
|
|
122
|
2 |
|
$this->transactionInfo = new TransactionResponse($dr); |
123
|
|
|
} |
124
|
2 |
|
} |
|
|
|
|
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() |
132
|
|
|
{ |
|
|
|
|
133
|
1 |
|
$output = '<table id="authnet-response">'."\n"; |
|
|
|
|
134
|
1 |
|
$output .= '<caption>Authorize.Net Response</caption>'."\n"; |
|
|
|
|
135
|
1 |
|
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>'."\n"; |
|
|
|
|
136
|
1 |
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
|
|
|
137
|
1 |
|
$output .= $this->responseJson."\n"; |
|
|
|
|
138
|
1 |
|
$output .= '</pre></td></tr>'."\n"; |
|
|
|
|
139
|
1 |
|
$output .= '</table>'; |
140
|
|
|
|
141
|
1 |
|
return $output; |
142
|
|
|
} |
|
|
|
|
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) |
151
|
|
|
{ |
|
|
|
|
152
|
1 |
|
return $this->response->{$var} ?? null; |
|
|
|
|
153
|
|
|
} |
|
|
|
|
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 |
161
|
|
|
{ |
|
|
|
|
162
|
2 |
|
return strtolower($this->messages->resultCode) === 'ok'; |
163
|
|
|
} |
|
|
|
|
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 |
171
|
|
|
{ |
|
|
|
|
172
|
2 |
|
return strtolower($this->messages->resultCode) === 'error'; |
173
|
|
|
} |
|
|
|
|
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 |
181
|
|
|
{ |
|
|
|
|
182
|
1 |
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_APPROVED); |
|
|
|
|
183
|
|
|
} |
|
|
|
|
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 |
191
|
|
|
{ |
|
|
|
|
192
|
1 |
|
return isset($this->transactionResponse->prePaidCard); |
193
|
|
|
} |
|
|
|
|
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 |
201
|
|
|
{ |
|
|
|
|
202
|
1 |
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_DECLINED); |
|
|
|
|
203
|
|
|
} |
|
|
|
|
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 |
|
|
|
|
212
|
|
|
{ |
|
|
|
|
213
|
2 |
|
if ($this->transactionInfo instanceof TransactionResponse) { |
214
|
1 |
|
$match = (int) $this->transactionInfo->getTransactionResponseField('ResponseCode') === $status; |
|
|
|
|
215
|
|
|
} else { |
216
|
1 |
|
$match = (int) $this->transactionResponse->responseCode === $status; |
|
|
|
|
217
|
|
|
} |
|
|
|
|
218
|
2 |
|
return $match; |
219
|
|
|
} |
|
|
|
|
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 |
242
|
|
|
{ |
|
|
|
|
243
|
1 |
|
return $this->responseJson; |
244
|
|
|
} |
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* An alias of self::getErrorText() |
248
|
|
|
* |
249
|
|
|
* @return string Error response from Authorize.Net |
|
|
|
|
250
|
|
|
*/ |
251
|
1 |
|
public function getErrorMessage() : string |
252
|
|
|
{ |
|
|
|
|
253
|
1 |
|
return $this->getErrorText(); |
254
|
|
|
} |
|
|
|
|
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 |
262
|
|
|
{ |
|
|
|
|
263
|
2 |
|
return $this->getError('text'); |
264
|
|
|
} |
|
|
|
|
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 |
272
|
|
|
{ |
|
|
|
|
273
|
2 |
|
return $this->getError('code'); |
274
|
|
|
} |
|
|
|
|
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 |
|
|
|
|
281
|
|
|
{ |
|
|
|
|
282
|
2 |
|
$msg = ''; |
283
|
2 |
|
if ($this->isError()) { |
284
|
2 |
|
$prop = sprintf('error%s', ucfirst($type)); |
285
|
2 |
|
$msg = $this->messages->message[0]->{$type}; |
|
|
|
|
286
|
2 |
|
if (@$this->transactionResponse->errors[0]->{$prop}) { |
|
|
|
|
287
|
1 |
|
$msg = $this->transactionResponse->errors[0]->{$prop}; |
288
|
|
|
} |
289
|
|
|
} |
|
|
|
|
290
|
2 |
|
return $msg; |
291
|
|
|
} |
|
|
|
|
292
|
|
|
} |
|
|
|
|
293
|
|
|
|