|
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 limit to total records per API call. |
|
49
|
|
|
* |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $page_size = 20; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the current page for list resources API calls. |
|
56
|
|
|
* |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $current_page = 1; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Toggle whether totals for list resources are returned after every API call. |
|
63
|
|
|
* |
|
64
|
|
|
* @var bool |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $show_totals = true; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set PayPal API Credentials. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $credentials |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \RuntimeException|\Exception |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setApiCredentials(array $credentials): void |
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($credentials)) { |
|
78
|
|
|
$this->throwConfigurationException(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Setting Default PayPal Mode If not set |
|
82
|
|
|
$this->setApiEnvironment($credentials); |
|
83
|
|
|
|
|
84
|
|
|
// Set API configuration for the PayPal provider |
|
85
|
|
|
$this->setApiProviderConfiguration($credentials); |
|
86
|
|
|
|
|
87
|
|
|
// Set default currency. |
|
88
|
|
|
$this->setCurrency($credentials['currency']); |
|
89
|
|
|
|
|
90
|
|
|
// Set Http Client configuration. |
|
91
|
|
|
$this->setHttpClientConfiguration(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Function to set currency. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $currency |
|
98
|
|
|
* |
|
99
|
|
|
* @throws \RuntimeException |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setCurrency(string $currency = 'USD'): \Srmklive\PayPal\Services\PayPal |
|
104
|
|
|
{ |
|
105
|
|
|
$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']; |
|
106
|
|
|
|
|
107
|
|
|
// Check if provided currency is valid. |
|
108
|
|
|
if (!in_array($currency, $allowedCurrencies, true)) { |
|
109
|
|
|
throw new RuntimeException('Currency is not supported by PayPal.'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->currency = $currency; |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Return the set currency. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getCurrency(): string |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->currency; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Function to add request header. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* @param string $value |
|
130
|
|
|
* |
|
131
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setRequestHeader(string $key, string $value): \Srmklive\PayPal\Services\PayPal |
|
134
|
|
|
{ |
|
135
|
|
|
$this->options['headers'][$key] = $value; |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Function to add multiple request headers. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $headers |
|
144
|
|
|
* |
|
145
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setRequestHeaders(array $headers): \Srmklive\PayPal\Services\PayPal |
|
148
|
|
|
{ |
|
149
|
|
|
foreach ($headers as $key=>$value) { |
|
150
|
|
|
$this->setRequestHeader($key, $value); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Return request options header. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $key |
|
160
|
|
|
* |
|
161
|
|
|
* @throws \RuntimeException |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getRequestHeader(string $key): string |
|
166
|
|
|
{ |
|
167
|
|
|
if (isset($this->options['headers'][$key])) { |
|
168
|
|
|
return $this->options['headers'][$key]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
throw new RuntimeException('Options header is not set.'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Function To Set PayPal API Configuration. |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $config |
|
178
|
|
|
* |
|
179
|
|
|
* @throws \Exception |
|
180
|
|
|
*/ |
|
181
|
|
|
private function setConfig(array $config): void |
|
182
|
|
|
{ |
|
183
|
|
|
if (empty($config) && function_exists('config') && !empty(config('paypal'))) { |
|
184
|
|
|
$api_config = config('paypal'); |
|
185
|
|
|
} else { |
|
186
|
|
|
$api_config = $config; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
// Set Api Credentials |
|
190
|
|
|
$this->setApiCredentials($api_config); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set API environment to be used by PayPal. |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $credentials |
|
197
|
|
|
*/ |
|
198
|
|
|
private function setApiEnvironment(array $credentials): void |
|
199
|
|
|
{ |
|
200
|
|
|
$this->mode = 'live'; |
|
201
|
|
|
|
|
202
|
|
|
if (!empty($credentials['mode'])) { |
|
203
|
|
|
$this->setValidApiEnvironment($credentials['mode']); |
|
204
|
|
|
} else { |
|
205
|
|
|
$this->throwConfigurationException(); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Validate & set the environment to be used by PayPal. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $mode |
|
213
|
|
|
*/ |
|
214
|
|
|
private function setValidApiEnvironment(string $mode): void |
|
215
|
|
|
{ |
|
216
|
|
|
$this->mode = !in_array($mode, ['sandbox', 'live']) ? 'live' : $mode; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Set configuration details for the provider. |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $credentials |
|
223
|
|
|
* |
|
224
|
|
|
* @throws \Exception |
|
225
|
|
|
*/ |
|
226
|
|
|
private function setApiProviderConfiguration(array $credentials): void |
|
227
|
|
|
{ |
|
228
|
|
|
// Setting PayPal API Credentials |
|
229
|
|
|
if (empty($credentials[$this->mode])) { |
|
230
|
|
|
$this->throwConfigurationException(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$config_params = ['client_id', 'client_secret']; |
|
234
|
|
|
|
|
235
|
|
|
foreach ($config_params as $item) { |
|
236
|
|
|
if (empty($credentials[$this->mode][$item])) { |
|
237
|
|
|
throw new RuntimeException("{$item} missing from the provided configuration. Please add your application {$item}."); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
collect($credentials[$this->mode])->map(function ($value, $key) { |
|
242
|
|
|
$this->config[$key] = $value; |
|
243
|
|
|
}); |
|
244
|
|
|
|
|
245
|
|
|
$this->paymentAction = $credentials['payment_action']; |
|
246
|
|
|
|
|
247
|
|
|
$this->locale = $credentials['locale']; |
|
248
|
|
|
$this->setRequestHeader('Accept-Language', $this->locale); |
|
249
|
|
|
|
|
250
|
|
|
$this->validateSSL = $credentials['validate_ssl']; |
|
251
|
|
|
|
|
252
|
|
|
$this->setOptions($credentials); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @throws RuntimeException |
|
257
|
|
|
*/ |
|
258
|
|
|
private function throwConfigurationException() |
|
259
|
|
|
{ |
|
260
|
|
|
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.'); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @throws RuntimeException |
|
265
|
|
|
*/ |
|
266
|
|
|
private function throwInvalidEvidenceFileException() |
|
267
|
|
|
{ |
|
268
|
|
|
throw new RuntimeException('Invalid evidence file type provided. |
|
269
|
|
|
1. The party can upload up to 50 MB of files per request. |
|
270
|
|
|
2. Individual files must be smaller than 10 MB. |
|
271
|
|
|
3. The supported file formats are JPG, JPEG, GIF, PNG, and PDF. |
|
272
|
|
|
'); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|