|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\eFinanc\Common; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Common\Tools, basic structures |
|
7
|
|
|
* |
|
8
|
|
|
* @category API |
|
9
|
|
|
* @package NFePHP\eFinanc |
|
10
|
|
|
* @copyright Copyright (c) 2018 |
|
11
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
12
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
13
|
|
|
* @link http://github.com/nfephp-org/sped-efinanceira for the canonical source repository |
|
14
|
|
|
*/ |
|
15
|
|
|
use stdClass; |
|
16
|
|
|
use NFePHP\Common\Certificate; |
|
17
|
|
|
use NFePHP\eFinanc\Common\Soap\SoapCurl; |
|
18
|
|
|
use NFePHP\eFinanc\Common\Soap\SoapInterface; |
|
19
|
|
|
use NFePHP\eFinanc\Common\Layouts; |
|
20
|
|
|
use DateTime; |
|
21
|
|
|
|
|
22
|
|
|
class Tools |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $request; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $response; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $path; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $der; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $soapnamespaces = [ |
|
44
|
|
|
'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", |
|
45
|
|
|
'xmlns:sped'=> "http://sped.fazenda.gov.br/" |
|
46
|
|
|
]; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var Certificate |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $certificate; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $tpAmb = 2; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $verAplic; |
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $eventoVersion; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $cnpjDeclarante; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var SoapInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $soap; |
|
71
|
|
|
/** |
|
72
|
|
|
* @var \DateTime |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $date; |
|
75
|
|
|
/** |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $versions; |
|
79
|
|
|
/** |
|
80
|
|
|
* Constructor |
|
81
|
|
|
* @param string $config |
|
82
|
|
|
* @param Certificate $certificate |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __construct( |
|
85
|
|
|
string $config, |
|
86
|
|
|
\NFePHP\Common\Certificate $certificate |
|
87
|
|
|
) { |
|
88
|
|
|
//set properties from config |
|
89
|
|
|
$stdConf = json_decode($config); |
|
90
|
|
|
$this->tpAmb = $stdConf->tpAmb; |
|
91
|
|
|
$this->verAplic = $stdConf->verAplic; |
|
92
|
|
|
$this->eventoVersion = $stdConf->eventoVersion; |
|
93
|
|
|
$this->date = new \DateTime(); |
|
94
|
|
|
$this->cnpjDeclarante = $stdConf->cnpjDeclarante; |
|
95
|
|
|
$this->certificate = $certificate; |
|
96
|
|
|
$this->path = realpath( |
|
97
|
|
|
__DIR__ . '/../../' |
|
98
|
|
|
).'/'; |
|
99
|
|
|
$lay = new Layouts($config); |
|
100
|
|
|
$this->versions = $lay->versions; |
|
101
|
|
|
//loads the encryption certificates distributed in the package |
|
102
|
|
|
$this->der = file_get_contents($this->path.'storage'.DIRECTORY_SEPARATOR.'preprod-efinanc_web.cer'); |
|
103
|
|
|
if ($this->tpAmb == 1) { |
|
104
|
|
|
$this->der = file_get_contents($this->path.'storage'.DIRECTORY_SEPARATOR.'efinanc_web.cer'); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Load Soap Class |
|
110
|
|
|
* @param SoapInterface $soap |
|
111
|
|
|
*/ |
|
112
|
|
|
public function loadSoapClass(SoapInterface $soap) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->soap = $soap; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns a string not subject to repetition indicating |
|
119
|
|
|
* the year, month, day, hour, minute, and second |
|
120
|
|
|
* This return is used to name the log files of the |
|
121
|
|
|
* SOAP communications sent and returned |
|
122
|
|
|
* @return string AAAMMDDHHMMSS |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function generateMark() |
|
125
|
|
|
{ |
|
126
|
|
|
return date('YmdHis'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* This method communicates with the webservice by sending the |
|
131
|
|
|
* pre-mounted message to the designated URL |
|
132
|
|
|
* @param string $body |
|
133
|
|
|
* @param string $method |
|
134
|
|
|
* @param string $url |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function sendRequest($body, $method, $url) |
|
138
|
|
|
{ |
|
139
|
|
|
if (!is_object($this->soap)) { |
|
140
|
|
|
$this->soap = new SoapCurl($this->certificate); |
|
141
|
|
|
} |
|
142
|
|
|
$request = "<soapenv:Envelope "; |
|
143
|
|
|
foreach ($this->soapnamespaces as $key => $xmlns) { |
|
144
|
|
|
$request .= "$key=\"$xmlns\" "; |
|
145
|
|
|
} |
|
146
|
|
|
$action = $this->soapnamespaces['xmlns:sped']."$method"; |
|
147
|
|
|
$request .= ">" |
|
148
|
|
|
. "<soapenv:Header/>" |
|
149
|
|
|
. "<soapenv:Body>" |
|
150
|
|
|
. $body |
|
151
|
|
|
. "</soapenv:Body>" |
|
152
|
|
|
. "</soapenv:Envelope>"; |
|
153
|
|
|
|
|
154
|
|
|
$msgSize = strlen($request); |
|
155
|
|
|
$parameters = [ |
|
156
|
|
|
"Content-Type: text/xml;charset=UTF-8", |
|
157
|
|
|
"SOAPAction: \"$action\"", |
|
158
|
|
|
"Content-length: $msgSize" |
|
159
|
|
|
]; |
|
160
|
|
|
$this->request = $request; |
|
161
|
|
|
$this->response = $this->soap->send( |
|
162
|
|
|
$method, |
|
163
|
|
|
$url, |
|
164
|
|
|
$action, |
|
165
|
|
|
$request, |
|
166
|
|
|
$parameters |
|
167
|
|
|
); |
|
168
|
|
|
return $this->response; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Includes missing or unsupported properties in stdClass |
|
173
|
|
|
* @param stdClass $std |
|
174
|
|
|
* @param array $possible |
|
175
|
|
|
* @return stdClass |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function equilizeParameters(stdClass $std, $possible) |
|
178
|
|
|
{ |
|
179
|
|
|
$arr = get_object_vars($std); |
|
180
|
|
|
foreach ($possible as $key) { |
|
181
|
|
|
if (!array_key_exists($key, $arr)) { |
|
182
|
|
|
$std->$key = null; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
return $std; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|