-
Notifications
You must be signed in to change notification settings - Fork 17
/
PagSeguroRequest.php
122 lines (102 loc) · 2.72 KB
/
PagSeguroRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace ValdeirPsr\PagSeguro\Exception;
use DOMDocument;
use DOMXPath;
use Throwable;
use ValdeirPsr\PagSeguro\Domains\Error;
use Curl\Curl;
class PagSeguroRequest extends \Exception
{
private $request;
private $requestBody;
private $errors = [];
public function __construct(Curl $curl, $requestBody, string $message = null, int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->request = $curl;
$this->requestBody = $requestBody;
$this->checkErrors();
}
/**
* @return Curl
*/
public function getRequest(): Curl
{
return $request;
}
/**
* @return mixed Retorna a resposta do servidor
*/
public function getResponse()
{
return $this->request->getResponse();
}
/**
* @return int Retorna o Status Code da resposta
*/
public function getHttpStatus(): int
{
return $this->request->getHttpstatus();
}
/**
* Define o corpo da mensagem enviada
*
* @param mixed $value
*
* @return self
*/
public function setRequestBody($value)
{
$this->requestBody = $value;
return $this;
}
/**
* @return mixed
*/
public function getRequestBody()
{
return $this->requestBody;
}
/**
* @return Error[]
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* Verifica os erros recebidos pela API
*
* @return void
*/
private function checkErrors()
{
$response = $this->getResponse();
if ($response) {
$responseDom = new DOMDocument();
$status = $responseDom->loadXML($response, LIBXML_NOERROR);
if ($status) {
$errors = $responseDom->getElementsByTagName('error');
foreach ($errors as $error) {
$code = $error->getElementsByTagName('code');
$msg = $error->getElementsByTagName('message');
if ($code->count() > 0) {
$code = intval($code->item(0)->textContent);
} else {
$code = 0;
}
if ($msg->count() > 0) {
$msg = trim($msg->item(0)->textContent);
} else {
$msg = $response;
}
$this->errors[] = new Error($msg, $code);
}
} else {
$this->errors[] = new Error($response);
}
} else {
$this->errors[] = new Error('PagSeguroRequest :: Response empty');
}
}
}