PHP 7.4.33
Preview: Http.php Size: 6.66 KB
/home/godevadmin/www/admin/braintree/final/braintree/lib/Braintree/Http.php

<?php
namespace Braintree;

/**
 * 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)
    {
        $response = $this->_doRequest('DELETE', $path);
        if ($response['status'] === 200) {
            return true;
        } 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 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',
            'Content-Type: 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)
    {
        return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . $path, $requestBody);
    }

    public function _doUrlRequest($httpVerb, $url, $requestBody = 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 = $this->_getHeaders($curl);
        $headers[] = 'User-Agent: Braintree PHP Library ' . Version::get();
        $headers[] = 'X-ApiVersion: ' . Configuration::API_VERSION;

        $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'];
        }
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        // curl_setopt($curl, CURLOPT_VERBOSE, true);
        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($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_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];
    }

    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');

Directory Contents

Dirs: 9 × Files: 97

Name Size Perms Modified Actions
Dispute DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Error DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Exception DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Result DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Test DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Xml DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.06 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.37 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
579 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1005 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.04 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
9.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.28 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.59 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.73 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.63 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.38 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.77 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.40 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
16.01 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
818 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
813 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.28 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
9.71 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
15.33 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.34 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.32 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.52 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
12.78 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
20.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.64 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
134 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.62 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.45 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
693 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
462 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
775 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.53 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
259 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.72 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
244 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
696 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.93 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
6.66 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.44 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.40 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.93 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.84 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
431 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
432 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.36 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
988 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
5.80 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.34 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
553 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
946 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.01 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
776 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.95 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
755 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.59 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
691 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
430 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
950 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
671 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.00 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
11.90 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.30 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.58 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.70 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
5.22 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.33 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
854 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
822 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.49 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
655 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.11 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.88 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
486 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.00 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
7.14 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.60 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.46 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
269 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
690 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
20.21 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
17.71 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
7.79 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.08 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
9.06 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.59 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.46 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.94 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
14.89 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.95 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
625 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
5.00 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.34 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.27 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
14.69 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
697 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).