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