1 | <?php |
||
11 | trait PayPalRequest |
||
12 | { |
||
13 | /** |
||
14 | * Http Client class object. |
||
15 | * |
||
16 | * @var HttpClient |
||
17 | */ |
||
18 | private $client; |
||
19 | |||
20 | /** |
||
21 | * Http Client configuration. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $httpClientConfig; |
||
26 | |||
27 | /** |
||
28 | * PayPal API Certificate data for authentication. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $certificate; |
||
33 | |||
34 | /** |
||
35 | * PayPal API mode to be used. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $mode; |
||
40 | |||
41 | /** |
||
42 | * Request data to be sent to PayPal. |
||
43 | * |
||
44 | * @var \Illuminate\Support\Collection |
||
45 | */ |
||
46 | protected $post; |
||
47 | |||
48 | /** |
||
49 | * PayPal API configuration. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $config; |
||
54 | |||
55 | /** |
||
56 | * Default currency for PayPal. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $currency; |
||
61 | |||
62 | /** |
||
63 | * Additional options for PayPal API request. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $options; |
||
68 | |||
69 | /** |
||
70 | * Default payment action for PayPal. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $paymentAction; |
||
75 | |||
76 | /** |
||
77 | * Default locale for PayPal. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $locale; |
||
82 | |||
83 | /** |
||
84 | * PayPal API Endpoint. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $apiUrl; |
||
89 | |||
90 | /** |
||
91 | * IPN notification url for PayPal. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | private $notifyUrl; |
||
96 | |||
97 | /** |
||
98 | * Http Client request body parameter name. |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | private $httpBodyParam; |
||
103 | |||
104 | /** |
||
105 | * Validate SSL details when creating HTTP client. |
||
106 | * |
||
107 | * @var bool |
||
108 | */ |
||
109 | private $validateSSL; |
||
110 | |||
111 | /** |
||
112 | * Function To Set PayPal API Configuration. |
||
113 | * |
||
114 | * @param array $config |
||
115 | * |
||
116 | * @throws \Exception |
||
117 | */ |
||
118 | private function setConfig(array $config = []) |
||
131 | |||
132 | /** |
||
133 | * Set default values for configuration. |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | private function setDefaultValues() |
||
154 | |||
155 | /** |
||
156 | * Function to initialize Http Client. |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | protected function setClient() |
||
166 | |||
167 | /** |
||
168 | * Function to set Http Client configuration. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | protected function setHttpClientConfiguration() |
||
173 | { |
||
174 | $this->httpClientConfig = [ |
||
175 | CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, |
||
176 | CURLOPT_SSL_VERIFYPEER => $this->validateSSL, |
||
177 | ]; |
||
178 | |||
179 | if (!empty($this->certificate)) { |
||
180 | $this->httpClientConfig[CURLOPT_SSLCERT] = $this->certificate; |
||
181 | } |
||
182 | |||
183 | // Initialize Http Client |
||
184 | $this->setClient(); |
||
185 | |||
186 | // Set default values. |
||
187 | $this->setDefaultValues(); |
||
188 | |||
189 | // Set PayPal API Endpoint. |
||
190 | $this->apiUrl = $this->config['api_url']; |
||
191 | |||
192 | // Set PayPal IPN Notification URL |
||
193 | $this->notifyUrl = $this->config['notify_url']; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Set PayPal API Credentials. |
||
198 | * |
||
199 | * @param array $credentials |
||
200 | * |
||
201 | * @throws \Exception |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function setApiCredentials($credentials) |
||
206 | { |
||
207 | // Setting Default PayPal Mode If not set |
||
208 | $this->setApiEnvironment($credentials); |
||
209 | |||
210 | // Set API configuration for the PayPal provider |
||
211 | $this->setApiProviderConfiguration($credentials); |
||
212 | |||
213 | // Set default currency. |
||
214 | $this->setCurrency($credentials['currency']); |
||
215 | |||
216 | // Set Http Client configuration. |
||
217 | $this->setHttpClientConfiguration(); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Set API environment to be used by PayPal. |
||
222 | * |
||
223 | * @param array $credentials |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | private function setApiEnvironment($credentials) |
||
235 | |||
236 | /** |
||
237 | * Set configuration details for the provider. |
||
238 | * |
||
239 | * @param array $credentials |
||
240 | * |
||
241 | * @throws \Exception |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | private function setApiProviderConfiguration($credentials) |
||
266 | |||
267 | /** |
||
268 | * Determines which API provider should be used. |
||
269 | * |
||
270 | * @param array $credentials |
||
271 | * |
||
272 | * @throws \Exception |
||
273 | */ |
||
274 | private function setApiProvider($credentials) |
||
284 | |||
285 | /** |
||
286 | * Setup request data to be sent to PayPal. |
||
287 | * |
||
288 | * @param array $data |
||
289 | * |
||
290 | * @return \Illuminate\Support\Collection |
||
291 | */ |
||
292 | protected function setRequestData(array $data = []) |
||
302 | |||
303 | /** |
||
304 | * Set other/override PayPal API parameters. |
||
305 | * |
||
306 | * @param array $options |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function addOptions(array $options) |
||
316 | |||
317 | /** |
||
318 | * Function to set currency. |
||
319 | * |
||
320 | * @param string $currency |
||
321 | * |
||
322 | * @throws \Exception |
||
323 | * |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function setCurrency($currency = 'USD') |
||
339 | |||
340 | /** |
||
341 | * Retrieve PayPal IPN Response. |
||
342 | * |
||
343 | * @param array $post |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | public function verifyIPN($post) |
||
355 | |||
356 | /** |
||
357 | * Create request payload to be sent to PayPal. |
||
358 | * |
||
359 | * @param string $method |
||
360 | */ |
||
361 | private function createRequestPayload($method) |
||
376 | |||
377 | /** |
||
378 | * Perform PayPal API request & return response. |
||
379 | * |
||
380 | * @throws \Exception |
||
381 | * |
||
382 | * @return \Psr\Http\Message\StreamInterface |
||
383 | */ |
||
384 | private function makeHttpRequest() |
||
398 | |||
399 | /** |
||
400 | * Function To Perform PayPal API Request. |
||
401 | * |
||
402 | * @param string $method |
||
403 | * |
||
404 | * @throws \Exception |
||
405 | * |
||
406 | * @return array|\Psr\Http\Message\StreamInterface |
||
407 | */ |
||
408 | private function doPayPalRequest($method) |
||
427 | |||
428 | /** |
||
429 | * Parse PayPal NVP Response. |
||
430 | * |
||
431 | * @param string $method |
||
432 | * @param array|\Psr\Http\Message\StreamInterface $response |
||
433 | * |
||
434 | * @return array |
||
435 | */ |
||
436 | private function retrieveData($method, $response) |
||
446 | } |
||
447 |