@@ -18,6 +18,14 @@ class AIMResponse extends AbstractResponse
18
18
*/
19
19
const ERROR_RESPONSE_CODE_CANNOT_ISSUE_CREDIT = 54 ;
20
20
21
+ /**
22
+ * The overall transaction result code.
23
+ */
24
+ const TRANSACTION_RESULT_CODE_APPROVED = 1 ;
25
+ const TRANSACTION_RESULT_CODE_DECLINED = 2 ;
26
+ const TRANSACTION_RESULT_CODE_ERROR = 3 ;
27
+ const TRANSACTION_RESULT_CODE_REVIEW = 4 ;
28
+
21
29
public function __construct (AbstractRequest $ request , $ data )
22
30
{
23
31
// Strip out the xmlns junk so that PHP can parse the XML
@@ -38,17 +46,25 @@ public function __construct(AbstractRequest $request, $data)
38
46
39
47
public function isSuccessful ()
40
48
{
41
- return 1 === $ this ->getResultCode ();
49
+ return static :: TRANSACTION_RESULT_CODE_APPROVED === $ this ->getResultCode ();
42
50
}
43
51
44
52
/**
45
- * Overall status of the transaction. This field is also known as "Response Code" in Authorize.NET terminology.
53
+ * Status of the transaction. This field is also known as "Response Code" in Authorize.NET terminology.
54
+ * A result of 0 is returned if there is no transaction response returned, e.g. a validation error in
55
+ * some data, or invalid login credentials.
46
56
*
47
57
* @return int 1 = Approved, 2 = Declined, 3 = Error, 4 = Held for Review
48
58
*/
49
59
public function getResultCode ()
50
60
{
51
- return intval ((string )$ this ->data ->transactionResponse [0 ]->responseCode );
61
+ // If there is a transaction response, then we get the code from that.
62
+ if (isset ($ this ->data ->transactionResponse [0 ])) {
63
+ return intval ((string )$ this ->data ->transactionResponse [0 ]->responseCode );
64
+ }
65
+
66
+ // No transaction response, so return 3 aka "error".
67
+ return static ::TRANSACTION_RESULT_CODE_ERROR ;
52
68
}
53
69
54
70
/**
@@ -62,11 +78,15 @@ public function getReasonCode()
62
78
63
79
if (isset ($ this ->data ->transactionResponse [0 ]->messages )) {
64
80
// In case of a successful transaction, a "messages" element is present
65
- $ code = intval ((string )$ this ->data ->transactionResponse [0 ]->messages [0 ]->message [ 0 ] ->code );
81
+ $ code = intval ((string )$ this ->data ->transactionResponse [0 ]->messages [0 ]->message ->code );
66
82
67
83
} elseif (isset ($ this ->data ->transactionResponse [0 ]->errors )) {
68
84
// In case of an unsuccessful transaction, an "errors" element is present
69
- $ code = intval ((string )$ this ->data ->transactionResponse [0 ]->errors [0 ]->error [0 ]->errorCode );
85
+ $ code = intval ((string )$ this ->data ->transactionResponse [0 ]->errors [0 ]->error ->errorCode );
86
+
87
+ } elseif (isset ($ this ->data ->messages [0 ]->message )) {
88
+ // In case of invalid request, the top-level message provides details.
89
+ $ code = (string )$ this ->data ->messages [0 ]->message ->code ;
70
90
}
71
91
72
92
return $ code ;
@@ -83,19 +103,27 @@ public function getMessage()
83
103
84
104
if (isset ($ this ->data ->transactionResponse [0 ]->messages )) {
85
105
// In case of a successful transaction, a "messages" element is present
86
- $ message = (string )$ this ->data ->transactionResponse [0 ]->messages [0 ]->message [ 0 ] ->description ;
106
+ $ message = (string )$ this ->data ->transactionResponse [0 ]->messages [0 ]->message ->description ;
87
107
88
108
} elseif (isset ($ this ->data ->transactionResponse [0 ]->errors )) {
89
109
// In case of an unsuccessful transaction, an "errors" element is present
90
- $ message = (string )$ this ->data ->transactionResponse [0 ]->errors [0 ]->error [0 ]->errorText ;
110
+ $ message = (string )$ this ->data ->transactionResponse [0 ]->errors [0 ]->error ->errorText ;
111
+
112
+ } elseif (isset ($ this ->data ->messages [0 ]->message )) {
113
+ // In case of invalid request, the top-level message provides details.
114
+ $ message = (string )$ this ->data ->messages [0 ]->message ->text ;
91
115
}
92
116
93
117
return $ message ;
94
118
}
95
119
96
120
public function getAuthorizationCode ()
97
121
{
98
- return (string )$ this ->data ->transactionResponse [0 ]->authCode ;
122
+ if (isset ($ this ->data ->transactionResponse [0 ])) {
123
+ return (string )$ this ->data ->transactionResponse [0 ]->authCode ;
124
+ } else {
125
+ return '' ;
126
+ }
99
127
}
100
128
101
129
/**
@@ -105,36 +133,48 @@ public function getAuthorizationCode()
105
133
*/
106
134
public function getAVSCode ()
107
135
{
108
- return (string )$ this ->data ->transactionResponse [0 ]->avsResultCode ;
136
+ if (isset ($ this ->data ->transactionResponse [0 ])) {
137
+ return (string )$ this ->data ->transactionResponse [0 ]->avsResultCode ;
138
+ } else {
139
+ return '' ;
140
+ }
109
141
}
110
142
111
143
/**
112
- * A composite key containing the gateway provided transaction reference as well as other data points that may be
113
- * required for subsequent transactions that may need to modify this one.
144
+ * A composite key containing the gateway provided transaction reference as
145
+ * well as other data points that may be required for subsequent transactions
146
+ * that may need to modify this one.
114
147
*
115
148
* @param bool $serialize Determines whether a string or object is returned
116
149
* @return TransactionReference|string
117
150
*/
118
151
public function getTransactionReference ($ serialize = true )
119
152
{
120
- $ body = $ this ->data ->transactionResponse [0 ];
121
- $ transactionRef = new TransactionReference ();
122
- $ transactionRef ->setApprovalCode ((string )$ body ->authCode );
123
- $ transactionRef ->setTransId ((string )$ body ->transId );
124
-
125
- try {
126
- // Need to store card details in the transaction reference since it is required when doing a refund
127
- if ($ card = $ this ->request ->getCard ()) {
128
- $ transactionRef ->setCard (array (
129
- 'number ' => $ card ->getNumberLastFour (),
130
- 'expiry ' => $ card ->getExpiryDate ('mY ' )
131
- ));
132
- } elseif ($ cardReference = $ this ->request ->getCardReference ()) {
133
- $ transactionRef ->setCardReference (new CardReference ($ cardReference ));
153
+ // The transactionResponse is only returned if succesful or declined
154
+ // for some reason, so don't assume it will always be there.
155
+
156
+ if (isset ($ this ->data ->transactionResponse [0 ])) {
157
+ $ body = $ this ->data ->transactionResponse [0 ];
158
+ $ transactionRef = new TransactionReference ();
159
+ $ transactionRef ->setApprovalCode ((string )$ body ->authCode );
160
+ $ transactionRef ->setTransId ((string )$ body ->transId );
161
+
162
+ try {
163
+ // Need to store card details in the transaction reference since it is required when doing a refund
164
+ if ($ card = $ this ->request ->getCard ()) {
165
+ $ transactionRef ->setCard (array (
166
+ 'number ' => $ card ->getNumberLastFour (),
167
+ 'expiry ' => $ card ->getExpiryDate ('mY ' )
168
+ ));
169
+ } elseif ($ cardReference = $ this ->request ->getCardReference ()) {
170
+ $ transactionRef ->setCardReference (new CardReference ($ cardReference ));
171
+ }
172
+ } catch (\Exception $ e ) {
134
173
}
135
- } catch (\Exception $ e ) {
174
+
175
+ return $ serialize ? (string )$ transactionRef : $ transactionRef ;
136
176
}
137
- return ( string ) $ body -> transId ;
138
- return $ serialize ? ( string ) $ transactionRef : $ transactionRef ;
177
+
178
+ return '' ;
139
179
}
140
180
}
0 commit comments