1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the AuthnetJSON package. |
5
|
|
|
* |
6
|
|
|
* (c) John Conde <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace JohnConde\Authnet; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Adapter for the Authorize.Net JSON API |
16
|
|
|
* |
17
|
|
|
* @package AuthnetJSON |
18
|
|
|
* @author John Conde <[email protected]> |
19
|
|
|
* @copyright John Conde <[email protected]> |
20
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
21
|
|
|
* @link https://github.com/stymiee/authnetjson |
22
|
|
|
* @see https://developer.authorize.net/api/reference/ |
23
|
|
|
* |
24
|
|
|
* @property object $messages |
25
|
|
|
* @property string $directResponse |
26
|
|
|
* @property string $validationDirectResponse |
27
|
|
|
* @property object $transactionResponse |
28
|
|
|
* |
29
|
|
|
* @method null createTransactionRequest(array $array) process a payment |
30
|
|
|
* @method null sendCustomerTransactionReceiptRequest(array $array) get a list of unsettled transactions |
31
|
|
|
* @method null ARBCancelSubscriptionRequest(array $array) cancel a subscription |
32
|
|
|
* @method null ARBCreateSubscriptionRequest(array $array) create a subscription |
33
|
|
|
* @method null ARBGetSubscriptionStatusRequest(array $array) get a subscription's status |
34
|
|
|
* @method null ARBUpdateSubscriptionRequest(array $array) update a subscription |
35
|
|
|
* @method null createCustomerPaymentProfileRequest(array $array) create a payment profile |
36
|
|
|
* @method null createCustomerProfileRequest(array $array) create a customer profile |
37
|
|
|
* @method null createCustomerProfileTransactionRequest_authCapture(array $array) process an Authorization and Capture transaction (Sale) |
38
|
|
|
* @method null createCustomerProfileTransactionRequest_authOnly(array $array) process an Authorization Only transaction |
39
|
|
|
* @method null createCustomerProfileTransactionRequest_captureOnly(array $array) process a Capture Only transaction |
40
|
|
|
* @method null createCustomerProfileTransactionRequest_priorAuthCapture(array $array) process a Prior Authorization Capture transaction |
41
|
|
|
* @method null createCustomerProfileTransactionRequest_refund(array $array) process a Refund (credit) |
42
|
|
|
* @method null createCustomerProfileTransactionRequest_void(array $array) void a transaction |
43
|
|
|
* @method null createCustomerShippingAddressRequest(array $array) create a shipping profile |
44
|
|
|
* @method null deleteCustomerPaymentProfileRequest(array $array) delete a payment profile |
45
|
|
|
* @method null deleteCustomerProfileRequest(array $array) delete a customer profile |
46
|
|
|
* @method null deleteCustomerShippingAddressRequest(array $array) delete a shipping profile |
47
|
|
|
* @method null getCustomerPaymentProfileRequest(array $array) retrieve a payment profile |
48
|
|
|
* @method null getCustomerProfileIdsRequest(array $array) retrieve a list of profile IDs |
49
|
|
|
* @method null getCustomerProfileRequest(array $array) retrieve a customer profile |
50
|
|
|
* @method null getCustomerShippingAddressRequest(array $array) retrieve a shipping address |
51
|
|
|
* @method null getHostedProfilePageRequest(array $array) retrieve a hosted payment page token |
52
|
|
|
* @method null updateCustomerPaymentProfileRequest(array $array) update a customer profile |
53
|
|
|
* @method null updateCustomerProfileRequest(array $array) update a customer profile |
54
|
|
|
* @method null updateCustomerShippingAddressRequest(array $array) update a shipping address |
55
|
|
|
* @method null updateSplitTenderGroupRequest(array $array) update a split tender transaction |
56
|
|
|
* @method null validateCustomerPaymentProfileRequest(array $array) validate a payment profile |
57
|
|
|
* @method null getBatchStatisticsRequest(array $array) get a summary of a settled batch |
58
|
|
|
* @method null getSettledBatchListRequest(array $array) get a list of settled batches |
59
|
|
|
* @method null getTransactionDetailsRequest(array $array) get the details of a transaction |
60
|
|
|
* @method null getTransactionListRequest(array $array) get a list of transaction in a batch |
61
|
|
|
* @method null getUnsettledTransactionListRequest(array $array) get a list of unsettled transactions |
62
|
|
|
*/ |
63
|
|
|
class AuthnetJsonResponse |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* @const Indicates the status code of an approved transaction |
67
|
|
|
*/ |
68
|
|
|
const STATUS_APPROVED = 1; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @const Indicates the status code of an declined transaction |
72
|
|
|
*/ |
73
|
|
|
const STATUS_DECLINED = 2; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @const Indicates the status code of an transaction which has encountered an error |
77
|
|
|
*/ |
78
|
|
|
const STATUS_ERROR = 3; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @const Indicates the status code of a transaction held for review |
82
|
|
|
*/ |
83
|
|
|
const STATUS_HELD = 4; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @const Indicates the status code of a transaction held for review |
87
|
|
|
*/ |
88
|
|
|
const STATUS_PAYPAL_NEED_CONSENT = 5; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var object SimpleXML object representing the API response |
92
|
|
|
*/ |
93
|
|
|
private $response; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var string JSON string that is the response sent by Authorize.Net |
97
|
|
|
*/ |
98
|
|
|
private $responseJson; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var object TransactionResponse |
102
|
|
|
*/ |
103
|
|
|
private $transactionInfo; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Creates the response object with the response json returned from the API call |
107
|
|
|
* |
108
|
|
|
* @param string $responseJson Response from Authorize.Net |
109
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
110
|
|
|
*/ |
111
|
|
|
public function __construct(string $responseJson) |
112
|
|
|
{ |
113
|
|
|
$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson); |
114
|
|
|
if (($this->response = json_decode($this->responseJson)) === null) { |
115
|
|
|
throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->transactionInfo = null; |
119
|
|
|
if (@$this->directResponse || @$this->validationDirectResponse) { |
120
|
|
|
$dr = (@$this->directResponse) ? $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 summary="Authorize.Net Response" id="authnet-response">'."\n"; |
133
|
|
|
$output .= '<tr>'."\n\t\t".'<th colspan="2"><b>Response JSON</b></th>'."\n".'</tr>'."\n"; |
134
|
|
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
135
|
|
|
$output .= $this->responseJson."\n"; |
136
|
|
|
$output .= '</pre></td></tr>'."\n"; |
137
|
|
|
$output .= '</table>'; |
138
|
|
|
|
139
|
|
|
return $output; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Gets a response variable from the API response |
144
|
|
|
* |
145
|
|
|
* @param string $var unused |
146
|
|
|
* @return string requested variable from the API call response |
147
|
|
|
*/ |
148
|
|
|
public function __get(string $var) |
149
|
|
|
{ |
150
|
|
|
return $this->response->{$var}; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Checks if the API call is not in an error state |
155
|
|
|
* |
156
|
|
|
* @return bool Whether the transaction was in an successful state |
157
|
|
|
*/ |
158
|
|
|
public function isSuccessful() : bool |
159
|
|
|
{ |
160
|
|
|
return strtolower($this->messages->resultCode) === 'ok'; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Checks if the API is reporting an error with the API call |
165
|
|
|
* |
166
|
|
|
* @return bool Whether the transaction was in an error state |
167
|
|
|
*/ |
168
|
|
|
public function isError() : bool |
169
|
|
|
{ |
170
|
|
|
return strtolower($this->messages->resultCode) === 'error'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Checks if a transaction was approved |
175
|
|
|
* |
176
|
|
|
* @return bool true if the transaction is approved |
177
|
|
|
*/ |
178
|
|
|
public function isApproved() : bool |
179
|
|
|
{ |
180
|
|
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_APPROVED); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Checks if a transaction was declined |
185
|
|
|
* |
186
|
|
|
* @return bool true if the transaction is declined |
187
|
|
|
*/ |
188
|
|
|
public function isDeclined() : bool |
189
|
|
|
{ |
190
|
|
|
return $this->isSuccessful() && $this->checkTransactionStatus(self::STATUS_DECLINED); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Check to see if the ResponseCode matches the expected value |
195
|
|
|
* |
196
|
|
|
* @param integer $status |
197
|
|
|
* @return bool Check to see if the ResponseCode matches the expected value |
198
|
|
|
*/ |
199
|
|
|
protected function checkTransactionStatus(int $status) : bool |
200
|
|
|
{ |
201
|
|
|
if ($this->transactionInfo instanceof TransactionResponse) { |
202
|
|
|
$match = $this->transactionInfo->getTransactionResponseField('ResponseCode') == $status; |
203
|
|
|
} else { |
204
|
|
|
$match = $this->transactionResponse->responseCode == $status; |
205
|
|
|
} |
206
|
|
|
return $match; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Gets the transaction response field for AIM and CIM transactions. |
211
|
|
|
* |
212
|
|
|
* @param mixed $field Name or key of the transaction field to be retrieved |
213
|
|
|
* @return string Transaction field to be retrieved |
214
|
|
|
* @throws \JohnConde\Authnet\AuthnetTransactionResponseCallException |
215
|
|
|
*/ |
216
|
|
|
public function getTransactionResponseField($field) : string |
217
|
|
|
{ |
218
|
|
|
if ($this->transactionInfo instanceof TransactionResponse) { |
219
|
|
|
return $this->transactionInfo->getTransactionResponseField($field); |
220
|
|
|
} |
221
|
|
|
throw new AuthnetTransactionResponseCallException('This API call does not have any transaction response data'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets the transaction response from Authorize.Net in JSON format for logging purposes |
226
|
|
|
* |
227
|
|
|
* @return string transaction response from Authorize.Net in JSON format |
228
|
|
|
*/ |
229
|
|
|
public function getRawResponse() : string |
230
|
|
|
{ |
231
|
|
|
return $this->responseJson; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* An alias of self::getErrorText() |
236
|
|
|
* |
237
|
|
|
* @return string Error response from Authorize.Net |
238
|
|
|
*/ |
239
|
|
|
public function getErrorMessage() : string |
240
|
|
|
{ |
241
|
|
|
return $this->getErrorText(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* If an error has occurred, returns the error message |
246
|
|
|
* |
247
|
|
|
* @return string Error response from Authorize.Net |
248
|
|
|
*/ |
249
|
|
|
public function getErrorText() : string |
250
|
|
|
{ |
251
|
|
|
return $this->getError('text'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* If an error has occurred, returns the error message |
256
|
|
|
* |
257
|
|
|
* @return string Error response from Authorize.Net |
258
|
|
|
*/ |
259
|
|
|
public function getErrorCode() : string |
260
|
|
|
{ |
261
|
|
|
return $this->getError('code'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $type Whether to get the error code or text |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
private function getError(string $type) : string |
269
|
|
|
{ |
270
|
|
|
$msg = ''; |
271
|
|
|
if ($this->isError()) { |
272
|
|
|
$prop = sprintf('error%s', ucfirst($type)); |
273
|
|
|
$msg = $this->messages->message[0]->{$type}; |
274
|
|
|
if (@$this->transactionResponse->errors[0]->{$prop}) { |
275
|
|
|
$msg = $this->transactionResponse->errors[0]->{$prop}; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
return $msg; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|