Passed
Push — v3.0 ( 254743...be49dd )
by Raza
01:52
created

PayPalRequest::setConfig()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 4
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
use RuntimeException;
6
7
trait PayPalRequest
8
{
9
    use PayPalHttpClient;
10
    use PayPalAPI;
11
12
    /**
13
     * PayPal API mode to be used.
14
     *
15
     * @var string
16
     */
17
    public $mode;
18
19
    /**
20
     * PayPal access token.
21
     *
22
     * @var string
23
     */
24
    protected $access_token;
25
26
    /**
27
     * PayPal API configuration.
28
     *
29
     * @var array
30
     */
31
    private $config;
32
33
    /**
34
     * Default currency for PayPal.
35
     *
36
     * @var string
37
     */
38
    protected $currency;
39
40
    /**
41
     * Additional options for PayPal API request.
42
     *
43
     * @var array
44
     */
45
    protected $options;
46
47
    /**
48
     * Set PayPal API Credentials.
49
     *
50
     * @param array $credentials
51
     *
52
     * @throws \RuntimeException|\Exception
53
     */
54
    public function setApiCredentials(array $credentials): void
55
    {
56
        if (empty($credentials)) {
57
            $this->throwConfigurationException();
58
        }
59
60
        // Setting Default PayPal Mode If not set
61
        $this->setApiEnvironment($credentials);
62
63
        // Set API configuration for the PayPal provider
64
        $this->setApiProviderConfiguration($credentials);
65
66
        // Set default currency.
67
        $this->setCurrency($credentials['currency']);
68
69
        // Set Http Client configuration.
70
        $this->setHttpClientConfiguration();
71
    }
72
73
    /**
74
     * Function to set currency.
75
     *
76
     * @param string $currency
77
     *
78
     * @throws \RuntimeException
79
     *
80
     * @return \Srmklive\PayPal\Services\PayPal
81
     */
82
    public function setCurrency(string $currency = 'USD'): \Srmklive\PayPal\Services\PayPal
83
    {
84
        $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', 'CNY'];
85
86
        // Check if provided currency is valid.
87
        if (!in_array($currency, $allowedCurrencies, true)) {
88
            throw new RuntimeException('Currency is not supported by PayPal.');
89
        }
90
91
        $this->currency = $currency;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Return the set currency.
98
     */
99
    public function getCurrency(): string
100
    {
101
        return $this->currency;
102
    }
103
104
    /**
105
     * Function to add request header.
106
     *
107
     * @param string $key
108
     * @param string $value
109
     *
110
     * @return \Srmklive\PayPal\Services\PayPal
111
     */
112
    public function setRequestHeader(string $key, string $value): \Srmklive\PayPal\Services\PayPal
113
    {
114
        $this->options['headers'][$key] = $value;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Return request options header.
121
     *
122
     * @param string $key
123
     *
124
     * @throws \RuntimeException
125
     *
126
     * @return string
127
     */
128
    public function getRequestHeader(string $key): string
129
    {
130
        if (isset($this->options['headers'][$key])) {
131
            return $this->options['headers'][$key];
132
        }
133
134
        throw new RuntimeException('Options header is not set.');
135
    }
136
137
    /**
138
     * Function To Set PayPal API Configuration.
139
     *
140
     * @param array $config
141
     *
142
     * @throws \Exception
143
     */
144
    private function setConfig(array $config): void
145
    {
146
        if (empty($config) && function_exists('config') && !empty(config('paypal'))) {
147
            $api_config = config('paypal');
148
        } else {
149
            $api_config = $config;
150
        }
151
152
        // Set Api Credentials
153
        $this->setApiCredentials($api_config);
154
    }
155
156
    /**
157
     * Set API environment to be used by PayPal.
158
     *
159
     * @param array $credentials
160
     */
161
    private function setApiEnvironment(array $credentials): void
162
    {
163
        $this->mode = 'live';
164
165
        if (!empty($credentials['mode'])) {
166
            $this->setValidApiEnvironment($credentials['mode']);
167
        } else {
168
            $this->throwConfigurationException();
169
        }
170
    }
171
172
    /**
173
     * Validate & set the environment to be used by PayPal.
174
     *
175
     * @param string $mode
176
     */
177
    private function setValidApiEnvironment(string $mode): void
178
    {
179
        $this->mode = !in_array($mode, ['sandbox', 'live']) ? 'live' : $mode;
180
    }
181
182
    /**
183
     * Set configuration details for the provider.
184
     *
185
     * @param array $credentials
186
     *
187
     * @throws \Exception
188
     */
189
    private function setApiProviderConfiguration(array $credentials): void
190
    {
191
        // Setting PayPal API Credentials
192
        if (empty($credentials[$this->mode])) {
193
            $this->throwConfigurationException();
194
        }
195
196
        $config_params = ['client_id', 'client_secret'];
197
198
        foreach ($config_params as $item) {
199
            if (empty($credentials[$this->mode][$item])) {
200
                throw new RuntimeException("{$item} missing from the provided configuration. Please add your application {$item}.");
201
            }
202
        }
203
204
        collect($credentials[$this->mode])->map(function ($value, $key) {
205
            $this->config[$key] = $value;
206
        });
207
208
        $this->paymentAction = $credentials['payment_action'];
209
210
        $this->locale = $credentials['locale'];
211
212
        $this->validateSSL = $credentials['validate_ssl'];
213
214
        $this->setOptions($credentials);
215
    }
216
217
    /**
218
     * @throws RuntimeException
219
     */
220
    private function throwConfigurationException()
221
    {
222
        throw new RuntimeException('Invalid configuration provided. Please provide valid configuration for PayPal API. You can also refer to the documentation at https://srmklive.github.io/laravel-paypal/docs.html to setup correct configuration.');
223
    }
224
}
225