forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 1
/
field_filter.go
125 lines (103 loc) · 2.85 KB
/
field_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package iso8583
import (
"fmt"
"unicode/utf8"
"github.com/moov-io/iso8583/field"
)
const (
emvFirstIndex = 4
emvLastIndex = 4
emvPattern = " ... "
pinFirstIndex = 2
pinLastIndex = 2
pinPattern = "****"
panFistIndex = 4
panLastIndex = 4
panPattern = "****"
)
type FilterFunc func(in string, data field.Field) string
type FieldFilter func(fieldFilters map[string]FilterFunc)
func FilterField(id string, filterFn FilterFunc) FieldFilter {
return func(fieldFilters map[string]FilterFunc) {
fieldFilters[id] = filterFn
}
}
var DefaultFilters = func() []FieldFilter {
filters := []FieldFilter{
FilterField("2", PANFilter),
FilterField("20", PANFilter),
FilterField("35", Track2Filter),
FilterField("36", Track3Filter),
FilterField("45", Track1Filter),
FilterField("52", PINFilter),
FilterField("55", EMVFilter),
}
return filters
}
var DoNotFilterFields = func() []FieldFilter {
filters := []FieldFilter{
FilterField("-1", NoOpFilter),
}
return filters
}
var NoOpFilter = func(in string, data field.Field) string {
return in
}
var EMVFilter = func(in string, data field.Field) string {
if utf8.RuneCountInString(in) < emvFirstIndex+emvLastIndex {
return in
}
return in[0:emvFirstIndex] + emvPattern + in[len(in)-emvLastIndex:]
}
var PINFilter = func(in string, data field.Field) string {
if utf8.RuneCountInString(in) < pinFirstIndex+pinLastIndex {
return in
}
return in[0:pinFirstIndex] + pinPattern + in[len(in)-pinLastIndex:]
}
var PANFilter = func(in string, data field.Field) string {
if utf8.RuneCountInString(in) < panFistIndex+panLastIndex {
return in
}
return in[0:panFistIndex] + panPattern + in[len(in)-panLastIndex:]
}
var Track1Filter = func(in string, data field.Field) string {
track := field.Track1{}
if err := newTrackData(data, &track); err != nil {
return in
}
track.PrimaryAccountNumber = PANFilter(track.PrimaryAccountNumber, nil)
return getTrackDataString(in, &track)
}
var Track2Filter = func(in string, data field.Field) string {
track := field.Track2{}
if err := newTrackData(data, &track); err != nil {
return in
}
track.PrimaryAccountNumber = PANFilter(track.PrimaryAccountNumber, nil)
return getTrackDataString(in, &track)
}
var Track3Filter = func(in string, data field.Field) string {
track := field.Track3{}
if err := newTrackData(data, &track); err != nil {
return in
}
track.PrimaryAccountNumber = PANFilter(track.PrimaryAccountNumber, nil)
return getTrackDataString(in, &track)
}
func newTrackData(data, track field.Field) error {
if raw, err := data.Pack(); err == nil {
track.SetSpec(data.Spec())
if _, err := track.Unpack(raw); err != nil {
return fmt.Errorf("creating new track data")
}
}
return nil
}
func getTrackDataString(in string, track field.Field) string {
if converted, packErr := track.String(); packErr != nil {
return in
} else {
return converted
}
}