Http.php
9.16 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
<?php
namespace Braintree;
use finfo;
/**
* Braintree HTTP Client
* processes Http requests using curl
*/
class Http
{
protected $_config;
private $_useClientCredentials = false;
public function __construct($config)
{
$this->_config = $config;
}
public function delete($path, $params = null)
{
$response = $this->_doRequest('DELETE', $path, $this->_buildXml($params));
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 204) {
return true;
} else if ($responseCode === 422) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($response['status']);
}
}
public function get($path)
{
$response = $this->_doRequest('GET', $path);
if ($response['status'] === 200) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($response['status']);
}
}
public function post($path, $params = null)
{
$response = $this->_doRequest('POST', $path, $this->_buildXml($params));
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($responseCode);
}
}
public function postMultipart($path, $params, $file)
{
$headers = [
'User-Agent: Braintree PHP Library ' . Version::get(),
'X-ApiVersion: ' . Configuration::API_VERSION
];
$response = $this->_doRequest('POST', $path, $params, $file, $headers);
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($responseCode);
}
}
public function put($path, $params = null)
{
$response = $this->_doRequest('PUT', $path, $this->_buildXml($params));
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($responseCode);
}
}
private function _buildXml($params)
{
return empty($params) ? null : Xml::buildXmlFromArray($params);
}
private function _getHeaders()
{
return [
'Accept: application/xml',
];
}
private function _getAuthorization()
{
if ($this->_useClientCredentials) {
return [
'user' => $this->_config->getClientId(),
'password' => $this->_config->getClientSecret(),
];
} else if ($this->_config->isAccessToken()) {
return [
'token' => $this->_config->getAccessToken(),
];
} else {
return [
'user' => $this->_config->getPublicKey(),
'password' => $this->_config->getPrivateKey(),
];
}
}
public function useClientCredentials()
{
$this->_useClientCredentials = true;
}
private function _doRequest($httpVerb, $path, $requestBody = null, $file = null, $headers = null)
{
return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . $path, $requestBody, $file, $headers);
}
public function _doUrlRequest($httpVerb, $url, $requestBody = null, $file = null, $customHeaders = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, $this->_config->timeout());
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpVerb);
curl_setopt($curl, CURLOPT_URL, $url);
if ($this->_config->acceptGzipEncoding()) {
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
}
if ($this->_config->sslVersion()) {
curl_setopt($curl, CURLOPT_SSLVERSION, $this->_config->sslVersion());
}
$headers = [];
if ($customHeaders) {
$headers = $customHeaders;
} else {
$headers = $this->_getHeaders($curl);
$headers[] = 'User-Agent: Braintree PHP Library ' . Version::get();
$headers[] = 'X-ApiVersion: ' . Configuration::API_VERSION;
$headers[] = 'Content-Type: application/xml';
}
$authorization = $this->_getAuthorization();
if (isset($authorization['user'])) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $authorization['user'] . ':' . $authorization['password']);
} else if (isset($authorization['token'])) {
$headers[] = 'Authorization: Bearer ' . $authorization['token'];
}
if ($this->_config->sslOn()) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, $this->getCaFile());
}
if (!empty($file)) {
$boundary = "---------------------" . md5(mt_rand() . microtime());
$headers[] = "Content-Type: multipart/form-data; boundary={$boundary}";
$this->prepareMultipart($curl, $requestBody, $file, $boundary);
} else if (!empty($requestBody)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
}
if ($this->_config->isUsingProxy()) {
$proxyHost = $this->_config->getProxyHost();
$proxyPort = $this->_config->getProxyPort();
$proxyType = $this->_config->getProxyType();
$proxyUser = $this->_config->getProxyUser();
$proxyPwd= $this->_config->getProxyPassword();
curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
if (!empty($proxyType)) {
curl_setopt($curl, CURLOPT_PROXYTYPE, $proxyType);
}
if ($this->_config->isAuthenticatedProxy()) {
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPwd);
}
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error_code = curl_errno($curl);
$error = curl_error($curl);
if ($error_code == 28 && $httpStatus == 0) {
throw new Exception\Timeout();
}
curl_close($curl);
if ($this->_config->sslOn()) {
if ($httpStatus == 0) {
throw new Exception\SSLCertificate($error, $error_code);
}
} else if ($error_code) {
throw new Exception\Connection($error, $error_code);
}
return ['status' => $httpStatus, 'body' => $response];
}
function prepareMultipart($ch, $requestBody, $file, $boundary) {
$disallow = ["\0", "\"", "\r", "\n"];
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
$filePath = stream_get_meta_data($file)['uri'];
$data = file_get_contents($filePath);
$mimeType = $fileInfo->buffer($data);
// build normal parameters
foreach ($requestBody as $k => $v) {
$k = str_replace($disallow, "_", $k);
$body[] = implode("\r\n", [
"Content-Disposition: form-data; name=\"{$k}\"",
"",
filter_var($v),
]);
}
// build file parameter
$splitFilePath = explode(DIRECTORY_SEPARATOR, $filePath);
$filePath = end($splitFilePath);
$filePath = str_replace($disallow, "_", $filePath);
$body[] = implode("\r\n", [
"Content-Disposition: form-data; name=\"file\"; filename=\"{$filePath}\"",
"Content-Type: {$mimeType}",
"",
$data,
]);
// add boundary for each parameters
array_walk($body, function (&$part) use ($boundary) {
$part = "--{$boundary}\r\n{$part}";
});
// add final boundary
$body[] = "--{$boundary}--";
$body[] = "";
// set options
return curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => implode("\r\n", $body)
]);
}
private function getCaFile()
{
static $memo;
if ($memo === null) {
$caFile = $this->_config->caFile();
if (substr($caFile, 0, 7) !== 'phar://') {
return $caFile;
}
$extractedCaFile = sys_get_temp_dir() . '/api_braintreegateway_com.ca.crt';
if (!file_exists($extractedCaFile) || sha1_file($extractedCaFile) != sha1_file($caFile)) {
if (!copy($caFile, $extractedCaFile)) {
throw new Exception\SSLCaFileNotFound();
}
}
$memo = $extractedCaFile;
}
return $memo;
}
}
class_alias('Braintree\Http', 'Braintree_Http');