diff --git a/pain/api.go b/pain/api.go index 686b10a..fe6a043 100644 --- a/pain/api.go +++ b/pain/api.go @@ -1,7 +1,7 @@ package pain import ( - // "fmt" + "fmt" "time" ) @@ -66,9 +66,6 @@ func (d *Document) SetMeta(creditorName, creditorAddr1, creditorAddr2, creditorI func (d *Document) Finalize(msgId string) error { // GroupHeader: - // Calculate CtrlSum - // Set Timestamp - // Set number of transactions // Set msgId /* @@ -80,8 +77,12 @@ func (d *Document) Finalize(msgId string) error { } */ - //csum := 0.0 + csum := 0.0 d.Contents.GroupHeader.MessageId = msgId + d.Contents.GroupHeader.Timestamp = time.Now().Format("2006-01-02T15:04:05") + d.Contents.GroupHeader.NumTx = fmt.Sprintf("%d", len(d.Contents.PaymentInformation[0].Transactions)) + d.Contents.GroupHeader.Sum = fmt.Sprintf("%.2f", csum) + d.Contents.PaymentInformation[0].Id = msgId return d.Valid() } diff --git a/pain/pain.go b/pain/pain.go index 04345cd..bbdd705 100644 --- a/pain/pain.go +++ b/pain/pain.go @@ -47,6 +47,11 @@ func (p *PainXML) Valid() error { if len(p.PaymentInformation) < 1 { err = append(err, "no payment information") } + for i, pi := range p.PaymentInformation { + if e := pi.Valid(); e != nil { + err = append(err, fmt.Sprintf("payment information at %d has errors: %v", i, e)) + } + } if len(err) > 0 { return fmt.Errorf("pain XML not valid: %v", strings.Join(err, ", ")) @@ -115,12 +120,13 @@ type PartyIdSEPA1 struct { } type PartyIdSEPA3 struct { - Id PartySEPA2 `xml:"Id"` + // Embed PartySEPA2, because we do not need another level of + PartySEPA2 } func NewPartyIdSEPA3(id string) PartyIdSEPA3 { return PartyIdSEPA3{ - Id: PartySEPA2{ + PartySEPA2{ PrivateId: PersonIdSEPA2{ Other: RestrictedPersonIdSEPA{ Id: id, @@ -134,7 +140,7 @@ func NewPartyIdSEPA3(id string) PartyIdSEPA3 { } func (p *PartyIdSEPA3) Valid() error { - return p.Id.Valid() + return p.Valid() } type PersonIdSEPA2 struct { @@ -166,7 +172,7 @@ func (r *RestrictedPersonIdSEPA) Valid() error { } type RestrictedPersonIdSchemeNameSEPA struct { - Party string `xml:"Prty"` // IdentificationSchemeNameSEPA + Party string `xml:"Prtry"` // IdentificationSchemeNameSEPA } func (r *RestrictedPersonIdSchemeNameSEPA) Valid() error { diff --git a/pain/pain_regexp.go b/pain/pain_regexp.go index 1c8cc3a..2441658 100644 --- a/pain/pain_regexp.go +++ b/pain/pain_regexp.go @@ -21,6 +21,8 @@ var ( IBAN2007Identifier = regexp.MustCompile(`[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}`) RestrictedPersonIdentifierSEPA = regexp.MustCompile(`[a-zA-Z]{2,2}[0-9]{2,2}([A-Za-z0-9]|[\+|\?|/|\-|:|\(|\)|\.|,|']){3,3}([A-Za-z0-9]|[\+|\?|/|\-|:|\(|\)|\.|,|']){1,28}`) BICIdentifier = regexp.MustCompile(`[A-Z]{6,6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3,3}){0,1}`) + + CountryCode = regexp.MustCompile(`[A-Z]{2,2}`) ) var ( @@ -28,6 +30,7 @@ var ( "BIC": BICIdentifier, "CreDtTm": ISODateTime, "CtrlSum": RestrictedDecimalNumber, + "Ctry": CountryCode, "EndToEndId": RestrictedIdentificationSEPA1, "IBAN": IBAN2007Identifier, "Id": RestrictedPersonIdentifierSEPA, diff --git a/pain/payment_information.go b/pain/payment_information.go index dabfe98..4943476 100644 --- a/pain/payment_information.go +++ b/pain/payment_information.go @@ -43,6 +43,9 @@ func (pmt *PmtInf) Valid() error { if e := pmt.SchemeId.Valid(); e != nil { err = append(err, fmt.Sprintf("payment scheme id not valid: %v", e)) } + if len(pmt.Transactions) == 0 { + err = append(err, fmt.Sprintf("need at least 1 transaction")) + } if len(err) > 0 { return fmt.Errorf("payment info (Id: %q) not valid: %v", pmt.Id, strings.Join(err, ", "))