@@ -48,7 +48,7 @@ public function emit(DpsData $dps): ReceiptData
4848 $ xml = (new XmlBuilder ())->buildDps ($ dps );
4949 $ signed = $ this ->signer ->sign ($ xml , $ dps ->cnpjPrestador );
5050
51- [$ httpStatus , $ body ] = $ this ->post ('/dps ' , $ signed );
51+ [$ httpStatus , $ body ] = $ this ->post ('/nfse ' , $ signed );
5252
5353 if ($ httpStatus >= 400 ) {
5454 throw new IssuanceException (
@@ -64,7 +64,7 @@ public function emit(DpsData $dps): ReceiptData
6464
6565 public function query (string $ chaveAcesso ): ReceiptData
6666 {
67- [$ httpStatus , $ body ] = $ this ->get ('/dps / ' . $ chaveAcesso );
67+ [$ httpStatus , $ body ] = $ this ->get ('/nfse / ' . $ chaveAcesso );
6868
6969 if ($ httpStatus >= 400 ) {
7070 throw new QueryException (
@@ -103,13 +103,18 @@ public function cancel(string $chaveAcesso, string $motivo): bool
103103 */
104104 private function post (string $ path , string $ xmlPayload ): array
105105 {
106+ $ payload = json_encode ([
107+ 'dpsXmlGZipB64 ' => base64_encode (gzencode ($ xmlPayload )),
108+ ], JSON_THROW_ON_ERROR );
109+
106110 $ context = stream_context_create ([
107111 'http ' => [
108112 'method ' => 'POST ' ,
109- 'header ' => "Content-Type: application/xml \r\nAccept: application/json \r\n" ,
110- 'content ' => $ xmlPayload ,
113+ 'header ' => "Content-Type: application/json \r\nAccept: application/json \r\n" ,
114+ 'content ' => $ payload ,
111115 'ignore_errors ' => true ,
112116 ],
117+ 'ssl ' => $ this ->sslContextOptions (),
113118 ]);
114119
115120 return $ this ->fetchAndDecode ($ path , $ context );
@@ -126,6 +131,7 @@ private function get(string $path): array
126131 'header ' => "Accept: application/json \r\n" ,
127132 'ignore_errors ' => true ,
128133 ],
134+ 'ssl ' => $ this ->sslContextOptions (),
129135 ]);
130136
131137 return $ this ->fetchAndDecode ($ path , $ context );
@@ -144,11 +150,30 @@ private function delete(string $path, string $motivo): array
144150 'content ' => $ payload ,
145151 'ignore_errors ' => true ,
146152 ],
153+ 'ssl ' => $ this ->sslContextOptions (),
147154 ]);
148155
149156 return $ this ->fetchAndDecode ($ path , $ context );
150157 }
151158
159+ /**
160+ * @return array<string, bool|string>
161+ */
162+ private function sslContextOptions (): array
163+ {
164+ $ options = [
165+ 'verify_peer ' => true ,
166+ 'verify_peer_name ' => true ,
167+ ];
168+
169+ if ($ this ->cert ->transportCertificatePath !== null && $ this ->cert ->transportPrivateKeyPath !== null ) {
170+ $ options ['local_cert ' ] = $ this ->cert ->transportCertificatePath ;
171+ $ options ['local_pk ' ] = $ this ->cert ->transportPrivateKeyPath ;
172+ }
173+
174+ return $ options ;
175+ }
176+
152177 /**
153178 * Perform the raw HTTP request and decode the JSON body.
154179 *
@@ -206,12 +231,26 @@ private function parseHttpStatus(array $headers): int
206231 */
207232 private function parseReceiptResponse (array $ response ): ReceiptData
208233 {
234+ $ rawXml = null ;
235+
236+ if (isset ($ response ['nfseXmlGZipB64 ' ]) && is_string ($ response ['nfseXmlGZipB64 ' ])) {
237+ $ decodedXml = base64_decode ($ response ['nfseXmlGZipB64 ' ], true );
238+
239+ if ($ decodedXml !== false ) {
240+ $ inflatedXml = gzdecode ($ decodedXml );
241+
242+ if ($ inflatedXml !== false ) {
243+ $ rawXml = $ inflatedXml ;
244+ }
245+ }
246+ }
247+
209248 return new ReceiptData (
210249 nfseNumber: (string ) ($ response ['nNFSe ' ] ?? $ response ['numero ' ] ?? '' ),
211- chaveAcesso: (string ) ($ response ['chaveAcesso ' ] ?? $ response [ ' id ' ] ?? '' ),
212- dataEmissao: (string ) ($ response ['dhEmi ' ] ?? $ response ['dataEmissao ' ] ?? '' ),
250+ chaveAcesso: (string ) ($ response ['chaveAcesso ' ] ?? '' ),
251+ dataEmissao: (string ) ($ response ['dhEmi ' ] ?? $ response ['dataHoraProcessamento ' ] ?? $ response [ ' dataEmissao ' ] ?? '' ),
213252 codigoVerificacao: isset ($ response ['codigoVerificacao ' ]) ? (string ) $ response ['codigoVerificacao ' ] : null ,
214- rawXml: null ,
253+ rawXml: $ rawXml ,
215254 );
216255 }
217256}
0 commit comments