Passed
Push — v2.0 ( a2699e...aa8e26 )
by Raza
02:04
created

PayPalRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Test Coverage

Coverage 7.41%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 219
ccs 2
cts 27
cp 0.0741
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setApiProvider() 0 9 2
A setApiProviderConfiguration() 0 14 1
A setApiCredentials() 0 13 1
A setApiEnvironment() 0 6 3
A addOptions() 0 5 1
A setConfig() 0 9 3
A setCurrency() 0 12 2
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
use Exception;
6
use GuzzleHttp\Client as HttpClient;
7
use RuntimeException;
8
use Srmklive\PayPal\Services\PayPal as PayPalClient;
9
10
trait PayPalRequest
11
{
12
    use PayPalHttpClient;
13
    use PayPalAPI;
14 1
15 1
    /**
16
     * Http Client class object.
17
     *
18
     * @var HttpClient
19
     */
20
    private $client;
21
22
    /**
23
     * Http Client configuration.
24
     *
25
     * @var array
26
     */
27
    private $httpClientConfig;
28
29
    /**
30
     * PayPal API mode to be used.
31
     *
32
     * @var string
33
     */
34
    public $mode;
35
36
    /**
37
     * PayPal access token.
38
     *
39
     * @var string
40
     */
41
    protected $access_token;
42
43
    /**
44
     * PayPal API configuration.
45
     *
46
     * @var array
47
     */
48
    private $config;
49
50
    /**
51
     * Default currency for PayPal.
52
     *
53
     * @var string
54
     */
55
    private $currency;
56
57
    /**
58
     * Additional options for PayPal API request.
59
     *
60
     * @var array
61
     */
62
    private $options;
63
64
    /**
65
     * PayPal API Endpoint.
66
     *
67
     * @var string
68
     */
69
    private $apiUrl;
70
71
    /**
72
     * PayPal API Endpoint.
73
     *
74
     * @var string
75
     */
76
    private $apiEndPoint;
77
78
    /**
79
     * IPN notification url for PayPal.
80
     *
81
     * @var string
82
     */
83
    private $notifyUrl;
84
85
    /**
86
     * Http Client request body parameter name.
87
     *
88
     * @var string
89
     */
90
    private $httpBodyParam;
91
92
    /**
93
     * Set PayPal API Credentials.
94
     *
95
     * @param array $credentials
96
     *
97
     * @throws Exception
98
     *
99
     * @return void
100
     */
101
    public function setApiCredentials($credentials)
102
    {
103
        // Setting Default PayPal Mode If not set
104
        $this->setApiEnvironment($credentials);
105
106
        // Set API configuration for the PayPal provider
107
        $this->setApiProviderConfiguration($credentials);
108
109
        // Set default currency.
110
        $this->setCurrency($credentials['currency']);
111
112
        // Set Http Client configuration.
113
        $this->setHttpClientConfiguration();
114
    }
115
116
    /**
117
     * Set other/override PayPal API parameters.
118
     *
119
     * @param array $options
120
     *
121
     * @return $this
122
     */
123
    public function addOptions(array $options)
124
    {
125
        $this->options = $options;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Function to set currency.
132
     *
133
     * @param string $currency
134
     *
135
     * @throws Exception
136
     *
137
     * @return $this
138
     */
139
    public function setCurrency($currency = 'USD')
140
    {
141
        $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'];
142
143
        // Check if provided currency is valid.
144
        if (!in_array($currency, $allowedCurrencies, true)) {
145
            throw new Exception('Currency is not supported by PayPal.');
146
        }
147
148
        $this->currency = $currency;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Function To Set PayPal API Configuration.
155
     *
156
     * @param array $config
157
     *
158
     * @throws Exception
159
     */
160
    private function setConfig(array $config = [])
161
    {
162
        // Set Api Credentials
163
        if (function_exists('config')) {
164
            $this->setApiCredentials(
165
                config('paypal')
166
            );
167
        } elseif (!empty($config)) {
168
            $this->setApiCredentials($config);
169
        }
170
    }
171
172
    /**
173
     * Set API environment to be used by PayPal.
174
     *
175
     * @param array $credentials
176
     *
177
     * @return void
178
     */
179
    private function setApiEnvironment($credentials)
180
    {
181
        if (empty($credentials['mode']) || !in_array($credentials['mode'], ['sandbox', 'live'])) {
182
            $this->mode = 'live';
183
        } else {
184
            $this->mode = $credentials['mode'];
185
        }
186
    }
187
188
    /**
189
     * Set configuration details for the provider.
190
     *
191
     * @param array $credentials
192
     *
193
     * @throws Exception
194
     *
195
     * @return void
196
     */
197
    private function setApiProviderConfiguration($credentials)
198
    {
199
        // Setting PayPal API Credentials
200
        collect($credentials[$this->mode])->map(function ($value, $key) {
201
            $this->config[$key] = $value;
202
        });
203
204
        $this->paymentAction = $credentials['payment_action'];
205
206
        $this->locale = $credentials['locale'];
207
208
        $this->validateSSL = $credentials['validate_ssl'];
209
210
        $this->setApiProvider($credentials);
211
    }
212
213
    /**
214
     * Determines which API provider should be used.
215
     *
216
     * @param array $credentials
217
     *
218
     * @throws \RuntimeException
219
     */
220
    private function setApiProvider($credentials)
221
    {
222
        if ($this instanceof PayPalClient) {
223
            $this->setOptions($credentials);
224
225
            return;
226
        }
227
228
        throw new RuntimeException('Invalid api credentials provided for PayPal!. Please provide the right api credentials.');
229
    }
230
}
231