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
|
|
|
* Function to add multiple request headers. |
121
|
|
|
* |
122
|
|
|
* @param array $headers |
123
|
|
|
* |
124
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
125
|
|
|
*/ |
126
|
|
|
public function setRequestHeaders(array $headers): \Srmklive\PayPal\Services\PayPal |
127
|
|
|
{ |
128
|
|
|
foreach ($headers as $key=>$value) |
129
|
|
|
$this->setRequestHeader($key, $value); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return request options header. |
136
|
|
|
* |
137
|
|
|
* @param string $key |
138
|
|
|
* |
139
|
|
|
* @throws \RuntimeException |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getRequestHeader(string $key): string |
144
|
|
|
{ |
145
|
|
|
if (isset($this->options['headers'][$key])) { |
146
|
|
|
return $this->options['headers'][$key]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
throw new RuntimeException('Options header is not set.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Function To Set PayPal API Configuration. |
154
|
|
|
* |
155
|
|
|
* @param array $config |
156
|
|
|
* |
157
|
|
|
* @throws \Exception |
158
|
|
|
*/ |
159
|
|
|
private function setConfig(array $config): void |
160
|
|
|
{ |
161
|
|
|
if (empty($config) && function_exists('config') && !empty(config('paypal'))) { |
162
|
|
|
$api_config = config('paypal'); |
163
|
|
|
} else { |
164
|
|
|
$api_config = $config; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Set Api Credentials |
168
|
|
|
$this->setApiCredentials($api_config); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set API environment to be used by PayPal. |
173
|
|
|
* |
174
|
|
|
* @param array $credentials |
175
|
|
|
*/ |
176
|
|
|
private function setApiEnvironment(array $credentials): void |
177
|
|
|
{ |
178
|
|
|
$this->mode = 'live'; |
179
|
|
|
|
180
|
|
|
if (!empty($credentials['mode'])) { |
181
|
|
|
$this->setValidApiEnvironment($credentials['mode']); |
182
|
|
|
} else { |
183
|
|
|
$this->throwConfigurationException(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Validate & set the environment to be used by PayPal. |
189
|
|
|
* |
190
|
|
|
* @param string $mode |
191
|
|
|
*/ |
192
|
|
|
private function setValidApiEnvironment(string $mode): void |
193
|
|
|
{ |
194
|
|
|
$this->mode = !in_array($mode, ['sandbox', 'live']) ? 'live' : $mode; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set configuration details for the provider. |
199
|
|
|
* |
200
|
|
|
* @param array $credentials |
201
|
|
|
* |
202
|
|
|
* @throws \Exception |
203
|
|
|
*/ |
204
|
|
|
private function setApiProviderConfiguration(array $credentials): void |
205
|
|
|
{ |
206
|
|
|
// Setting PayPal API Credentials |
207
|
|
|
if (empty($credentials[$this->mode])) { |
208
|
|
|
$this->throwConfigurationException(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$config_params = ['client_id', 'client_secret']; |
212
|
|
|
|
213
|
|
|
foreach ($config_params as $item) { |
214
|
|
|
if (empty($credentials[$this->mode][$item])) { |
215
|
|
|
throw new RuntimeException("{$item} missing from the provided configuration. Please add your application {$item}."); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
collect($credentials[$this->mode])->map(function ($value, $key) { |
220
|
|
|
$this->config[$key] = $value; |
221
|
|
|
}); |
222
|
|
|
|
223
|
|
|
$this->paymentAction = $credentials['payment_action']; |
224
|
|
|
|
225
|
|
|
$this->locale = $credentials['locale']; |
226
|
|
|
$this->setRequestHeader('Accept-Language', $this->locale); |
227
|
|
|
|
228
|
|
|
$this->validateSSL = $credentials['validate_ssl']; |
229
|
|
|
|
230
|
|
|
$this->setOptions($credentials); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @throws RuntimeException |
235
|
|
|
*/ |
236
|
|
|
private function throwConfigurationException() |
237
|
|
|
{ |
238
|
|
|
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.'); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|