1
1
<?php
2
2
namespace net \authorize \util ;
3
3
4
- /**
5
- * Easily interact with the Authorize.Net XML API.
6
- *
7
- * @package AuthorizeNet
8
- * @subpackage AuthorizeNetARB
9
- * @link http://www.authorize.net/support/ARB_guide.pdf ARB Guide
10
- */
4
+ //use \net\authorize\api\constants\ANetEnvironment;
11
5
12
6
/**
13
7
* A class to send a request to the XML API.
17
11
*/
18
12
class HttpClient
19
13
{
20
- const URL = "https://apitest.authorize.net/xml/v1/request.api " ;
21
-
14
+ // const URL = "https://apitest.authorize.net/xml/v1/request.api";
15
+ private $ _Url = "" ;
22
16
23
17
public $ VERIFY_PEER = true ; // attempt trust validation of SSL certificates when establishing secure connections.
24
18
private $ _log_file = false ;
25
19
26
20
/**
27
21
* Constructor.
28
22
*
29
- * @param string $api_login_id The Merchant's API Login ID.
30
- * @param string $transaction_key The Merchant's Transaction Key.
31
23
*/
32
- public function __construct ()//$api_login_id = false, $transaction_key = false)
24
+ public function __construct ()
33
25
{
34
- //$this->_api_login = ($api_login_id ? $api_login_id : (defined('AUTHORIZENET_API_LOGIN_ID') ? AUTHORIZENET_API_LOGIN_ID : ""));
35
- //$this->_transaction_key = ($transaction_key ? $transaction_key : (defined('AUTHORIZENET_TRANSACTION_KEY') ? AUTHORIZENET_TRANSACTION_KEY : ""));
36
- //$this->_sandbox = (defined('AUTHORIZENET_SANDBOX') ? AUTHORIZENET_SANDBOX : true);
37
- //$this->_log_file = (defined('AUTHORIZENET_LOG_FILE') ? AUTHORIZENET_LOG_FILE : false);
38
26
$ this ->_log_file = AUTHORIZENET_LOG_FILE ;
27
+ date_default_timezone_set ('UTC ' );
39
28
}
40
29
41
- /*
42
- public function cancelSubscription($subscriptionId)
43
- {
44
- $this->_request_type = "CancelSubscriptionRequest";
45
- $this->_request_payload .= "<subscriptionId>$subscriptionId</subscriptionId>";
46
- return $this->_sendRequest();
47
- }
48
- */
30
+ /**
31
+ * Set a log file.
32
+ *
33
+ * @param string $endPoint end point to hit from \net\authorize\api\constants\ANetEnvironment
34
+ */
35
+ public function setPostUrl ( $ endPoint = \net \authorize \api \constants \ANetEnvironment::CUSTOM )
36
+ {
37
+ $ this ->_Url = sprintf ( "%s/xml/v1/request.api " , $ endPoint );
38
+ }
49
39
50
40
/**
51
41
* @return string
52
42
*/
53
43
public function _getPostUrl ()
54
44
{
55
- return (self ::URL );
45
+ //return (self::URL);
46
+ return ($ this ->_Url );
56
47
}
57
48
58
49
/**
@@ -66,12 +57,15 @@ public function setLogFile($filepath)
66
57
}
67
58
68
59
/**
69
- * Posts the request to AuthorizeNet & returns response.
60
+ * Posts the request to AuthorizeNet endpoint using Curl & returns response.
70
61
*
71
- * @return $xmlResponse The response.
62
+ * @param $xmlRequest
63
+ * @return bool|mixed $xmlResponse The response.
72
64
*/
73
65
public function _sendRequest ($ xmlRequest )
74
66
{
67
+ $ xmlResponse = "" ;
68
+
75
69
$ post_url = $ this ->_getPostUrl ();
76
70
$ curl_request = curl_init ($ post_url );
77
71
curl_setopt ($ curl_request , CURLOPT_POSTFIELDS , $ xmlRequest );
@@ -80,37 +74,48 @@ public function _sendRequest($xmlRequest)
80
74
curl_setopt ($ curl_request , CURLOPT_RETURNTRANSFER , 1 );
81
75
curl_setopt ($ curl_request , CURLOPT_SSL_VERIFYHOST , 2 );
82
76
83
- file_put_contents ($ this ->_log_file , sprintf ("Url: %s " , $ post_url ), FILE_APPEND );
77
+ file_put_contents ($ this ->_log_file , sprintf ("\n %s: Url: %s " , $ this -> now () , $ post_url ), FILE_APPEND );
84
78
// Do not log requests that could contain CC info.
85
- file_put_contents ($ this ->_log_file , sprintf ("Request: %s " , $ xmlRequest ), FILE_APPEND );
79
+ file_put_contents ($ this ->_log_file , sprintf ("\n %s: Request to AnetApi: \n %s " , $ this -> now () , $ xmlRequest ), FILE_APPEND );
86
80
87
81
if ($ this ->VERIFY_PEER ) {
88
82
curl_setopt ($ curl_request , CURLOPT_CAINFO , dirname (dirname (__FILE__ )) . '../../../ssl/cert.pem ' );//..\..\..\ssl\cert.pem
89
83
} else {
90
84
if ($ this ->_log_file ) {
91
- file_put_contents ($ this ->_log_file , "----Request---- \nInvalid SSL option \n " , FILE_APPEND );
85
+ file_put_contents ($ this ->_log_file , "\nInvalid SSL option for the request " , FILE_APPEND );
92
86
}
93
87
return false ;
94
88
}
95
89
96
90
if (preg_match ('/xml/ ' ,$ post_url )) {
97
91
curl_setopt ($ curl_request , CURLOPT_HTTPHEADER , Array ("Content-Type: text/xml " ));
98
- file_put_contents ($ this ->_log_file , "Sending XML Request type " , FILE_APPEND );
92
+ file_put_contents ($ this ->_log_file , "\n Sending ' XML' Request type " , FILE_APPEND );
99
93
}
100
94
101
- $ xmlResponse = curl_exec ($ curl_request );
102
- file_put_contents ($ this ->_log_file , sprintf ("Response: %s " , $ xmlResponse ), FILE_APPEND );
103
-
95
+ try
96
+ {
97
+ file_put_contents ($ this ->_log_file , sprintf ("\n%s:Sending http request via Curl " , $ this ->now ()), FILE_APPEND );
98
+ $ xmlResponse = curl_exec ($ curl_request );
99
+ file_put_contents ($ this ->_log_file , sprintf ("\n%s:Response from AnetApi: \n%s \n" , $ this ->now (), $ xmlResponse ), FILE_APPEND );
100
+ } catch (\Exception $ ex )
101
+ {
102
+ $ errorMessage = sprintf ("\n%s:Error making http request via curl: Code:'%s', Message:'%s', Trace:'%s', File:'%s':'%s' " ,
103
+ $ this ->now (), $ ex ->getCode (), $ ex ->getMessage (), $ ex ->getTraceAsString (), $ ex ->getFile (), $ ex ->getLine () );
104
+ file_put_contents ($ this ->_log_file , $ errorMessage , FILE_APPEND );
105
+ }
104
106
if ($ this ->_log_file ) {
105
-
106
107
if ($ curl_error = curl_error ($ curl_request )) {
107
- file_put_contents ($ this ->_log_file , " ---- CURL ERROR---- \n $ curl_error\n\n" , FILE_APPEND );
108
+ file_put_contents ($ this ->_log_file , sprintf ( "\n %s: CURL ERROR: %s " , $ this -> now (), $ curl_error) , FILE_APPEND );
108
109
}
109
110
110
111
}
111
112
curl_close ($ curl_request );
112
113
113
- //return $this->_handleResponse($response);
114
114
return $ xmlResponse ;
115
115
}
116
+
117
+ private function now ()
118
+ {
119
+ return date ( DATE_RFC2822 );
120
+ }
116
121
}
0 commit comments