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