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