-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorize.php
275 lines (257 loc) · 12.2 KB
/
authorize.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
require_once 'vendor/autoload.php';
require_once 'lib.php';
require_once "conf.php";
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
// define("AUTHORIZENET_LOG_FILE", "phplog");
function authorizeCreditCard( $amount,
$creDitCardNumber,
$expiratonDate,
$cardCode,
$firstName,
$lastName,
$address,
$city,
$state,
$zip,
$country,
$email
)
{
/* Create a merchantAuthenticationType object with authentication details
retrieved from the constants file */
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(Config::$MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(Config::$MERCHANT_TRANSACTION_KEY);
// Set the transaction's refId
$refId = 'ref' . time();
// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber($creDitCardNumber);
$creditCard->setExpirationDate($expiratonDate);
$creditCard->setCardCode($cardCode);
// Add the payment data to a paymentType object
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);
// Create order information
$order = new AnetAPI\OrderType();
$order->setInvoiceNumber(getInvoiceNumber());
$order->setDescription("Paycheck Stub");
// Set the customer's Bill To address
$customerAddress = new AnetAPI\CustomerAddressType();
$customerAddress->setFirstName($firstName);
$customerAddress->setLastName($lastName);
//$customerAddress->setCompany("Souveniropolis");
$customerAddress->setAddress($address);
$customerAddress->setCity($city);
$customerAddress->setState($state);
$customerAddress->setZip($zip);
$customerAddress->setCountry($country);
// Set the customer's identifying information
$customerData = new AnetAPI\CustomerDataType();
$customerData->setType("individual");
//$customerData->setId("99999456654");
$customerData->setEmail($email);
// Add values for transaction settings
$duplicateWindowSetting = new AnetAPI\SettingType();
///$duplicateWindowSetting->setSettingName("duplicateWindow");
///$duplicateWindowSetting->setSettingValue("60");
// Add some merchant defined fields. These fields won't be stored with the transaction,
// but will be echoed back in the response.
/*
$merchantDefinedField1 = new AnetAPI\UserFieldType();
$merchantDefinedField1->setName("customerLoyaltyNum");
$merchantDefinedField1->setValue("1128836273");
$merchantDefinedField2 = new AnetAPI\UserFieldType();
$merchantDefinedField2->setName("favoriteColor");
$merchantDefinedField2->setValue("blue");
*/
// Create a TransactionRequestType object and add the previous objects to it
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authOnlyTransaction");
$transactionRequestType->setAmount($amount);
$transactionRequestType->setOrder($order);
$transactionRequestType->setPayment($paymentOne);
$transactionRequestType->setBillTo($customerAddress);
$transactionRequestType->setCustomer($customerData);
//$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);
//$transactionRequestType->addToUserFields($merchantDefinedField1);
//$transactionRequestType->addToUserFields($merchantDefinedField2);
// Assemble the complete transaction request
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);
// Create the controller and get the response
$controller = new AnetController\CreateTransactionController($request);
if(Config::$IS_PRODUCTION) {
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
} else {
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
}
//die("++" . print_r($response, true) . "++");
if ($response != null) {
// Check to see if the API request was successfully received and acted upon
if ($response->getMessages()->getResultCode() == "Ok") {
// Since the API request was successful, look for a transaction response
// and parse it to display the results of authorizing the card
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
///echo " Successfully created transaction with Transaction ID: " . $tresponse->getTransId() . "\n";
///echo " Transaction Response Code: " . $tresponse->getResponseCode() . "\n";
///echo " Message Code: " . $tresponse->getMessages()[0]->getCode() . "\n";
///echo " Auth Code: " . $tresponse->getAuthCode() . "\n";
///echo " Description: " . $tresponse->getMessages()[0]->getDescription() . "\n";
} else {
///echo "Transaction Failed \n";
if ($tresponse->getErrors() != null) {
///echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
///echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
}
// Or, print errors if the API request wasn't successful
} else {
///echo "Transaction Failed \n";
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getErrors() != null) {
///echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
///echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
} else {
///echo " Error Code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
///echo " Error Message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
}
} else {
///echo "No response returned \n";
return null;
}
//die("++" . print_r($response, true) . "++");
return $response;
}
function capturePreviouslyAuthorizedAmount($transactionid)
{
/* Create a merchantAuthenticationType object with authentication details
retrieved from the constants file */
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(Config::$MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(Config::$MERCHANT_TRANSACTION_KEY);
// Set the transaction's refId
$refId = 'ref' . time();
// Now capture the previously authorized amount
//echo "Capturing the Authorization with transaction ID : " . $transactionid . "\n";
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("priorAuthCaptureTransaction");
$transactionRequestType->setRefTransId($transactionid);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setTransactionRequest( $transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
if(Config::$IS_PRODUCTION) {
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
} else {
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
}
if ($response != null)
{
if($response->getMessages()->getResultCode() == "Ok")
{
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null)
{
//echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n";
//echo "Successful." . "\n";
//echo "Capture Previously Authorized Amount, Trans ID : " . $tresponse->getRefTransId() . "\n";
//echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n";
//echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n";
}
else
{
//echo "Transaction Failed \n";
if($tresponse->getErrors() != null)
{
//echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
//echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
}
}
else
{
//echo "Transaction Failed \n";
$tresponse = $response->getTransactionResponse();
if($tresponse != null && $tresponse->getErrors() != null)
{
//echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
//echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
else
{
//echo " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
//echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
}
}
else
{
//echo "No response returned \n";
}
return $response;
}
function voidTransaction($transactionid)
{
/* Create a merchantAuthenticationType object with authentication details
retrieved from the constants file */
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(Config::$MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(Config::$MERCHANT_TRANSACTION_KEY);
// Set the transaction's refId
$refId = 'ref' . time();
//create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType( "voidTransaction");
$transactionRequestType->setRefTransId($transactionid);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest( $transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
if(Config::$IS_PRODUCTION) {
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
} else {
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
}
if ($response != null) {
if($response->getMessages()->getResultCode() == "Ok") {
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
/*
echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n";
echo " Void transaction SUCCESS AUTH CODE: " . $tresponse->getAuthCode() . "\n";
echo " Void transaction SUCCESS TRANS ID : " . $tresponse->getTransId() . "\n";
echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n";
echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n";
*/
}
else {
//echo "Transaction Failed \n";
if($tresponse->getErrors() != null) {
// echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
// echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
}
} else {
// echo "Transaction Failed \n";
$tresponse = $response->getTransactionResponse();
if($tresponse != null && $tresponse->getErrors() != null) {
// echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
// echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
} else {
// echo " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
// echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
}
} else {
// echo "No response returned \n";
}
return $response;
}