1 | <?php |
||
11 | trait PayPalRequest |
||
12 | { |
||
13 | /** |
||
14 | * Http Client class object. |
||
15 | * |
||
16 | * @var HttpClient |
||
17 | */ |
||
18 | private $client; |
||
19 | |||
20 | /** |
||
21 | * PayPal API mode to be used. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | public $mode; |
||
26 | |||
27 | /** |
||
28 | * Request data to be sent to PayPal. |
||
29 | * |
||
30 | * @var \Illuminate\Support\Collection |
||
31 | */ |
||
32 | protected $post; |
||
33 | |||
34 | /** |
||
35 | * PayPal API configuration. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private $config; |
||
40 | |||
41 | /** |
||
42 | * Default currency for PayPal. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $currency; |
||
47 | |||
48 | /** |
||
49 | * Additional options for PayPal API request. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $options; |
||
54 | |||
55 | /** |
||
56 | * Default payment action for PayPal. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $paymentAction; |
||
61 | |||
62 | /** |
||
63 | * Default locale for PayPal. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $locale; |
||
68 | |||
69 | /** |
||
70 | * PayPal API Endpoint. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $apiUrl; |
||
75 | |||
76 | /** |
||
77 | * IPN notification url for PayPal. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $notifyUrl; |
||
82 | |||
83 | /** |
||
84 | * Http Client request body parameter name. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $httpBodyParam; |
||
89 | |||
90 | /** |
||
91 | * Function To Set PayPal API Configuration. |
||
92 | * |
||
93 | * @param array $config |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | private function setConfig(array $config = []) |
||
113 | |||
114 | /** |
||
115 | * Function to initialize Http Client. |
||
116 | * |
||
117 | * @return HttpClient |
||
118 | */ |
||
119 | protected function setClient() |
||
127 | |||
128 | /** |
||
129 | * Set PayPal API Credentials. |
||
130 | * |
||
131 | * @param array $credentials |
||
132 | * |
||
133 | * @throws \Exception |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | public function setApiCredentials($credentials) |
||
160 | |||
161 | /** |
||
162 | * Set API environment to be used by PayPal. |
||
163 | * |
||
164 | * @param array $credentials |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | private function setApiEnvironment($credentials) |
||
176 | |||
177 | /** |
||
178 | * Set configuration details for the provider. |
||
179 | * |
||
180 | * @param array $credentials |
||
181 | * |
||
182 | * @throws \Exception |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | private function setApiProviderConfiguration($credentials) |
||
205 | |||
206 | /** |
||
207 | * Setup request data to be sent to PayPal. |
||
208 | * |
||
209 | * @param array $data |
||
210 | * |
||
211 | * @return \Illuminate\Support\Collection |
||
212 | */ |
||
213 | protected function setRequestData(array $data = []) |
||
223 | |||
224 | /** |
||
225 | * Set other/override PayPal API parameters. |
||
226 | * |
||
227 | * @param array $options |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function addOptions(array $options) |
||
237 | |||
238 | /** |
||
239 | * Function to set currency. |
||
240 | * |
||
241 | * @param string $currency |
||
242 | * |
||
243 | * @throws \Exception |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function setCurrency($currency = 'USD') |
||
260 | |||
261 | /** |
||
262 | * Retrieve PayPal IPN Response. |
||
263 | * |
||
264 | * @param array $post |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function verifyIPN($post) |
||
276 | |||
277 | /** |
||
278 | * Refund PayPal Transaction. |
||
279 | * |
||
280 | * @param string $transaction |
||
281 | * @param float $amount |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function refundTransaction($transaction, $amount = 0.00) |
||
300 | |||
301 | /** |
||
302 | * Search Transactions On PayPal. |
||
303 | * |
||
304 | * @param array $post |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | public function searchTransactions($post) |
||
314 | |||
315 | /** |
||
316 | * Create request payload to be sent to PayPal. |
||
317 | * |
||
318 | * @param string $method |
||
319 | */ |
||
320 | private function createRequestPayload($method) |
||
321 | { |
||
322 | $config = array_merge([ |
||
323 | 'USER' => $this->config['username'], |
||
324 | 'PWD' => $this->config['password'], |
||
325 | 'SIGNATURE' => $this->config['signature'], |
||
326 | 'VERSION' => 123, |
||
327 | 'METHOD' => $method, |
||
328 | ], $this->options); |
||
329 | |||
330 | $this->post = $this->post->merge($config) |
||
331 | ->filter(function ($value, $key) use ($method) { |
||
332 | return (($method === 'verifyipn') && ($key === 'METHOD')) ?: $value; |
||
333 | }); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Perform PayPal API request & return response. |
||
338 | * |
||
339 | * @throws \Exception |
||
340 | * |
||
341 | * @return \Psr\Http\Message\StreamInterface |
||
342 | */ |
||
343 | private function makeHttpRequest() |
||
357 | |||
358 | /** |
||
359 | * Function To Perform PayPal API Request. |
||
360 | * |
||
361 | * @param string $method |
||
362 | * |
||
363 | * @throws \Exception |
||
364 | * |
||
365 | * @return array|\Psr\Http\Message\StreamInterface |
||
366 | */ |
||
367 | private function doPayPalRequest($method) |
||
386 | |||
387 | /** |
||
388 | * Parse PayPal NVP Response. |
||
389 | * |
||
390 | * @param string $method |
||
391 | * @param array|\Psr\Http\Message\StreamInterface $response |
||
392 | * |
||
393 | * @return array |
||
394 | */ |
||
395 | private function retrieveData($method, $response) |
||
405 | } |
||
406 |