Skip to content

Segment ACH File

ACH File creation should default to NACHA rules by supporting both credits and debits in the same ACH file. Some legacy systems require the file to be segmented and contain only debits or only credits in a file.

Segmenting files is a post file creation process that takes an input of an ACH.File and returns two ACH.Files that have been segmented (debits and credits).

Note: Segmented files (via SegmentFile()) are only in ACH v1.1.0 and later.

Creating Segmented Files

An ACH File supports calling SegmentFile to create a debit ach file and credit ach file from an ach file that contains mixed debits and credits.

// open an ACH file
f, err := os.Open(filepath.Join("examples", "ach-ppd-read-mixedDebitCredit", "ppd-mixedDebitCredit.ach"))
if err != nil {
    log.Fatal(err)
}

// read file
r := ach.NewReader(f)
achFile, err := r.Read()
if err != nil {
    fmt.Printf("Issue reading file: %+v \n", err)
}

// Segment File
creditFile, debitFile, err := achFile.SegmentFile()
if err != nil {
    fmt.Printf("Could not segment the file: %v", err)
}

// write the file to std out. Anything io.Writer
w := ach.NewWriter(os.Stdout)
if err := w.Write(creditFile); err != nil {
    log.Fatalf("Unexpected error: %s\n", err)
}
if err := w.Write(debitFile); err != nil {
    log.Fatalf("Unexpected error: %s\n", err)
}

w.Flush()