Completed
Push — v2 ( 1a5c3b...ed0a80 )
by Raza
05:30
created

PayPalRequest::createRequestPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
    use PayPalHttpClient;
15
16
    /**
17
     * Http Client class object.
18
     *
19
     * @var HttpClient
20
     */
21
    private $client;
22
23
    /**
24
     * Http Client configuration.
25
     *
26
     * @var array
27
     */
28
    private $httpClientConfig;
29
30
    /**
31
     * PayPal API mode to be used.
32
     *
33
     * @var string
34
     */
35
    public $mode;
36
37
    /**
38
     * Request data to be sent to PayPal.
39
     *
40
     * @var Collection
41
     */
42
    protected $post;
43
44
    /**
45
     * PayPal API configuration.
46
     *
47
     * @var array
48
     */
49
    private $config;
50
51
    /**
52
     * Item subtotal.
53
     *
54
     * @var float
55
     */
56
    private $subtotal;
57
58
    /**
59
     * Default currency for PayPal.
60
     *
61
     * @var string
62
     */
63
    private $currency;
64
65
    /**
66
     * Additional options for PayPal API request.
67
     *
68
     * @var array
69
     */
70
    private $options;
71
72
    /**
73
     * Default payment action for PayPal.
74
     *
75
     * @var string
76
     */
77
    private $paymentAction;
78
79
    /**
80
     * Default locale for PayPal.
81
     *
82
     * @var string
83
     */
84
    private $locale;
85
86
    /**
87
     * PayPal API Endpoint.
88
     *
89
     * @var string
90
     */
91
    private $apiUrl;
92
93
    /**
94
     * IPN notification url for PayPal.
95
     *
96
     * @var string
97
     */
98
    private $notifyUrl;
99
100
    /**
101
     * Http Client request body parameter name.
102
     *
103
     * @var string
104
     */
105
    private $httpBodyParam;
106
107
    /**
108
     * Validate SSL details when creating HTTP client.
109
     *
110
     * @var bool
111
     */
112
    private $validateSSL;
113
114
    /**
115
     * Set PayPal API Credentials.
116
     *
117
     * @param array $credentials
118
     *
119
     * @throws Exception
120
     *
121
     * @return void
122
     */
123
    public function setApiCredentials($credentials)
124
    {
125
        // Setting Default PayPal Mode If not set
126
        $this->setApiEnvironment($credentials);
127
128
        // Set API configuration for the PayPal provider
129
        $this->setApiProviderConfiguration($credentials);
130
131
        // Set default currency.
132
        $this->setCurrency($credentials['currency']);
133
134
        // Set Http Client configuration.
135
        $this->setHttpClientConfiguration();
136
    }
137
138
    /**
139
     * Set other/override PayPal API parameters.
140
     *
141
     * @param array $options
142
     *
143
     * @return $this
144
     */
145
    public function addOptions(array $options)
146
    {
147
        $this->options = $options;
148
149
        return $this;
150
    }
151
152
    /**
153
     * Function to set currency.
154
     *
155
     * @param string $currency
156
     *
157
     * @throws Exception
158
     *
159
     * @return $this
160
     */
161
    public function setCurrency($currency = 'USD')
162
    {
163
        $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'];
164
165
        // Check if provided currency is valid.
166
        if (!in_array($currency, $allowedCurrencies, true)) {
167
            throw new Exception('Currency is not supported by PayPal.');
168
        }
169
170
        $this->currency = $currency;
171
172
        return $this;
173
    }
174
175
    /**
176
     * Retrieve PayPal IPN Response.
177
     *
178
     * @param array $post
179
     *
180
     * @throws Exception
181
     *
182
     * @return array
183
     */
184
    public function verifyIPN($post)
185
    {
186
        $this->setRequestData($post);
187
188
        $this->apiUrl = $this->config['ipn_url'];
189
190
        return $this->doPayPalRequest('verifyipn');
191
    }
192
193
    /**
194
     * Setup request data to be sent to PayPal.
195
     *
196
     * @param array $data
197
     *
198
     * @return Collection
199
     */
200
    protected function setRequestData(array $data = [])
201
    {
202
        if (($this->post instanceof Collection) && (!$this->post->isEmpty())) {
203
            unset($this->post);
204
        }
205
206
        $this->post = new Collection($data);
207
208
        return $this->post;
209
    }
210
211
    /**
212
     * Function To Set PayPal API Configuration.
213
     *
214
     * @param array $config
215
     *
216
     * @throws Exception
217
     */
218
    private function setConfig(array $config = [])
219
    {
220
        // Set Api Credentials
221
        if (function_exists('config')) {
222
            $this->setApiCredentials(
223
                config('paypal')
224
            );
225
        } elseif (!empty($config)) {
226
            $this->setApiCredentials($config);
227
        }
228
229
        $this->setRequestData();
230
    }
231
232
    /**
233
     * Set default values for configuration.
234
     *
235
     * @return void
236
     */
237
    private function setDefaultValues()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
238
    {
239
        // Set default payment action.
240
        if (empty($this->paymentAction)) {
241
            $this->paymentAction = 'Sale';
242
        }
243
244
        // Set default locale.
245
        if (empty($this->locale)) {
246
            $this->locale = 'en_US';
247
        }
248
249
        // Set default value for SSL validation.
250
        if (empty($this->validateSSL)) {
251
            $this->validateSSL = false;
252
        }
253
    }
254
255
    /**
256
     * Set API environment to be used by PayPal.
257
     *
258
     * @param array $credentials
259
     *
260
     * @return void
261
     */
262
    private function setApiEnvironment($credentials)
263
    {
264
        if (empty($credentials['mode']) || !in_array($credentials['mode'], ['sandbox', 'live'])) {
265
            $this->mode = 'live';
266
        } else {
267
            $this->mode = $credentials['mode'];
268
        }
269
    }
270
271
    /**
272
     * Set configuration details for the provider.
273
     *
274
     * @param array $credentials
275
     *
276
     * @throws Exception
277
     *
278
     * @return void
279
     */
280
    private function setApiProviderConfiguration($credentials)
281
    {
282
        // Setting PayPal API Credentials
283
        collect($credentials[$this->mode])->map(function ($value, $key) {
284
            $this->config[$key] = $value;
285
        });
286
287
        $this->paymentAction = $credentials['payment_action'];
288
289
        $this->locale = $credentials['locale'];
290
291
        $this->validateSSL = $credentials['validate_ssl'];
292
293
        $this->setApiProvider($credentials);
294
    }
295
296
    /**
297
     * Determines which API provider should be used.
298
     *
299
     * @param array $credentials
300
     *
301
     * @throws \RuntimeException
302
     */
303
    private function setApiProvider($credentials)
304
    {
305
        if ($this instanceof PayPalClient) {
306
            $this->setOptions($credentials);
307
        }
308
309
        throw new RuntimeException('Invalid api credentials provided for PayPal!. Please provide the right api credentials.');
310
    }
311
312
    /**
313
     * Parse PayPal NVP Response.
314
     *
315
     * @param string                $method
316
     * @param array|StreamInterface $response
317
     *
318
     * @return array
319
     */
320
    private function retrieveData($method, $response)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
321
    {
322
        if ($method === 'verifyipn') {
323
            return $response;
324
        }
325
326
        parse_str($response, $output);
327
328
        return $output;
329
    }
330
}
331