1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MercadoPago; |
4
|
|
|
|
5
|
|
|
use MercadoPago\Exceptions\MercadoPagoException; |
6
|
|
|
use MercadoPago\Http\Client; |
7
|
|
|
use MercadoPago\Http\Response; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class MercadoPago |
11
|
|
|
{ |
12
|
|
|
const VERSION = '0.5.2'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string|null |
16
|
|
|
*/ |
17
|
|
|
protected $clientId = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|null |
21
|
|
|
*/ |
22
|
|
|
protected $clientSecret = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $accessToken = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $sandbox = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Client |
36
|
|
|
*/ |
37
|
|
|
protected $client; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* MercadoPago constructor. |
41
|
|
|
* @param Client $client |
42
|
|
|
*/ |
43
|
36 |
|
public function __construct(Client $client) |
44
|
|
|
{ |
45
|
36 |
|
$this->client = $client; |
46
|
36 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get Http Client. |
50
|
|
|
* @return Client |
51
|
|
|
*/ |
52
|
|
|
public function getClient() |
53
|
|
|
{ |
54
|
|
|
return $this->client; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Enable sandbox mode. |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function enableSandboxMode() |
62
|
|
|
{ |
63
|
|
|
$this->sandbox = true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Disable sandbox mode. |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function disableSandboxMode() |
71
|
|
|
{ |
72
|
|
|
$this->sandbox = false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get current access token. If not set, it will use credentials to request one. |
77
|
|
|
* @return string|null |
78
|
|
|
* @throws MercadoPagoException If server response is not successful. |
79
|
|
|
* @throws RuntimeException If Client ID or Client Secret are not set. |
80
|
|
|
*/ |
81
|
30 |
|
public function getAccessToken() |
82
|
|
|
{ |
83
|
30 |
|
if (!is_null($this->accessToken)) { |
84
|
24 |
|
return $this->accessToken; |
85
|
|
|
} else { |
86
|
6 |
|
if (empty($this->clientId) || empty($this->clientSecret)) { |
87
|
|
|
throw new RuntimeException('Client ID and Client Secret are required to request a new access token.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$data = [ |
91
|
6 |
|
'client_id' => $this->clientId, |
92
|
6 |
|
'client_secret' => $this->clientSecret, |
93
|
2 |
|
'grant_type' => 'client_credentials' |
94
|
4 |
|
]; |
95
|
|
|
|
96
|
6 |
|
$response = $this->client->post( |
97
|
6 |
|
'/oauth/token', |
98
|
4 |
|
$data, |
99
|
6 |
|
['form' => true] |
100
|
4 |
|
); |
101
|
|
|
|
102
|
6 |
|
if ($response->getStatusCode() !== 200) { |
103
|
|
|
throw new MercadoPagoException($response->get('message'), $response->getStatusCode()); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
$this->accessToken = $response->get('access_token'); |
107
|
|
|
|
108
|
6 |
|
return $this->accessToken; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set an access token. |
114
|
|
|
* @param mixed $accessToken |
115
|
|
|
*/ |
116
|
24 |
|
public function setAccessToken($accessToken = null) |
117
|
|
|
{ |
118
|
24 |
|
$this->accessToken = $accessToken; |
119
|
24 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param null $clientId |
123
|
|
|
* @param null $clientSecret |
124
|
|
|
*/ |
125
|
6 |
|
public function setCredentials($clientId = null, $clientSecret = null) |
126
|
|
|
{ |
127
|
6 |
|
$this->clientId = $clientId; |
128
|
6 |
|
$this->clientSecret = $clientSecret; |
129
|
6 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get information for specific authorized payment. |
133
|
|
|
* @param int $id |
134
|
|
|
* @return array |
135
|
|
|
* @throws MercadoPagoException If request failed. |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function getAuthorizedPayment($id) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$response = $this->client->get( |
140
|
|
|
'/authorized_payments/' . $id, |
141
|
|
|
[], |
142
|
|
|
['access_token' => $this->getAccessToken()] |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $this->handleResponse($response); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Refund accredited payment. |
150
|
|
|
* @param int $id |
151
|
|
|
* @return array |
152
|
|
|
* @throws MercadoPagoException If request failed. |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function refundPayment($id) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$response = $this->client->put( |
157
|
|
|
'/collections/' . $id, |
158
|
|
|
[ |
159
|
|
|
'status' => 'refunded', |
160
|
|
|
], |
161
|
|
|
['access_token' => $this->getAccessToken()] |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
return $this->handleResponse($response); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Cancel pending payment |
169
|
|
|
* @param int $id |
170
|
|
|
* @return array |
171
|
|
|
* @throws MercadoPagoException If request failed. |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function cancelPayment($id) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$response = $this->client->put( |
176
|
|
|
'/collections/' . $id, |
177
|
|
|
[ |
178
|
|
|
'status' => 'cancelled', |
179
|
|
|
], |
180
|
|
|
['access_token' => $this->getAccessToken()] |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
return $this->handleResponse($response); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Cancel preapproval payment. |
188
|
|
|
* @param int $id |
189
|
|
|
* @return array |
190
|
|
|
* @throws MercadoPagoException If request failed. |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function cancelPreapprovalPayment($id) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$response = $this->client->put( |
195
|
|
|
'/preapproval/' . $id, |
196
|
|
|
[ |
197
|
|
|
'status' => 'cancelled', |
198
|
|
|
], |
199
|
|
|
['access_token' => $this->getAccessToken()] |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
return $this->handleResponse($response); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Search payments according to filters, with pagination. |
207
|
|
|
* @param array $filters |
208
|
|
|
* @param int $offset |
209
|
|
|
* @param int $limit |
210
|
|
|
* @return array |
211
|
|
|
* @throws MercadoPagoException If request failed. |
212
|
|
|
*/ |
213
|
6 |
|
public function searchPayments(array $filters = [], $offset = 0, $limit = 0) |
214
|
|
|
{ |
215
|
6 |
|
$response = $this->client->get( |
216
|
6 |
|
($this->sandbox ? '/sandbox' : '') . '/collections/search', |
217
|
6 |
|
$filters + compact('limit', 'offset'), |
218
|
6 |
|
['access_token' => $this->getAccessToken()] |
219
|
4 |
|
); |
220
|
|
|
|
221
|
6 |
|
return $this->handleResponse($response); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Create a checkout preference. |
226
|
|
|
* @param array $preference |
227
|
|
|
* @return array |
228
|
|
|
* @throws MercadoPagoException If request failed. |
229
|
|
|
*/ |
230
|
12 |
View Code Duplication |
public function createPreference($preference) |
|
|
|
|
231
|
|
|
{ |
232
|
12 |
|
$response = $this->client->post( |
233
|
12 |
|
'/checkout/preferences', |
234
|
8 |
|
$preference, |
235
|
12 |
|
['access_token' => $this->getAccessToken()] |
236
|
8 |
|
); |
237
|
|
|
|
238
|
12 |
|
return $this->handleResponse($response); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Update a checkout preference. |
243
|
|
|
* @param string $id |
244
|
|
|
* @param array $preference |
245
|
|
|
* @return array |
246
|
|
|
* @throws MercadoPagoException If request failed. |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function updatePreference($id, $preference) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$response = $this->client->put( |
251
|
|
|
'/checkout/preferences/' . $id, |
252
|
|
|
$preference, |
253
|
|
|
['access_token' => $this->getAccessToken()] |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
return $this->handleResponse($response); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get a checkout preference. |
261
|
|
|
* @param string $id |
262
|
|
|
* @return array |
263
|
|
|
* @throws MercadoPagoException If request failed. |
264
|
|
|
*/ |
265
|
6 |
View Code Duplication |
public function getPreference($id) |
|
|
|
|
266
|
|
|
{ |
267
|
6 |
|
$response = $this->client->get( |
268
|
6 |
|
'/checkout/preferences/' . $id, |
269
|
6 |
|
[], |
270
|
6 |
|
['access_token' => $this->getAccessToken()] |
271
|
4 |
|
); |
272
|
|
|
|
273
|
6 |
|
return $this->handleResponse($response); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Create a preapproval payment. |
278
|
|
|
* @param array $preapprovalPayment |
279
|
|
|
* @return array |
280
|
|
|
* @throws MercadoPagoException If request failed. |
281
|
|
|
*/ |
282
|
|
View Code Duplication |
public function createPreapprovalPayment($preapprovalPayment) |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
$response = $this->client->post( |
285
|
|
|
'/preapproval', |
286
|
|
|
$preapprovalPayment, |
287
|
|
|
['access_token' => $this->getAccessToken()] |
288
|
|
|
); |
289
|
|
|
|
290
|
|
|
return $this->handleResponse($response); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get a preapproval payment. |
295
|
|
|
* @param int $id |
296
|
|
|
* @return array |
297
|
|
|
* @throws MercadoPagoException If request failed. |
298
|
|
|
*/ |
299
|
|
View Code Duplication |
public function getPreapprovalPayment($id) |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
$response = $this->client->get( |
302
|
|
|
'/preapproval/' . $id, |
303
|
|
|
[], |
304
|
|
|
['access_token' => $this->getAccessToken()] |
305
|
|
|
); |
306
|
|
|
|
307
|
|
|
return $this->handleResponse($response); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Update a preapproval payment. |
312
|
|
|
* @param int $id |
313
|
|
|
* @param array $payment |
314
|
|
|
* @return array |
315
|
|
|
* @throws MercadoPagoException If request failed. |
316
|
|
|
*/ |
317
|
|
View Code Duplication |
public function updatePreapprovalPayment($id, array $payment = []) |
|
|
|
|
318
|
|
|
{ |
319
|
|
|
$response = $this->client->put( |
320
|
|
|
'/preapproval/' . $id, |
321
|
|
|
$payment, |
322
|
|
|
['access_token' => $this->getAccessToken()] |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
return $this->handleResponse($response); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param Response $response |
330
|
|
|
* @return array |
331
|
|
|
* @throws MercadoPagoException If request failed. |
332
|
|
|
*/ |
333
|
24 |
|
private function handleResponse(Response $response) |
334
|
|
|
{ |
335
|
24 |
|
if ($response->isError()) { |
336
|
11 |
|
throw new MercadoPagoException($response->getError(), $response->getStatusCode()); |
337
|
|
|
} |
338
|
|
|
|
339
|
13 |
|
return $response->getData(); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
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.