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
|
|
|
$response = $this->doPayPalRequest('SetExpressCheckout'); |
126
|
|
|
|
127
|
|
|
if (isset($response['TOKEN'])) { |
128
|
|
|
$response['paypal_link'] = $this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $response; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Function to perform GetExpressCheckoutDetails PayPal API operation. |
136
|
|
|
* |
137
|
|
|
* @param string $token |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getExpressCheckoutDetails($token) |
142
|
|
|
{ |
143
|
|
|
$this->setRequestData([ |
144
|
|
|
'TOKEN' => $token, |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
return $this->doPayPalRequest('GetExpressCheckoutDetails'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Function to perform DoExpressCheckoutPayment PayPal API operation. |
152
|
|
|
* |
153
|
|
|
* @param array $data |
154
|
|
|
* @param string $token |
155
|
|
|
* @param string $payerid |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function doExpressCheckoutPayment($data, $token, $payerid) |
160
|
|
|
{ |
161
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
162
|
|
|
'TOKEN' => $token, |
163
|
|
|
'PAYERID' => $payerid, |
164
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $data['total'], |
165
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
166
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale', |
167
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
168
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
169
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
170
|
|
|
'PAYMENTREQUEST_0_NOTIFYURL' => $this->notifyUrl, |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
return $this->doPayPalRequest('DoExpressCheckoutPayment'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Function to perform DoCapture PayPal API operation. |
178
|
|
|
* |
179
|
|
|
* @param string $authorization_id Transaction ID |
180
|
|
|
* @param float $amount Amount to capture |
181
|
|
|
* @param string $complete Indicates whether or not this is the last capture. |
182
|
|
|
* @param array $data Optional request fields |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = []) |
187
|
|
|
{ |
188
|
|
|
$this->setRequestData( |
189
|
|
|
array_merge($data, [ |
190
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
191
|
|
|
'AMT' => $amount, |
192
|
|
|
'COMPLETETYPE' => $complete, |
193
|
|
|
'CURRENCYCODE' => $this->currency, |
194
|
|
|
]) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
return $this->doPayPalRequest('DoCapture'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Function to perform DoAuthorization PayPal API operation. |
202
|
|
|
* |
203
|
|
|
* @param string $authorization_id Transaction ID |
204
|
|
|
* @param float $amount Amount to capture |
205
|
|
|
* @param array $data Optional request fields |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function doAuthorization($authorization_id, $amount, $data = []) |
210
|
|
|
{ |
211
|
|
|
$this->setRequestData( |
212
|
|
|
array_merge($data, [ |
213
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
214
|
|
|
'AMT' => $amount, |
215
|
|
|
]) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
return $this->doPayPalRequest('DoAuthorization'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Function to perform DoVoid PayPal API operation. |
223
|
|
|
* |
224
|
|
|
* @param string $authorization_id Transaction ID |
225
|
|
|
* @param array $data Optional request fields |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
public function doVoid($authorization_id, $data = []) |
230
|
|
|
{ |
231
|
|
|
$this->setRequestData( |
232
|
|
|
array_merge($data, [ |
233
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
234
|
|
|
]) |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
return $this->doPayPalRequest('DoVoid'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Function to perform CreateBillingAgreement PayPal API operation. |
242
|
|
|
* |
243
|
|
|
* @param string $token |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
public function createBillingAgreement($token) |
248
|
|
|
{ |
249
|
|
|
$this->setRequestData([ |
250
|
|
|
'TOKEN' => $token, |
251
|
|
|
]); |
252
|
|
|
|
253
|
|
|
return $this->doPayPalRequest('CreateBillingAgreement'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Function to perform CreateRecurringPaymentsProfile PayPal API operation. |
258
|
|
|
* |
259
|
|
|
* @param array $data |
260
|
|
|
* @param string $token |
261
|
|
|
* |
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
|
|
public function createRecurringPaymentsProfile($data, $token) |
265
|
|
|
{ |
266
|
|
|
$this->post = $this->setRequestData($data)->merge([ |
267
|
|
|
'TOKEN' => $token, |
268
|
|
|
]); |
269
|
|
|
|
270
|
|
|
return $this->doPayPalRequest('CreateRecurringPaymentsProfile'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Function to perform GetRecurringPaymentsProfileDetails PayPal API operation. |
275
|
|
|
* |
276
|
|
|
* @param string $id |
277
|
|
|
* |
278
|
|
|
* @return array |
279
|
|
|
*/ |
280
|
|
|
public function getRecurringPaymentsProfileDetails($id) |
281
|
|
|
{ |
282
|
|
|
$this->setRequestData([ |
283
|
|
|
'PROFILEID' => $id, |
284
|
|
|
]); |
285
|
|
|
|
286
|
|
|
return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Function to perform UpdateRecurringPaymentsProfile PayPal API operation. |
291
|
|
|
* |
292
|
|
|
* @param array $data |
293
|
|
|
* @param string $id |
294
|
|
|
* |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
|
|
public function updateRecurringPaymentsProfile($data, $id) |
298
|
|
|
{ |
299
|
|
|
$this->post = $this->setRequestData($data)->merge([ |
300
|
|
|
'PROFILEID' => $id, |
301
|
|
|
]); |
302
|
|
|
|
303
|
|
|
return $this->doPayPalRequest('UpdateRecurringPaymentsProfile'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Change Recurring payment profile status on PayPal. |
308
|
|
|
* |
309
|
|
|
* @param string $id |
310
|
|
|
* @param string $status |
311
|
|
|
* |
312
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
313
|
|
|
*/ |
314
|
|
|
protected function manageRecurringPaymentsProfileStatus($id, $status) |
315
|
|
|
{ |
316
|
|
|
$this->setRequestData([ |
317
|
|
|
'PROFILEID' => $id, |
318
|
|
|
'ACTION' => $status, |
319
|
|
|
]); |
320
|
|
|
|
321
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Cancel RecurringPaymentsProfile on PayPal. |
326
|
|
|
* |
327
|
|
|
* @param string $id |
328
|
|
|
* |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
|
|
public function cancelRecurringPaymentsProfile($id) |
332
|
|
|
{ |
333
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Cancel'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Suspend an active RecurringPaymentsProfile on PayPal. |
338
|
|
|
* |
339
|
|
|
* @param string $id |
340
|
|
|
* |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
|
|
public function suspendRecurringPaymentsProfile($id) |
344
|
|
|
{ |
345
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Suspend'); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Reactivate a suspended RecurringPaymentsProfile on PayPal. |
350
|
|
|
* |
351
|
|
|
* @param string $id |
352
|
|
|
* |
353
|
|
|
* @return array |
354
|
|
|
*/ |
355
|
|
|
public function reactivateRecurringPaymentsProfile($id) |
356
|
|
|
{ |
357
|
|
|
return $this->manageRecurringPaymentsProfileStatus($id, 'Reactivate'); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|