2017-03-11 07:15:15 -05:00
|
|
|
package ofxgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2020-03-25 01:40:03 -04:00
|
|
|
"fmt"
|
2017-03-11 07:15:15 -05:00
|
|
|
"io"
|
2017-03-31 11:54:43 -04:00
|
|
|
"reflect"
|
2017-03-11 07:15:15 -05:00
|
|
|
"strings"
|
2020-03-25 01:40:03 -04:00
|
|
|
|
|
|
|
"github.com/aclindsa/xml"
|
2017-03-11 07:15:15 -05:00
|
|
|
)
|
|
|
|
|
2017-03-31 15:29:57 -04:00
|
|
|
// Response is the top-level object returned from a parsed OFX response file.
|
|
|
|
// It can be inspected by using type assertions or switches on the message set
|
|
|
|
// you're interested in.
|
2017-03-11 07:18:02 -05:00
|
|
|
type Response struct {
|
2017-04-17 10:54:20 -04:00
|
|
|
Version ofxVersion // OFX header version
|
2017-03-31 11:54:43 -04:00
|
|
|
Signon SignonResponse //<SIGNONMSGSETV1>
|
|
|
|
Signup []Message //<SIGNUPMSGSETV1>
|
|
|
|
Bank []Message //<BANKMSGSETV1>
|
|
|
|
CreditCard []Message //<CREDITCARDMSGSETV1>
|
|
|
|
Loan []Message //<LOANMSGSETV1>
|
|
|
|
InvStmt []Message //<INVSTMTMSGSETV1>
|
|
|
|
InterXfer []Message //<INTERXFERMSGSETV1>
|
|
|
|
WireXfer []Message //<WIREXFERMSGSETV1>
|
|
|
|
Billpay []Message //<BILLPAYMSGSETV1>
|
|
|
|
Email []Message //<EMAILMSGSETV1>
|
|
|
|
SecList []Message //<SECLISTMSGSETV1>
|
|
|
|
PresDir []Message //<PRESDIRMSGSETV1>
|
|
|
|
PresDlv []Message //<PRESDLVMSGSETV1>
|
2017-04-03 19:50:16 -04:00
|
|
|
Prof []Message //<PROFMSGSETV1>
|
2017-03-31 11:54:43 -04:00
|
|
|
Image []Message //<IMAGEMSGSETV1>
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 07:18:02 -05:00
|
|
|
func (or *Response) readSGMLHeaders(r *bufio.Reader) error {
|
2017-03-11 07:15:15 -05:00
|
|
|
var seenHeader, seenVersion bool = false, false
|
|
|
|
for {
|
2019-01-04 11:18:55 -05:00
|
|
|
// Some financial institutions do not properly leave an empty line after the last header.
|
|
|
|
// Avoid attempting to read another header in that case.
|
|
|
|
next, err := r.Peek(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if next[0] == '<' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-03-11 07:15:15 -05:00
|
|
|
line, err := r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// r.ReadString leaves the '\n' on the end...
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
if seenHeader {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header := strings.SplitN(line, ":", 2)
|
|
|
|
if header == nil || len(header) != 2 {
|
|
|
|
return errors.New("OFX headers malformed")
|
|
|
|
}
|
|
|
|
|
2017-04-03 20:49:48 -04:00
|
|
|
// Some OFX servers put a space after the colon
|
|
|
|
headervalue := strings.TrimSpace(header[1])
|
|
|
|
|
2017-03-11 07:15:15 -05:00
|
|
|
switch header[0] {
|
|
|
|
case "OFXHEADER":
|
2017-04-03 20:49:48 -04:00
|
|
|
if headervalue != "100" {
|
2017-03-11 07:15:15 -05:00
|
|
|
return errors.New("OFXHEADER is not 100")
|
|
|
|
}
|
|
|
|
seenHeader = true
|
|
|
|
case "DATA":
|
2017-04-03 20:49:48 -04:00
|
|
|
if headervalue != "OFXSGML" {
|
2017-03-11 07:15:15 -05:00
|
|
|
return errors.New("OFX DATA header does not contain OFXSGML")
|
|
|
|
}
|
|
|
|
case "VERSION":
|
2017-04-17 10:54:20 -04:00
|
|
|
err := or.Version.FromString(headervalue)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
seenVersion = true
|
|
|
|
|
|
|
|
if or.Version > OfxVersion160 {
|
|
|
|
return errors.New("OFX VERSION > 160 in SGML header")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
case "SECURITY":
|
2017-04-03 20:49:48 -04:00
|
|
|
if headervalue != "NONE" {
|
2017-03-11 07:15:15 -05:00
|
|
|
return errors.New("OFX SECURITY header not NONE")
|
|
|
|
}
|
|
|
|
case "COMPRESSION":
|
2017-04-03 20:49:48 -04:00
|
|
|
if headervalue != "NONE" {
|
2017-03-11 07:15:15 -05:00
|
|
|
return errors.New("OFX COMPRESSION header not NONE")
|
|
|
|
}
|
|
|
|
case "ENCODING", "CHARSET", "OLDFILEUID", "NEWFILEUID":
|
|
|
|
// TODO check/handle these headers?
|
|
|
|
default:
|
|
|
|
return errors.New("Invalid OFX header: " + header[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !seenVersion {
|
|
|
|
return errors.New("OFX VERSION header missing")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-11 07:18:02 -05:00
|
|
|
func (or *Response) readXMLHeaders(decoder *xml.Decoder) error {
|
2017-03-13 21:10:19 -04:00
|
|
|
var tok xml.Token
|
|
|
|
tok, err := nextNonWhitespaceToken(decoder)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if xmlElem, ok := tok.(xml.ProcInst); !ok || xmlElem.Target != "xml" {
|
|
|
|
return errors.New("Missing xml processing instruction")
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the OFX header
|
2017-03-13 21:10:19 -04:00
|
|
|
tok, err = nextNonWhitespaceToken(decoder)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if ofxElem, ok := tok.(xml.ProcInst); ok && ofxElem.Target == "OFX" {
|
|
|
|
var seenHeader, seenVersion bool = false, false
|
|
|
|
|
|
|
|
headers := bytes.TrimSpace(ofxElem.Inst)
|
|
|
|
for len(headers) > 0 {
|
|
|
|
tmp := bytes.SplitN(headers, []byte("=\""), 2)
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
return errors.New("Malformed OFX header")
|
|
|
|
}
|
|
|
|
header := string(tmp[0])
|
|
|
|
headers = tmp[1]
|
|
|
|
tmp = bytes.SplitN(headers, []byte("\""), 2)
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
return errors.New("Malformed OFX header")
|
|
|
|
}
|
|
|
|
value := string(tmp[0])
|
|
|
|
headers = bytes.TrimSpace(tmp[1])
|
|
|
|
|
|
|
|
switch header {
|
|
|
|
case "OFXHEADER":
|
|
|
|
if value != "200" {
|
|
|
|
return errors.New("OFXHEADER is not 200")
|
|
|
|
}
|
|
|
|
seenHeader = true
|
|
|
|
case "VERSION":
|
2017-04-17 10:54:20 -04:00
|
|
|
err := or.Version.FromString(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
seenVersion = true
|
|
|
|
|
|
|
|
if or.Version < OfxVersion200 {
|
|
|
|
return errors.New("OFX VERSION < 200 in XML header")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
case "SECURITY":
|
|
|
|
if value != "NONE" {
|
|
|
|
return errors.New("OFX SECURITY header not NONE")
|
|
|
|
}
|
|
|
|
case "OLDFILEUID", "NEWFILEUID":
|
|
|
|
// TODO check/handle these headers?
|
|
|
|
default:
|
|
|
|
return errors.New("Invalid OFX header: " + header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !seenHeader {
|
|
|
|
return errors.New("OFXHEADER version missing")
|
|
|
|
}
|
|
|
|
if !seenVersion {
|
|
|
|
return errors.New("OFX VERSION header missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return errors.New("Missing xml 'OFX' processing instruction")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:14:58 -04:00
|
|
|
// Number of bytes of response to read when attempting to figure out whether
|
|
|
|
// we're using OFX or SGML
|
2017-03-15 20:56:51 -04:00
|
|
|
const guessVersionCheckBytes = 1024
|
|
|
|
|
2017-03-16 11:14:58 -04:00
|
|
|
// Defaults to XML if it can't determine the version or if there is any
|
|
|
|
// ambiguity
|
2020-03-31 01:29:11 -04:00
|
|
|
// Returns false for SGML, true (for XML) otherwise.
|
2017-03-15 20:56:51 -04:00
|
|
|
func guessVersion(r *bufio.Reader) (bool, error) {
|
|
|
|
b, _ := r.Peek(guessVersionCheckBytes)
|
|
|
|
if b == nil {
|
|
|
|
return false, errors.New("Failed to read OFX header")
|
|
|
|
}
|
|
|
|
sgmlIndex := bytes.Index(b, []byte("OFXHEADER:"))
|
|
|
|
xmlIndex := bytes.Index(b, []byte("OFXHEADER="))
|
|
|
|
if sgmlIndex < 0 {
|
|
|
|
return true, nil
|
|
|
|
} else if xmlIndex < 0 {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return xmlIndex <= sgmlIndex, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 15:29:57 -04:00
|
|
|
// A map of message set tags to a map of transaction wrapper tags to the
|
|
|
|
// reflect.Type of the struct for that transaction type. Used when decoding
|
|
|
|
// Responses. Newly-implemented response transaction types *must* be added to
|
|
|
|
// this map in order to be unmarshalled.
|
2017-03-31 11:54:43 -04:00
|
|
|
var responseTypes = map[string]map[string]reflect.Type{
|
2017-04-08 11:38:07 -04:00
|
|
|
SignupRs.String(): {
|
2017-03-31 11:54:43 -04:00
|
|
|
(&AcctInfoResponse{}).Name(): reflect.TypeOf(AcctInfoResponse{})},
|
2017-04-08 11:38:07 -04:00
|
|
|
BankRs.String(): {
|
2017-03-31 11:54:43 -04:00
|
|
|
(&StatementResponse{}).Name(): reflect.TypeOf(StatementResponse{})},
|
2017-04-08 11:38:07 -04:00
|
|
|
CreditCardRs.String(): {
|
2017-03-31 11:54:43 -04:00
|
|
|
(&CCStatementResponse{}).Name(): reflect.TypeOf(CCStatementResponse{})},
|
2017-04-08 11:38:07 -04:00
|
|
|
LoanRs.String(): {},
|
|
|
|
InvStmtRs.String(): {
|
2017-03-31 11:54:43 -04:00
|
|
|
(&InvStatementResponse{}).Name(): reflect.TypeOf(InvStatementResponse{})},
|
2017-04-08 11:38:07 -04:00
|
|
|
InterXferRs.String(): {},
|
|
|
|
WireXferRs.String(): {},
|
|
|
|
BillpayRs.String(): {},
|
|
|
|
EmailRs.String(): {},
|
|
|
|
SecListRs.String(): {
|
2017-03-31 11:54:43 -04:00
|
|
|
(&SecListResponse{}).Name(): reflect.TypeOf(SecListResponse{}),
|
|
|
|
(&SecurityList{}).Name(): reflect.TypeOf(SecurityList{})},
|
2017-04-08 11:38:07 -04:00
|
|
|
PresDirRs.String(): {},
|
|
|
|
PresDlvRs.String(): {},
|
|
|
|
ProfRs.String(): {
|
2017-03-31 11:54:43 -04:00
|
|
|
(&ProfileResponse{}).Name(): reflect.TypeOf(ProfileResponse{})},
|
2017-04-08 11:38:07 -04:00
|
|
|
ImageRs.String(): {},
|
2017-03-31 11:54:43 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:37:40 -04:00
|
|
|
func decodeMessageSet(d *xml.Decoder, start xml.StartElement, msgs *[]Message, version ofxVersion) error {
|
2017-03-31 11:54:43 -04:00
|
|
|
setTypes, ok := responseTypes[start.Name.Local]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Invalid message set: " + start.Name.Local)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
tok, err := nextNonWhitespaceToken(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local {
|
|
|
|
// If we found the end of our starting element, we're done parsing
|
2020-03-31 01:29:11 -04:00
|
|
|
return nil
|
2017-03-31 11:54:43 -04:00
|
|
|
} else if startElement, ok := tok.(xml.StartElement); ok {
|
|
|
|
responseType, ok := setTypes[startElement.Name.Local]
|
|
|
|
if !ok {
|
2017-03-31 15:29:57 -04:00
|
|
|
// If you are a developer and received this message after you
|
|
|
|
// thought you added a new transaction type, make sure you
|
|
|
|
// added it to the responseTypes map above
|
|
|
|
return errors.New("Unsupported response transaction for " +
|
|
|
|
start.Name.Local + ": " + startElement.Name.Local)
|
2017-03-31 11:54:43 -04:00
|
|
|
}
|
|
|
|
response := reflect.New(responseType).Interface()
|
|
|
|
responseMessage := response.(Message)
|
|
|
|
if err := d.DecodeElement(responseMessage, &startElement); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*msgs = append(*msgs, responseMessage)
|
|
|
|
} else {
|
|
|
|
return errors.New("Didn't find an opening element")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 01:29:11 -04:00
|
|
|
// ParseResponse parses and validates an OFX response in SGML or XML into a
|
|
|
|
// Response object from the given io.Reader
|
2017-03-16 11:13:21 -04:00
|
|
|
//
|
|
|
|
// It is commonly used as part of Client.Request(), but may be used on its own
|
|
|
|
// to parse already-downloaded OFX files (such as those from 'Web Connect'). It
|
|
|
|
// performs version autodetection if it can and attempts to be as forgiving as
|
|
|
|
// possible about the input format.
|
|
|
|
func ParseResponse(reader io.Reader) (*Response, error) {
|
2020-03-31 01:29:11 -04:00
|
|
|
resp, err := DecodeResponse(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = resp.Valid()
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeResponse parses an OFX response in SGML or XML into a Response object
|
|
|
|
// from the given io.Reader
|
|
|
|
func DecodeResponse(reader io.Reader) (*Response, error) {
|
2017-03-16 11:13:21 -04:00
|
|
|
var or Response
|
|
|
|
|
2017-03-15 20:56:51 -04:00
|
|
|
r := bufio.NewReaderSize(reader, guessVersionCheckBytes)
|
|
|
|
xmlVersion, err := guessVersion(r)
|
|
|
|
if err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-15 20:56:51 -04:00
|
|
|
}
|
2017-03-11 07:15:15 -05:00
|
|
|
|
|
|
|
// parse SGML headers before creating XML decoder
|
|
|
|
if !xmlVersion {
|
|
|
|
if err := or.readSGMLHeaders(r); err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := xml.NewDecoder(r)
|
|
|
|
if !xmlVersion {
|
|
|
|
decoder.Strict = false
|
|
|
|
decoder.AutoCloseAfterCharData = ofxLeafElements
|
|
|
|
}
|
2017-03-13 21:10:43 -04:00
|
|
|
decoder.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
|
|
|
|
return input, nil
|
|
|
|
}
|
2017-03-11 07:15:15 -05:00
|
|
|
|
|
|
|
if xmlVersion {
|
|
|
|
// parse the xml header
|
|
|
|
if err := or.readXMLHeaders(decoder); err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 21:10:19 -04:00
|
|
|
tok, err := nextNonWhitespaceToken(decoder)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-11 07:15:15 -05:00
|
|
|
} else if ofxStart, ok := tok.(xml.StartElement); !ok || ofxStart.Name.Local != "OFX" {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, errors.New("Missing opening OFX xml element")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal the signon message
|
2017-03-13 21:10:19 -04:00
|
|
|
tok, err = nextNonWhitespaceToken(decoder)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-31 15:30:10 -04:00
|
|
|
} else if signonStart, ok := tok.(xml.StartElement); ok && signonStart.Name.Local == SignonRs.String() {
|
2017-03-11 07:15:15 -05:00
|
|
|
if err := decoder.Decode(&or.Signon); err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, errors.New("Missing opening SIGNONMSGSRSV1 xml element")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 21:10:19 -04:00
|
|
|
tok, err = nextNonWhitespaceToken(decoder)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-31 15:30:10 -04:00
|
|
|
} else if signonEnd, ok := tok.(xml.EndElement); !ok || signonEnd.Name.Local != SignonRs.String() {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, errors.New("Missing closing SIGNONMSGSRSV1 xml element")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-31 11:54:43 -04:00
|
|
|
var messageSlices = map[string]*[]Message{
|
|
|
|
SignupRs.String(): &or.Signup,
|
|
|
|
BankRs.String(): &or.Bank,
|
|
|
|
CreditCardRs.String(): &or.CreditCard,
|
|
|
|
LoanRs.String(): &or.Loan,
|
|
|
|
InvStmtRs.String(): &or.InvStmt,
|
|
|
|
InterXferRs.String(): &or.InterXfer,
|
|
|
|
WireXferRs.String(): &or.WireXfer,
|
|
|
|
BillpayRs.String(): &or.Billpay,
|
|
|
|
EmailRs.String(): &or.Email,
|
|
|
|
SecListRs.String(): &or.SecList,
|
|
|
|
PresDirRs.String(): &or.PresDir,
|
|
|
|
PresDlvRs.String(): &or.PresDlv,
|
2017-04-03 19:50:16 -04:00
|
|
|
ProfRs.String(): &or.Prof,
|
2017-03-31 11:54:43 -04:00
|
|
|
ImageRs.String(): &or.Image,
|
|
|
|
}
|
|
|
|
|
2017-03-11 07:15:15 -05:00
|
|
|
for {
|
2017-03-13 21:10:19 -04:00
|
|
|
tok, err = nextNonWhitespaceToken(decoder)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, err
|
2017-03-11 07:15:15 -05:00
|
|
|
} else if ofxEnd, ok := tok.(xml.EndElement); ok && ofxEnd.Name.Local == "OFX" {
|
2020-03-31 01:29:11 -04:00
|
|
|
return &or, nil // found closing XML element, so we're done
|
2017-03-11 07:15:15 -05:00
|
|
|
} else if start, ok := tok.(xml.StartElement); ok {
|
2017-03-31 11:54:43 -04:00
|
|
|
slice, ok := messageSlices[start.Name.Local]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Invalid message set: " + start.Name.Local)
|
|
|
|
}
|
2017-04-17 20:37:40 -04:00
|
|
|
if err := decodeMessageSet(decoder, start, slice, or.Version); err != nil {
|
2020-03-31 01:29:11 -04:00
|
|
|
return nil, err
|
2017-03-11 12:59:47 -05:00
|
|
|
}
|
2017-03-11 07:15:15 -05:00
|
|
|
} else {
|
2017-03-16 11:13:21 -04:00
|
|
|
return nil, errors.New("Found unexpected token")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 22:40:49 -05:00
|
|
|
|
2020-03-31 01:29:11 -04:00
|
|
|
// Valid returns whether the Response is valid according to the OFX spec
|
|
|
|
func (or *Response) Valid() (bool, error) {
|
2020-03-31 00:19:18 -04:00
|
|
|
var errs errInvalid
|
2020-03-31 01:29:11 -04:00
|
|
|
if ok, err := or.Signon.Valid(or.Version); !ok {
|
|
|
|
errs.AddErr(err)
|
|
|
|
}
|
|
|
|
for _, messageSet := range [][]Message{
|
|
|
|
or.Signup,
|
|
|
|
or.Bank,
|
|
|
|
or.CreditCard,
|
|
|
|
or.Loan,
|
|
|
|
or.InvStmt,
|
|
|
|
or.InterXfer,
|
|
|
|
or.WireXfer,
|
|
|
|
or.Billpay,
|
|
|
|
or.Email,
|
|
|
|
or.SecList,
|
|
|
|
or.PresDir,
|
|
|
|
or.PresDlv,
|
|
|
|
or.Prof,
|
|
|
|
or.Image,
|
|
|
|
} {
|
|
|
|
for _, message := range messageSet {
|
|
|
|
if ok, err := message.Valid(or.Version); !ok {
|
|
|
|
errs.AddErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := errs.ErrOrNil()
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:40:49 -05:00
|
|
|
// Marshal this Response into its SGML/XML representation held in a bytes.Buffer
|
|
|
|
//
|
|
|
|
// If error is non-nil, this bytes.Buffer is ready to be sent to an OFX client
|
|
|
|
func (or *Response) Marshal() (*bytes.Buffer, error) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
// Write the header appropriate to our version
|
2019-06-14 01:40:22 -04:00
|
|
|
writeHeader(&b, or.Version, false)
|
2019-03-01 22:40:49 -05:00
|
|
|
|
|
|
|
encoder := xml.NewEncoder(&b)
|
|
|
|
encoder.Indent("", " ")
|
|
|
|
|
|
|
|
ofxElement := xml.StartElement{Name: xml.Name{Local: "OFX"}}
|
|
|
|
|
|
|
|
if err := encoder.EncodeToken(ofxElement); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := or.Signon.Valid(or.Version); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signonMsgSet := xml.StartElement{Name: xml.Name{Local: SignonRs.String()}}
|
|
|
|
if err := encoder.EncodeToken(signonMsgSet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(&or.Signon); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := encoder.EncodeToken(signonMsgSet.End()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
messageSets := []struct {
|
|
|
|
Messages []Message
|
|
|
|
Type messageType
|
|
|
|
}{
|
|
|
|
{or.Signup, SignupRs},
|
|
|
|
{or.Bank, BankRs},
|
|
|
|
{or.CreditCard, CreditCardRs},
|
|
|
|
{or.Loan, LoanRs},
|
|
|
|
{or.InvStmt, InvStmtRs},
|
|
|
|
{or.InterXfer, InterXferRs},
|
|
|
|
{or.WireXfer, WireXferRs},
|
|
|
|
{or.Billpay, BillpayRs},
|
|
|
|
{or.Email, EmailRs},
|
|
|
|
{or.SecList, SecListRs},
|
|
|
|
{or.PresDir, PresDirRs},
|
|
|
|
{or.PresDlv, PresDlvRs},
|
|
|
|
{or.Prof, ProfRs},
|
|
|
|
{or.Image, ImageRs},
|
|
|
|
}
|
|
|
|
for _, set := range messageSets {
|
|
|
|
if err := encodeMessageSet(encoder, set.Messages, set.Type, or.Version); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := encoder.EncodeToken(ofxElement.End()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := encoder.Flush(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &b, nil
|
|
|
|
}
|
2020-03-25 01:40:03 -04:00
|
|
|
|
2020-03-31 00:19:18 -04:00
|
|
|
// errInvalid represents validation failures while parsing an OFX response
|
2020-03-25 01:40:03 -04:00
|
|
|
// If an institution returns slightly malformed data, ParseResponse will return a best-effort parsed response and a validation error.
|
2020-03-31 00:19:18 -04:00
|
|
|
type errInvalid []error
|
2020-03-25 01:40:03 -04:00
|
|
|
|
2020-03-31 00:19:18 -04:00
|
|
|
func (e errInvalid) Error() string {
|
2020-03-25 01:40:03 -04:00
|
|
|
var errStrings []string
|
|
|
|
for _, err := range e {
|
|
|
|
errStrings = append(errStrings, err.Error())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Validation failed: %s", strings.Join(errStrings, "; "))
|
|
|
|
}
|
|
|
|
|
2020-03-31 00:19:18 -04:00
|
|
|
func (e *errInvalid) AddErr(err error) {
|
2020-03-25 01:40:03 -04:00
|
|
|
if err != nil {
|
2020-03-31 00:19:18 -04:00
|
|
|
if errs, ok := err.(errInvalid); ok {
|
2020-03-25 01:40:03 -04:00
|
|
|
*e = append(*e, errs...)
|
|
|
|
} else {
|
|
|
|
*e = append(*e, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 00:19:18 -04:00
|
|
|
func (e errInvalid) ErrOrNil() error {
|
2020-03-25 01:40:03 -04:00
|
|
|
if len(e) > 0 {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|