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