-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsoap.go
53 lines (46 loc) · 1.47 KB
/
soap.go
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
package nfe
import (
"encoding/xml"
"fmt"
)
// Envelope representa o XML do envelope SOAP que será usado na comunicação.
type Envelope struct {
XMLName xml.Name `xml:"soap12:Envelope"`
Xsi string `xml:"xmlns:xsi,attr"`
Xsd string `xml:"xmlns:xsd,attr"`
Soap12 string `xml:"xmlns:soap12,attr"`
Body struct {
NfeDadosMsg struct {
Xmlns string `xml:"xmlns,attr"`
Value []byte `xml:",innerxml"`
} `xml:"nfeDadosMsg"`
} `xml:"soap12:Body"`
}
// EnvelopeResult representa o XML do envelope SOAP de retorno da requisição.
type EnvelopeResult struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
NfeResultMsg struct {
Value []byte `xml:",innerxml"`
} `xml:"nfeResultMsg"`
} `xml:"Body"`
}
// getSoapEnvelope envelopa um XML fornecido de acordo com o padrão SOAP.
func getSoapEnvelope(msg []byte, xmlns string) ([]byte, error) {
var env Envelope
env.Xsi = "http://www.w3.org/2001/XMLSchema-instance"
env.Xsd = "http://www.w3.org/2001/XMLSchema"
env.Soap12 = "http://www.w3.org/2003/05/soap-envelope"
env.Body.NfeDadosMsg.Xmlns = xmlns
env.Body.NfeDadosMsg.Value = msg
return xml.Marshal(env)
}
// readSoapEnvelope extrai o XML de resposta de um envelope SOAP de retorno.
func readSoapEnvelope(msg []byte) ([]byte, error) {
var env EnvelopeResult
err := xml.Unmarshal(msg, &env)
if err != nil {
return nil, fmt.Errorf("Erro na desserialização do arquivo XML: %w. Arquivo: %s", err, msg)
}
return env.Body.NfeResultMsg.Value, nil
}