1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Srmklive\PayPal\Traits\PayPalRequest as PayPalAPIRequest; |
7
|
|
|
|
8
|
|
|
class ExpressCheckout |
9
|
|
|
{ |
10
|
|
|
// Integrate PayPal Request trait |
11
|
|
|
use PayPalAPIRequest; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PayPal Processor Constructor. |
15
|
|
|
* |
16
|
|
|
* @param array $config |
17
|
|
|
*/ |
18
|
|
|
public function __construct(array $config = []) |
19
|
|
|
{ |
20
|
|
|
// Setting PayPal API Credentials |
21
|
|
|
$this->setConfig($config); |
22
|
|
|
|
23
|
|
|
$this->httpBodyParam = 'form_params'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set Http Client request body param. Should only be called when Guzzle version 5 is used. |
28
|
|
|
*/ |
29
|
|
|
public function setPreviousHttpBodyParam() |
30
|
|
|
{ |
31
|
|
|
$this->httpBodyParam = 'body'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set ExpressCheckout API endpoints & options. |
36
|
|
|
* |
37
|
|
|
* @param array $credentials |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function setExpressCheckoutOptions($credentials) |
42
|
|
|
{ |
43
|
|
|
// Setting API Endpoints |
44
|
|
|
if ($this->mode === 'sandbox') { |
45
|
|
|
$this->config['api_url'] = !empty($this->config['secret']) ? |
46
|
|
|
'https://api-3t.sandbox.paypal.com/nvp' : 'https://api.sandbox.paypal.com/nvp'; |
47
|
|
|
|
48
|
|
|
$this->config['gateway_url'] = 'https://www.sandbox.paypal.com'; |
49
|
|
|
} else { |
50
|
|
|
$this->config['api_url'] = !empty($this->config['secret']) ? |
51
|
|
|
'https://api-3t.paypal.com/nvp' : 'https://api.paypal.com/nvp'; |
52
|
|
|
|
53
|
|
|
$this->config['gateway_url'] = 'https://www.paypal.com'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Adding params outside sandbox / live array |
57
|
|
|
$this->config['payment_action'] = $credentials['payment_action']; |
58
|
|
|
$this->config['notify_url'] = $credentials['notify_url']; |
59
|
|
|
$this->config['locale'] = $credentials['locale']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set cart item details for PayPal. |
64
|
|
|
* |
65
|
|
|
* @param array $items |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Support\Collection |
68
|
|
|
*/ |
69
|
|
|
protected function setCartItems($items) |
70
|
|
|
{ |
71
|
|
|
return (new Collection($items))->map(function ($item, $num) { |
72
|
|
|
return [ |
73
|
|
|
'L_PAYMENTREQUEST_0_NAME'.$num => $item['name'], |
74
|
|
|
'L_PAYMENTREQUEST_0_AMT'.$num => $item['price'], |
75
|
|
|
'L_PAYMENTREQUEST_0_QTY'.$num => $item['qty'], |
76
|
|
|
]; |
77
|
|
|
})->flatMap(function ($value) { |
78
|
|
|
return $value; |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set Recurring payments details for SetExpressCheckout API call. |
84
|
|
|
* |
85
|
|
|
* @param array $data |
86
|
|
|
* @param bool $subscription |
87
|
|
|
*/ |
88
|
|
|
protected function setExpressCheckoutRecurringPaymentConfig($data, $subscription = false) |
89
|
|
|
{ |
90
|
|
|
if ($subscription) { |
91
|
|
|
$this->post = $this->post->merge([ |
92
|
|
|
'L_BILLINGTYPE0' => 'RecurringPayments', |
93
|
|
|
'L_BILLINGAGREEMENTDESCRIPTION0' => !empty($data['subscription_desc']) ? |
94
|
|
|
$data['subscription_desc'] : $data['invoice_description'], |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Function to perform SetExpressCheckout PayPal API operation. |
101
|
|
|
* |
102
|
|
|
* @param array $data |
103
|
|
|
* @param bool $subscription |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function setExpressCheckout($data, $subscription = false) |
108
|
|
|
{ |
109
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
110
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $data['total'], |
111
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
112
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => $this->paymentAction, |
113
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
114
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
115
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
116
|
|
|
'NOSHIPPING' => 1, |
117
|
|
|
'RETURNURL' => $data['return_url'], |
118
|
|
|
'CANCELURL' => $data['cancel_url'], |
119
|
|
|
'LOCALE' => $this->locale, |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$this->setExpressCheckoutRecurringPaymentConfig($data, $subscription); |
123
|
|
|
|
124
|
|
|
$response = $this->doPayPalRequest('SetExpressCheckout'); |
125
|
|
|
|
126
|
|
|
$response['paypal_link'] = empty($response['TOKEN']) ?: |
127
|
|
|
$this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN']; |
128
|
|
|
|
129
|
|
|
return $response; |
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->setRequestData( |
265
|
|
|
array_merge(['TOKEN' => $token], $data) |
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 = (new Collection([ |
298
|
|
|
'PROFILEID' => $id, |
299
|
|
|
]))->merge($data); |
300
|
|
|
|
301
|
|
|
return $this->doPayPalRequest('UpdateRecurringPaymentsProfile'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Function to cancel RecurringPaymentsProfile on PayPal. |
306
|
|
|
* |
307
|
|
|
* @param string $id |
308
|
|
|
* |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
|
|
public function cancelRecurringPaymentsProfile($id) |
312
|
|
|
{ |
313
|
|
|
$this->setRequestData([ |
314
|
|
|
'PROFILEID' => $id, |
315
|
|
|
'ACTION' => 'Cancel', |
316
|
|
|
]); |
317
|
|
|
|
318
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Function to suspend an active RecurringPaymentsProfile on PayPal. |
323
|
|
|
* |
324
|
|
|
* @param string $id |
325
|
|
|
* |
326
|
|
|
* @return array |
327
|
|
|
*/ |
328
|
|
|
public function suspendRecurringPaymentsProfile($id) |
329
|
|
|
{ |
330
|
|
|
$this->setRequestData([ |
331
|
|
|
'PROFILEID' => $id, |
332
|
|
|
'ACTION' => 'Suspend', |
333
|
|
|
]); |
334
|
|
|
|
335
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Function to reactivate a suspended RecurringPaymentsProfile on PayPal. |
340
|
|
|
* |
341
|
|
|
* @param string $id |
342
|
|
|
* |
343
|
|
|
* @return array |
344
|
|
|
*/ |
345
|
|
|
public function reactivateRecurringPaymentsProfile($id) |
346
|
|
|
{ |
347
|
|
|
$this->setRequestData([ |
348
|
|
|
'PROFILEID' => $id, |
349
|
|
|
'ACTION' => 'Reactivate', |
350
|
|
|
]); |
351
|
|
|
|
352
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|