diff --git a/field/packer_unpacker.go b/field/packer_unpacker.go index 4babec5..35408c8 100644 --- a/field/packer_unpacker.go +++ b/field/packer_unpacker.go @@ -90,16 +90,10 @@ func (p Track2Unpacker) Unpack(packedFieldValue []byte, spec *Spec) ([]byte, int return nil, 0, fmt.Errorf("failed to decode length: %w", err) } - // Peek and see if the first value is equal to the padding rune if set - // if it is, we need to increase the length to decode by 1 - // This is because the packer will pad the value, but set the value length - // equal to the unpadded value length - if spec.Pad != nil { - b := spec.Pad.Inspect() - r := b[0] - if packedFieldValue[prefBytes] == r { - valueLength++ - } + // if valueLength is odd we need to make it even to adjust for + // the padding in our Packer + if valueLength%2 != 0 { + valueLength++ } // decode the value diff --git a/field/packer_unpacker_test.go b/field/packer_unpacker_test.go index 4581dd3..565c8af 100644 --- a/field/packer_unpacker_test.go +++ b/field/packer_unpacker_test.go @@ -157,7 +157,13 @@ func TestTrack2Packer(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - fd := field.NewTrack2Value(tc.primaryAccountNumber, &tc.expirationDate, tc.serviceCode, tc.discretionaryData, field.WithSeparator(tc.separator)) + fd := field.NewTrack2Value( + tc.primaryAccountNumber, + &tc.expirationDate, + tc.serviceCode, + tc.discretionaryData, + tc.separator, + ) fd.SetSpec(s) packed, err := fd.Pack() diff --git a/field/track1.go b/field/track1.go index e64d0ec..56b8ff7 100644 --- a/field/track1.go +++ b/field/track1.go @@ -30,14 +30,6 @@ const ( var track1Regex = regexp.MustCompile(`^([A-Z]{1})([0-9]{1,19})\^([^\^]{2,26})\^([0-9]{4}|\^)([0-9]{3}|\^)([^\?]+)$`) -type Track1Option func(*Track1) - -func WithFormatCode(code string) Track1Option { - return func(t *Track1) { - t.FormatCode = code - } -} - func NewTrack1(spec *Spec) *Track1 { return &Track1{ spec: spec, @@ -49,8 +41,9 @@ func NewTrack1Value( name string, expirationDate *time.Time, serviceCode, - discretionaryData string, - opts ...Track1Option, + discretionaryData, + formatCode string, + fixedLength bool, ) *Track1 { t := &Track1{ PrimaryAccountNumber: primaryAccountNumber, @@ -58,10 +51,8 @@ func NewTrack1Value( ExpirationDate: expirationDate, ServiceCode: serviceCode, DiscretionaryData: discretionaryData, - } - - for _, opt := range opts { - opt(t) + FormatCode: formatCode, + FixedLength: fixedLength, } return t diff --git a/field/track2.go b/field/track2.go index ae3b036..8245641 100644 --- a/field/track2.go +++ b/field/track2.go @@ -29,14 +29,6 @@ const ( var track2Regex = regexp.MustCompile(`^([0-9]{1,19})(=|D)([0-9]{4})([0-9]{3})([^?]+)$`) -type Track2Option func(*Track2) - -func WithSeparator(sep string) Track2Option { - return func(t *Track2) { - t.Separator = sep - } -} - func NewTrack2(spec *Spec) *Track2 { return &Track2{ spec: spec, @@ -47,20 +39,17 @@ func NewTrack2Value( primaryAccountNumber string, expirationDate *time.Time, serviceCode, - discretionaryData string, - opts ...Track2Option, + discretionaryData, + separator string, ) *Track2 { t := &Track2{ PrimaryAccountNumber: primaryAccountNumber, + Separator: separator, ExpirationDate: expirationDate, ServiceCode: serviceCode, DiscretionaryData: discretionaryData, } - for _, opt := range opts { - opt(t) - } - return t } diff --git a/field/track_test.go b/field/track_test.go index b171998..367c881 100644 --- a/field/track_test.go +++ b/field/track_test.go @@ -147,16 +147,18 @@ func TestTrack1(t *testing.T) { expDate, err := time.Parse("0601", "9901") require.NoError(t, err) + fd := field.NewTrack1Value( + "1234567890123445", + "PADILLA/L.", + &expDate, + "120", + "0000000000000**XXX******", + "B", + true, + ) + track := field.NewTrack1(track1Spec) - err = track.Marshal(&field.Track1{ - FixedLength: true, - FormatCode: "B", - PrimaryAccountNumber: "1234567890123445", - ServiceCode: "120", - DiscretionaryData: "0000000000000**XXX******", - ExpirationDate: &expDate, - Name: "PADILLA/L.", - }) + err = track.Marshal(fd) require.NoError(t, err) data := &field.Track1{}