1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Srmklive\PayPal\Traits\PayPalRequest as PayPalAPIRequest; |
7
|
|
|
use Srmklive\PayPal\Traits\RecurringProfiles; |
8
|
|
|
|
9
|
|
|
class ExpressCheckout |
10
|
|
|
{ |
11
|
|
|
// Integrate PayPal Request trait |
12
|
|
|
use PayPalAPIRequest, RecurringProfiles; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PayPal Processor Constructor. |
16
|
|
|
* |
17
|
|
|
* @param array $config |
18
|
|
|
*/ |
19
|
|
|
public function __construct(array $config = []) |
20
|
|
|
{ |
21
|
|
|
// Setting PayPal API Credentials |
22
|
|
|
$this->setConfig($config); |
23
|
|
|
|
24
|
|
|
$this->httpBodyParam = 'form_params'; |
25
|
|
|
|
26
|
|
|
$this->options = []; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set Http Client request body param. Should only be called when Guzzle version 5 is used. |
31
|
|
|
*/ |
32
|
|
|
public function setPreviousHttpBodyParam() |
33
|
|
|
{ |
34
|
|
|
$this->httpBodyParam = 'body'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set ExpressCheckout API endpoints & options. |
39
|
|
|
* |
40
|
|
|
* @param array $credentials |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function setExpressCheckoutOptions($credentials) |
45
|
|
|
{ |
46
|
|
|
// Setting API Endpoints |
47
|
|
|
if ($this->mode === 'sandbox') { |
48
|
|
|
$this->config['api_url'] = !empty($this->config['secret']) ? |
49
|
|
|
'https://api-3t.sandbox.paypal.com/nvp' : 'https://api.sandbox.paypal.com/nvp'; |
50
|
|
|
|
51
|
|
|
$this->config['gateway_url'] = 'https://www.sandbox.paypal.com'; |
52
|
|
|
} else { |
53
|
|
|
$this->config['api_url'] = !empty($this->config['secret']) ? |
54
|
|
|
'https://api-3t.paypal.com/nvp' : 'https://api.paypal.com/nvp'; |
55
|
|
|
|
56
|
|
|
$this->config['gateway_url'] = 'https://www.paypal.com'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Adding params outside sandbox / live array |
60
|
|
|
$this->config['payment_action'] = $credentials['payment_action']; |
61
|
|
|
$this->config['notify_url'] = $credentials['notify_url']; |
62
|
|
|
$this->config['locale'] = $credentials['locale']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set cart item details for PayPal. |
67
|
|
|
* |
68
|
|
|
* @param array $items |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Support\Collection |
71
|
|
|
*/ |
72
|
|
|
protected function setCartItems($items) |
73
|
|
|
{ |
74
|
|
|
return (new Collection($items))->map(function ($item, $num) { |
75
|
|
|
return [ |
76
|
|
|
'L_PAYMENTREQUEST_0_NAME'.$num => $item['name'], |
77
|
|
|
'L_PAYMENTREQUEST_0_AMT'.$num => $item['price'], |
78
|
|
|
'L_PAYMENTREQUEST_0_QTY'.$num => isset($item['qty']) ? $item['qty'] : 1, |
79
|
|
|
]; |
80
|
|
|
})->flatMap(function ($value) { |
81
|
|
|
return $value; |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set Recurring payments details for SetExpressCheckout API call. |
87
|
|
|
* |
88
|
|
|
* @param array $data |
89
|
|
|
* @param bool $subscription |
90
|
|
|
*/ |
91
|
|
|
protected function setExpressCheckoutRecurringPaymentConfig($data, $subscription = false) |
92
|
|
|
{ |
93
|
|
|
$this->post = $this->post->merge([ |
94
|
|
|
'L_BILLINGTYPE0' => ($subscription) ? 'RecurringPayments' : 'MerchantInitiatedBilling', |
95
|
|
|
'L_BILLINGAGREEMENTDESCRIPTION0' => !empty($data['subscription_desc']) ? |
96
|
|
|
$data['subscription_desc'] : $data['invoice_description'], |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Function to perform SetExpressCheckout PayPal API operation. |
102
|
|
|
* |
103
|
|
|
* @param array $data |
104
|
|
|
* @param bool $subscription |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function setExpressCheckout($data, $subscription = false) |
109
|
|
|
{ |
110
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
111
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $data['total'], |
112
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
113
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => $this->paymentAction, |
114
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
115
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
116
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
117
|
|
|
'NOSHIPPING' => 1, |
118
|
|
|
'RETURNURL' => $data['return_url'], |
119
|
|
|
'CANCELURL' => $data['cancel_url'], |
120
|
|
|
'LOCALE' => $this->locale, |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$this->setExpressCheckoutRecurringPaymentConfig($data, $subscription); |
124
|
|
|
|
125
|
|
|
return collect( |
126
|
|
|
$this->doPayPalRequest('SetExpressCheckout') |
127
|
|
|
)->merge([ |
128
|
|
|
'paypal_link' => !empty($response['TOKEN']) ? $this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN'] : null |
|
|
|
|
129
|
|
|
])->toArray(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Function to perform GetExpressCheckoutDetails PayPal API operation. |
134
|
|
|
* |
135
|
|
|
* @param string $token |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getExpressCheckoutDetails($token) |
140
|
|
|
{ |
141
|
|
|
$this->setRequestData([ |
142
|
|
|
'TOKEN' => $token, |
143
|
|
|
]); |
144
|
|
|
|
145
|
|
|
return $this->doPayPalRequest('GetExpressCheckoutDetails'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Function to perform DoExpressCheckoutPayment PayPal API operation. |
150
|
|
|
* |
151
|
|
|
* @param array $data |
152
|
|
|
* @param string $token |
153
|
|
|
* @param string $payerid |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function doExpressCheckoutPayment($data, $token, $payerid) |
158
|
|
|
{ |
159
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
160
|
|
|
'TOKEN' => $token, |
161
|
|
|
'PAYERID' => $payerid, |
162
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $data['total'], |
163
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
164
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale', |
165
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
166
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
167
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
168
|
|
|
'PAYMENTREQUEST_0_NOTIFYURL' => $this->notifyUrl, |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
return $this->doPayPalRequest('DoExpressCheckoutPayment'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Function to perform DoCapture PayPal API operation. |
176
|
|
|
* |
177
|
|
|
* @param string $authorization_id Transaction ID |
178
|
|
|
* @param float $amount Amount to capture |
179
|
|
|
* @param string $complete Indicates whether or not this is the last capture. |
180
|
|
|
* @param array $data Optional request fields |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = []) |
185
|
|
|
{ |
186
|
|
|
$this->setRequestData( |
187
|
|
|
array_merge($data, [ |
188
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
189
|
|
|
'AMT' => $amount, |
190
|
|
|
'COMPLETETYPE' => $complete, |
191
|
|
|
'CURRENCYCODE' => $this->currency, |
192
|
|
|
]) |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
return $this->doPayPalRequest('DoCapture'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Function to perform DoAuthorization PayPal API operation. |
200
|
|
|
* |
201
|
|
|
* @param string $authorization_id Transaction ID |
202
|
|
|
* @param float $amount Amount to capture |
203
|
|
|
* @param array $data Optional request fields |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function doAuthorization($authorization_id, $amount, $data = []) |
208
|
|
|
{ |
209
|
|
|
$this->setRequestData( |
210
|
|
|
array_merge($data, [ |
211
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
212
|
|
|
'AMT' => $amount, |
213
|
|
|
]) |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
return $this->doPayPalRequest('DoAuthorization'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Function to perform DoVoid PayPal API operation. |
221
|
|
|
* |
222
|
|
|
* @param string $authorization_id Transaction ID |
223
|
|
|
* @param array $data Optional request fields |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public function doVoid($authorization_id, $data = []) |
228
|
|
|
{ |
229
|
|
|
$this->setRequestData( |
230
|
|
|
array_merge($data, [ |
231
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
232
|
|
|
]) |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
return $this->doPayPalRequest('DoVoid'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Function to perform CreateBillingAgreement PayPal API operation. |
240
|
|
|
* |
241
|
|
|
* @param string $token |
242
|
|
|
* |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
|
|
public function createBillingAgreement($token) |
246
|
|
|
{ |
247
|
|
|
$this->setRequestData([ |
248
|
|
|
'TOKEN' => $token, |
249
|
|
|
]); |
250
|
|
|
|
251
|
|
|
return $this->doPayPalRequest('CreateBillingAgreement'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Function to perform CreateRecurringPaymentsProfile PayPal API operation. |
256
|
|
|
* |
257
|
|
|
* @param array $data |
258
|
|
|
* @param string $token |
259
|
|
|
* |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
public function createRecurringPaymentsProfile($data, $token) |
263
|
|
|
{ |
264
|
|
|
$this->post = $this->setRequestData($data)->merge([ |
265
|
|
|
'TOKEN' => $token, |
266
|
|
|
]); |
267
|
|
|
|
268
|
|
|
return $this->doPayPalRequest('CreateRecurringPaymentsProfile'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Function to perform GetRecurringPaymentsProfileDetails PayPal API operation. |
273
|
|
|
* |
274
|
|
|
* @param string $id |
275
|
|
|
* |
276
|
|
|
* @return array |
277
|
|
|
*/ |
278
|
|
|
public function getRecurringPaymentsProfileDetails($id) |
279
|
|
|
{ |
280
|
|
|
$this->setRequestData([ |
281
|
|
|
'PROFILEID' => $id, |
282
|
|
|
]); |
283
|
|
|
|
284
|
|
|
return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Function to perform UpdateRecurringPaymentsProfile PayPal API operation. |
289
|
|
|
* |
290
|
|
|
* @param array $data |
291
|
|
|
* @param string $id |
292
|
|
|
* |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
|
|
public function updateRecurringPaymentsProfile($data, $id) |
296
|
|
|
{ |
297
|
|
|
$this->post = $this->setRequestData($data)->merge([ |
298
|
|
|
'PROFILEID' => $id, |
299
|
|
|
]); |
300
|
|
|
|
301
|
|
|
return $this->doPayPalRequest('UpdateRecurringPaymentsProfile'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Change Recurring payment profile status on PayPal. |
306
|
|
|
* |
307
|
|
|
* @param string $id |
308
|
|
|
* @param string $status |
309
|
|
|
* |
310
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
311
|
|
|
*/ |
312
|
|
|
protected function manageRecurringPaymentsProfileStatus($id, $status) |
313
|
|
|
{ |
314
|
|
|
$this->setRequestData([ |
315
|
|
|
'PROFILEID' => $id, |
316
|
|
|
'ACTION' => $status, |
317
|
|
|
]); |
318
|
|
|
|
319
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Cancel RecurringPaymentsProfile on PayPal. |
324
|
|
|
* |
325
|
|
|
* @param string $id |
326
|
|
|
* |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
|
|
public function cancelRecurringPaymentsProfile($id) |
330
|
|
|
{ |
331
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Cancel'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Suspend an active RecurringPaymentsProfile on PayPal. |
336
|
|
|
* |
337
|
|
|
* @param string $id |
338
|
|
|
* |
339
|
|
|
* @return array |
340
|
|
|
*/ |
341
|
|
|
public function suspendRecurringPaymentsProfile($id) |
342
|
|
|
{ |
343
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Suspend'); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Reactivate a suspended RecurringPaymentsProfile on PayPal. |
348
|
|
|
* |
349
|
|
|
* @param string $id |
350
|
|
|
* |
351
|
|
|
* @return array |
352
|
|
|
*/ |
353
|
|
|
public function reactivateRecurringPaymentsProfile($id) |
354
|
|
|
{ |
355
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Reactivate'); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.