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
|
|
|
if (empty($credentials['payment_action'])) { |
57
|
|
|
$credentials['payment_action'] = 'Sale'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($credentials['locale'])) { |
61
|
|
|
$credentials['locale'] = 'en_US'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Adding params outside sandbox / live array |
65
|
|
|
$this->config['payment_action'] = $credentials['payment_action']; |
66
|
|
|
$this->config['notify_url'] = $credentials['notify_url']; |
67
|
|
|
$this->config['locale'] = $credentials['locale']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set cart item details for PayPal. |
72
|
|
|
* |
73
|
|
|
* @param array $items |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Support\Collection |
76
|
|
|
*/ |
77
|
|
|
protected function setCartItems($items) |
78
|
|
|
{ |
79
|
|
|
return (new Collection($items))->map(function ($item, $num) { |
80
|
|
|
return [ |
81
|
|
|
'L_PAYMENTREQUEST_0_NAME'.$num => $item['name'], |
82
|
|
|
'L_PAYMENTREQUEST_0_AMT'.$num => $item['price'], |
83
|
|
|
'L_PAYMENTREQUEST_0_QTY'.$num => $item['qty'], |
84
|
|
|
]; |
85
|
|
|
})->flatMap(function ($value) { |
86
|
|
|
return $value; |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Function to perform SetExpressCheckout PayPal API operation. |
92
|
|
|
* |
93
|
|
|
* @param array $data |
94
|
|
|
* @param bool $subscription |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function setExpressCheckout($data, $subscription = false) |
99
|
|
|
{ |
100
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
101
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $data['total'], |
102
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
103
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => $this->paymentAction, |
104
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
105
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
106
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
107
|
|
|
'NOSHIPPING' => 1, |
108
|
|
|
'RETURNURL' => $data['return_url'], |
109
|
|
|
'CANCELURL' => $data['cancel_url'], |
110
|
|
|
'LOCALE' => $this->locale, |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
if ($subscription) { |
114
|
|
|
$this->post->merge([ |
115
|
|
|
'L_BILLINGTYPE0' => 'RecurringPayment', |
116
|
|
|
'L_BILLINGAGREEMENTDESCRIPTION0' => !empty($data['subscription_desc']) ? |
117
|
|
|
$data['subscription_desc'] : $data['invoice_description'], |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$response = $this->doPayPalRequest('SetExpressCheckout'); |
122
|
|
|
|
123
|
|
|
$response['paypal_link'] = empty($response['TOKEN']) ?: |
124
|
|
|
$this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN']; |
125
|
|
|
|
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Function to perform GetExpressCheckoutDetails PayPal API operation. |
131
|
|
|
* |
132
|
|
|
* @param string $token |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getExpressCheckoutDetails($token) |
137
|
|
|
{ |
138
|
|
|
$this->setRequestData([ |
139
|
|
|
'TOKEN' => $token, |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
return $this->doPayPalRequest('GetExpressCheckoutDetails'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Function to perform DoExpressCheckoutPayment PayPal API operation. |
147
|
|
|
* |
148
|
|
|
* @param array $data |
149
|
|
|
* @param string $token |
150
|
|
|
* @param string $payerid |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function doExpressCheckoutPayment($data, $token, $payerid) |
155
|
|
|
{ |
156
|
|
|
$this->post = $this->setCartItems($data['items'])->merge([ |
157
|
|
|
'TOKEN' => $token, |
158
|
|
|
'PAYERID' => $payerid, |
159
|
|
|
'PAYMENTREQUEST_0_ITEMAMT' => $data['total'], |
160
|
|
|
'PAYMENTREQUEST_0_AMT' => $data['total'], |
161
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale', |
162
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->currency, |
163
|
|
|
'PAYMENTREQUEST_0_DESC' => $data['invoice_description'], |
164
|
|
|
'PAYMENTREQUEST_0_INVNUM' => $data['invoice_id'], |
165
|
|
|
'PAYMENTREQUEST_0_NOTIFYURL' => $this->notifyUrl, |
166
|
|
|
]); |
167
|
|
|
|
168
|
|
|
return $this->doPayPalRequest('DoExpressCheckoutPayment'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Function to perform DoCapture PayPal API operation. |
173
|
|
|
* |
174
|
|
|
* @param string $authorization_id Transaction ID |
175
|
|
|
* @param float $amount Amount to capture |
176
|
|
|
* @param string $complete Indicates whether or not this is the last capture. |
177
|
|
|
* @param array $data Optional request fields |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = []) |
182
|
|
|
{ |
183
|
|
|
$this->setRequestData( |
184
|
|
|
array_merge($data, [ |
185
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
186
|
|
|
'AMT' => $amount, |
187
|
|
|
'COMPLETETYPE' => $complete, |
188
|
|
|
'CURRENCYCODE' => $this->currency, |
189
|
|
|
]) |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
return $this->doPayPalRequest('DoCapture'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Function to perform DoAuthorization PayPal API operation. |
197
|
|
|
* |
198
|
|
|
* @param string $authorization_id Transaction ID |
199
|
|
|
* @param float $amount Amount to capture |
200
|
|
|
* @param array $data Optional request fields |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function doAuthorization($authorization_id, $amount, $data = []) |
205
|
|
|
{ |
206
|
|
|
$this->setRequestData( |
207
|
|
|
array_merge($data, [ |
208
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
209
|
|
|
'AMT' => $amount, |
210
|
|
|
]) |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
return $this->doPayPalRequest('DoAuthorization'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Function to perform DoVoid PayPal API operation. |
218
|
|
|
* |
219
|
|
|
* @param string $authorization_id Transaction ID |
220
|
|
|
* @param array $data Optional request fields |
221
|
|
|
* |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function doVoid($authorization_id, $data = []) |
225
|
|
|
{ |
226
|
|
|
$this->setRequestData( |
227
|
|
|
array_merge($data, [ |
228
|
|
|
'AUTHORIZATIONID' => $authorization_id, |
229
|
|
|
]) |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
return $this->doPayPalRequest('DoVoid'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Function to perform CreateBillingAgreement PayPal API operation. |
237
|
|
|
* |
238
|
|
|
* @param string $token |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
public function createBillingAgreement($token) |
243
|
|
|
{ |
244
|
|
|
$this->setRequestData([ |
245
|
|
|
'TOKEN' => $token, |
246
|
|
|
]); |
247
|
|
|
|
248
|
|
|
return $this->doPayPalRequest('CreateBillingAgreement'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Function to perform CreateRecurringPaymentsProfile PayPal API operation. |
253
|
|
|
* |
254
|
|
|
* @param array $data |
255
|
|
|
* @param string $token |
256
|
|
|
* |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
|
|
public function createRecurringPaymentsProfile($data, $token) |
260
|
|
|
{ |
261
|
|
|
$this->setRequestData( |
262
|
|
|
array_merge(['TOKEN' => $token], $data) |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
return $this->doPayPalRequest('CreateRecurringPaymentsProfile'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Function to perform GetRecurringPaymentsProfileDetails PayPal API operation. |
270
|
|
|
* |
271
|
|
|
* @param string $id |
272
|
|
|
* |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
public function getRecurringPaymentsProfileDetails($id) |
276
|
|
|
{ |
277
|
|
|
$this->setRequestData([ |
278
|
|
|
'PROFILEID' => $id, |
279
|
|
|
]); |
280
|
|
|
|
281
|
|
|
return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Function to perform UpdateRecurringPaymentsProfile PayPal API operation. |
286
|
|
|
* |
287
|
|
|
* @param array $data |
288
|
|
|
* @param string $id |
289
|
|
|
* |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
|
|
public function updateRecurringPaymentsProfile($data, $id) |
293
|
|
|
{ |
294
|
|
|
$this->post = (new Collection([ |
295
|
|
|
'PROFILEID' => $id, |
296
|
|
|
]))->merge($data); |
297
|
|
|
|
298
|
|
|
return $this->doPayPalRequest('UpdateRecurringPaymentsProfile'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Function to cancel RecurringPaymentsProfile on PayPal. |
303
|
|
|
* |
304
|
|
|
* @param string $id |
305
|
|
|
* |
306
|
|
|
* @return array |
307
|
|
|
*/ |
308
|
|
|
public function cancelRecurringPaymentsProfile($id) |
309
|
|
|
{ |
310
|
|
|
$this->setRequestData([ |
311
|
|
|
'PROFILEID' => $id, |
312
|
|
|
'ACTION' => 'Cancel', |
313
|
|
|
]); |
314
|
|
|
|
315
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Function to suspend an active RecurringPaymentsProfile on PayPal. |
320
|
|
|
* |
321
|
|
|
* @param string $id |
322
|
|
|
* |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
|
|
public function suspendRecurringPaymentsProfile($id) |
326
|
|
|
{ |
327
|
|
|
$this->setRequestData([ |
328
|
|
|
'PROFILEID' => $id, |
329
|
|
|
'ACTION' => 'Suspend', |
330
|
|
|
]); |
331
|
|
|
|
332
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Function to reactivate a suspended RecurringPaymentsProfile on PayPal. |
337
|
|
|
* |
338
|
|
|
* @param string $id |
339
|
|
|
* |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
public function reactivateRecurringPaymentsProfile($id) |
343
|
|
|
{ |
344
|
|
|
$this->setRequestData([ |
345
|
|
|
'PROFILEID' => $id, |
346
|
|
|
'ACTION' => 'Reactivate', |
347
|
|
|
]); |
348
|
|
|
|
349
|
|
|
return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus'); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|