Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added validation option to allow $0 entry amount #1451

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,9 @@ func (batch *Batch) ValidAmountForCodes(entry *EntryDetail) error {
return fieldError("Amount", ErrBatchAmountNonZero, entry.Amount)
} else {
if entry.Amount == 0 {
if batch.validateOpts != nil && batch.validateOpts.AllowZeroEntryAmount {
return nil
}
return fieldError("Amount", ErrBatchAmountZero, entry.Amount)
}
}
Expand Down
17 changes: 17 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,23 @@ func TestBatch__ValidAmountForCodes(t *testing.T) {
})
}

func TestBatch_ValidAmountForCodes_AllowZeroEntryAmount(t *testing.T) {
b1 := mockBatchWEB(t)

// Standard EntryDetails should pass
require.NoError(t, b1.Create())

// Verify a zero amount fails
b1.Entries[0].Amount = 0
require.ErrorContains(t, b1.Create(), ErrBatchAmountZero.Error())

// Bypass zero amount check
b1.SetValidation(&ValidateOpts{
AllowZeroEntryAmount: true,
})
require.NoError(t, b1.Create())
}

func TestBatch_AllowInvalidAmounts(t *testing.T) {
bh := &BatchHeader{
OriginatorStatusCode: 1,
Expand Down
3 changes: 3 additions & 0 deletions docs/custom-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ UnequalAddendaCounts bool `json:"unequalAddendaCounts"`
```
// AllowInvalidAmounts will skip verifying the Amount is valid for the TransactionCode and entry type.
AllowInvalidAmounts bool `json:"allowInvalidAmounts"`

// AllowZeroEntryAmount will skip enforcing the entry Amount to be non-zero.
AllowZeroEntryAmount bool `json:"allowZeroEntryAmount"`
```

### File Header
Expand Down
3 changes: 3 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ type ValidateOpts struct {

// AllowInvalidAmounts will skip verifying the Amount is valid for the TransactionCode and entry type.
AllowInvalidAmounts bool `json:"allowInvalidAmounts"`

// AllowZeroEntryAmount will skip enforcing the entry Amount to be non-zero
AllowZeroEntryAmount bool `json:"allowZeroEntryAmount"`
}

// merge will combine two ValidateOpts structs and keep any non-zero field values.
Expand Down
4 changes: 4 additions & 0 deletions server/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
unequalAddendaCounts = "unequalAddendaCounts"
preserveSpaces = "preserveSpaces"
allowInvalidAmounts = "allowInvalidAmounts"
allowZeroEntryAmount = "allowZeroEntryAmount"
)

// readValidateOpts parses ValidateOpts from the URL query parameters and from the request body.
Expand Down Expand Up @@ -75,6 +76,7 @@ func readValidateOpts(request *http.Request) (io.Reader, *ach.ValidateOpts, erro
unequalAddendaCounts,
preserveSpaces,
allowInvalidAmounts,
allowZeroEntryAmount,
}

var buf bytes.Buffer
Expand Down Expand Up @@ -130,6 +132,8 @@ func readValidateOpts(request *http.Request) (io.Reader, *ach.ValidateOpts, erro
opts.PreserveSpaces = yes
case allowInvalidAmounts:
opts.AllowInvalidAmounts = yes
case allowZeroEntryAmount:
opts.AllowZeroEntryAmount = yes
}
}

Expand Down
Loading