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 = []) |
||
116 | |||
117 | /** |
||
118 | * Function to initialize Http Client. |
||
119 | * |
||
120 | * @return HttpClient |
||
121 | */ |
||
122 | protected function setClient() |
||
130 | |||
131 | /** |
||
132 | * Set PayPal API Credentials. |
||
133 | * |
||
134 | * @param array $credentials |
||
135 | * |
||
136 | * @throws \Exception |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public function setApiCredentials($credentials) |
||
141 | { |
||
142 | // Setting Default PayPal Mode If not set |
||
143 | $this->setApiEnvironment($credentials); |
||
144 | |||
145 | // Set API configuration for the PayPal provider |
||
146 | $this->setApiProviderConfiguration($credentials); |
||
147 | |||
148 | // Set default currency. |
||
149 | $this->setCurrency($credentials['currency']); |
||
150 | |||
151 | // Set default payment action. |
||
152 | $this->paymentAction = !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale'; |
||
153 | |||
154 | // Set default locale. |
||
155 | $this->locale = !empty($this->config['locale']) ? $this->config['locale'] : 'en_US'; |
||
156 | |||
157 | // Set PayPal API Endpoint. |
||
158 | $this->apiUrl = $this->config['api_url']; |
||
159 | |||
160 | // Set PayPal IPN Notification URL |
||
161 | $this->notifyUrl = $credentials['notify_url']; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set API environment to be used by PayPal. |
||
166 | * |
||
167 | * @param array $credentials |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | private function setApiEnvironment($credentials) |
||
179 | |||
180 | /** |
||
181 | * Set configuration details for the provider. |
||
182 | * |
||
183 | * @param array $credentials |
||
184 | * |
||
185 | * @throws \Exception |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | private function setApiProviderConfiguration($credentials) |
||
208 | |||
209 | /** |
||
210 | * Setup request data to be sent to PayPal. |
||
211 | * |
||
212 | * @param array $data |
||
213 | * |
||
214 | * @return \Illuminate\Support\Collection |
||
215 | */ |
||
216 | protected function setRequestData(array $data = []) |
||
226 | |||
227 | /** |
||
228 | * Set other/override PayPal API parameters. |
||
229 | * |
||
230 | * @param array $options |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function addOptions(array $options) |
||
240 | |||
241 | /** |
||
242 | * Function to set currency. |
||
243 | * |
||
244 | * @param string $currency |
||
245 | * |
||
246 | * @throws \Exception |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function setCurrency($currency = 'USD') |
||
263 | |||
264 | /** |
||
265 | * Retrieve PayPal IPN Response. |
||
266 | * |
||
267 | * @param array $post |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public function verifyIPN($post) |
||
279 | |||
280 | /** |
||
281 | * Refund PayPal Transaction. |
||
282 | * |
||
283 | * @param string $transaction |
||
284 | * @param float $amount |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | public function refundTransaction($transaction, $amount = 0.00) |
||
303 | |||
304 | /** |
||
305 | * Search Transactions On PayPal. |
||
306 | * |
||
307 | * @param array $post |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | public function searchTransactions($post) |
||
317 | |||
318 | /** |
||
319 | * Create request payload to be sent to PayPal. |
||
320 | * |
||
321 | * @param string $method |
||
322 | */ |
||
323 | private function createRequestPayload($method) |
||
338 | |||
339 | /** |
||
340 | * Perform PayPal API request & return response. |
||
341 | * |
||
342 | * @throws \Exception |
||
343 | * |
||
344 | * @return \Psr\Http\Message\StreamInterface |
||
345 | */ |
||
346 | private function makeHttpRequest() |
||
360 | |||
361 | /** |
||
362 | * Function To Perform PayPal API Request. |
||
363 | * |
||
364 | * @param string $method |
||
365 | * |
||
366 | * @throws \Exception |
||
367 | * |
||
368 | * @return array|\Psr\Http\Message\StreamInterface |
||
369 | */ |
||
370 | private function doPayPalRequest($method) |
||
389 | |||
390 | /** |
||
391 | * Parse PayPal NVP Response. |
||
392 | * |
||
393 | * @param string $method |
||
394 | * @param array|\Psr\Http\Message\StreamInterface $response |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | private function retrieveData($method, $response) |
||
412 | } |
||
413 |