Passed
Push — v2.0 ( cf221d...00ccf5 )
by Raza
02:12
created

PayPalRequest::setCurrency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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