1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\EFDReinf; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Classe Tools, performs communication with the EFDReinf webservice |
7
|
|
|
* |
8
|
|
|
* @category API |
9
|
|
|
* @package NFePHP\EFDReinf\Tools |
10
|
|
|
* @copyright Copyright (c) 2017 |
11
|
|
|
* @license https://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3 |
12
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3 |
13
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT |
14
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
15
|
|
|
* @link http://github.com/nfephp-org/sped-efdreinf for the canonical source repository |
16
|
|
|
*/ |
17
|
|
|
use NFePHP\Common\Certificate; |
18
|
|
|
use NFePHP\Common\Validator; |
19
|
|
|
use NFePHP\EFDReinf\Common\Tools as ToolsBase; |
20
|
|
|
use NFePHP\EFDReinf\Common\FactoryInterface; |
21
|
|
|
use NFePHP\EFDReinf\Common\Soap\SoapCurl; |
22
|
|
|
use NFePHP\EFDReinf\Common\Soap\SoapInterface; |
23
|
|
|
use NFePHP\EFDReinf\Exception\ProcessException; |
24
|
|
|
|
25
|
|
|
class Tools extends ToolsBase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $lastRequest; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $lastResponse; |
35
|
|
|
/** |
36
|
|
|
* @var SoapInterface |
37
|
|
|
*/ |
38
|
|
|
protected $soap; |
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $soapnamespaces = [ |
43
|
|
|
'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", |
44
|
|
|
'xmlns:sped'=> "http://sped.fazenda.gov.br/" |
45
|
|
|
]; |
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $uri = [ |
50
|
|
|
'1' => '', |
51
|
|
|
'2' => 'https://preprodefdreinf.receita.fazenda.gov.br/RecepcaoLoteReinf.svc', |
52
|
|
|
'3' => 'https://preprodefdreinf.receita.fazenda.gov.br/RecepcaoLoteReinf.svc' |
53
|
|
|
]; |
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $action; |
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $method; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor |
65
|
|
|
* @param string $config |
66
|
|
|
* @param Certificate $certificate |
67
|
|
|
*/ |
68
|
|
|
public function __construct($config, Certificate $certificate) |
69
|
|
|
{ |
70
|
|
|
parent::__construct($config, $certificate); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* SOAP communication dependency injection |
75
|
|
|
* @param SoapInterface $soap |
76
|
|
|
*/ |
77
|
|
|
public function loadSoapClass(SoapInterface $soap) |
78
|
|
|
{ |
79
|
|
|
$this->soap = $soap; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Event batch query |
84
|
|
|
* @param string $protocolo |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function consultaLoteEventos($protocolo) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Send batch of events |
94
|
|
|
* @param array $eventos |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function enviarLoteEventos($eventos = []) |
98
|
|
|
{ |
99
|
|
|
if (empty($eventos)) { |
100
|
|
|
return ''; |
101
|
|
|
} |
102
|
|
|
//check number of events |
103
|
|
|
$nEvt = count($eventos); |
104
|
|
|
if ($nEvt > 100) { |
105
|
|
|
throw ProcessException::wrongArgument(2000, $nEvt); |
106
|
|
|
} |
107
|
|
|
$this->method = "ReceberLoteEventos"; |
108
|
|
|
$this->action = "http://sped.fazenda.gov.br/RecepcaoLoteReinf/ReceberLoteEventos"; |
109
|
|
|
$xml = ""; |
110
|
|
|
foreach ($eventos as $evt) { |
111
|
|
|
if (!is_a($evt, '\NFePHP\EFDReinf\Common\FactoryInterface')) { |
112
|
|
|
throw ProcessException::wrongArgument(2002, ''); |
113
|
|
|
} |
114
|
|
|
$this->checkCertificate($evt); |
115
|
|
|
$xml .= "<evento id=\"".$evt->getId()."\">"; |
116
|
|
|
$xml .= $evt->toXML(); |
117
|
|
|
$xml .= "</evento>"; |
118
|
|
|
} |
119
|
|
|
//build request |
120
|
|
|
$request = "<Reinf xmlns=\"http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v" |
121
|
|
|
. $this->serviceVersion."\" >" |
122
|
|
|
. "<loteEventos>" |
123
|
|
|
. $xml |
124
|
|
|
. "</loteEventos>" |
125
|
|
|
. "</Reinf>"; |
126
|
|
|
//validate requisition with XSD |
127
|
|
|
$xsd = $this->path |
128
|
|
|
. "schemes/comunicacao/v$this->serviceVersion/" |
129
|
|
|
. $this->serviceXsd['EnvioLoteEventos']['name']; |
130
|
|
|
Validator::isValid($request, $xsd); |
131
|
|
|
//build soap body |
132
|
|
|
$body = "<sped:ReceberLoteEventos>" |
133
|
|
|
. "<sped:loteEventos>" |
134
|
|
|
. $request |
135
|
|
|
. "</sped:loteEventos>" |
136
|
|
|
. "</sped:ReceberLoteEventos>"; |
137
|
|
|
$this->lastRequest = $body; |
138
|
|
|
$this->lastResponse = $this->sendRequest($body); |
139
|
|
|
return $this->lastResponse; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Send request to webservice |
144
|
|
|
* @param string $request |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
protected function sendRequest($request) |
148
|
|
|
{ |
149
|
|
|
if (empty($this->soap)) { |
150
|
|
|
$this->soap = new SoapCurl($this->certificate); |
151
|
|
|
} |
152
|
|
|
$envelope = "<soapenv:Envelope "; |
153
|
|
|
foreach ($this->soapnamespaces as $key => $xmlns) { |
154
|
|
|
$envelope .= "$key = \"$xmlns\" "; |
155
|
|
|
} |
156
|
|
|
$envelope .= ">" |
157
|
|
|
. "<soapenv:Header/>" |
158
|
|
|
. "<soapenv:Body>" |
159
|
|
|
. $request |
160
|
|
|
. "</soapenv:Body>" |
161
|
|
|
. "</soapenv:Envelope>"; |
162
|
|
|
|
163
|
|
|
$msgSize = strlen($envelope); |
164
|
|
|
$parameters = [ |
165
|
|
|
"Content-Type: text/xml;charset=UTF-8", |
166
|
|
|
"SOAPAction: \"$this->action\"", |
167
|
|
|
"Content-length: $msgSize" |
168
|
|
|
]; |
169
|
|
|
$url = $this->uri[$this->tpAmb]; |
170
|
|
|
return (string) $this->soap->send( |
171
|
|
|
$this->method, |
172
|
|
|
$url, |
173
|
|
|
$this->action, |
174
|
|
|
$envelope, |
175
|
|
|
$parameters |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Verify the availability of a digital certificate. |
181
|
|
|
* If available, place it where it is needed |
182
|
|
|
* @param FactoryInterface $evento |
183
|
|
|
* @throws RuntimeException |
184
|
|
|
*/ |
185
|
|
|
protected function checkCertificate(FactoryInterface $evento) |
186
|
|
|
{ |
187
|
|
|
//try to get certificate from event |
188
|
|
|
$certificate = $evento->getCertificate(); |
189
|
|
|
if (empty($certificate)) { |
190
|
|
|
$evento->setCertificate($this->certificate); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.