-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
179 lines (154 loc) · 3.55 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package iso8583SDK
import (
"fmt"
"log"
)
type FormatType int
const (
Format_unknow FormatType = 0
Format_a FormatType = 1
Format_an FormatType = 2
Format_as FormatType = 3
Format_ns FormatType = 4
Format_ans FormatType = 5
Format_z FormatType = 6
Format_n FormatType = 7
Format_b FormatType = 8
Format_s FormatType = 9
)
type LenType string
const (
Len_Fix LenType = "FIX"
Len_VarL LenType = "VARL"
Len_VarLL LenType = "VARLL"
Len_VarLLL LenType = "VARLLL"
Len_TLV LenType = "TLV"
)
type Attr struct {
Len int
LenType LenType
Format FormatType
}
func (a Attr) valid() bool {
if a.Len > 0 && a.LenType != "" {
return true
}
return false
}
type header struct {
MessageType string
}
type ISO8583 struct {
header
bInited bool
FieldsArray []int
ValueMap map[int]interface{}
AttrMap map[int]Attr
}
func CreateISO8583(msgType string) *ISO8583 {
return &ISO8583{
ValueMap: make(map[int]interface{}),
AttrMap: make(map[int]Attr),
bInited: true,
header: header{
MessageType: msgType,
},
}
}
type Config struct {
Host string //后台地址
TPDU string //tpdu
EDS string //eds
TerminalId string
MerchantId string
TMK string
TMKIndex string
TimeOut int
}
func (iso *ISO8583) AddField(field int, attr Attr, value string) {
if !iso.valid() {
log.Println("error: the iso8583 is not init !")
return
}
if field < 2 || field > 63 {
panic("the field must in 1 < x < 64")
}
if attr.Len <= 0 || attr.LenType == "" || attr.Format == Format_unknow {
panic("the attr must fill all para !")
}
iso.FieldsArray = append(iso.FieldsArray, field)
iso.ValueMap[field] = value
iso.AttrMap[field] = attr
}
func (iso *ISO8583) AddFieldDefault(field int, value string) {
if !iso.valid() {
log.Println("error: the iso8583 is not init !")
return
}
if field < 2 || field > 63 {
panic("the field must in 1 < x < 64")
}
if attr, ok := defaultAttrMap[field]; ok {
iso.ValueMap[field] = value
iso.AttrMap[field] = attr
iso.FieldsArray = append(iso.FieldsArray, field)
} else {
panic(fmt.Sprintf("the default field %d not exist !", field))
}
}
func (iso *ISO8583) AddSubField(parentField, field int, attr Attr, value string) {
if !iso.valid() {
log.Println("error: the iso8583 is not init, please use createISO8583 first !")
return
}
if iso.AttrMap[parentField].Len == 0 {
log.Println("error: the parent field is empty !")
return
}
subIso := new(ISO8583)
subIso.FieldsArray = append(subIso.FieldsArray, field)
subIso.AttrMap[field] = attr
iso.ValueMap[parentField] = subIso
}
func (iso *ISO8583) DeleteField(field int) {
delete(iso.ValueMap, field)
delete(iso.AttrMap, field)
}
func (iso *ISO8583) DeleteSubField(parentField, field int) {
if iso.AttrMap[parentField].Len == 0 {
log.Println("error: the parent field is empty !")
return
}
if subISO, OK := iso.ValueMap[parentField].(ISO8583); OK {
subISO.DeleteField(field)
} else {
log.Printf("the parentField: %d not exist subFiled: %d \n", parentField, field)
}
}
func (iso *ISO8583) valid() bool {
if !iso.bInited {
return false
}
return true
}
func (iso *ISO8583) ToJson() []byte {
return nil
}
func (iso *ISO8583) ToXml() []byte {
return nil
}
func ParseBytes(dataBytes, formatBytes []byte) *ISO8583 {
return nil
}
func LoadFromJson(dataBytes []byte) *ISO8583 {
return nil
}
func LoadFromXml(dataBytes []byte) *ISO8583 {
return nil
}
func Marshal(v interface{}) ([]byte, error) {
return nil, nil
}
func Unmarshal(data []byte, v interface{}) error {
return nil
}