1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\Ebanx\Message; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest; |
6
|
|
|
use Omnipay\Common\Exception\InvalidRequestException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract Request |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractRequest extends BaseAbstractRequest |
12
|
|
|
{ |
13
|
|
|
protected $liveEndpoint = 'https://api.ebanx.com.br/ws'; |
14
|
|
|
protected $testEndpoint = 'https://staging.ebanx.com.br/ws'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get the endpoint where the request should be made. |
18
|
|
|
* |
19
|
|
|
* @return string the URL of the endpoint |
20
|
|
|
*/ |
21
|
18 |
|
public function getEndpoint() |
22
|
|
|
{ |
23
|
18 |
|
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the gateway Integration Key. |
28
|
|
|
* |
29
|
|
|
* Authentication is by means of a single secret API key set as |
30
|
|
|
* the integrationKey parameter when creating the gateway object. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
30 |
|
public function getIntegrationKey() |
35
|
|
|
{ |
36
|
30 |
|
return $this->getParameter('integration_key'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set Integration key |
41
|
|
|
* |
42
|
|
|
* @param string $value |
43
|
|
|
* @return AbstractRequest provides a fluent interface. |
44
|
|
|
*/ |
45
|
60 |
|
public function setIntegrationKey($value) |
46
|
|
|
{ |
47
|
60 |
|
return $this->setParameter('integration_key', $value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the boleto due date |
52
|
|
|
* |
53
|
|
|
* @return string boleto due date |
54
|
|
|
*/ |
55
|
3 |
|
public function getBoletoDueDate($format = 'd/m/Y') |
56
|
|
|
{ |
57
|
3 |
|
$value = $this->getParameter('boletoDueDate'); |
58
|
|
|
|
59
|
3 |
|
return $value ? $value->format($format) : null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the boleto due date |
64
|
|
|
* |
65
|
|
|
* @param string $value defaults to atual date + 30 days |
66
|
|
|
* @return AbstractRequest |
67
|
|
|
*/ |
68
|
3 |
|
public function setBoletoDueDate($value) |
69
|
|
|
{ |
70
|
3 |
|
if ($value) { |
71
|
3 |
|
$value = new \DateTime($value, new \DateTimeZone('UTC')); |
72
|
3 |
|
$value = new \DateTime($value->format('Y-m-d\T03:00:00'), new \DateTimeZone('UTC')); |
73
|
|
|
} else { |
74
|
|
|
$value = null; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
return $this->setParameter('boletoDueDate', $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get Document number (CPF or CNPJ). |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
9 |
|
public function getDocumentNumber() |
86
|
|
|
{ |
87
|
9 |
|
return $this->getParameter('documentNumber'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set Document Number (CPF or CNPJ) |
92
|
|
|
* |
93
|
|
|
* Non-numeric characters are stripped out of the document number, so |
94
|
|
|
* it's safe to pass in strings such as "224.158.178-40" etc. |
95
|
|
|
* |
96
|
|
|
* @param string $value Parameter value |
97
|
|
|
* @return AbstractRequest |
98
|
|
|
*/ |
99
|
9 |
|
public function setDocumentNumber($value) |
100
|
|
|
{ |
101
|
|
|
// strip non-numeric characters |
102
|
9 |
|
return $this->setParameter('documentNumber', preg_replace('/\D/', '', $value)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the type of person that is making the payment |
107
|
|
|
* This allow to a payment be made by a company |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
9 |
|
public function getPersonType() |
112
|
|
|
{ |
113
|
9 |
|
return $this->getParameter('personType') ?: 'personal'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set Person Type |
118
|
|
|
* |
119
|
|
|
* @param string $value Person type value |
120
|
|
|
* @return AbstractRequest |
121
|
|
|
*/ |
122
|
6 |
|
public function setPersonType($value) |
123
|
|
|
{ |
124
|
6 |
|
return $this->setParameter('personType', $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the company name that is making the payment |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
6 |
|
public function getCompanyName() |
133
|
|
|
{ |
134
|
6 |
|
return $this->getParameter('companyName'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set Company Name |
139
|
|
|
* |
140
|
|
|
* @param string $value Company name value |
141
|
|
|
* @return AbstractRequest |
142
|
|
|
*/ |
143
|
6 |
|
public function setCompanyName($value) |
144
|
|
|
{ |
145
|
6 |
|
return $this->setParameter('companyName', $value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the split param |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
12 |
|
public function getSplit() |
154
|
|
|
{ |
155
|
12 |
|
return $this->getParameter('split') ?: []; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set Split |
160
|
|
|
* |
161
|
|
|
* @param array $value Array containing the required fields to split the payment |
162
|
|
|
* @return AbstractRequest |
163
|
|
|
*/ |
164
|
6 |
|
public function setSplit($value = []) |
165
|
|
|
{ |
166
|
6 |
|
return $this->setParameter('split', $value); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* A note about the payment. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
6 |
|
public function getNote() |
175
|
|
|
{ |
176
|
6 |
|
return $this->getParameter('note'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set a note about the payment. The value of this parameter |
181
|
|
|
* will be shown along with payment details. |
182
|
|
|
* |
183
|
|
|
* @param string $value Person type value |
184
|
|
|
* @return AbstractRequest |
185
|
|
|
*/ |
186
|
3 |
|
public function setNote($value) |
187
|
|
|
{ |
188
|
3 |
|
return $this->setParameter('note', $value); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get HTTP Method. |
193
|
|
|
* |
194
|
|
|
* This is nearly always POST but can be over-ridden in sub classes. |
195
|
|
|
* |
196
|
|
|
* @return string the HTTP method |
197
|
|
|
*/ |
198
|
6 |
|
public function getHttpMethod() |
199
|
|
|
{ |
200
|
6 |
|
return 'POST'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
6 |
|
public function sendData($data) |
207
|
|
|
{ |
208
|
6 |
|
$response = $this->httpClient->request( |
209
|
6 |
|
$this->getHttpMethod(), |
210
|
6 |
|
$this->getEndpoint(), |
211
|
6 |
|
$this->getHeaders(), |
212
|
6 |
|
json_encode($data) |
213
|
|
|
); |
214
|
|
|
|
215
|
6 |
|
$payload = json_decode($response->getBody()->getContents(), true); |
216
|
|
|
|
217
|
6 |
|
return $this->createResponse($payload); |
218
|
|
|
} |
219
|
|
|
|
220
|
6 |
|
protected function createResponse($data) |
221
|
|
|
{ |
222
|
6 |
|
return $this->response = new Response($this, $data); |
223
|
|
|
} |
224
|
|
|
|
225
|
6 |
|
protected function getHeaders() |
226
|
|
|
{ |
227
|
6 |
|
return []; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get the base data. |
232
|
|
|
* |
233
|
|
|
* Because the Ebanx gateway requires a common of fields for every request |
234
|
|
|
* this function can be called to this common data in the format that the |
235
|
|
|
* API requires. |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
15 |
|
protected function getDefaultParameters() |
240
|
|
|
{ |
241
|
15 |
|
$data = array(); |
242
|
15 |
|
$data['integration_key'] = $this->getIntegrationKey(); |
243
|
|
|
|
244
|
15 |
|
return $data; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get the customer data. |
249
|
|
|
* |
250
|
|
|
* Because the Ebanx gateway uses a common format for passing |
251
|
|
|
* customer data to the API, this function can be called to get the |
252
|
|
|
* data from the associated card object in the format that the |
253
|
|
|
* API requires. |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
6 |
|
protected function getCustomerData() |
258
|
|
|
{ |
259
|
6 |
|
$this->validate('card', 'documentNumber'); |
260
|
6 |
|
$card = $this->getCard(); |
261
|
|
|
|
262
|
6 |
|
$data = array(); |
263
|
6 |
|
$data['name'] = $card->getName(); |
264
|
6 |
|
$data['email'] = $card->getEmail(); |
265
|
6 |
|
$data['document'] = $this->getDocumentNumber(); |
266
|
6 |
|
$data['phone_number'] = $card->getPhone(); |
267
|
|
|
|
268
|
6 |
|
$personType = $this->getPersonType(); |
269
|
|
|
//If you need to create a payment for a company a couple of extra parameters are need. |
270
|
6 |
|
if ($personType == 'business') { |
271
|
3 |
|
$this->validate('companyName'); |
272
|
3 |
|
$data['name'] = $this->getCompanyName(); |
273
|
3 |
|
$data['person_type'] = $personType; |
274
|
3 |
|
$data['responsible']['name'] = $card->getName(); |
275
|
|
|
} |
276
|
6 |
|
return $data; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get the card data. |
281
|
|
|
* |
282
|
|
|
* Because the Ebanx gateway uses a common format for passing |
283
|
|
|
* card data to the API, this function can be called to get the |
284
|
|
|
* data from the associated card object in the format that the |
285
|
|
|
* API requires. |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
9 |
|
protected function getCardData() |
290
|
|
|
{ |
291
|
9 |
|
$card = $this->getCard(); |
292
|
9 |
|
$cardReference = $this->getCardReference(); |
293
|
9 |
|
$data = array(); |
294
|
9 |
|
$data['instalments'] = $this->getInstallments(); |
295
|
|
|
|
296
|
|
|
// We try first for the card reference |
297
|
9 |
|
if ($cardReference) { |
298
|
3 |
|
$data['creditcard']['token'] = $cardReference; |
299
|
6 |
|
} elseif ($card) { |
300
|
3 |
|
$card->validate(); |
301
|
3 |
|
$data['creditcard']['card_name'] = $card->getName(); |
302
|
3 |
|
$data['creditcard']['card_number'] = $card->getNumber(); |
303
|
3 |
|
$data['creditcard']['card_due_date'] = $card->getExpiryMonth() . '/' . $card->getExpiryYear(); |
304
|
3 |
|
if ($card->getCvv()) { |
305
|
3 |
|
$data['creditcard']['card_cvv'] = $card->getCvv(); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
9 |
|
return $data; |
310
|
|
|
} |
311
|
|
|
/** |
312
|
|
|
* Get the boleto data. |
313
|
|
|
* |
314
|
|
|
* Because the Ebanx gateway uses a common format for passing |
315
|
|
|
* boleto data to the API, this function can be called to get the |
316
|
|
|
* data from the associated request object in the format that the |
317
|
|
|
* API requires. |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
protected function getBoletoData() |
322
|
|
|
{ |
323
|
|
|
$this->validate('boletoDueDate'); |
324
|
|
|
|
325
|
|
|
$data = array(); |
326
|
|
|
$data['due_date'] = $this->getBoletoDueDate(); |
327
|
|
|
return $data; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Get the address data. |
332
|
|
|
* |
333
|
|
|
* Because the Ebanx gateway uses a common format for passing |
334
|
|
|
* address data to the API, this function can be called to get the |
335
|
|
|
* data from the associated card object in the format that the |
336
|
|
|
* API requires. |
337
|
|
|
* |
338
|
|
|
* @return array |
339
|
|
|
*/ |
340
|
9 |
|
protected function getAddressData() |
341
|
|
|
{ |
342
|
9 |
|
$card = $this->getCard(); |
343
|
9 |
|
$address = array_map('trim', explode(',', $card->getAddress1())); |
344
|
|
|
|
345
|
9 |
|
$data = array(); |
346
|
9 |
|
$data['address'] = $address[0]; |
347
|
9 |
|
$data['street_number'] = isset($address[1]) ? $address[1] : ''; |
348
|
9 |
|
$data['street_complement'] = isset($address[2]) ? $address[2] : ''; |
349
|
9 |
|
$data['city'] = $card->getCity(); |
350
|
9 |
|
$data['state'] = $card->getState(); |
351
|
9 |
|
$data['country'] = $card->getCountry(); |
352
|
9 |
|
$data['zipcode'] = $card->getPostcode(); |
353
|
|
|
|
354
|
9 |
|
return $data; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Get the split data. |
359
|
|
|
* |
360
|
|
|
* Because the Ebanx gateway uses a common format for passing |
361
|
|
|
* split payment data to the API, this function can be called to get the |
362
|
|
|
* data from the associated request in the format that the |
363
|
|
|
* API requires. |
364
|
|
|
* |
365
|
|
|
* @return array |
366
|
|
|
*/ |
367
|
9 |
|
protected function getSplitData() |
368
|
|
|
{ |
369
|
9 |
|
$split = $this->getSplit(); |
370
|
9 |
|
return !empty($split) ? ['split' => $split] : []; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Get the payment data. |
375
|
|
|
* |
376
|
|
|
* Because the Ebanx gateway uses a common format for passing |
377
|
|
|
* payment data to the API, this function can be called to get the |
378
|
|
|
* data from the associated card object in the format that the |
379
|
|
|
* API requires. |
380
|
|
|
* |
381
|
|
|
* @return array |
382
|
|
|
*/ |
383
|
3 |
|
protected function getPaymentData($aditionalPaymentData = []) |
384
|
|
|
{ |
385
|
3 |
|
$this->validate('transactionId', 'currency', 'amount', 'paymentMethod'); |
386
|
|
|
|
387
|
3 |
|
$customerData = $this->getCustomerData(); |
388
|
3 |
|
$addressData = $this->getAddressData(); |
389
|
3 |
|
$splitData = $this->getSplitData(); |
390
|
|
|
|
391
|
3 |
|
$paymentData = array(); |
392
|
3 |
|
$paymentData['merchant_payment_code'] = $this->getTransactionId(); |
393
|
3 |
|
$paymentData['currency_code'] = $this->getCurrency(); |
394
|
3 |
|
$paymentData['amount_total'] = $this->getAmount(); |
395
|
3 |
|
$paymentData['payment_type_code'] = $this->getPaymentMethod(); |
396
|
|
|
|
397
|
3 |
|
if ($notifyUrl = $this->getNotifyUrl()) { |
398
|
|
|
$paymentData['notification_url'] = $notifyUrl; |
399
|
|
|
} |
400
|
3 |
|
if ($returnUrl = $this->getReturnUrl()) { |
401
|
|
|
$paymentData['redirect_url'] = $returnUrl; |
402
|
|
|
} |
403
|
|
|
|
404
|
3 |
|
if ($paymentNote = $this->getNote()) { |
405
|
|
|
$paymentData['note'] = $paymentNote; |
406
|
|
|
} |
407
|
|
|
|
408
|
3 |
|
$paymentData = array_merge( |
409
|
3 |
|
$customerData, |
410
|
3 |
|
$addressData, |
411
|
3 |
|
$paymentData, |
412
|
3 |
|
$splitData, |
413
|
3 |
|
$aditionalPaymentData |
414
|
|
|
); |
415
|
|
|
|
416
|
3 |
|
return ['payment' => $paymentData]; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Get installments. |
421
|
|
|
* |
422
|
|
|
* @return integer the number of installments |
423
|
|
|
*/ |
424
|
12 |
|
public function getInstallments() |
425
|
|
|
{ |
426
|
12 |
|
return $this->getParameter('installments') ?: 1; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Set Installments. |
431
|
|
|
* |
432
|
|
|
* The number must be between 1 and 12. |
433
|
|
|
* If the payment method is boleto defaults to 1. |
434
|
|
|
* |
435
|
|
|
* @param integer $value |
436
|
|
|
* @return AuthorizeRequest provides a fluent interface. |
437
|
|
|
*/ |
438
|
6 |
|
public function setInstallments($value) |
439
|
|
|
{ |
440
|
6 |
|
return $this->setParameter('installments', (int) $value); |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|