Passed
Push — v3.0 ( b76c53...a45102 )
by Raza
07:40
created

PayPalHttpClient::setCurlConstants()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 4
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\ClientException as HttpClientException;
7
use GuzzleHttp\Utils;
8
use Psr\Http\Message\StreamInterface;
9
use RuntimeException;
10
11
trait PayPalHttpClient
12
{
13
    /**
14
     * Http Client class object.
15
     *
16
     * @var HttpClient
17
     */
18
    private $client;
19
20
    /**
21
     * Http Client configuration.
22
     *
23
     * @var array
24
     */
25
    private $httpClientConfig;
26
27
    /**
28
     * PayPal API Endpoint.
29
     *
30
     * @var string
31
     */
32
    private $apiUrl;
33
34
    /**
35
     * PayPal API Endpoint.
36
     *
37
     * @var string
38
     */
39
    private $apiEndPoint;
40
41
    /**
42
     * IPN notification url for PayPal.
43
     *
44
     * @var string
45
     */
46
    private $notifyUrl;
47
48
    /**
49
     * Http Client request body parameter name.
50
     *
51
     * @var string
52
     */
53
    private $httpBodyParam;
54
55
    /**
56
     * Default payment action for PayPal.
57
     *
58
     * @var string
59
     */
60
    private $paymentAction;
61
62
    /**
63
     * Default locale for PayPal.
64
     *
65
     * @var string
66
     */
67
    private $locale;
68
69
    /**
70
     * Validate SSL details when creating HTTP client.
71
     *
72
     * @var bool
73
     */
74
    private $validateSSL;
75
76
    /**
77
     * Request type.
78
     *
79
     * @var string
80
     */
81
    protected $verb = 'post';
82
83
    /**
84
     * Set curl constants if not defined.
85
     *
86
     * @return void
87
     */
88
    protected function setCurlConstants()
89
    {
90
        $constants = [
91
            'CURLOPT_SSLVERSION'        => 32,
92
            'CURL_SSLVERSION_TLSv1_2'   => 6,
93
            'CURLOPT_SSL_VERIFYPEER'    => 64,
94
            'CURLOPT_SSLCERT'           => 10025,
95
        ];
96
97
        foreach ($constants as $key => $value) {
98
            $this->defineCurlConstants($key, $value);
99
        }
100
    }
101
102
103
    /**
104
     * Define curl constants.
105
     *
106
     * @param string $key
107
     * @param string $value
108
     *
109
     * @return bool|void
110
     */
111
    protected function defineCurlConstants($key, $value)
112
    {
113
        return defined($key) ? true : define($key, $value);
114
    }
115
116
    /**
117
     * Function to initialize/override Http Client.
118
     *
119
     * @param \GuzzleHttp\Client|null $client
120
     *
121
     * @return void
122
     */
123
    public function setClient(HttpClient $client = null)
124
    {
125
        if ($client instanceof HttpClient) {
126
            $this->client = $client;
127
128
            return;
129
        }
130
131
        $this->client = new HttpClient([
132
            'curl' => $this->httpClientConfig,
133
        ]);
134
    }
135
136
    /**
137
     * Function to set Http Client configuration.
138
     *
139
     * @return void
140
     */
141
    protected function setHttpClientConfiguration()
142
    {
143
        $this->setCurlConstants();
144
145
        $this->httpClientConfig = [
146
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
147
            CURLOPT_SSL_VERIFYPEER => $this->validateSSL,
148
        ];
149
150
        // Initialize Http Client
151
        $this->setClient();
152
153
        // Set default values.
154
        $this->setDefaultValues();
155
156
        // Set PayPal IPN Notification URL
157
        $this->notifyUrl = $this->config['notify_url'];
158
    }
159
160
    /**
161
     * Set default values for configuration.
162
     *
163
     * @return void
164
     */
165
    private function setDefaultValues()
166
    {
167
        $paymentAction = empty($this->paymentAction) ? 'Sale' : $this->paymentAction;
168
        $this->paymentAction = $paymentAction;
169
170
        $locale = empty($this->locale) ? 'en_US' : $this->locale;
171
        $this->locale = $locale;
172
173
        $validateSSL = empty($this->validateSSL) ? true : $this->validateSSL;
174
        $this->validateSSL = $validateSSL;
175
    }
176
177
    /**
178
     * Perform PayPal API request & return response.
179
     *
180
     * @throws \Throwable
181
     *
182
     * @return StreamInterface
183
     */
184
    private function makeHttpRequest(): StreamInterface
185
    {
186
        try {
187
            return $this->client->{$this->verb}(
188
                $this->apiUrl,
189
                $this->options
190
            )->getBody();
191
        } catch (HttpClientException $e) {
192
            throw new RuntimeException($e->getRequest()->getBody().' '.$e->getResponse()->getBody());
193
        }
194
    }
195
196
    /**
197
     * Function To Perform PayPal API Request.
198
     *
199
     * @param bool $decode
200
     *
201
     * @throws \Throwable
202
     *
203
     * @return array|StreamInterface|string
204
     */
205
    private function doPayPalRequest(bool $decode = true)
206
    {
207
        try {
208
            $this->apiUrl = collect([$this->config['api_url'], $this->apiEndPoint])->implode('/');
209
210
            // Perform PayPal HTTP API request.
211
            $response = $this->makeHttpRequest();
212
213
            return ($decode === false) ? $response->getContents() : Utils::jsonDecode($response, true);
214
        } catch (RuntimeException $t) {
215
            $message = collect($t->getMessage())->implode('\n');
216
        }
217
218
        return [
219
            'type'    => 'error',
220
            'message' => $message,
221
        ];
222
    }
223
}
224