-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGatewayService.php
More file actions
997 lines (894 loc) · 33.2 KB
/
GatewayService.php
File metadata and controls
997 lines (894 loc) · 33.2 KB
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
<?php
/*
* Copyright notice:
* (c) Copyright 2019 RocketGate
* All rights reserved.
*
* The copyright notice must not be removed without specific, prior
* written permission from RocketGate.
*
* This software is protected as an unpublished work under the U.S. copyright
* laws. The above copyright notice is not intended to effect a publication of
* this work.
* This software is the confidential and proprietary information of RocketGate.
* Neither the binaries nor the source code may be redistributed without prior
* written permission from RocketGate.
*
* The software is provided "as-is" and without warranty of any kind, express, implied
* or otherwise, including without limitation, any warranty of merchantability or fitness
* for a particular purpose. In no event shall RocketGate be liable for any direct,
* special, incidental, indirect, consequential or other damages of any kind, or any damages
* whatsoever arising out of or in connection with the use or performance of this software,
* including, without limitation, damages resulting from loss of use, data or profits, and
* whether or not advised of the possibility of damage, regardless of the theory of liability.
*
*/
namespace RocketGate\Sdk;
////////////////////////////////////////////////////////////////////////////////
//
// Compute the version number.
//
////////////////////////////////////////////////////////////////////////////////
//
GatewayChecksum::SetVersion();
////////////////////////////////////////////////////////////////////////////////
//
// GatewayService() - Object that performs sends transactions
// to a RocketGate Gateway Server.
//
////////////////////////////////////////////////////////////////////////////////
//
class GatewayService
{
var $rocketGateHost; // Gateway hostname
var $rocketGateProtocol; // Message protocol
var $rocketGatePortNo; // Network connection port
var $rocketGateServlet; // Destination servlet
var $rocketGateConnectTimeout; // Timeout for network connection
var $rocketGateReadTimeout; // Timeout for network read
private $rocketGateLatestResponseCode = 0; // Latest request response code
private $rocketGateLatestExecutionTime = 0.0; // Latest request execution time
private $rocketGateLatestConnectionTime = 0.0; // Latest request connection time
//
// Optional curl callback function before curl_exec()
//
private $curlCallback;
//
// Optional curl response callback function after curl_exec()
//
private $curlResponseCallback;
//////////////////////////////////////////////////////////////////////
//
// GatewayService() - Constructor for class.
//
//////////////////////////////////////////////////////////////////////
//
public function __construct($testMode = false)
{
//
// Set the standard production destinations for the
// service.
//
$this->SetTestMode($testMode); // By default assume production mode
$this->rocketGateConnectTimeout = 10;// 10 second connection timeout
$this->rocketGateReadTimeout = 90; // 90 second operation timeout
$this->rocketGateServlet = "gateway/servlet/ServiceDispatcherAccess";
}
//////////////////////////////////////////////////////////////////////
//
// PerformAuthOnly() - Perform an auth-only transaction.
//
//////////////////////////////////////////////////////////////////////
//
function PerformAuthOnly(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CC_AUTH");
if ($request->Get(GatewayRequest::REFERENCE_GUID()) != null) {
if (!($this->PerformTargetedTransaction($request, $response))) {
return false;
}
} else {
if (!($this->PerformTransaction($request, $response))) {
return false;
}
}
return $this->PerformConfirmation($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformTicket() - Perform a ticket operation for a previous
// auth-only transaction.
//
//////////////////////////////////////////////////////////////////////
//
function PerformTicket(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CC_TICKET");
return $this->PerformTargetedTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformPurchase() - Perform a complete purchase transaction
// using the information contained in
// a request.
//
//////////////////////////////////////////////////////////////////////
//
function PerformPurchase(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CC_PURCHASE");
if ($request->Get(GatewayRequest::REFERENCE_GUID()) != null) {
if (!($this->PerformTargetedTransaction($request, $response))) {
return false;
}
} else {
if (!($this->PerformTransaction($request, $response))) {
return false;
}
}
return $this->PerformConfirmation($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformCredit() - Perform a credit operation for a previously
// completed transaction.
//
//////////////////////////////////////////////////////////////////////
//
function PerformCredit(GatewayRequest $request, GatewayResponse $response)
{
//
// Apply the transaction type to the request.
//
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CC_CREDIT");
//
// If the credit references a previous transaction, we
// need to send it back to the origination site. Otherwise,
// it can be sent to any server.
//
if ($request->Get(GatewayRequest::REFERENCE_GUID()) != null) {
return $this->PerformTargetedTransaction($request, $response);
}
return $this->PerformTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformVoid() - Perform a void operation for a previously
// completed transaction.
//
//////////////////////////////////////////////////////////////////////
//
function PerformVoid(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CC_VOID");
return $this->PerformTargetedTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformCardScrub() - Perform scrubbing on a card/customer.
//
//////////////////////////////////////////////////////////////////////
//
function PerformCardScrub(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CARDSCRUB");
return $this->PerformTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformRebillCancel() - Schedule cancellation of rebilling.
//
//////////////////////////////////////////////////////////////////////
//
function PerformRebillCancel(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "REBILL_CANCEL");
return $this->PerformTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformRebillUpdate() - Update terms of rebilling.
//
//////////////////////////////////////////////////////////////////////
//
function PerformRebillUpdate(GatewayRequest $request, GatewayResponse $response)
{
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "REBILL_UPDATE");
//
// If there is no prorated charage, just perform the update.
//
$amount = $request->Get(GatewayRequest::AMOUNT());
if (($amount == null) || ($amount <= 0.0)) {
return $this->PerformTransaction($request, $response);
}
//
// If there is a charge, perform the update and confirm
// the charge.
//
if (!($this->PerformTransaction($request, $response))) {
return false;
}
return $this->PerformConfirmation($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformLookup() - Lookup previous transaction.
//
//////////////////////////////////////////////////////////////////////
//
function PerformLookup(GatewayRequest $request, GatewayResponse $response)
{
//
// Apply the transaction type to the request.
//
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "LOOKUP");
if ($request->Get(GatewayRequest::REFERENCE_GUID()) != null) {
return $this->PerformTargetedTransaction($request, $response);
}
return $this->PerformTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// PerformCardUpload() - Upload card data to the servers.
//
//////////////////////////////////////////////////////////////////////
//
function PerformCardUpload(GatewayRequest $request, GatewayResponse $response)
{
//
// Apply the transaction type to the request.
//
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CARDUPLOAD");
return $this->PerformTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// GenerateXsell() - Add an entry to the XsellQueue.
//
//////////////////////////////////////////////////////////////////////
//
function GenerateXsell(GatewayRequest $request, GatewayResponse $response)
{
//
// Apply the transaction type to the request.
//
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "GENERATEXSELL");
$request->Set(GatewayRequest::REFERENCE_GUID(),
$request->Get(GatewayRequest::XSELL_REFERENCE_XACT()));
if ($request->Get(GatewayRequest::REFERENCE_GUID()) != null) {
return $this->PerformTargetedTransaction($request, $response);
}
return $this->PerformTransaction($request, $response);
}
//////////////////////////////////////////////////////////////////////
//
// BuildPaymentLink() - Create an embeddable RG hosted payment link
//
//////////////////////////////////////////////////////////////////////
//
function BuildPaymentLink($request, $response)
{
if($request->Get(GatewayRequest::EMBEDDED_FIELDS_TOKEN()) != null) {
$embeddedFieldsToken = $request->Get(GatewayRequest::EMBEDDED_FIELDS_TOKEN());
$gatewayURL = str_replace("EmbeddedFieldsProxy", "BuildPaymentLinkSubmit", $embeddedFieldsToken);
$request->Set(GatewayRequest::GATEWAY_URL(), $gatewayURL);
} else {
$request->Set(GatewayRequest::GATEWAY_SERVLET(), "/hostedpage/servlet/BuildPaymentLinkSubmit");
}
$this->PerformTransaction($request, $response);
return ($response->Get(GatewayResponse::RESPONSE_CODE()) == GatewayCodes::RESPONSE_SUCCESS &&
$response->Get(GatewayResponse::PAYMENT_LINK_URL()) != NULL);
}
//////////////////////////////////////////////////////////////////////
//
// SetTestMode() - Set the communications parameters for
// production or test mode.
//
//////////////////////////////////////////////////////////////////////
//
function SetTestMode($testFlag)
{
//
// If the test flag is set, use the test setup parameters.
//
if ($testFlag) {// In test mode?
$this->rocketGateHost = "dev-gateway.rocketgate.com";
$this->rocketGateProtocol = "https";// Use SSL
$this->rocketGatePortNo = "443";// SSL port
//
// If the test flag is not set, use the production parameters.
//
} else {
$this->rocketGateHost = "gateway.rocketgate.com";
$this->rocketGateProtocol = "https";// Use SSL
$this->rocketGatePortNo = "443";// SSL port
}
}
//////////////////////////////////////////////////////////////////////
//
// SetHost() - Set the host used by the service.
//
//////////////////////////////////////////////////////////////////////
//
function SetHost($hostname)
{
$this->rocketGateHost = $hostname;// Use this hostname
}
//////////////////////////////////////////////////////////////////////
//
// SetProtocol() - Set the communications protocol used by
// the service.
//
//////////////////////////////////////////////////////////////////////
//
function SetProtocol($protocol)
{
$this->rocketGateProtocol = $protocol;// HTTP, HTTPS, etc.
}
//////////////////////////////////////////////////////////////////////
//
// SetPortNo() - Set the port number used by the service.
//
//////////////////////////////////////////////////////////////////////
//
function SetPortNo($portNo)
{
$this->rocketGatePortNo = $portNo;// IP port
}
//////////////////////////////////////////////////////////////////////
//
// SetServlet() - Set the servlet used by the service.
//
//////////////////////////////////////////////////////////////////////
//
function SetServlet($servlet)
{
$this->rocketGateServlet = $servlet;// Tomcat servlet
}
//////////////////////////////////////////////////////////////////////
//
// SetConnectTimeout() - Set the timeout used during connection
// to the servlet.
//
//////////////////////////////////////////////////////////////////////
//
function SetConnectTimeout($timeout)
{
$this->rocketGateConnectTimeout = $timeout;// Number of seconds
}
//////////////////////////////////////////////////////////////////////
//
// SetReadTimeout() - Set the timeout used while waiting for
// the servlet to answer.
//
//////////////////////////////////////////////////////////////////////
//
function SetReadTimeout($timeout)
{
$this->rocketGateReadTimeout = $timeout;// Number of seconds
}
//////////////////////////////////////////////////////////////////////
//
// SetCurlCallback() - Set optional curl callback
// that will allow to manipulate
// with CURL instance before curl_exec().
//
//////////////////////////////////////////////////////////////////////
//
function SetCurlCallback($callback)
{
$this->curlCallback = $callback;
}
//////////////////////////////////////////////////////////////////////
//
// SetCurlResponseCallback() - Set optional curl response callback
// that will allow to manipulate
// with CURL instance after curl_exec().
//
//////////////////////////////////////////////////////////////////////
//
function SetCurlResponseCallback($callback)
{
$this->curlResponseCallback = $callback;
}
//////////////////////////////////////////////////////////////////////
//
// PerformTransaction() - Perform the transaction outlined
// in a GatewayRequest.
//
//////////////////////////////////////////////////////////////////////
//
function PerformTransaction(GatewayRequest $request, GatewayResponse $response)
{
//
// Check if an override is requested.
//
$fullURL = $request->Get("gatewayURL");
if ($fullURL == null) {
$fullURL = $request->Get("embeddedFieldsToken");
}
//
// If an override is in use, split it into
// its individual elements.
//
if ($fullURL != null) {// Overriding?
$urlBits = parse_url($fullURL);// Split the URL
if ($request->Get("gatewayServer") == null) {
$request->Set("gatewayServer", $urlBits['host']);
}
if (array_key_exists("scheme", $urlBits)) {
$request->Set("gatewayProtocol", $urlBits['scheme']);
}
if (array_key_exists("port", $urlBits)) {
$request->Set("gatewayPortNo", $urlBits['port']);
}
$request->Set("gatewayServlet", $urlBits['path'] . "?" . $urlBits['query']);
}
//
// If the request specifies a server name, use it.
// Otherwise, use the default for the service.
//
$serverName = $request->Get("gatewayServer");
if ($serverName == null) {
$serverName = $this->rocketGateHost;
}
//
// Clear any error tracking that may be leftover in
// a re-used request.
//
$request->Clear(GatewayRequest::FAILED_SERVER());
$request->Clear(GatewayRequest::FAILED_RESPONSE_CODE());
$request->Clear(GatewayRequest::FAILED_REASON_CODE());
$request->Clear(GatewayRequest::FAILED_GUID());
//
// Lookup the hostname in DNS.
//
if (strcmp($serverName, "gateway.rocketgate.com") != 0) {
$hostList = array();// Create an array
$hostList[0] = $serverName;// Use name directly
} else {
$hostList = gethostbynamel($serverName);// Lookup the hostname
if (!($hostList)) {// Lookup failed?
$hostList = array();// Create an array
$hostList[0] = "gateway-16.rocketgate.com";// Add default resolution
$hostList[1] = "gateway-17.rocketgate.com";
} else {
$index = 0;// Initialize index
$listSize = count($hostList);// Get element count
while ($index < $listSize) {// Loop over all entries
if (strcmp($hostList[$index], "69.20.127.91") == 0) {
$hostList[$index] = "gateway-16.rocketgate.com";
}
if (strcmp($hostList[$index], "72.32.126.131") == 0) {
$hostList[$index] = "gateway-17.rocketgate.com";
}
$index++;// Look at next in list
}
}
}
//
// Randomly select an end-point to use first.
//
if (($listSize = count($hostList)) > 1) {// More than one address?
$index = rand(0, ($listSize - 1));// Get random index
if ($index > 0) {// Want to swap?
$swapper = $hostList[0];// Save this one
$hostList[0] = $hostList[$index];// Put this one first
$hostList[$index] = $swapper;// And put this one here
}
}
//
// Loop over the hosts in the DNS entry. Try to send the
// transaction to each host until it finally succeeds. If it
// fails due to an unrecoverable system error, we must quit.
//
$index = 0; // Start with first entry
while ($index < $listSize) {// Loop over all entries
$results = $this->PerformCURLTransaction($hostList[$index],
$request,
$response);
if ($results == GatewayCodes::RESPONSE_SUCCESS) {
return true;
}
if ($results != GatewayCodes::RESPONSE_SYSTEM_ERROR) {
return false;
}
//
// Save any errors in the response so they can be
// transmitted along with the next request.
//
$request->Set(GatewayRequest::FAILED_SERVER(), $hostList[$index]);
$request->Set(GatewayRequest::FAILED_RESPONSE_CODE(),
$response->Get(GatewayResponse::RESPONSE_CODE()));
$request->Set(GatewayRequest::FAILED_REASON_CODE(),
$response->Get(GatewayResponse::REASON_CODE()));
$request->Set(GatewayRequest::FAILED_GUID(),
$response->Get(GatewayResponse::TRANSACT_ID()));
$index++; // Try next host in list
}
return false;// Transaction failed
}
//////////////////////////////////////////////////////////////////////
//
// PerformTargetedTransaction() - Send a transaction to a server
// based upon the reference GUID.
//
//////////////////////////////////////////////////////////////////////
//
function PerformTargetedTransaction(GatewayRequest $request, GatewayResponse $response)
{
//
// Clear any error tracking that may be leftover in
// a re-used request.
//
$request->Clear(GatewayRequest::FAILED_SERVER());
$request->Clear(GatewayRequest::FAILED_RESPONSE_CODE());
$request->Clear(GatewayRequest::FAILED_REASON_CODE());
$request->Clear(GatewayRequest::FAILED_GUID());
//
// Check if an override is requested.
//
$fullURL = $request->Get("gatewayURL");
if ($fullURL == null) {
$fullURL = $request->Get("embeddedFieldsToken");
}
//
// If an override is in use, split it into
// its individual elements.
//
if ($fullURL != null) {// Overriding?
$urlBits = parse_url($fullURL);// Split the URL
if ($request->Get("gatewayServer") == null) {
$request->Set("gatewayServer", $urlBits['host']);
}
if (array_key_exists("scheme", $urlBits)) {
$request->Set("gatewayProtocol", $urlBits['scheme']);
}
if (array_key_exists("port", $urlBits)) {
$request->Set("gatewayPortNo", $urlBits['port']);
}
$request->Set("gatewayServlet", $urlBits['path'] . "?" . $urlBits['query']);
}
//
// This transaction must go to the host that processed a
// previous referenced transaction. Get the GUID of the
// reference transaction.
//
$referenceGUID = $request->Get(GatewayRequest::REFERENCE_GUID());
if ($referenceGUID == null) {// Don't have reference?
$response->SetResults(GatewayCodes::RESPONSE_REQUEST_ERROR,
GatewayCodes::REASON_INVALID_REFGUID);
return false;// Transaction failed
}
//
// Strip off the bits that indicate which server should
// be used.
//
if (strlen($referenceGUID) > 15) {// Server 16 and above?
$siteNo = substr($referenceGUID, 0, 2);// Get first two digits
} else {
$siteNo = substr($referenceGUID, 0, 1);// Get first digit only
}
$siteNo = hexdec($siteNo);// Convert to decimal
//
// Build the hostname to which the transaction should
// be directed.
//
$serverName = $request->Get("gatewayServer");
if ($serverName == null) {// Was server specified?
$serverName = $this->rocketGateHost;// No - Use default
if (($separator = strpos($serverName, ".")) > 0) {
$prefix = substr($serverName, 0, $separator);
$serverName = substr($serverName, $separator);
$serverName = $prefix . "-" . $siteNo . $serverName;
}
}
//
// Send the transaction to the named host.
//
$results = $this->PerformCURLTransaction($serverName, $request, $response);
if ($results == GatewayCodes::RESPONSE_SUCCESS) {
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////
//
// PerformConfirmation() - Perform the confirmation pass that
// tells the server we have received
// transaction reply.
//
//////////////////////////////////////////////////////////////////////
//
function PerformConfirmation(GatewayRequest $request, GatewayResponse $response)
{
//
// Verify that we have a transaction ID for the confirmation
// message.
//
$confirmGUID = $response->Get(GatewayResponse::TRANSACT_ID());
if ($confirmGUID == null) {// Don't have reference?
$response->Set(GatewayResponse::EXCEPTION(),
"BUG-CHECK - Missing confirmation GUID");
$response->SetResults(GatewayCodes::RESPONSE_SYSTEM_ERROR,
GatewayCodes::REASON_BUGCHECK);
return false;// Transaction failed
}
//
// Add the GUID to the request and send it back to the
// original server for confirmation.
//
$confirmResponse = new GatewayResponse();// Need a new response object
$request->Set(GatewayRequest::TRANSACTION_TYPE(), "CC_CONFIRM");
$request->Set(GatewayRequest::REFERENCE_GUID(), $confirmGUID);
if ($this->PerformTargetedTransaction($request, $confirmResponse)) {
return true;
}
//////////////////////////////////////////////////////////////////////
//
// 12-21-2011 darcy
//
// If we experienced a system error, retry the confirmation.
//
if ($confirmResponse->Get(GatewayResponse::RESPONSE_CODE()) == GatewayCodes::RESPONSE_SYSTEM_ERROR) {
sleep(2); // Short delay
if ($this->PerformTargetedTransaction($request, $confirmResponse)) {
return true;
}
}
//
//////////////////////////////////////////////////////////////////////
//
// If the confirmation failed, copy the reason and response code
// into the original response object to override the success.
//
$response->SetResults(
$confirmResponse->Get(GatewayResponse::RESPONSE_CODE()),
$confirmResponse->Get(GatewayResponse::REASON_CODE()));
$response->Set(GatewayResponse::EXCEPTION(),
$confirmResponse->Get(GatewayResponse::EXCEPTION()));
return false;// And quit
}
//////////////////////////////////////////////////////////////////////
//
// PerformCURLTransaction() - Perform a transaction exchange
// with a given host.
//
//////////////////////////////////////////////////////////////////////
//
function PerformCURLTransaction($host, GatewayRequest $request, GatewayResponse $response)
{
$results_headers = [];
//
// Reset the response object and turn the request into
// a string that can be transmitted.
//
$response->Reset();// Clear old contents
//
// indicate in version that a user function has been used
//
if (is_callable($this->curlCallback) || is_callable($this->curlResponseCallback)) {
$request->Set(GatewayRequest::VERSION_INDICATOR(),
GatewayChecksum::$versionNo. "c");
}
$requestBytes = $request->ToXMLString();// Change to XML request
//
// Gather override attibutes used for the connection URL.
//
$urlServlet = $request->Get("gatewayServlet");
$urlProtocol = $request->Get("gatewayProtocol");
$urlPortNo = $request->Get("gatewayPortNo");
//
// If the parameters were not set in the request,
// use the system defaults.
//
if ($urlServlet == null) {
$urlServlet = $this->rocketGateServlet;
}
if ($urlProtocol == null) {
$urlProtocol = $this->rocketGateProtocol;
}
if ($urlPortNo == null) {
$urlPortNo = $this->rocketGatePortNo;
}
//
// Build the URL for the gateway service.
//
$url = $urlProtocol . "://"// Start with protocol
. $host . ":"// Add the host
. $urlPortNo . "/"// Add the port number
. $urlServlet;// Add servlet path
//
// Gather the override timeout values that will be used
// for the connection.
//
$connectTimeout = $request->Get("gatewayConnectTimeout");
$readTimeout = $request->Get("gatewayReadTimeout");
//
// Use default values if the parameters were not set.
//
if ($connectTimeout == null)// No connect timeout specified?
{
$connectTimeout = $this->rocketGateConnectTimeout;
}
if ($readTimeout == null) {
$readTimeout = $this->rocketGateReadTimeout;
}
//
// Create a handle that can be used for the URL operation.
//
if (!($handle = curl_init())) {// Failed to initialize?
$response->Set(GatewayResponse::EXCEPTION(), "curl_init() error");
$response->SetResults(GatewayCodes::RESPONSE_REQUEST_ERROR,
GatewayCodes::REASON_INVALID_URL);
return GatewayCodes::RESPONSE_REQUEST_ERROR;
}
//
// Set timeout values used in the operation.
//
curl_setopt($handle, CURLOPT_NOSIGNAL, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
curl_setopt($handle, CURLOPT_TIMEOUT, $readTimeout);
//////////////////////////////////////////////////////////////////////
//
// 03-24-2015 darcy
//
// Remove SSL override.
//
////
//// Setup verification for SSL connections.
////
// curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, FALSE);
//
//////////////////////////////////////////////////////////////////////
//
// Setup the call to the URL.
//
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $requestBytes);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_FAILONERROR, true);
//////////////////////////////////////////////////////////////////////
//
// 04-30-2013 darcy
//
// Updated user agent.
//
// 12-20-2011 darcy
//
// Updated user agent.
//
// 08-25-2011 darcy
//
// Updated user agent.
//
// 05-31-2009 darcy
//
// Updated the user agent.
//
// 04-27-2009 darcy
//
// Set the user-agent.
//
// curl_setopt($handle, CURLOPT_USERAGENT, "RG PHP Client 2.0");
// curl_setopt($handle, CURLOPT_USERAGENT, "RG PHP Client 2.1");
// curl_setopt($handle, CURLOPT_USERAGENT, "RG PHP Client 3.0");
//
// 12-11-2017 Jason Set the user-agent dynamically
curl_setopt($handle, CURLOPT_USERAGENT, "RG PHP Client " . GatewayChecksum::$versionNo);
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//
// 2/21/2010 Jason. Set content-type.
//
curl_setopt($handle, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
//////////////////////////////////////////////////////////////////////
//
// Apply optional curl callback if available
//
if (is_callable($this->curlCallback)) {
$handle = call_user_func($this->curlCallback, $handle);
curl_setopt($handle, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$results_headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$results_headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
}
//
// Execute the operation.
//
$results = curl_exec($handle);// Execute the operation
//
// Apply optional curlResponseCallback if available
//
if (is_callable($this->curlResponseCallback)) {
call_user_func($this->curlResponseCallback, $handle, $request, $response, $results_headers, $results);
}
if (!($results)) {// Did it fail?
$errorCode = curl_errno($handle);// Get the error code
if (!$errorCode) {
$this->rocketGateLatestConnectionTime = curl_getinfo($handle, CURLINFO_CONNECT_TIME);
$this->rocketGateLatestExecutionTime = curl_getinfo($handle, CURLINFO_TOTAL_TIME);
$this->rocketGateLatestResponseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
}
$errorString = curl_error($handle);// Get the error text
curl_close($handle);// Done with handle
//
// Translate the CURL error code into a Gateway code.
//
switch ($errorCode) {// Classify error code
case CURLE_SSL_CONNECT_ERROR:// Connection failures
case CURLE_COULDNT_CONNECT:
$internalCode = GatewayCodes::REASON_UNABLE_TO_CONNECT;
break;// Done with request
case CURLE_SEND_ERROR:// Failed sending data
$internalCode = GatewayCodes::REASON_REQUEST_XMIT_ERROR;
break;// Done with request
case CURLE_OPERATION_TIMEOUTED:// Time-out reached
$internalCode = GatewayCodes::REASON_RESPONSE_READ_TIMEOUT;
break;// Done with request
case CURLE_RECV_ERROR:// Failed reading data
case CURLE_READ_ERROR:
default:
$internalCode = GatewayCodes::REASON_RESPONSE_READ_ERROR;
}
//
// If the operation failed, return an error code.
//
if (strlen($errorString) != 0)// Have an error?
{
$response->Set(GatewayResponse::EXCEPTION(), $errorString);
}
$response->SetResults(GatewayCodes::RESPONSE_SYSTEM_ERROR,
$internalCode);
return GatewayCodes::RESPONSE_SYSTEM_ERROR;
}
//
// Parse the returned message into the response
// object.
//
curl_close($handle);// Done with handle
$response->SetFromXML($results);// Set response
return $response->Get(GatewayResponse::RESPONSE_CODE());
}
//////////////////////////////////////////////////////////////////////
//
// GetLatestResponseCode() - Get response code from latest executed
// request
//
//////////////////////////////////////////////////////////////////////
//
public function GetLatestResponseCode()
{
return $this->rocketGateLatestResponseCode;
}
//////////////////////////////////////////////////////////////////////
//
// GetLatestExecutionTime() - Get latest request execution time
//
//////////////////////////////////////////////////////////////////////
//
public function GetLatestExecutionTime()
{
return $this->rocketGateLatestExecutionTime;
}
//////////////////////////////////////////////////////////////////////
//
// GetLatestConnectionTime() - Get latest request connection time
//
//////////////////////////////////////////////////////////////////////
//
function GetLatestConnectionTime()
{
return $this->rocketGateLatestConnectionTime;
}
}