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\PayPalTransactions; |
8
|
|
|
use Srmklive\PayPal\Traits\RecurringProfiles; |
9
|
|
|
|
10
|
|
|
class ExpressCheckout |
11
|
|
|
{ |
12
|
|
|
// Integrate PayPal Request trait |
13
|
|
|
use PayPalAPIRequest, PayPalTransactions, RecurringProfiles; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ExpressCheckout constructor. |
17
|
|
|
* |
18
|
|
|
* @param array $config |
19
|
|
|
* |
20
|
|
|
* @throws \Exception |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $config = []) |
23
|
|
|
{ |
24
|
|
|
// Setting PayPal API Credentials |
25
|
|
|
$this->setConfig($config); |
26
|
|
|
|
27
|
|
|
$this->httpBodyParam = 'form_params'; |
28
|
|
|
|
29
|
|
|
$this->options = []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set ExpressCheckout API endpoints & options. |
34
|
|
|
* |
35
|
|
|
* @param array $credentials |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function setExpressCheckoutOptions($credentials) |
40
|
|
|
{ |
41
|
|
|
// Setting API Endpoints |
42
|
|
|
if ($this->mode === 'sandbox') { |
43
|
|
|
$this->config['api_url'] = !empty($this->config['secret']) ? |
44
|
|
|
'https://api-3t.sandbox.paypal.com/nvp' : 'https://api.sandbox.paypal.com/nvp'; |
45
|
|
|
|
46
|
|
|
$this->config['gateway_url'] = 'https://www.sandbox.paypal.com'; |
47
|
|
|
} else { |
48
|
|
|
$this->config['api_url'] = !empty($this->config['secret']) ? |
49
|
|
|
'https://api-3t.paypal.com/nvp' : 'https://api.paypal.com/nvp'; |
50
|
|
|
|
51
|
|
|
$this->config['gateway_url'] = 'https://www.paypal.com'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Adding params outside sandbox / live array |
55
|
|
|
$this->config['payment_action'] = $credentials['payment_action']; |
56
|
|
|
$this->config['notify_url'] = $credentials['notify_url']; |
57
|
|
|
$this->config['locale'] = $credentials['locale']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set cart item details for PayPal. |
62
|
|
|
* |
63
|
|
|
* @param array $items |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Support\Collection |
66
|
|
|
*/ |
67
|
|
|
protected function setCartItems($items) |
68
|
|
|
{ |
69
|
|
|
return (new Collection($items))->map(function ($item, $num) { |
70
|
|
|
return [ |
71
|
|
|
'L_PAYMENTREQUEST_0_NAME'.$num => $item['name'], |
72
|
|
|
'L_PAYMENTREQUEST_0_AMT'.$num => $item['price'], |
73
|
|
|
'L_PAYMENTREQUEST_0_QTY'.$num => isset($item['qty']) ? $item['qty'] : 1, |
74
|
|
|
]; |
75
|
|
|
})->flatMap(function ($value) { |
76
|
|
|
return $value; |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set Recurring payments details for SetExpressCheckout API call. |
82
|
|
|
* |
83
|
|
|
* @param array $data |
84
|
|
|
* @param bool $subscription |
85
|
|
|
*/ |
86
|
|
|
protected function setExpressCheckoutRecurringPaymentConfig($data, $subscription = false) |
87
|
|
|
{ |
88
|
|
|
$this->post = $this->post->merge([ |
89
|
|
|
'L_BILLINGTYPE0' => ($subscription) ? 'RecurringPayments' : 'MerchantInitiatedBilling', |
90
|
|
|
'L_BILLINGAGREEMENTDESCRIPTION0' => !empty($data['subscription_desc']) ? |
91
|
|
|
$data['subscription_desc'] : $data['invoice_description'], |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set item subtotal if available. |
97
|
|
|
* |
98
|
|
|
* @param array $data |
99
|
|
|
*/ |
100
|
|
|
protected function setItemSubTotal($data) |
101
|
|
|
{ |
102
|
|
|
$this->subtotal = isset($data['subtotal']) ? $data['subtotal'] : $data['total']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set shipping amount if available. |
107
|
|
|
* |
108
|
|
|
* @param array $data |
109
|
|
|
*/ |
110
|
|
|
protected function setShippingAmount($data) |
111
|
|
|
{ |
112
|
|
|
if (isset($data['shipping'])) { |
113
|
|
|
$this->post = $this->post->merge([ |
114
|
|
|
'PAYMENTREQUEST_0_SHIPPINGAMT' => $data['shipping'], |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set shipping discount if available. |
121
|
|
|
* |
122
|
|
|
* @param array $data |
123
|
|
|
*/ |
124
|
|
|
protected function setShippingDiscount($data) |
125
|
|
|
{ |
126
|
|
|
if (isset($data['shipping_discount'])) { |
127
|
|
|
$this->post = $this->post->merge([ |
128
|
|
|
'PAYMENTREQUEST_0_SHIPDISCAMT' => $data['shipping_discount'] * -1, |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Perform a SetExpressCheckout API call on PayPal. |
135
|
|
|
* |
136
|
|
|
* @param array $data |
137
|
|
|
* @param bool $subscription |
138
|
|
|
* |
139
|
|
|
* @throws \Exception |
140
|
|
|
* |
141
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
142
|
|
|
*/ |
143
|
|
|
public function setExpressCheckout($data, $subscription = false) |
144
|
|
|
{ |
145
|
|
|
$this->setItemSubTotal($data); |
146
|
|
|
|
147
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
148
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $this->subtotal, |
149
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
150
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => $this->paymentAction, |
151
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
152
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
153
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
154
|
|
|
'NOSHIPPING' => 1, |
155
|
|
|
'RETURNURL' => $data['return_url'], |
156
|
|
|
'CANCELURL' => $data['cancel_url'], |
157
|
|
|
'LOCALE' => $this->locale, |
158
|
|
|
]); |
159
|
|
|
|
160
|
|
|
$this->setShippingAmount($data); |
161
|
|
|
|
162
|
|
|
$this->setShippingDiscount($data); |
163
|
|
|
|
164
|
|
|
$this->setExpressCheckoutRecurringPaymentConfig($data, $subscription); |
165
|
|
|
|
166
|
|
|
$response = $this->doPayPalRequest('SetExpressCheckout'); |
167
|
|
|
|
168
|
|
|
return collect($response)->merge([ |
169
|
|
|
'paypal_link' => !empty($response['TOKEN']) ? $this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN'] : null, |
170
|
|
|
])->toArray(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Perform a GetExpressCheckoutDetails API call on PayPal. |
175
|
|
|
* |
176
|
|
|
* @param string $token |
177
|
|
|
* |
178
|
|
|
* @throws \Exception |
179
|
|
|
* |
180
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
181
|
|
|
*/ |
182
|
|
|
public function getExpressCheckoutDetails($token) |
183
|
|
|
{ |
184
|
|
|
$this->setRequestData([ |
185
|
|
|
'TOKEN' => $token, |
186
|
|
|
]); |
187
|
|
|
|
188
|
|
|
return $this->doPayPalRequest('GetExpressCheckoutDetails'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Perform DoExpressCheckoutPayment API call on PayPal. |
193
|
|
|
* |
194
|
|
|
* @param array $data |
195
|
|
|
* @param string $token |
196
|
|
|
* @param string $payerid |
197
|
|
|
* |
198
|
|
|
* @throws \Exception |
199
|
|
|
* |
200
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
201
|
|
|
*/ |
202
|
|
|
public function doExpressCheckoutPayment($data, $token, $payerid) |
203
|
|
|
{ |
204
|
|
|
$this->setItemSubTotal($data); |
205
|
|
|
|
206
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
207
|
|
|
'TOKEN' => $token, |
208
|
|
|
'PAYERID' => $payerid, |
209
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $this->subtotal, |
210
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
211
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale', |
212
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
213
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
214
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
215
|
|
|
'PAYMENTREQUEST_0_NOTIFYURL' => $this->notifyUrl, |
216
|
|
|
]); |
217
|
|
|
|
218
|
|
|
$this->setShippingAmount($data); |
219
|
|
|
|
220
|
|
|
return $this->doPayPalRequest('DoExpressCheckoutPayment'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Perform a DoAuthorization API call on PayPal. |
225
|
|
|
* |
226
|
|
|
* @param string $authorization_id Transaction ID |
227
|
|
|
* @param float $amount Amount to capture |
228
|
|
|
* @param array $data Optional request fields |
229
|
|
|
* |
230
|
|
|
* @throws \Exception |
231
|
|
|
* |
232
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
233
|
|
|
*/ |
234
|
|
|
public function doAuthorization($authorization_id, $amount, $data = []) |
235
|
|
|
{ |
236
|
|
|
$this->setRequestData( |
237
|
|
|
array_merge($data, [ |
238
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
239
|
|
|
'AMT' => $amount, |
240
|
|
|
]) |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
return $this->doPayPalRequest('DoAuthorization'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Perform a DoCapture API call on PayPal. |
248
|
|
|
* |
249
|
|
|
* @param string $authorization_id Transaction ID |
250
|
|
|
* @param float $amount Amount to capture |
251
|
|
|
* @param string $complete Indicates whether or not this is the last capture. |
252
|
|
|
* @param array $data Optional request fields |
253
|
|
|
* |
254
|
|
|
* @throws \Exception |
255
|
|
|
* |
256
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
257
|
|
|
*/ |
258
|
|
|
public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = []) |
259
|
|
|
{ |
260
|
|
|
$this->setRequestData( |
261
|
|
|
array_merge($data, [ |
262
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
263
|
|
|
'AMT' => $amount, |
264
|
|
|
'COMPLETETYPE' => $complete, |
265
|
|
|
'CURRENCYCODE' => $this->currency, |
266
|
|
|
]) |
267
|
|
|
); |
268
|
|
|
|
269
|
|
|
return $this->doPayPalRequest('DoCapture'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Perform a DoReauthorization API call on PayPal to reauthorize an existing authorization transaction. |
274
|
|
|
* |
275
|
|
|
* @param string $authorization_id |
276
|
|
|
* @param float $amount |
277
|
|
|
* @param array $data |
278
|
|
|
* |
279
|
|
|
* @throws \Exception |
280
|
|
|
* |
281
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
282
|
|
|
*/ |
283
|
|
|
public function doReAuthorization($authorization_id, $amount, $data = []) |
284
|
|
|
{ |
285
|
|
|
$this->setRequestData( |
286
|
|
|
array_merge($data, [ |
287
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
288
|
|
|
'AMOUNT' => $amount, |
289
|
|
|
]) |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
return $this->doPayPalRequest('DoReauthorization'); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Perform a DoVoid API call on PayPal. |
297
|
|
|
* |
298
|
|
|
* @param string $authorization_id Transaction ID |
299
|
|
|
* @param array $data Optional request fields |
300
|
|
|
* |
301
|
|
|
* @throws \Exception |
302
|
|
|
* |
303
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
304
|
|
|
*/ |
305
|
|
|
public function doVoid($authorization_id, $data = []) |
306
|
|
|
{ |
307
|
|
|
$this->setRequestData( |
308
|
|
|
array_merge($data, [ |
309
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
310
|
|
|
]) |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
return $this->doPayPalRequest('DoVoid'); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Perform a CreateBillingAgreement API call on PayPal. |
318
|
|
|
* |
319
|
|
|
* @param string $token |
320
|
|
|
* |
321
|
|
|
* @throws \Exception |
322
|
|
|
* |
323
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
324
|
|
|
*/ |
325
|
|
|
public function createBillingAgreement($token) |
326
|
|
|
{ |
327
|
|
|
$this->setRequestData([ |
328
|
|
|
'TOKEN' => $token, |
329
|
|
|
]); |
330
|
|
|
|
331
|
|
|
return $this->doPayPalRequest('CreateBillingAgreement'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Perform a CreateRecurringPaymentsProfile API call on PayPal. |
336
|
|
|
* |
337
|
|
|
* @param array $data |
338
|
|
|
* @param string $token |
339
|
|
|
* |
340
|
|
|
* @throws \Exception |
341
|
|
|
* |
342
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
343
|
|
|
*/ |
344
|
|
|
public function createRecurringPaymentsProfile($data, $token) |
345
|
|
|
{ |
346
|
|
|
$this->post = $this->setRequestData($data)->merge([ |
347
|
|
|
'TOKEN' => $token, |
348
|
|
|
]); |
349
|
|
|
|
350
|
|
|
return $this->doPayPalRequest('CreateRecurringPaymentsProfile'); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Perform a GetRecurringPaymentsProfileDetails API call on PayPal. |
355
|
|
|
* |
356
|
|
|
* @param string $id |
357
|
|
|
* |
358
|
|
|
* @throws \Exception |
359
|
|
|
* |
360
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
361
|
|
|
*/ |
362
|
|
|
public function getRecurringPaymentsProfileDetails($id) |
363
|
|
|
{ |
364
|
|
|
$this->setRequestData([ |
365
|
|
|
'PROFILEID' => $id, |
366
|
|
|
]); |
367
|
|
|
|
368
|
|
|
return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails'); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Perform a UpdateRecurringPaymentsProfile API call on PayPal. |
373
|
|
|
* |
374
|
|
|
* @param array $data |
375
|
|
|
* @param string $id |
376
|
|
|
* |
377
|
|
|
* @throws \Exception |
378
|
|
|
* |
379
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
380
|
|
|
*/ |
381
|
|
|
public function updateRecurringPaymentsProfile($data, $id) |
382
|
|
|
{ |
383
|
|
|
$this->post = $this->setRequestData($data)->merge([ |
384
|
|
|
'PROFILEID' => $id, |
385
|
|
|
]); |
386
|
|
|
|
387
|
|
|
return $this->doPayPalRequest('UpdateRecurringPaymentsProfile'); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Change Recurring payment profile status on PayPal. |
392
|
|
|
* |
393
|
|
|
* @param string $id |
394
|
|
|
* @param string $status |
395
|
|
|
* |
396
|
|
|
* @throws \Exception |
397
|
|
|
* |
398
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
399
|
|
|
*/ |
400
|
|
|
protected function manageRecurringPaymentsProfileStatus($id, $status) |
401
|
|
|
{ |
402
|
|
|
$this->setRequestData([ |
403
|
|
|
'PROFILEID' => $id, |
404
|
|
|
'ACTION' => $status, |
405
|
|
|
]); |
406
|
|
|
|
407
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Perform a ManageRecurringPaymentsProfileStatus API call on PayPal to cancel a RecurringPaymentsProfile. |
412
|
|
|
* |
413
|
|
|
* @param string $id |
414
|
|
|
* |
415
|
|
|
* @throws \Exception |
416
|
|
|
* |
417
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
418
|
|
|
*/ |
419
|
|
|
public function cancelRecurringPaymentsProfile($id) |
420
|
|
|
{ |
421
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Cancel'); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Perform a ManageRecurringPaymentsProfileStatus API call on PayPal to suspend a RecurringPaymentsProfile. |
426
|
|
|
* |
427
|
|
|
* @param string $id |
428
|
|
|
* |
429
|
|
|
* @throws \Exception |
430
|
|
|
* |
431
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
432
|
|
|
*/ |
433
|
|
|
public function suspendRecurringPaymentsProfile($id) |
434
|
|
|
{ |
435
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Suspend'); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Perform a ManageRecurringPaymentsProfileStatus API call on PayPal to reactivate a RecurringPaymentsProfile. |
440
|
|
|
* |
441
|
|
|
* @param string $id |
442
|
|
|
* |
443
|
|
|
* @throws \Exception |
444
|
|
|
* |
445
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
446
|
|
|
*/ |
447
|
|
|
public function reactivateRecurringPaymentsProfile($id) |
448
|
|
|
{ |
449
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Reactivate'); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|