package pain import ( "fmt" "strconv" "time" ) func NewDocument(initParty string) *Document { return &Document{ XmlnsXsi: PAIN_XMLNS_XSI, Namespace: PAIN_XMLNS, Contents: &PainXML{ GroupHeader: NewGrpHdr(initParty), PaymentInformation: make([]PmtInf, 0), }, } } func (d *Document) SetMeta(creditorName, creditorAddr1, creditorAddr2, creditorIBAN, creditorBIC, creditorId string, collectionDate time.Time, sequenceType string) { creditor := Cdtr{ Name: creditorName, PostalAddress: PstlAdr{ Country: "NL", // XXX: make customizable? AddressLines: []AdrLine{ AdrLine(creditorAddr1), AdrLine(creditorAddr2), }, }, } creditorAccount := CdtrAcct{ Id: IBAN{ IBAN: creditorIBAN, }, } creditorAgent := CdtrAgt{ InstitutionId: FinInstnId{ BIC: creditorBIC, }, } creditorSchemeId := CdtrSchmeId{ Id: NewPartyIdSEPA3(creditorId), } paymentInfo := PmtInf{ //Id: set in Finalize() Method: "DD", PaymentMeta: PmtTpInf{ ServiceLevel: Code{ Code: "SEPA", }, LocalInstrument: Code{ Code: "CORE", }, SequenceType: sequenceType, }, CollectionDate: collectionDate.Format("2006-01-02"), Creditor: creditor, CreditorAccount: creditorAccount, CreditorAgent: creditorAgent, SchemeId: creditorSchemeId, Transactions: make([]DrctDbtTxInf, 0), } d.Contents.PaymentInformation = append(d.Contents.PaymentInformation, paymentInfo) } func (d *Document) Finalize(msgId string) error { if len(d.Contents.PaymentInformation) == 0 { return fmt.Errorf("no payment information, aborting") } if len(d.Contents.PaymentInformation[0].Transactions) == 0 { return fmt.Errorf("no transaction information, aborting") } 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 for _, tx := range d.Contents.PaymentInformation[0].Transactions { flt, err := strconv.ParseFloat(tx.Amount.Value, 64) if err != nil { panic(fmt.Errorf("could not parse float from %q: %v", tx.Amount.Value, err)) } csum += flt } d.Contents.GroupHeader.Sum = fmt.Sprintf("%.2f", csum) return d.Valid() } func (d *Document) AddTransaction(tx DrctDbtTxInf) error { if len(d.Contents.PaymentInformation) == 0 { return fmt.Errorf("payment information not yet initialized") } d.Contents.PaymentInformation[0].Transactions = append(d.Contents.PaymentInformation[0].Transactions, tx) return nil }