Skip to content

Commit

Permalink
Pre-sanitize IBAN by removing common separators
Browse files Browse the repository at this point in the history
The IBAN parsing library currently only strips spaces.

This allows more "dirty" IBAN strings, by removing dashes and
underscores before passing them to the library.

Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Aug 9, 2024
1 parent ffd6285 commit 9babf23
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions payment/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package payment

import (
"fmt"
"regexp"
"strconv"
"strings"

Expand All @@ -10,6 +11,10 @@ import (

// See: https://www.europeanpaymentscouncil.eu/document-library/guidance-documents/quick-response-code-guidelines-enable-data-capture-initiation

// commonSeparatorChars is a regular expression that matches common separator characters for IBAN
// These characters are removed before the IBAN is validated
var commonSeparatorChars = regexp.MustCompile("[ _-]")

// Payment encapsulates all fields needed to generate the QR code
type Payment struct {
// ServiceTag should always be BCD
Expand Down Expand Up @@ -80,9 +85,10 @@ func (p *Payment) IBANBeneficiaryString() string {
return i.PrintCode
}

// IBAN returns the parsed IBAN of the beneficiary
// IBAN returns the parsed, sanitized IBAN of the beneficiary
func (p *Payment) IBAN() (*iban.IBAN, error) {
return iban.NewIBAN(p.IBANBeneficiary)
s := commonSeparatorChars.ReplaceAllString(p.IBANBeneficiary, "")
return iban.NewIBAN(s)
}

// PurposeString returns the parsed purpose
Expand Down

0 comments on commit 9babf23

Please sign in to comment.