1
|
|
|
<?php |
2
|
|
|
namespace net\authorize\util; |
3
|
|
|
|
4
|
|
|
use net\authorize\util\LogFactory; |
5
|
|
|
use net\authorize\util\Log; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A class to send a request to the XML API. |
9
|
|
|
* |
10
|
|
|
* @package AuthorizeNet |
11
|
|
|
* @subpackage net\authorize\util |
12
|
|
|
*/ |
13
|
|
|
class HttpClient |
14
|
|
|
{ |
15
|
|
|
private $_Url = ""; |
16
|
|
|
|
17
|
|
|
public $VERIFY_PEER = true; // attempt trust validation of SSL certificates when establishing secure connections. |
18
|
|
|
private $logger = NULL; |
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
8 |
|
{ |
25
|
|
|
$this->logger = LogFactory::getLog(get_class($this)); |
26
|
8 |
|
} |
27
|
8 |
|
|
28
|
8 |
|
/** |
29
|
8 |
|
* Set a log file. |
30
|
|
|
* |
31
|
|
|
* @param string $endPoint end point to hit from \net\authorize\api\constants\ANetEnvironment |
32
|
|
|
*/ |
33
|
|
|
public function setPostUrl( $endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM) |
34
|
|
|
{ |
35
|
|
|
$this->_Url = sprintf( "%s/xml/v1/request.api", $endPoint); |
36
|
8 |
|
} |
37
|
|
|
|
38
|
8 |
|
/** |
39
|
8 |
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function _getPostUrl() |
42
|
|
|
{ |
43
|
|
|
//return (self::URL); |
|
|
|
|
44
|
8 |
|
return ($this->_Url); |
45
|
|
|
} |
46
|
|
|
|
47
|
8 |
|
/** |
48
|
|
|
* Set a log file. |
49
|
|
|
* |
50
|
|
|
* @param string $filepath Path to log file. |
51
|
|
|
*/ |
52
|
|
|
public function setLogFile($filepath) |
53
|
|
|
{ |
54
|
|
|
$this->logger->setLogFile($filepath); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Posts the request to AuthorizeNet endpoint using Curl & returns response. |
59
|
|
|
* |
60
|
|
|
* @param string $xmlRequest |
61
|
|
|
* @return string $xmlResponse The response. |
62
|
|
|
*/ |
63
|
|
|
public function _sendRequest($xmlRequest) |
64
|
|
|
{ |
65
|
|
|
$xmlResponse = ""; |
66
|
8 |
|
|
67
|
|
|
$post_url = $this->_getPostUrl(); |
68
|
8 |
|
$curl_request = curl_init($post_url); |
69
|
|
|
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $xmlRequest); |
70
|
8 |
|
curl_setopt($curl_request, CURLOPT_HEADER, 0); |
71
|
8 |
|
curl_setopt($curl_request, CURLOPT_TIMEOUT, 45); |
72
|
8 |
|
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); |
73
|
8 |
|
curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 2); |
74
|
8 |
|
|
75
|
8 |
|
$this->logger->info(sprintf(" Url: %s", $post_url)); |
76
|
8 |
|
// Do not log requests that could contain CC info. |
77
|
|
|
$this->logger->info(sprintf("Request to AnetApi: \n%s", $xmlRequest)); |
78
|
8 |
|
|
79
|
|
|
if ($this->VERIFY_PEER) { |
80
|
8 |
|
curl_setopt($curl_request, CURLOPT_CAINFO, dirname(dirname(__FILE__)) . '/../../ssl/cert.pem'); |
81
|
|
|
} else { |
82
|
8 |
|
$this->logger->error("Invalid SSL option for the request"); |
83
|
8 |
|
return false; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (preg_match('/xml/',$post_url)) { |
87
|
|
|
curl_setopt($curl_request, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); |
88
|
|
|
// file_put_contents($this->_log_file, "\nSending 'XML' Request type", FILE_APPEND); |
|
|
|
|
89
|
8 |
|
$this->logger->info("Sending 'XML' Request type"); |
90
|
8 |
|
} |
91
|
|
|
|
92
|
8 |
|
try |
93
|
|
|
{ |
94
|
|
|
$this->logger->info("Sending http request via Curl"); |
95
|
|
|
$xmlResponse = curl_exec($curl_request); |
96
|
|
|
$this->logger->info("Response from AnetApi: $xmlResponse"); |
97
|
8 |
|
|
98
|
8 |
|
} catch (\Exception $ex) |
99
|
8 |
|
{ |
100
|
|
|
$errorMessage = sprintf("\n%s:Error making http request via curl: Code:'%s', Message:'%s', Trace:'%s', File:'%s':'%s'", |
101
|
|
|
$this->now(), $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), $ex->getFile(), $ex->getLine() ); |
102
|
|
|
$this->logger->error($errorMessage); |
103
|
|
|
} |
104
|
|
|
if ($this->logger && $this->logger->getLogFile()) { |
105
|
|
|
if ($curl_error = curl_error($curl_request)) { |
106
|
|
|
$this->logger->error("CURL ERROR: $curl_error"); |
107
|
8 |
|
} |
108
|
8 |
|
|
109
|
|
|
} |
110
|
|
|
curl_close($curl_request); |
111
|
|
|
|
112
|
|
|
return $xmlResponse; |
113
|
8 |
|
} |
114
|
|
|
|
115
|
8 |
|
private function now() |
116
|
|
|
{ |
117
|
|
|
return date( DATE_RFC2822); |
118
|
|
|
} |
119
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.