Issues (27)

src/Traits/ChargifyHttpClient.php (1 issue)

1
<?php
2
3
namespace Srmklive\Chargify\Traits;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\ClientException as HttpClientException;
7
use Psr\Http\Message\StreamInterface;
8
use RuntimeException;
9
10
trait ChargifyHttpClient
11
{
12
    /**
13
     * Http Client class object.
14
     *
15
     * @var HttpClient
16
     */
17
    private $client;
18
19
    /**
20
     * Http Client configuration.
21
     *
22
     * @var array
23
     */
24
    private $httpClientConfig;
25
26
    /**
27
     * Chargify API URL.
28
     *
29
     * @var string
30
     */
31
    private $apiUrl;
32
33
    /**
34
     * Chargify API Endpoint.
35
     *
36
     * @var string
37
     */
38
    private $apiEndPoint;
39
40
    /**
41
     * Http Client request body parameter name.
42
     *
43
     * @var string
44
     */
45
    private $httpBodyParam;
46
47
    /**
48
     * Request type.
49
     *
50
     * @var string
51
     */
52
    protected $verb = 'post';
53
54
    /**
55
     * Validate SSL details when creating HTTP client.
56
     *
57
     * @var bool
58
     */
59
    protected $validateSSL;
60
61
    /**
62
     * Set curl constants if not defined.
63
     *
64
     * @return void
65
     */
66
    protected function setCurlConstants(): void
67
    {
68
        $constants = [
69
            'CURLOPT_SSLVERSION'        => 32,
70
            'CURL_SSLVERSION_TLSv1_2'   => 6,
71
            'CURLOPT_SSL_VERIFYPEER'    => 64,
72
            'CURLOPT_SSLCERT'           => 10025,
73
        ];
74
75
        foreach ($constants as $key => $value) {
76
            if (!defined($key)) {
77
                define($key, $constants[$key]);
78
            }
79
        }
80
    }
81
82
    /**
83
     * Function to initialize/override Http Client.
84
     *
85
     * @param \GuzzleHttp\Client|null $client
86
     *
87
     * @return void
88
     */
89
    public function setClient($client = null): void
90
    {
91
        if ($client instanceof HttpClient) {
92
            $this->client = $client;
93
94
            return;
95
        }
96
97
        $this->client = new HttpClient([
98
            'curl' => $this->httpClientConfig,
99
        ]);
100
    }
101
102
    /**
103
     * Function to set Http Client configuration.
104
     *
105
     * @return void
106
     */
107
    protected function setHttpClientConfiguration(): void
108
    {
109
        $this->setCurlConstants();
110
111
        $this->httpClientConfig = [
112
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
113
            CURLOPT_SSL_VERIFYPEER => $this->validateSSL,
114
        ];
115
116
        // Initialize Http Client
117
        $this->setClient();
118
    }
119
120
    /**
121
     * Perform Chargify API request & return response.
122
     *
123
     * @throws \Throwable
124
     *
125
     * @return StreamInterface
126
     */
127
    private function makeHttpRequest(): \Psr\Http\Message\StreamInterface
128
    {
129
        try {
130
            $this->options['headers'] = [
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
131
                'Content-Type'  => 'application/json',
132
                'Authorization' => 'Basic '.base64_encode($this->config['api_key']),
133
            ];
134
135
            return $this->client->{$this->verb}(
136
                "{$this->apiUrl}{$this->apiEndPoint}",
137
                $this->options
138
            )->getBody();
139
        } catch (HttpClientException $e) {
140
            throw new RuntimeException($e->getRequest()->getBody().' '.$e->getResponse()->getBody());
141
        }
142
    }
143
144
    /**
145
     * Function To Perform Chargify API Request.
146
     *
147
     * @param bool $decode
148
     *
149
     * @throws \Throwable
150
     *
151
     * @return array|StreamInterface|string
152
     */
153
    private function doChargifyRequest($decode = true)
154
    {
155
        try {
156
            // Perform PayPal HTTP API request.
157
            $response = $this->makeHttpRequest();
158
159
            return ($decode === false) ? $response->getContents() : \GuzzleHttp\json_decode($response, true);
160
        } catch (RuntimeException $t) {
161
            $message = collect($t->getMessage())->implode('\n');
162
        }
163
164
        return [
165
            'type'    => 'error',
166
            'message' => $message,
167
        ];
168
    }
169
}
170