Completed
Branch v2.0 (1ad81b)
by Raza
07:29
created

PayPalRequest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Test Coverage

Coverage 3.85%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 56
c 2
b 0
f 0
dl 0
loc 285
ccs 2
cts 52
cp 0.0385
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequestData() 0 9 3
A setApiCredentials() 0 13 1
A verifyIPN() 0 7 1
A addOptions() 0 5 1
A setConfig() 0 12 3
A setCurrency() 0 12 2
A setApiProvider() 0 9 2
A setApiProviderConfiguration() 0 14 1
A setApiEnvironment() 0 6 3
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
use Exception;
6
use GuzzleHttp\Client as HttpClient;
7
use Illuminate\Support\Collection;
8
use Psr\Http\Message\StreamInterface;
9
use RuntimeException;
10
use Srmklive\PayPal\Services\PayPal as PayPalClient;
11
12
trait PayPalRequest
13
{
14 1
    use PayPalHttpClient;
15 1
    use PayPalAPI;
16
17
    /**
18
     * Http Client class object.
19
     *
20
     * @var HttpClient
21
     */
22
    private $client;
23
24
    /**
25
     * Http Client configuration.
26
     *
27
     * @var array
28
     */
29
    private $httpClientConfig;
30
31
    /**
32
     * PayPal API mode to be used.
33
     *
34
     * @var string
35
     */
36
    public $mode;
37
38
    /**
39
     * PayPal access token.
40
     *
41
     * @var string
42
     */
43
    protected $access_token;
44
45
    /**
46
     * Request data to be sent to PayPal.
47
     *
48
     * @var Collection
49
     */
50
    protected $post;
51
52
    /**
53
     * PayPal API configuration.
54
     *
55
     * @var array
56
     */
57
    private $config;
58
59
    /**
60
     * Default currency for PayPal.
61
     *
62
     * @var string
63
     */
64
    private $currency;
65
66
    /**
67
     * Additional options for PayPal API request.
68
     *
69
     * @var array
70
     */
71
    private $options;
72
73
    /**
74
     * Default payment action for PayPal.
75
     *
76
     * @var string
77
     */
78
    private $paymentAction;
79
80
    /**
81
     * Default locale for PayPal.
82
     *
83
     * @var string
84
     */
85
    private $locale;
86
87
    /**
88
     * PayPal API Endpoint.
89
     *
90
     * @var string
91
     */
92
    private $apiUrl;
93
94
    /**
95
     * PayPal API Endpoint.
96
     *
97
     * @var string
98
     */
99
    private $apiEndPoint;
100
101
    /**
102
     * IPN notification url for PayPal.
103
     *
104
     * @var string
105
     */
106
    private $notifyUrl;
107
108
    /**
109
     * Http Client request body parameter name.
110
     *
111
     * @var string
112
     */
113
    private $httpBodyParam;
114
115
    /**
116
     * Validate SSL details when creating HTTP client.
117
     *
118
     * @var bool
119
     */
120
    private $validateSSL;
121
122
    /**
123
     * Set PayPal API Credentials.
124
     *
125
     * @param array $credentials
126
     *
127
     * @throws Exception
128
     *
129
     * @return void
130
     */
131
    public function setApiCredentials($credentials)
132
    {
133
        // Setting Default PayPal Mode If not set
134
        $this->setApiEnvironment($credentials);
135
136
        // Set API configuration for the PayPal provider
137
        $this->setApiProviderConfiguration($credentials);
138
139
        // Set default currency.
140
        $this->setCurrency($credentials['currency']);
141
142
        // Set Http Client configuration.
143
        $this->setHttpClientConfiguration();
144
    }
145
146
    /**
147
     * Set other/override PayPal API parameters.
148
     *
149
     * @param array $options
150
     *
151
     * @return $this
152
     */
153
    public function addOptions(array $options)
154
    {
155
        $this->options = $options;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Function to set currency.
162
     *
163
     * @param string $currency
164
     *
165
     * @throws Exception
166
     *
167
     * @return $this
168
     */
169
    public function setCurrency($currency = 'USD')
170
    {
171
        $allowedCurrencies = ['AUD', 'BRL', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'INR', 'JPY', 'MYR', 'MXN', 'NOK', 'NZD', 'PHP', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'TWD', 'THB', 'USD', 'RUB'];
172
173
        // Check if provided currency is valid.
174
        if (!in_array($currency, $allowedCurrencies, true)) {
175
            throw new Exception('Currency is not supported by PayPal.');
176
        }
177
178
        $this->currency = $currency;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Retrieve PayPal IPN Response.
185
     *
186
     * @param array|StreamInterface $post
187
     *
188
     * @throws \Throwable
189
     *
190
     * @return array
191
     */
192
    public function verifyIPN($post)
193
    {
194
        $this->setRequestData($post);
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type Psr\Http\Message\StreamInterface; however, parameter $data of Srmklive\PayPal\Traits\P...quest::setRequestData() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

194
        $this->setRequestData(/** @scrutinizer ignore-type */ $post);
Loading history...
195
196
        $this->apiUrl = $this->config['ipn_url'];
197
198
        return $this->doPayPalRequest('verifyipn');
0 ignored issues
show
Unused Code introduced by
The call to Srmklive\PayPal\Traits\P...uest::doPayPalRequest() has too many arguments starting with 'verifyipn'. ( Ignorable by Annotation )

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

198
        return $this->/** @scrutinizer ignore-call */ doPayPalRequest('verifyipn');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
199
    }
200
201
    /**
202
     * Setup request data to be sent to PayPal.
203
     *
204
     * @param array $data
205
     *
206
     * @return Collection
207
     */
208
    protected function setRequestData(array $data = [])
209
    {
210
        if (($this->post instanceof Collection) && (!$this->post->isEmpty())) {
211
            unset($this->post);
212
        }
213
214
        $this->post = new Collection($data);
215
216
        return $this->post;
217
    }
218
219
    /**
220
     * Function To Set PayPal API Configuration.
221
     *
222
     * @param array $config
223
     *
224
     * @throws Exception
225
     */
226
    private function setConfig(array $config = [])
227
    {
228
        // Set Api Credentials
229
        if (function_exists('config')) {
230
            $this->setApiCredentials(
231
                config('paypal')
232
            );
233
        } elseif (!empty($config)) {
234
            $this->setApiCredentials($config);
235
        }
236
237
        $this->setRequestData();
238
    }
239
240
    /**
241
     * Set API environment to be used by PayPal.
242
     *
243
     * @param array $credentials
244
     *
245
     * @return void
246
     */
247
    private function setApiEnvironment($credentials)
248
    {
249
        if (empty($credentials['mode']) || !in_array($credentials['mode'], ['sandbox', 'live'])) {
250
            $this->mode = 'live';
251
        } else {
252
            $this->mode = $credentials['mode'];
253
        }
254
    }
255
256
    /**
257
     * Set configuration details for the provider.
258
     *
259
     * @param array $credentials
260
     *
261
     * @throws Exception
262
     *
263
     * @return void
264
     */
265
    private function setApiProviderConfiguration($credentials)
266
    {
267
        // Setting PayPal API Credentials
268
        collect($credentials[$this->mode])->map(function ($value, $key) {
269
            $this->config[$key] = $value;
270
        });
271
272
        $this->paymentAction = $credentials['payment_action'];
273
274
        $this->locale = $credentials['locale'];
275
276
        $this->validateSSL = $credentials['validate_ssl'];
277
278
        $this->setApiProvider($credentials);
279
    }
280
281
    /**
282
     * Determines which API provider should be used.
283
     *
284
     * @param array $credentials
285
     *
286
     * @throws \RuntimeException
287
     */
288
    private function setApiProvider($credentials)
289
    {
290
        if ($this instanceof PayPalClient) {
291
            $this->setOptions($credentials);
292
293
            return;
294
        }
295
296
        throw new RuntimeException('Invalid api credentials provided for PayPal!. Please provide the right api credentials.');
297
    }
298
}
299