-
Notifications
You must be signed in to change notification settings - Fork 2
/
Class.AuthorizeNet.php
568 lines (514 loc) · 19.1 KB
/
Class.AuthorizeNet.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
/**
* Submits payment transactions to Authorize.NET using the AIM API
* @author Richard Castera
* @link http://www.richardcastera.com/projects/authorizenet-api-wrapper-class
* @see http://developer.authorize.net/
* @license GNU LESSER GENERAL PUBLIC LICENSE
*/
class AuthorizeNet {
/**
* Environment test/live.
* @var String
*/
private $environment = 'test';
/**
* Contains the URLS for submitting a transaction.
* @var Array
*/
private $gatewayURL = array (
'live'=>'https://secure.authorize.net/gateway/transact.dll',
'test'=>'https://test.authorize.net/gateway/transact.dll',
);
/**
* Contains the keys for submitting a transaction type to Authorize.
* @var Array
*/
private $NVP = '';
/**
* Contains an array of values returned from processing the transaction.
* @var Array
*/
private $response = '';
/**
* The delimeter to seperate the values returned from the transaction.
* @var String
*/
private $responseDelimeter = '|';
/**
* Constructor - The API Login ID and Transaction Key together provide the merchant authentication required for access to the payment gateway.
* @param String $apiLoginID - The merchant's unique API Login ID.
* @param String $apiTransKey - The merchant's unique Transaction Key.
*/
public function __construct($apiLoginID = '', $apiTransKey = '') {
$this->NVP = array (
'x_login'=>$this->truncateChars($apiLoginID, 20),
'x_tran_key'=>$this->truncateChars($apiTransKey, 16),
);
$this->setupDefaults();
}
/**
* Destructor.
*/
public function __destruct() {
unset($this);
}
/**
* Sets up default transaction information.
*/
private function setupDefaults() {
$defaults = array(
// Indicates to the system the set of fields that will be included in the response: 3.0 is the default version. 3.1
// allows the merchant to utilize the Card Code feature, and is the current standard version.
'x_version'=>'3.1',
// In order to receive a delimited response from the payment gateway, this field must be submitted with a value
// of TRUE or the merchant has to configure a delimited response through the Merchant Interface.
'x_delim_data'=>'TRUE',
// The character that is used to separate fields in the transaction response. The payment gateway will use the
// character passed in this field or the value stored in the Merchant Interface if no value is passed. If this field is passed,
// and the value is null, it will override the value stored in the Merchant Interface and there is no delimiting character in the transaction response.
// A single symbol Ex. , (comma) | (pipe) " (double quotes) ' (single quote) : (colon) ; (semicolon) / (forward slash) \ (back slash) - (dash) * (star)
'x_delim_char'=>$this->responseDelimeter,
// SIM applications use relay response. Set this to false if you are using AIM.
'x_relay_response'=>'FALSE',
// IP address of the customer initiating the transaction. If this value is not passed, it will default to 255.255.255.255.
'x_customer_ip'=>$_SERVER['REMOTE_ADDR'],
);
$this->NVP = array_merge($this->NVP, $defaults);
}
/**
* Sets the Environment of the transaction.
* @param String $environment - Possible values: ('test', 'live').
*/
public function setEnvironment($environment = 'test') {
if(strtolower($environment) == 'test') {
$this->environment = $this->gatewayURL['test'];
}
else {
$this->environment = $this->gatewayURL['live'];
}
}
/**
* Returns the Environment of the transaction.
* @return String - The environment set.
*/
public function getEnvironment() {
return $this->environment;
}
/**
* Sets the window of time after the submission of a transaction that a duplicate transaction can not be submitted.
* @param Integer $seconds - Any value between 0 and 28800 (no comma).
*/
public function submissionWindow($seconds = 28800) {
$window = array(
'x_duplicate_window'=>(int)$seconds,
);
$this->NVP = array_merge($this->NVP, $window);
}
/**
* Sets the type of Credit Card transaction.
* @param String $transactionType - AUTH_CAPTURE (default), AUTH_ONLY, CAPTURE_ONLY, CREDIT, PRIOR_AUTH_CAPTURE, VOID.
*/
public function setTransactionType($transactionType = 'AUTH_CAPTURE') {
$type = array(
'x_type'=>strtoupper($transactionType),
);
$this->NVP = array_merge($this->NVP, $type);
}
/**
* Sets the Payment Method.
* @param String $paymentMethod - CC or ECHECK.
*/
public function setPaymentMethod($paymentMethod = 'CC') {
$method = array(
'x_method'=>strtoupper($paymentMethod),
);
$this->NVP = array_merge($this->NVP, $method);
}
/**
* Sets the Amount of the transaction. Up to 15 digits with a decimal point (no dollar symbol)
* @param String/Integer/Float - $amount - 150.00.
* @param Boolean - $wholeAmt - True to remove cents false, to keep it.
*/
public function setAmount($amount = 0, $wholeAmt) {
$amt = array(
'x_amount'=>$this->cleanAmt($amount, $wholeAmt),
);
$this->NVP = array_merge($this->NVP, $amt);
}
/**
* Sets the Customer's Credit Card Number. Between 13 and 16 digits without spaces.
* @param String $number - The Credit Card Number. Dashes will be striped.
*/
public function setCCNumber($number = '') {
$cc = array(
'x_card_num'=>$this->cleanCCNumber($number),
);
$this->NVP = array_merge($this->NVP, $cc);
}
/**
* Sets the Customer's Credit Card Expiration Date. MMYY, MM/YY, MM-YY, MMYYYY, MM/YYYY, MM-YYYY
* @param String $expiration - The Customer's Credit Card Expiration Date
*/
public function setExpiration($expiration = '0000') {
$exp = array(
'x_exp_date'=>$this->cleanExpDate($expiration),
);
$this->NVP = array_merge($this->NVP, $exp);
}
/**
* Sets the Customer's card code. The three- or four-digit number on the back of a credit card (on the front for American Express).
* @param String $cvv - The Customer's Credit Card Security Code
*/
public function setCVV($cvv = '') {
$security = array(
'x_card_code'=>$cvv,
);
$this->NVP = array_merge($this->NVP, $security);
}
/**
* Sets the Payment Description. This is just a commment regarding the transaction for your reference.
* @param String $description - The Payment description.
*/
public function setPaymentDescription($description = '') {
$desc = array(
'x_description'=>$this->truncateChars($description, 255),
);
$this->NVP = array_merge($this->NVP, $desc);
}
/**
* Sets the First Name associated with the Customer's Billing Address. Up to 50 characters (no symbols)
* @param String $firstName - The First Name associated with the Customer's Billing Address.
*/
public function setCustomerFirstName($firstName = '') {
$first = array(
'x_first_name'=>$this->truncateChars($firstName, 50),
);
$this->NVP = array_merge($this->NVP, $first);
}
/**
* Sets the Last Name associated with the Customer's Billing Address. Up to 50 characters (no symbols)
* @param String $lastName - The Last Name associated with the Customer's Billing Address.
* @example $Auth->setCustomerLastName('Castera');
*/
public function setCustomerLastName($lastName = '') {
$last = array(
'x_last_name'=>$this->truncateChars($lastName, 50),
);
$this->NVP = array_merge($this->NVP, $last);
}
/**
* Sets the Company Name associated with the Customer's Billing. Up to 50 characters (no symbols)
* @param String $companyName - The company name associated with the Customer's Billing Address.
*/
public function setCustomerCompany($companyName = '') {
$company = array(
'x_company'=>$this->truncateChars($companyName, 50),
);
$this->NVP = array_merge($this->NVP, $company);
}
/**
* Sets the Customer's Billing address. Up to 60 characters (no symbols)
* @param String $customerAddress - The Customer's Billing address.
*/
public function setCustomerAddress($customerAddress = '') {
$address = array(
'x_address'=>$this->truncateChars($customerAddress, 60),
);
$this->NVP = array_merge($this->NVP, $address);
}
/**
* Sets the Customer's Billing City. Up to 40 characters (no symbols)
* @param String $customerCity - The Customer's Billing City.
*/
public function setCustomerCity($customerCity = '') {
$city = array(
'x_city'=>$this->truncateChars($customerCity, 40),
);
$this->NVP = array_merge($this->NVP, $city);
}
/**
* Sets the Customer's Billing State. Up to 40 characters (no symbols) or a valid two-character state code.
* @param String $customerState - The Customer's Billing State.
*/
public function setCustomerState($customerState = '') {
$state = array(
'x_state'=>$this->truncateChars($customerState, 40),
);
$this->NVP = array_merge($this->NVP, $state);
}
/**
* Sets the Customer's Billing Zip. Up to 20 characters (no symbols).
* @param String $customerZip - The Customer's Billing Zip.
*/
public function setCustomerZip($customerZip = '') {
$zip = array(
'x_zip'=>$this->truncateChars($customerZip, 20),
);
$this->NVP = array_merge($this->NVP, $zip);
}
/**
* Sets the Customer's Billing Country. Up to 60 characters (no symbols).
* @param String $customerCountry - The Customer's Billing Country.
* @example $Auth->setCustomerCountry('United States');
*/
public function setCustomerCountry($customerCountry = '') {
$country = array(
'x_country'=>$this->truncateChars($customerCountry, 60),
);
$this->NVP = array_merge($this->NVP, $country);
}
/**
* Sets the Customer's Billing Phone. Up to 25 digits (no letters) Ex. 123-123-1234.
* @param String $customerPhone - The Customer's Billing Phone.
*/
public function setCustomerPhone($customerPhone = '000-000-0000') {
$phone = array(
'x_phone'=>$this->truncateChars($this->cleanPhoneNumber($customerPhone), 25),
);
$this->NVP = array_merge($this->NVP, $phone);
}
/**
* Sets the Customer's Billing Fax. Up to 25 digits (no letters) Ex. 123-123-1234.
* @param String $customerFax - The Customer's Billing Fax.
*/
public function setCustomerFax($customerFax = '000-000-0000') {
$fax = array(
'x_fax'=>$this->truncateChars($this->cleanPhoneNumber($customerFax), 25),
);
$this->NVP = array_merge($this->NVP, $fax);
}
/**
* Sets the Customer's Email Address. Up to 255 characters.
* @param String $customerEmail - The Customer's Email Address.
*/
public function setCustomerEmail($customerEmail = '') {
$email = array(
'x_email'=>$this->truncateChars($customerEmail, 255),
);
$this->NVP = array_merge($this->NVP, $email);
}
/**
* Sets the First Name associated with the Customer's Shipping Address. Up to 50 characters (no symbols)
* @param String $firstName - The First Name associated with the Customer's Shipping Address.
*/
public function setShippingFirstName($firstName = '') {
$first = array(
'x_ship_to_first_name'=>$this->truncateChars($firstName, 50),
);
$this->NVP = array_merge($this->NVP, $first);
}
/**
* Sets the Last Name associated with the Customer's Shipping Address. Up to 50 characters (no symbols)
* @param String $lastName - The Last Name associated with the Customer's Shipping Address.
*/
public function setShippingLastName($lastName = '') {
$last = array(
'x_ship_to_last_name'=>$this->truncateChars($lastName, 50),
);
$this->NVP = array_merge($this->NVP, $last);
}
/**
* Sets the Company Name associated with the Customer's Shipping. Up to 50 characters (no symbols)
* @param String $companyName - The Company name associated with the Customer's Shipping Address.
*/
public function setShippingCompany($companyName = '') {
$company = array(
'x_ship_to_company'=>$this->truncateChars($companyName, 50),
);
$this->NVP = array_merge($this->NVP, $company);
}
/**
* Sets the Customer's Shipping address. Up to 60 characters (no symbols)
* @param String $shippingAddress - The Customer's Shipping address.
*/
public function setShippingAddress($shippingAddress = '') {
$address = array(
'x_ship_to_address'=>$this->truncateChars($shippingAddress, 60),
);
$this->NVP = array_merge($this->NVP, $address);
}
/**
* Sets the Customer's Shipping City. Up to 40 characters (no symbols)
* @param String $shippingCity - The Customer's Shipping City.
*/
public function setShippingCity($shippingCity = '') {
$city = array(
'x_ship_to_city'=>$this->truncateChars($shippingCity, 40),
);
$this->NVP = array_merge($this->NVP, $city);
}
/**
* Sets the Customer's Shipping State. Up to 40 characters (no symbols) or a valid two-character state code.
* @param String $shippingState - The Customer's Shipping State.
*/
public function setShippingState($shippingState = '') {
$state = array(
'x_ship_to_state'=>$this->truncateChars($shippingState, 40),
);
$this->NVP = array_merge($this->NVP, $state);
}
/**
* Sets the Customer's Shipping Zip. Up to 20 characters (no symbols).
* @param String $shippingZip - The Customer's Shipping Zip.
*/
public function setShippingZip($shippingZip = '') {
$zip = array(
'x_ship_to_zip'=>$this->truncateChars($shippingZip, 20),
);
$this->NVP = array_merge($this->NVP, $zip);
}
/**
* Sets the Customer's Shipping Country. Up to 60 characters (no symbols).
* @param String $shippingCountry - The Customer's Shipping Country.
*/
public function setShippingCountry($shippingCountry = '') {
$country = array(
'x_ship_to_country'=>$this->truncateChars($shippingCountry, 60),
);
$this->NVP = array_merge($this->NVP, $country);
}
/**
* If set to TRUE, an email will be sent to the customer after the transaction is processed. If FALSE, no email is sent to the customer.
* @param String $sendReceipt - Indicate whether an email receipt should be sent to the customer.
*/
public function sendCustomerReceipt($sendReceipt = TRUE) {
$receipt = array(
'x_email_customer'=>(int)$sendReceipt,
);
$this->NVP = array_merge($this->NVP, $receipt);
}
/**
* Sets a Merchant-defined field to submit to Authorize.
* @param String $name - The name of the custom field.
* @param String $value - The value of the custom field.
*/
public function setCustomField($name = '', $value = '') {
$custom = array(
$name=>(string)$value,
);
$this->NVP = array_merge($this->NVP, $custom);
}
/**
* This get the NVP's that will be sent to Authorize.
* @return String - A string of NVP's.
*/
private function getNVP() {
$post = '';
foreach($this->NVP as $key=>$value) {
$post .= "$key=" . urlencode($value) . "&";
}
return (string)rtrim($post, "& ");
}
/**
* Sends the request to Authorize for processing.
* @return Boolean - True if the transaction was successful False, if not.
*/
public function processTransaction() {
// Uses the CURL library for php to establish a connection,
// submit the post, and record the response.
if(function_exists('curl_init') && extension_loaded('curl')) {
$request = curl_init($this->getEnvironment()); // Initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // Set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $this->getNVP()); // Use HTTP POST to send the data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // Uncomment this line if you get no gateway response.
$postResponse = curl_exec($request); // Execute curl post and store results in $post_response
// Additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close($request); // close curl object
// Get the response.
$this->response = $postResponse;
return TRUE;
}
else {
return FALSE;
}
}
/**
* Gets the response from Authorize.
* @return Array/String - Returns an array of Authorize's response or empty string if not return.
*/
public function getResponse() {
if($this->response) {
$response = explode($this->responseDelimeter, $this->response);
if(is_array($response)) {
return $response;
}
else {
return '';
}
}
else {
return '';
}
}
/**
* Formats the monetary amount sent to Authorize.
* @param String/Integer/Float $amount - The amount to clean.
* @param Boolean $wholeAmt - True to remove cents false, to keep it.
* @return Integer/Float - Returns the monetary amount formatted based on the $wholeAmt parameter.
*/
private function cleanAmt($amount = 0, $wholeAmt = FALSE) {
if($wholeAmt) {
$amount = preg_replace('/[^0-9.]/', '', trim($amount));
return (int)$amount;
}
else {
$amount = preg_replace('/[^0-9.]/', '', trim($amount));
return (float)$amount;
}
}
/**
* Removes all characters from the credit card number except for numbers.
* @param String $cc - The crdeit card number.
* @return String - Returns the credit card number with only numeric characters.
*/
private function cleanCCNumber($cc = '') {
$cc = preg_replace('/[^0-9]/', '', trim($cc));
return (string)$cc;
}
/**
* Removes all characters from the telephone number except for numbers and dashes.
* @param String $phone - The phone number.
* @return String - Returns the phone number with dashes.
*/
private function cleanPhoneNumber($phone = '') {
$phone = preg_replace('/[^0-9-]/', '', trim($phone));
return (string)$phone;
}
/**
* Removes all characters from the Expiration date except for numbers, slashes and dashes.
* @param String $exp - The expiration date.
* @return String - Returns the expiration date formatted for authorize.
*/
private function cleanExpDate($exp = '') {
$exp = preg_replace('/[^0-9]-\//', '', trim($exp));
return (string)$exp;
}
/**
* Used to debug values that will be sent to Authorize.
* @param String $string - The string to truncate.
* @param Integer $limit - The amount to truncate.
* @return String - Returns the string truncated.
*/
private function truncateChars($string = '', $limit = 0) {
for($i = 0; $i <= $limit AND $i < strlen($string); $i++){
$output .= $string[$i];
}
return (string)trim($output);
}
/**
* Used to debug values that will be sent to Authorize.
* @param String $type - Valid values are 'array' or 'string'.
* @return Mixed - This returns either and array of the NVP's or a string based on the parameter chosen.
*/
public function debugNVP($type = 'array') {
if($type == 'array') {
return $this->NVP;
}
else {
return $this->getNVP();
}
}
}