-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathusaepay.php
1379 lines (1212 loc) · 49 KB
/
usaepay.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// USAePay PHP Library.
// v1.7.1
//
// Copyright (c) 2001-2015 USAePay
// For assistance please contact [email protected]
//
define("USAEPAY_VERSION", "1.7.1");
/**
* USAePay Transaction Class
*
*/
class umTransaction {
// Required for all transactions
var $key; // Source key
var $pin; // Source pin (optional)
var $amount; // the entire amount that will be charged to the customers card
// (including tax, shipping, etc)
var $invoice; // invoice number. must be unique. limited to 10 digits. use orderid if you need longer.
// Required for Level 2 - Commercial Card support
var $ponum; // Purchase Order Number
var $tax; // Tax
var $nontaxable; // Order is non taxable
var $digitalgoods; //digital goods indicator (ecommerce)
// Level 3 support
var $duty;
var $shipfromzip;
// Amount details (optional)
var $tip; // Tip
var $shipping; // Shipping charge
var $discount; // Discount amount (ie gift certificate or coupon code)
var $subtotal; // if subtotal is set, then
// subtotal + tip + shipping - discount + tax must equal amount
// or the transaction will be declined. If subtotal is left blank
// then it will be ignored
var $currency; // Currency of $amount
var $allowpartialauth; //set to true if a partial authorization (less than the full $amount) will be accepted
// Required Fields for Card Not Present transacitons (Ecommerce)
var $card; // card number, no dashes, no spaces
var $exp; // expiration date 4 digits no /
var $cardholder; // name of card holder
var $street; // street address
var $zip; // zip code
// Fields for Card Present (POS)
var $magstripe; // mag stripe data. can be either Track 1, Track2 or Both (Required if card,exp,cardholder,street and zip aren't filled in)
var $cardpresent; // Must be set to true if processing a card present transaction (Default is false)
var $termtype; // The type of terminal being used: Optons are POS - cash register, StandAlone - self service terminal, Unattended - ie gas pump, Unkown (Default: Unknown)
var $magsupport; // Support for mag stripe reader: yes, no, contactless, unknown (default is unknown unless magstripe has been sent)
var $contactless; // Magstripe was read with contactless reader: yes, no (default is no)
var $dukpt; // DUK/PT for PIN Debit
var $signature; // Signature Capture data
var $reasoncode; // reversal/adjustment reason code
// fields required for check transactions
var $account; // bank account number
var $routing; // bank routing number
var $ssn; // social security number
var $dlnum; // drivers license number (required if not using ssn)
var $dlstate; // drivers license issuing state
var $checknum; // Check Number
var $accounttype; // Checking or Savings
var $checkformat; // Override default check record format
var $checkimage_front; // Check front
var $checkimage_back; // Check back
var $auxonus;
var $epccode;
var $micr;
// Fields required for Secure Vault Payments (Direct Pay)
var $svpbank; // ID of cardholders bank
var $svpreturnurl; // URL that the bank should return the user to when tran is completed
var $svpcancelurl; // URL that the bank should return the user if they cancel
// Option parameters
var $origauthcode; // required if running postauth transaction.
var $command; // type of command to run; Possible values are:
// sale, credit, void, preauth, postauth, check and checkcredit.
// Default is sale.
var $orderid; // Unique order identifier. This field can be used to reference
// the order for which this transaction corresponds to. This field
// can contain up to 64 characters and should be used instead of
// UMinvoice when orderids longer that 10 digits are needed.
var $custid; // Alpha-numeric id that uniquely identifies the customer.
var $description; // description of charge
var $cvv2; // cvv2 code
var $custemail; // customers email address
var $custreceipt; // send customer a receipt
var $custreceiptname; // name of custom receipt template
var $ignoreduplicate; // prevent the system from detecting and folding duplicates
var $ip; // ip address of remote host
var $geolocation; // geo location information for transaciton. Format lat,long
var $testmode; // test transaction but don't process it
var $usesandbox; // use sandbox server instead of production
var $timeout; // transaction timeout. defaults to 45 seconds
var $gatewayurl; // url for the gateway
var $proxyurl; // proxy server to use (if required by network)
var $ignoresslcerterrors; // Bypasses ssl certificate errors. It is highly recommended that you do not use this option. Fix your openssl installation instead!
var $cabundle; // manually specify location of root ca bundle (useful of root ca is not in default location)
var $transport; // manually select transport to use (curl or stream), by default the library will auto select based on what is available
var $clerk; // Indicates the clerk/person processing transaction, for reporting purposes. (optional)
var $terminal; // Indiactes the terminal used to process transaction, for reporting purposes. (optional)
var $restaurant_table; // Indicates the restaurant table, for reporting purposes. (optional)
var $ticketedevent; // ID of Event when doing a ticket sale
var $ifauthexpired; // controls what happens when capturing an authorization that has expire. options are 'ignore','error','reauth'.
var $authexpiredays; //set the number of days an authorization is valid for. defaults to merchant account setting.
var $inventorylocation; // set the warehouse to pull inventory from. defaults to source key setting.
// Card Authorization - Verified By Visa and Mastercard SecureCode
var $cardauth; // enable card authentication
var $pares; //
// Third Party Card Authorization
var $xid;
var $cavv;
var $eci;
// Customer Database
var $addcustomer; // Save transaction as a recurring transaction: yes/no
var $recurring; // (obsolete, see the addcustomer)
var $schedule; // How often to run transaction: daily, weekly, biweekly, monthly, bimonthly, quarterly, annually. Default is monthly, set to disabled if you don't want recurring billing
var $numleft; // The number of times to run. Either a number or * for unlimited. Default is unlimited.
var $start; // When to start the schedule. Default is tomorrow. Must be in YYYYMMDD format.
var $end; // When to stop running transactions. Default is to run forever. If both end and numleft are specified, transaction will stop when the earliest condition is met.
var $billamount; // Optional recurring billing amount. If not specified, the amount field will be used for future recurring billing payments
var $billtax;
var $billsourcekey;
// tokenization
var $savecard; // create a card token if the sale is approved, returns a cardref variable
// Manually mark as recurring
var $isrecurring;
// Billing Fields
var $billfname;
var $billlname;
var $billcompany;
var $billstreet;
var $billstreet2;
var $billcity;
var $billstate;
var $billzip;
var $billcountry;
var $billphone;
var $email;
var $fax;
var $website;
// Shipping Fields
var $delivery; // type of delivery method ('ship','pickup','download')
var $shipfname;
var $shiplname;
var $shipcompany;
var $shipstreet;
var $shipstreet2;
var $shipcity;
var $shipstate;
var $shipzip;
var $shipcountry;
var $shipphone;
// Custom Fields
var $custom1;
var $custom2;
var $custom3;
var $custom4;
var $custom5;
var $custom6;
var $custom7;
var $custom8;
var $custom9;
var $custom10;
var $custom11;
var $custom12;
var $custom13;
var $custom14;
var $custom15;
var $custom16;
var $custom17;
var $custom18;
var $custom19;
var $custom20;
// Line items (see addLine)
var $lineitems;
var $comments; // Additional transaction details or comments (free form text field supports up to 65,000 chars)
var $software; // Allows developers to identify their application to the gateway (for troubleshooting purposes)
// Fraud Profiling
var $session;
// response fields
var $rawresult; // raw result from gateway
var $result; // full result: Approved, Declined, Error
var $resultcode; // abreviated result code: A D E
var $authcode; // authorization code
var $refnum; // reference number
var $batch; // batch number
var $avs_result; // avs result
var $avs_result_code; // avs result
var $avs; // obsolete avs result
var $cvv2_result; // cvv2 result
var $cvv2_result_code; // cvv2 result
var $vpas_result_code; // vpas result
var $isduplicate; // system identified transaction as a duplicate
var $convertedamount; // transaction amount after server has converted it to merchants currency
var $convertedamountcurrency; // merchants currency
var $conversionrate; // the conversion rate that was used
var $custnum; // gateway assigned customer ref number for recurring billing
var $authamount; // amount that was authorized
var $balance; //remaining balance
var $cardlevelresult;
var $procrefnum;
var $cardref; // card number token
// Cardinal Response Fields
var $acsurl; // card auth url
var $pareq; // card auth request
var $cctransid; // cardinal transid
// Fraud Profiler
var $profiler_score;
var $profiler_response;
var $profiler_reason;
// Errors Response Feilds
var $error; // error message if result is an error
var $errorcode; // numerical error code
var $blank; // blank response
var $transporterror; // transport error
// Constructor
function umTransaction()
{
// Set default values.
$this->command="sale";
$this->result="Error";
$this->resultcode="E";
$this->error="Transaction not processed yet.";
$this->timeout=45;
$this->cardpresent=false;
$this->lineitems = array();
if(isset($_SERVER['REMOTE_ADDR'])) $this->ip=$_SERVER['REMOTE_ADDR'];
$this->software="USAePay PHP API v" . USAEPAY_VERSION;
}
/**
* Add a line item to the transaction
*
* @param string $sku
* @param string $name
* @param string $description
* @param double $cost
* @param string $taxable
* @param int $qty
* @param string $refnum
*/
function addLine($sku, $name='', $description='', $cost='', $qty='', $taxable='', $refnum=0)
{
if(is_array($sku))
{
$vars = $sku;
$this->lineitems[] = array(
'sku' => @$vars['sku'],
'name' => @$vars['name'],
'description' => @$vars['description'],
'cost' => @$vars['cost'],
'taxable' => @$vars['taxable'],
'qty' => @$vars['qty'],
'refnum' => @$vars['refnum'],
'um' => @$vars['um'],
'taxrate' => @$vars['taxrate'],
'taxamount' => @$vars['taxamount'],
'taxclass' => @$vars['taxclass'],
'commoditycode' => @$vars['commoditycode'],
'discountrate' => @$vars['discountrate'],
'discountamount' => @$vars['discountamount'],
);
} else {
$this->lineitems[] = array(
'sku' => $sku,
'name' => $name,
'description' => $description,
'cost' => $cost,
'taxable' => $taxable,
'qty' => $qty,
'refnum' => $refnum
);
}
}
function getLineTotal()
{
$total = 0;
foreach($this->lineitems as $line)
{
$total+=(intval($line['cost']*100) * intval($line['qty']));
}
return number_format($total/100, 2, '.','');
}
/**
* Remove all line items
*
*/
function clearLines()
{
$this->lineitems=array();
}
/**
* Clear all transaction parameters except for the key and pin. Should be run between transactions if not
* reinsantiating the umTransaction class
*
*/
function clearData()
{
$map =$this->getFieldMap();
foreach($map as $apifield => $classfield)
{
if($classfield=='software' || $classfield=='key' || $classfield=='pin') continue;
unset($this->$classfield);
}
// Set default values.
$this->command="sale";
$this->result="Error";
$this->resultcode="E";
$this->error="Transaction not processed yet.";
$this->cardpresent=false;
$this->lineitems = array();
if(isset($_SERVER['REMOTE_ADDR'])) $this->ip=$_SERVER['REMOTE_ADDR'];
}
/**
* Verify that all required data has been set
*
* @return string
*/
function CheckData()
{
if(!$this->key) return "Source Key is required";
if(in_array(strtolower($this->command), array("cc:capture", "cc:refund", "refund", "check:refund","capture", "creditvoid", 'quicksale', 'quickcredit','refund:adjust','void:release', 'check:void','void')))
{
if(!$this->refnum) return "Reference Number is required";
}else if(in_array(strtolower($this->command), array("svp")))
{
if(!$this->svpbank) return "Bank ID is required";
if(!$this->svpreturnurl) return "Return URL is required";
if(!$this->svpcancelurl) return "Cancel URL is required";
} else {
if(in_array(strtolower($this->command), array("check:sale","check:credit", "check", "checkcredit","reverseach") )) {
if(!$this->account) return "Account Number is required";
if(!$this->routing) return "Routing Number is required";
} else if (in_array(strtolower($this->command) , array('cash','cash:refund','cash:sale','external:check:sale','external:cc:sale','external:gift:sale'))) {
// nothing needs to be validated for cash
} else {
if(!$this->magstripe &&!preg_match('~^enc://~',$this->card)) {
if(!$this->card) return "Credit Card Number is required ({$this->command})";
if(!$this->exp) return "Expiration Date is required";
}
}
if ($this->command !== "cc:save"){
$this->amount=preg_replace("/[^0-9\.]/","",$this->amount);
if(!$this->amount) return "Amount is required";
if(!$this->invoice && !$this->orderid) return "Invoice number or Order ID is required";
if(!$this->magstripe) {
//if(!$this->cardholder) return "Cardholder Name is required";
//if(!$this->street) return "Street Address is required";
//if(!$this->zip) return "Zipcode is required";
}
}
}
return 0;
}
/**
* Send transaction to the USAePay Gateway and parse response
*
* @return boolean
*/
function Process()
{
// check that we have the needed data
$tmp=$this->CheckData();
if($tmp)
{
$this->result="Error";
$this->resultcode="E";
$this->error=$tmp;
$this->errorcode=10129;
return false;
}
// populate the data
$map = $this->getFieldMap();
$data = array();
foreach($map as $apifield =>$classfield)
{
switch($classfield)
{
case 'isrecurring':
if($this->isrecurring) $data['UMisRecurring'] = 'Y';
break;
case 'nontaxable':
if($this->nontaxable) $data['UMnontaxable'] = 'Y';
break;
case 'checkimage_front':
case 'checkimage_back':
$data[$apifield] = base64_encode($this->$classfield);
break;
case 'billsourcekey':
if($this->billsourcekey) $data['UMbillsourcekey'] = 'yes';
break;
case 'cardpresent':
if($this->cardpresent) $data['UMcardpresent'] = '1';
break;
case 'allowpartialauth':
if($this->allowpartialauth===true) $data['UMallowPartialAuth'] = 'Yes';
break;
case 'savecard':
if($this->savecard) $data['UMsaveCard'] = 'y';
break;
default:
$data[$apifield] = $this->$classfield;
}
}
if(isset($data['UMcheckimagefront']) || isset($data['UMcheckimageback'])) $data['UMcheckimageencoding'] = 'base64';
// tack on custom fields
for($i=1; $i<=20; $i++)
{
if($this->{"custom$i"}) $data["UMcustom$i"] = $this->{"custom$i"};
}
// tack on line level detail
$c=1;
if(!is_array($this->lineitems)) $this->lineitems=array();
foreach($this->lineitems as $lineitem)
{
$data["UMline{$c}sku"] = $lineitem['sku'];
$data["UMline{$c}name"] = $lineitem['name'];
$data["UMline{$c}description"] = $lineitem['description'];
$data["UMline{$c}cost"] = $lineitem['cost'];
$data["UMline{$c}taxable"] = $lineitem['taxable'];
$data["UMline{$c}qty"] = $lineitem['qty'];
if($lineitem['refnum'])
$data["UMline{$c}prodRefNum"] = $lineitem['refnum'];
//optional level 3 data
if($lineitem['um']) $data["UMline{$c}um"] = $lineitem['um'];
if($lineitem['taxrate']) $data["UMline{$c}taxrate"] = $lineitem['taxrate'];
if($lineitem['taxamount']) $data["UMline{$c}taxamount"] = $lineitem['taxamount'];
if($lineitem['taxclass']) $data["UMline{$c}taxclass"] = $lineitem['taxclass'];
if($lineitem['commoditycode']) $data["UMline{$c}commoditycode"] = $lineitem['commoditycode'];
if($lineitem['discountrate']) $data["UMline{$c}discountrate"] = $lineitem['discountrate'];
if($lineitem['discountamount']) $data["UMline{$c}discountamount"] = $lineitem['discountamount'];
$c++;
}
// Create hash if pin has been set.
if(trim($this->pin))
{
// generate random seed value
$seed = microtime(true) . rand();
// assemble prehash data
$prehash = $this->command . ":" . trim($this->pin) . ":" . $this->amount . ":" . $this->invoice . ":" . $seed;
// if sha1 is available, create sha1 hash, else use md5
if(function_exists('sha1')) $hash = 's/' . $seed . '/' . sha1($prehash) . '/n';
else $hash = 'm/' . $seed . '/' . md5($prehash) . '/n';
// populate hash value
$data['UMhash'] = $hash;
}
// Tell the gateway what the client side timeout is
if(@$this->timeout) $data['UMtimeout'] = intval($this->timeout);
// Figure out URL
$url = ($this->gatewayurl?$this->gatewayurl:"https://www.usaepay.com/gate");
if($this->usesandbox) $url = "https://sandbox.usaepay.com/gate";
// Post data to Gateway
$result=$this->httpPost($url, $data);
if($result===false) return false;
// result is in urlencoded format, parse into an array
parse_str($result,$tmp);
// check to make sure we received the correct fields
if(!isset($tmp["UMversion"]) || !isset($tmp["UMstatus"]))
{
$this->result="Error";
$this->resultcode="E";
$this->error="Error parsing data from card processing gateway.";
$this->errorcode=10132;
return false;
}
// Store results
$this->result=(isset($tmp["UMstatus"])?$tmp["UMstatus"]:"Error");
$this->resultcode=(isset($tmp["UMresult"])?$tmp["UMresult"]:"E");
$this->authcode=(isset($tmp["UMauthCode"])?$tmp["UMauthCode"]:"");
$this->refnum=(isset($tmp["UMrefNum"])?$tmp["UMrefNum"]:"");
$this->batch=(isset($tmp["UMbatch"])?$tmp["UMbatch"]:"");
$this->avs_result=(isset($tmp["UMavsResult"])?$tmp["UMavsResult"]:"");
$this->avs_result_code=(isset($tmp["UMavsResultCode"])?$tmp["UMavsResultCode"]:"");
$this->cvv2_result=(isset($tmp["UMcvv2Result"])?$tmp["UMcvv2Result"]:"");
$this->cvv2_result_code=(isset($tmp["UMcvv2ResultCode"])?$tmp["UMcvv2ResultCode"]:"");
$this->vpas_result_code=(isset($tmp["UMvpasResultCode"])?$tmp["UMvpasResultCode"]:"");
$this->convertedamount=(isset($tmp["UMconvertedAmount"])?$tmp["UMconvertedAmount"]:"");
$this->convertedamountcurrency=(isset($tmp["UMconvertedAmountCurrency"])?$tmp["UMconvertedAmountCurrency"]:"");
$this->conversionrate=(isset($tmp["UMconversionRate"])?$tmp["UMconversionRate"]:"");
$this->error=(isset($tmp["UMerror"])?$tmp["UMerror"]:"");
$this->errorcode=(isset($tmp["UMerrorcode"])?$tmp["UMerrorcode"]:"10132");
$this->custnum=(isset($tmp["UMcustnum"])?$tmp["UMcustnum"]:"");
$this->authamount=(isset($tmp["UMauthAmount"])?$tmp["UMauthAmount"]:"");
$this->balance=(isset($tmp["UMremainingBalance"])?$tmp["UMremainingBalance"]:"");
$this->cardlevelresult=(isset($tmp["UMcardLevelResult"])?$tmp["UMcardLevelResult"]:"");
$this->procrefnum=(isset($tmp["UMprocRefNum"])?$tmp["UMprocRefNum"]:"");
$this->profiler_score=(isset($tmp["UMprofilerScore"])?$tmp["UMprofilerScore"]:"");
$this->profiler_response=(isset($tmp["UMprofilerResponse"])?$tmp["UMprofilerResponse"]:"");
$this->profiler_reason=(isset($tmp["UMprofilerReason"])?$tmp["UMprofilerReason"]:"");
$this->cardref=(isset($tmp["UMcardRef"])?$tmp["UMcardRef"]:"");
$this->isduplicate=(isset($tmp["UMisDuplicate"])?$tmp["UMisDuplicate"]:"");
// Obsolete variable (for backward compatibility) At some point they will no longer be set.
//$this->avs=(isset($tmp["UMavsResult"])?$tmp["UMavsResult"]:"");
//$this->cvv2=(isset($tmp["UMcvv2Result"])?$tmp["UMcvv2Result"]:"");
if(isset($tmp["UMcctransid"])) $this->cctransid=$tmp["UMcctransid"];
if(isset($tmp["UMacsurl"])) $this->acsurl=$tmp["UMacsurl"];
if(isset($tmp["UMpayload"])) $this->pareq=$tmp["UMpayload"];
if($this->resultcode == "A") return true;
return false;
}
function ProcessQuickSale()
{
$this->command='quicksale';
// check that we have the needed data
$tmp=$this->CheckData();
if($tmp)
{
$this->result="Error";
$this->resultcode="E";
$this->error=$tmp;
$this->errorcode=10129;
return false;
}
// Create hash if pin has been set.
if(!trim($this->pin))
{
$this->result="Error";
$this->resultcode="E";
$this->error='Source key must have pin assigned to run transaction';
$this->errorcode=999;
return false;
}
// generate random seed value
$seed = microtime(true) . rand();
// assemble prehash data
$prehash = $this->key . $seed . trim($this->pin);
// hash the data
$hash = sha1($prehash);
$data = '<?xml version="1.0" encoding="UTF-8"?>' .
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:usaepay" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' .
'<SOAP-ENV:Body>' .
'<ns1:runQuickSale>' .
'<Token xsi:type="ns1:ueSecurityToken">' .
'<ClientIP xsi:type="xsd:string">' . $this->ip . '</ClientIP>' .
'<PinHash xsi:type="ns1:ueHash">' .
'<HashValue xsi:type="xsd:string">' . $hash . '</HashValue>' .
'<Seed xsi:type="xsd:string">' . $seed . '</Seed>' .
'<Type xsi:type="xsd:string">sha1</Type>' .
'</PinHash>' .
'<SourceKey xsi:type="xsd:string">' . $this->key . '</SourceKey>' .
'</Token>' .
'<RefNum xsi:type="xsd:integer">' . preg_replace('/[^0-9]/','',$this->refnum) . '</RefNum>' .
'<Details xsi:type="ns1:TransactionDetail">' .
'<Amount xsi:type="xsd:double">' . $this->xmlentities($this->amount) . '</Amount>' .
'<Description xsi:type="xsd:string">' . $this->xmlentities($this->description) . '</Description>'.
'<Discount xsi:type="xsd:double">' . $this->xmlentities($this->discount) . '</Discount>' .
'<Invoice xsi:type="xsd:string">' . $this->xmlentities($this->invoice) . '</Invoice>' .
'<NonTax xsi:type="xsd:boolean">' . ($this->nontaxable?'true':'false') . '</NonTax>' .
'<OrderID xsi:type="xsd:string">' . $this->xmlentities($this->orderid) . '</OrderID>' .
'<PONum xsi:type="xsd:string">' . $this->xmlentities($this->ponum) . '</PONum>' .
'<Shipping xsi:type="xsd:double">' . $this->xmlentities($this->shipping) . '</Shipping>' .
'<Subtotal xsi:type="xsd:double">' . $this->xmlentities($this->subtotal) . '</Subtotal>' .
'<Tax xsi:type="xsd:double">' . $this->xmlentities($this->tax) . '</Tax>' .
'<Tip xsi:type="xsd:double">' . $this->xmlentities($this->tip) . '</Tip>' .
'</Details>' .
'<AuthOnly xsi:type="xsd:boolean">false</AuthOnly>' .
'</ns1:runQuickSale>' .
'</SOAP-ENV:Body>' .
'</SOAP-ENV:Envelope>';
// Figure out URL
$url = ($this->gatewayurl?$this->gatewayurl:"https://www.usaepay.com/soap/gate/15E7FB61");
if($this->usesandbox) $url = "https://sandbox.usaepay.com/soap/gate/15E7FB61";
// Post data to Gateway
$result=$this->httpPost($url, array('xml'=>$data));
if($result===false) return false;
if(preg_match('~<AuthCode[^>]*>(.*)</AuthCode>~', $result, $m)) $this->authcode=$m[1];
if(preg_match('~<AvsResult[^>]*>(.*)</AvsResult>~', $result, $m)) $this->avs_result=$m[1];
if(preg_match('~<AvsResultCode[^>]*>(.*)</AvsResultCode>~', $result, $m)) $this->avs_result_code=$m[1];
if(preg_match('~<BatchRefNum[^>]*>(.*)</BatchRefNum>~', $result, $m)) $this->batch=$m[1];
if(preg_match('~<CardCodeResult[^>]*>(.*)</CardCodeResult>~', $result, $m)) $this->cvv2_result=$m[1];
if(preg_match('~<CardCodeResultCode[^>]*>(.*)</CardCodeResultCode>~', $result, $m)) $this->cvv2_result_code=$m[1];
//if(preg_match('~<CardLevelResult[^>]*>(.*)</CardLevelResult>~', $result, $m)) $this->cardlevel_result=$m[1];
//if(preg_match('~<CardLevelResultCode[^>]*>(.*)</CardLevelResultCode>~', $result, $m)) $this->cardlevel_result_code=$m[1];
if(preg_match('~<ConversionRate[^>]*>(.*)</ConversionRate>~', $result, $m)) $this->conversionrate=$m[1];
if(preg_match('~<ConvertedAmount[^>]*>(.*)</ConvertedAmount>~', $result, $m)) $this->convertedamount=$m[1];
if(preg_match('~<ConvertedAmountCurrency[^>]*>(.*)</ConvertedAmountCurrency>~', $result, $m)) $this->convertedamountcurrency=$m[1];
if(preg_match('~<Error[^>]*>(.*)</Error>~', $result, $m)) $this->error=$m[1];
if(preg_match('~<ErrorCode[^>]*>(.*)</ErrorCode>~', $result, $m)) $this->errorcode=$m[1];
//if(preg_match('~<isDuplicate[^>]*>(.*)</isDuplicate>~', $result, $m)) $this->isduplicate=$m[1];
if(preg_match('~<RefNum[^>]*>(.*)</RefNum>~', $result, $m)) $this->refnum=$m[1];
if(preg_match('~<Result[^>]*>(.*)</Result>~', $result, $m)) $this->result=$m[1];
if(preg_match('~<ResultCode[^>]*>(.*)</ResultCode>~', $result, $m)) $this->resultcode=$m[1];
if(preg_match('~<VpasResultCode[^>]*>(.*)</VpasResultCode>~', $result, $m)) $this->vpas_result_code=$m[1];
// Store results
if($this->resultcode == "A") return true;
return false;
}
function ProcessQuickCredit()
{
$this->command='quickcredit';
// check that we have the needed data
$tmp=$this->CheckData();
if($tmp)
{
$this->result="Error";
$this->resultcode="E";
$this->error=$tmp;
$this->errorcode=10129;
return false;
}
// Create hash if pin has been set.
if(!trim($this->pin))
{
$this->result="Error";
$this->resultcode="E";
$this->error='Source key must have pin assigned to run transaction';
$this->errorcode=999;
return false;
}
// generate random seed value
$seed = microtime(true) . rand();
// assemble prehash data
$prehash = $this->key . $seed . trim($this->pin);
// hash the data
$hash = sha1($prehash);
$data = '<?xml version="1.0" encoding="UTF-8"?>' .
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:usaepay" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' .
'<SOAP-ENV:Body>' .
'<ns1:runQuickCredit>' .
'<Token xsi:type="ns1:ueSecurityToken">' .
'<ClientIP xsi:type="xsd:string">' . $this->ip . '</ClientIP>' .
'<PinHash xsi:type="ns1:ueHash">' .
'<HashValue xsi:type="xsd:string">' . $hash . '</HashValue>' .
'<Seed xsi:type="xsd:string">' . $seed . '</Seed>' .
'<Type xsi:type="xsd:string">sha1</Type>' .
'</PinHash>' .
'<SourceKey xsi:type="xsd:string">' . $this->key . '</SourceKey>' .
'</Token>' .
'<RefNum xsi:type="xsd:integer">' . preg_replace('/[^0-9]/','',$this->refnum) . '</RefNum>' .
'<Details xsi:type="ns1:TransactionDetail">' .
'<Amount xsi:type="xsd:double">' . $this->xmlentities($this->amount) . '</Amount>' .
'<Description xsi:type="xsd:string">' . $this->xmlentities($this->description) . '</Description>'.
'<Discount xsi:type="xsd:double">' . $this->xmlentities($this->discount) . '</Discount>' .
'<Invoice xsi:type="xsd:string">' . $this->xmlentities($this->invoice) . '</Invoice>' .
'<NonTax xsi:type="xsd:boolean">' . ($this->nontaxable?'true':'false') . '</NonTax>' .
'<OrderID xsi:type="xsd:string">' . $this->xmlentities($this->orderid) . '</OrderID>' .
'<PONum xsi:type="xsd:string">' . $this->xmlentities($this->ponum) . '</PONum>' .
'<Shipping xsi:type="xsd:double">' . $this->xmlentities($this->shipping) . '</Shipping>' .
'<Subtotal xsi:type="xsd:double">' . $this->xmlentities($this->subtotal) . '</Subtotal>' .
'<Tax xsi:type="xsd:double">' . $this->xmlentities($this->tax) . '</Tax>' .
'<Tip xsi:type="xsd:double">' . $this->xmlentities($this->tip) . '</Tip>' .
'</Details>' .
'<AuthOnly xsi:type="xsd:boolean">false</AuthOnly>' .
'</ns1:runQuickCredit>' .
'</SOAP-ENV:Body>' .
'</SOAP-ENV:Envelope>';
// Figure out URL
$url = ($this->gatewayurl?$this->gatewayurl:"https://www.usaepay.com/soap/gate/15E7FB61");
if($this->usesandbox) $url = "https://sandbox.usaepay.com/soap/gate/15E7FB61";
// Post data to Gateway
$result=$this->httpPost($url, array('xml'=>$data));
if($result===false) return false;
if(preg_match('~<AuthCode[^>]*>(.*)</AuthCode>~', $result, $m)) $this->authcode=$m[1];
if(preg_match('~<AvsResult[^>]*>(.*)</AvsResult>~', $result, $m)) $this->avs_result=$m[1];
if(preg_match('~<AvsResultCode[^>]*>(.*)</AvsResultCode>~', $result, $m)) $this->avs_result_code=$m[1];
if(preg_match('~<BatchRefNum[^>]*>(.*)</BatchRefNum>~', $result, $m)) $this->batch=$m[1];
if(preg_match('~<CardCodeResult[^>]*>(.*)</CardCodeResult>~', $result, $m)) $this->cvv2_result=$m[1];
if(preg_match('~<CardCodeResultCode[^>]*>(.*)</CardCodeResultCode>~', $result, $m)) $this->cvv2_result_code=$m[1];
//if(preg_match('~<CardLevelResult[^>]*>(.*)</CardLevelResult>~', $result, $m)) $this->cardlevel_result=$m[1];
//if(preg_match('~<CardLevelResultCode[^>]*>(.*)</CardLevelResultCode>~', $result, $m)) $this->cardlevel_result_code=$m[1];
if(preg_match('~<ConversionRate[^>]*>(.*)</ConversionRate>~', $result, $m)) $this->conversionrate=$m[1];
if(preg_match('~<ConvertedAmount[^>]*>(.*)</ConvertedAmount>~', $result, $m)) $this->convertedamount=$m[1];
if(preg_match('~<ConvertedAmountCurrency[^>]*>(.*)</ConvertedAmountCurrency>~', $result, $m)) $this->convertedamountcurrency=$m[1];
if(preg_match('~<Error[^>]*>(.*)</Error>~', $result, $m)) $this->error=$m[1];
if(preg_match('~<ErrorCode[^>]*>(.*)</ErrorCode>~', $result, $m)) $this->errorcode=$m[1];
//if(preg_match('~<isDuplicate[^>]*>(.*)</isDuplicate>~', $result, $m)) $this->isduplicate=$m[1];
if(preg_match('~<RefNum[^>]*>(.*)</RefNum>~', $result, $m)) $this->refnum=$m[1];
if(preg_match('~<Result[^>]*>(.*)</Result>~', $result, $m)) $this->result=$m[1];
if(preg_match('~<ResultCode[^>]*>(.*)</ResultCode>~', $result, $m)) $this->resultcode=$m[1];
if(preg_match('~<VpasResultCode[^>]*>(.*)</VpasResultCode>~', $result, $m)) $this->vpas_result_code=$m[1];
// Store results
if($this->resultcode == "A") return true;
return false;
}
function getSession()
{
$seed = md5(mt_rand());
$action = 'getsession';
// build hash
$prehash = $action . ":" . $this->pin . ":" . $seed;
$hash = 's/' . $seed . '/' . sha1($prehash);
// Figure out URL
$url = 'https://www.usaepay.com/interface/profiler/';
if(preg_match('~https://([^/]*)/~i', $this->gatewayurl, $m)) {
$url = 'https://' . $m[1] . '/interface/profiler/';
}
// Look for usesandbox
if($this->usesandbox) $url = "https://sandbox.usaepay.com/interface/profiler/";
// Add request paramters
$url .= $action . '?SourceKey=' . rawurlencode($this->key) . '&Hash=' . rawurlencode($hash);
// Make Rest Call
$output = file_get_contents($url);
if(!$output) {
$this->error = "Blank response";
return false;
}
// Parse response
$xml = simplexml_load_string($output);
if(!$xml) {
$this->error = "Unable to parse response";
return false;
}
if($xml->Response!='A') {
$this->error = $xml->Reason;
$this->errorcode = $xml->ErrorCode;
return false;
}
// assemble template
$query ='org_id=' .$xml->OrgID . '&session_id=' . $xml->SessionID;
$baseurl = "https://h.online-metrix.net/fp/";
$out = '';
$out .= '<p style="background:url('.$baseurl.'clear.png?'.$query.'&m=1)"></p>';
$out .= '<img src="'.$baseurl.'clear.png?'.$query.'&m=2" width="1" height="1" alt="">';
$out .= '<script src="'.$baseurl.'check.js?'.$query.'" type="text/javascript"></script>';
$out .= '<object type="application/x-shockwave-flash" data="'.$baseurl.'fp.swf?'.$query.'" width="1" height="1" id="thm_fp">';
$out .= ' <param name="movie" value="'.$baseurl.'fp.swf?'.$query.'" />';
$out .= '<div></div></object>';
return array('sessionid'=>$xml->SessionID, 'orgid'=>$xml->OrgID, 'html'=>$out);
}
/**
* Verify Proper Installation of USAePay class and required PHP Support
*
*/
function Test()
{
$curl_version=false;
?>
<table border=1>
<tr><th><b>Test</b></th><th><b>Result</b></th></tr>
<tr><td valign="Top">Checking PHP Version</td>
<td valign="top"><?php
if(version_compare(phpversion(),"4.3.0")) {
?><font color="green">Ok</font><br>
PHP version <?php echo phpversion()?> on <?php echo PHP_OS?> detected.
<?php
} else {
?><font color="red">Warning</font><br>
PHP version <?php echo phpversion()?> detected. It is recommended that you
upgrade to the most recent release of PHP.
<?php
}
?></td></tr>
<tr><td valign="Top">Checking CURL Extension</td>
<td valign="top"><?php
if(function_exists("curl_version")) {
$tmp=curl_version();
// PHP 5 returns an array, version 4 returns a string
if(is_array($tmp)) {
$curl_version=$tmp["version"];
$curl_ssl_version=$tmp["ssl_version"];
} else {
$tmp=explode(" ", $tmp);
foreach($tmp as $piece)
{
list($lib,$version)=explode("/",$piece);
if(stristr($lib,"curl")) $curl_version=$version;
elseif(stristr($lib,"ssl")) $curl_ssl_version=$version;
}
}
?><font color="green">Ok</font><br>
Curl Extension (<?php echo $curl_version?>) detected.
<?php
} else {
?><font color="red">Error</font><br>
Your PHP installation does not include the Curl Extension. You must either enable the curl
extension in your php.ini file by removing the semi-colon from the line ";extension=php_curl.dll" or recompile
php with the configuration flag: "--with-curl"
<?php
}
?>
</td></tr>
<tr><td valign="Top">Checking CURL SSL Support</td>
<td valign="top"><?php
if($curl_ssl_version) {
?><font color="green">Ok</font><br>
SSL Version (<?php echo $curl_ssl_version?>) detected.
<?php
} else {
?><font color="red">Error</font><br>
It appears that your curl installation does not include support for the HTTPS (ssl) protocal
Proper SSL installation is required to communicate with the USAePay gateway securely. Please recompile curl
with SSL support.
<?php
}
?>
</td></tr>
<tr><td valign="Top">Checking Communication with the Gateway</td>
<td valign="top"><?php
$ssl_failed=true;
if(!$curl_version) {
?><font color="red">Error</font><br>
No curl support
<?php
} else {
$ch = curl_init(($this->gatewayurl?$this->gatewayurl:"https://www.usaepay.com/secure/gate.php") . "?VersionCheck=1&UMsoftware=" . rawurlencode($this->software));
if(!$ch) {
?><font color="red">Error</font><br>
Curl failed to connect to server: (NULL $ch returned)
<?php
} else {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_SSLVERSION, 6);
if($this->cabundle) curl_setopt($ch, CURLOPT_CAINFO, $this->cabundle);
$result=curl_exec($ch);
parse_str($result, $return);
if($return["UMversion"]) {
$ssl_failed=false;
?><font color="green">Ok</font><br>
Successfully connected to the gateway. Detected version (<?php echo $return["UMversion"]?>) of the USAePay gateway API.
<?php
} else {
$ch = curl_init(($this->gatewayurl?$this->gatewayurl:"https://www.usaepay.com/secure/gate.php") . "?VersionCheck=1&UMsoftware=" . rawurlencode($this->software));
if($this->cabundle) curl_setopt($ch, CURLOPT_CAINFO, $this->cabundle);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result=curl_exec($ch);
parse_str($result, $return);
if($return["UMversion"]) {
?><font color="red">Warning</font><br>
Successfully connected to the gateway but could not verify SSL certificate. This usually indicates that you
do not have curl set up with the proper root CA certificate. It is recommended that you install an up to date
root ca bundle. As a <b>temporary</b> work around you may disable the ssl cert check by adding the following to your script:<br><br><tt>
$tran->ignoresslcerterrors=true;<br>
<br></tt>
<?php
$ch = curl_init("https://www.verisign.com");
if($this->cabundle) curl_setopt($ch, CURLOPT_CAINFO, $this->cabundle);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_SSLVERSION, 6);
$result=curl_exec($ch);
if(strlen($result)) {
?>
SSL certificate for VeriSign was validated sucessfully. This would indicate that you have a root CA bundle installed but that it
is outdated. See below.
<?php
} else {
?>
Unable to verify SSL certificate for VeriSign. This would indicate that you do not have a root CA bundle installed. See below.
<?php
}
} else {
$ch = curl_init("https://216.133.244.70/secure/gate.php?VersionCheck=1&UMsoftware=" . rawurlencode($this->software));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result=curl_exec($ch);
parse_str($result, $return);
if($return["UMversion"]) {
?><font color="red">Warning</font><br>
Successfully connected to the gateway using a static IP but could not connect using hostname. This would indicate
that you do not have DNS setup correctly. Please correct your server configuration to include a valid
DNS server. As a temporary work around you may add the following to your script:<br><br><tt>
$tran->ignoresslcerterrors=true;<br>
$tran->gatewayurl="https://216.133.244.70/gate.php";<br>
<br></tt>
<?php
} else {
?><font color="red">Failed</font><br>
Unable to establish connection to the USAePay Gateway. It is possible that you are firewalling
outbound traffic from your server. To correct this problem, you must add an allow entry in
your firewall for ip <?php echo gethostbyname("www.usaepay.com")?> on port 443;
<?php
}
}
}
}
}
?>
</td></tr>
<?php if($ssl_failed) {?>
<tr><td valign="Top">Looking for root CA Bundle</td>
<td valign="top"><?php
if(strstr(PHP_OS,"WIN")!==false) {
foreach(array('c:\windows\ca-bundle.crt','c:\windows\curl-ca-bundle.crt') as $certpath)
{
if(is_file($certpath)) break;
else unset($certpath);
}
if(!$certpath) $certpath="c:\windows\curl-ca-bundle.crt";