Passed
Push — v3.0 ( b76c53...9d9642 )
by Raza
02:45
created

PayPalHttpClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 16
eloc 53
c 6
b 0
f 0
dl 0
loc 209
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpClientConfiguration() 0 17 1
A makeHttpRequest() 0 9 2
A doPayPalRequest() 0 16 3
A defineCurlConstant() 0 3 2
A setDefaultValues() 0 10 4
A setCurlConstants() 0 11 2
A setClient() 0 10 2
1
<?php
2
3
namespace Srmklive\PayPal\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 PayPalHttpClient
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
     * PayPal API Endpoint.
28
     *
29
     * @var string
30
     */
31
    private $apiUrl;
32
33
    /**
34
     * PayPal API Endpoint.
35
     *
36
     * @var string
37
     */
38
    private $apiEndPoint;
39
40
    /**
41
     * IPN notification url for PayPal.
42
     *
43
     * @var string
44
     */
45
    private $notifyUrl;
46
47
    /**
48
     * Http Client request body parameter name.
49
     *
50
     * @var string
51
     */
52
    private $httpBodyParam;
53
54
    /**
55
     * Default payment action for PayPal.
56
     *
57
     * @var string
58
     */
59
    private $paymentAction;
60
61
    /**
62
     * Default locale for PayPal.
63
     *
64
     * @var string
65
     */
66
    private $locale;
67
68
    /**
69
     * Validate SSL details when creating HTTP client.
70
     *
71
     * @var bool
72
     */
73
    private $validateSSL;
74
75
    /**
76
     * Request type.
77
     *
78
     * @var string
79
     */
80
    protected $verb = 'post';
81
82
    /**
83
     * Set curl constants if not defined.
84
     *
85
     * @return void
86
     */
87
    protected function setCurlConstants()
88
    {
89
        $constants = [
90
            'CURLOPT_SSLVERSION'        => 32,
91
            'CURL_SSLVERSION_TLSv1_2'   => 6,
92
            'CURLOPT_SSL_VERIFYPEER'    => 64,
93
            'CURLOPT_SSLCERT'           => 10025,
94
        ];
95
96
        foreach ($constants as $key => $item) {
97
            $this->defineCurlConstant($key, $item);
98
        }
99
    }
100
101
    /**
102
     * Declare a curl constant.
103
     *
104
     * @param string $key
105
     * @param string $value
106
     *
107
     * @return bool
108
     */
109
    protected function defineCurlConstant(string $key, string $value)
110
    {
111
        return defined($key) ? true : define($key, $value);
112
    }
113
114
    /**
115
     * Function to initialize/override Http Client.
116
     *
117
     * @param \GuzzleHttp\Client|null $client
118
     *
119
     * @return void
120
     */
121
    public function setClient(HttpClient $client = null)
122
    {
123
        if ($client instanceof HttpClient) {
124
            $this->client = $client;
125
126
            return;
127
        }
128
129
        $this->client = new HttpClient([
130
            'curl' => $this->httpClientConfig,
131
        ]);
132
    }
133
134
    /**
135
     * Function to set Http Client configuration.
136
     *
137
     * @return void
138
     */
139
    protected function setHttpClientConfiguration()
140
    {
141
        $this->setCurlConstants();
142
143
        $this->httpClientConfig = [
144
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
145
            CURLOPT_SSL_VERIFYPEER => $this->validateSSL,
146
        ];
147
148
        // Initialize Http Client
149
        $this->setClient();
150
151
        // Set default values.
152
        $this->setDefaultValues();
153
154
        // Set PayPal IPN Notification URL
155
        $this->notifyUrl = $this->config['notify_url'];
156
    }
157
158
    /**
159
     * Set default values for configuration.
160
     *
161
     * @return void
162
     */
163
    private function setDefaultValues()
164
    {
165
        $paymentAction = empty($this->paymentAction) ? 'Sale' : $this->paymentAction;
166
        $this->paymentAction = $paymentAction;
167
168
        $locale = empty($this->locale) ? 'en_US' : $this->locale;
169
        $this->locale = $locale;
170
171
        $validateSSL = empty($this->validateSSL) ? true : $this->validateSSL;
172
        $this->validateSSL = $validateSSL;
173
    }
174
175
    /**
176
     * Perform PayPal API request & return response.
177
     *
178
     * @throws \Throwable
179
     *
180
     * @return StreamInterface
181
     */
182
    private function makeHttpRequest(): StreamInterface
183
    {
184
        try {
185
            return $this->client->{$this->verb}(
186
                $this->apiUrl,
187
                $this->options
188
            )->getBody();
189
        } catch (HttpClientException $e) {
190
            throw new RuntimeException($e->getRequest()->getBody().' '.$e->getResponse()->getBody());
191
        }
192
    }
193
194
    /**
195
     * Function To Perform PayPal API Request.
196
     *
197
     * @param bool $decode
198
     *
199
     * @throws \Throwable
200
     *
201
     * @return array|StreamInterface|string
202
     */
203
    private function doPayPalRequest(bool $decode = true)
204
    {
205
        try {
206
            $this->apiUrl = collect([$this->config['api_url'], $this->apiEndPoint])->implode('/');
207
208
            // Perform PayPal HTTP API request.
209
            $response = $this->makeHttpRequest();
210
211
            return ($decode === false) ? $response->getContents() : \GuzzleHttp\json_decode($response, true);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\json_decode() has been deprecated: json_decode will be removed in guzzlehttp/guzzle:8.0. Use Utils::jsonDecode instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

211
            return ($decode === false) ? $response->getContents() : /** @scrutinizer ignore-deprecated */ \GuzzleHttp\json_decode($response, true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
212
        } catch (RuntimeException $t) {
213
            $message = collect($t->getMessage())->implode('\n');
214
        }
215
216
        return [
217
            'type'    => 'error',
218
            'message' => $message,
219
        ];
220
    }
221
}
222