-
Notifications
You must be signed in to change notification settings - Fork 92
/
AIMResponse.php
204 lines (179 loc) · 7.02 KB
/
AIMResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Omnipay\AuthorizeNet\Message;
use Omnipay\AuthorizeNet\Model\CardReference;
use Omnipay\AuthorizeNet\Model\TransactionReference;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\AbstractResponse;
/**
* Authorize.Net AIM Response
*/
class AIMResponse extends AbstractResponse
{
/**
* For Error codes: @see https://developer.authorize.net/api/reference/responseCodes.html
*/
const ERROR_RESPONSE_CODE_CANNOT_ISSUE_CREDIT = 54;
/**
* The overall transaction result code.
*/
const TRANSACTION_RESULT_CODE_APPROVED = 1;
const TRANSACTION_RESULT_CODE_DECLINED = 2;
const TRANSACTION_RESULT_CODE_ERROR = 3;
const TRANSACTION_RESULT_CODE_REVIEW = 4;
public function __construct(AbstractRequest $request, $data)
{
// Strip out the xmlns junk so that PHP can parse the XML
$xml = preg_replace('/<createTransactionResponse[^>]+>/', '<createTransactionResponse>', (string)$data);
try {
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOWARNING);
} catch (\Exception $e) {
throw new InvalidResponseException();
}
if (!$xml) {
throw new InvalidResponseException();
}
parent::__construct($request, $xml);
}
public function isSuccessful()
{
return static::TRANSACTION_RESULT_CODE_APPROVED === $this->getResultCode();
}
/**
* Status of the transaction. This field is also known as "Response Code" in Authorize.NET terminology.
* A result of 0 is returned if there is no transaction response returned, e.g. a validation error in
* some data, or invalid login credentials.
*
* @return int 1 = Approved, 2 = Declined, 3 = Error, 4 = Held for Review
*/
public function getResultCode()
{
// If there is a transaction response, then we get the code from that.
if (isset($this->data->transactionResponse)) {
return intval((string)$this->data->transactionResponse->responseCode);
}
// No transaction response, so return 3 aka "error".
return static::TRANSACTION_RESULT_CODE_ERROR;
}
/**
* A more detailed version of the Result/Response code.
*
* @return int|null
*/
public function getReasonCode()
{
$code = null;
if (isset($this->data->transactionResponse->messages)) {
// In case of a successful transaction, a "messages" element is present
$code = intval((string)$this->data->transactionResponse->messages->message->code);
} elseif (isset($this->data->transactionResponse->errors)) {
// In case of an unsuccessful transaction, an "errors" element is present
$code = intval((string)$this->data->transactionResponse->errors->error->errorCode);
} elseif (isset($this->data->messages->message)) {
// In case of invalid request, the top-level message provides details.
$code = (string)$this->data->messages->message->code;
}
return $code;
}
/**
* Text description of the status.
*
* @return string|null
*/
public function getMessage()
{
$message = null;
if (isset($this->data->transactionResponse->messages)) {
// In case of a successful transaction, a "messages" element is present
$message = (string)$this->data->transactionResponse->messages->message->description;
} elseif (isset($this->data->transactionResponse->errors)) {
// In case of an unsuccessful transaction, an "errors" element is present
$message = (string)$this->data->transactionResponse->errors->error->errorText;
} elseif (isset($this->data->messages->message)) {
// In case of invalid request, the top-level message provides details.
$message = (string)$this->data->messages->message->text;
}
return $message;
}
public function getAuthorizationCode()
{
if (isset($this->data->transactionResponse)) {
return (string)$this->data->transactionResponse->authCode;
} else {
return '';
}
}
/**
* Returns the Address Verification Service return code.
*
* @return string A single character. Can be A, B, E, G, N, P, R, S, U, X, Y, or Z.
*/
public function getAVSCode()
{
if (isset($this->data->transactionResponse)) {
return (string)$this->data->transactionResponse->avsResultCode;
} else {
return '';
}
}
/**
* Returns the Card Code Verfication return code.
*
* @return string A single character. Can be M, N, P, S, or U.
*/
public function getCVVCode()
{
if (isset($this->data->transactionResponse[0]->cvvResultCode)) {
return (string)$this->data->transactionResponse[0]->cvvResultCode;
} else {
return '';
}
}
/**
* A composite key containing the gateway provided transaction reference as
* well as other data points that may be required for subsequent transactions
* that may need to modify this one.
*
* @param bool $serialize Determines whether a string or object is returned
* @return TransactionReference|string
*/
public function getTransactionReference($serialize = true)
{
// The transactionResponse is only returned if succesful or declined
// for some reason, so don't assume it will always be there.
if (isset($this->data->transactionResponse)) {
$body = $this->data->transactionResponse;
$transactionRef = new TransactionReference();
$transactionRef->setApprovalCode((string)$body->authCode);
$transactionRef->setTransId((string)$body->transId);
try {
// Need to store card details in the transaction reference since it is required when doing a refund
if ($card = $this->request->getCard()) {
$transactionRef->setCard(array(
'number' => $card->getNumberLastFour(),
'expiry' => $card->getExpiryDate('mY')
));
} elseif ($cardReference = $this->request->getCardReference()) {
$transactionRef->setCardReference(new CardReference($cardReference));
}
} catch (\Exception $e) {
}
return $serialize ? (string)$transactionRef : $transactionRef;
}
return '';
}
/**
* Returns the account type used for the transaction.
*
* @return string A multicharacter string.
* Can be Visa, MasterCard, Discover, AmericanExpress, DinersClub, JCB, or eCheck.
*/
public function getAccountType()
{
if (isset($this->data->transactionResponse[0]->accountType)) {
return (string)$this->data->transactionResponse[0]->accountType;
} else {
return '';
}
}
}