-
Notifications
You must be signed in to change notification settings - Fork 0
/
cortex.go
378 lines (342 loc) · 10.1 KB
/
cortex.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"errors"
"fmt"
"io"
"sort"
"github.com/moov-io/iso8583"
"github.com/moov-io/iso8583/encoding"
"github.com/moov-io/iso8583/field"
"github.com/moov-io/iso8583/network"
"github.com/rs/zerolog/log"
)
const header = "ISO8583-1993001000000"
type cortexSwitch struct {
spec iso8583.MessageSpec
}
var fisGlobalSpec = &iso8583.MessageSpec{
Fields: map[int]field.Field{
0: field.NewString(&field.Spec{
Length: 4,
Description: "Message Type Indicator",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
1: field.NewBitmap(&field.Spec{
Description: "Bitmap",
Enc: encoding.Binary,
Pref: BCDPrefixer.Fixed,
}),
2: field.NewString(&field.Spec{
Length: 99,
Description: "Primary Account Number",
Enc: encoding.BCD,
Pref: BCDPrefixer.LL,
}),
3: field.NewString(&field.Spec{
Length: 6,
Description: "Processing Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
4: field.NewString(&field.Spec{
Length: 12,
Description: "Transaction Amount",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
6: field.NewString(&field.Spec{
Length: 12,
Description: "Card Holder Billing Amount",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
7: field.NewString(&field.Spec{
Length: 10,
Description: "Transmission Date and Time",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
11: field.NewString(&field.Spec{
Length: 6,
Description: "Trace Number",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
12: field.NewString(&field.Spec{
Length: 12,
Description: "Local Transaction Date and Time",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
26: field.NewString(&field.Spec{
Length: 4,
Description: "Merchant Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
30: field.NewString(&field.Spec{
Length: 12,
Description: "Original Amount",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
32: field.NewString(&field.Spec{
Length: 99,
Description: "Acquirer Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.LL,
}),
33: field.NewString(&field.Spec{
Length: 99,
Description: "Forwarding Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.LL,
}),
37: field.NewString(&field.Spec{
Length: 12,
Description: "Retrieval Reference Number",
Enc: encoding.ASCII,
Pref: BCDPrefixer.Fixed,
}),
39: field.NewString(&field.Spec{
Length: 3,
Description: "Response Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
41: field.NewString(&field.Spec{
Length: 8,
Description: "Terminal ID",
Enc: encoding.ASCII,
Pref: BCDPrefixer.Fixed,
}),
43: field.NewString(&field.Spec{
Length: 99,
Description: "Terminal Name and Location",
Enc: encoding.ASCII,
Pref: BCDPrefixer.LL,
}),
46: field.NewString(&field.Spec{
Length: 999,
Description: "Transaction Fee",
Enc: encoding.ASCII,
Pref: BCDPrefixer.LLL,
}),
49: field.NewString(&field.Spec{
Length: 3,
Description: "Transaction Currency Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
51: field.NewString(&field.Spec{
Length: 3,
Description: "Card Holder Currency Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.Fixed,
}),
54: field.NewString(&field.Spec{
Length: 999,
Description: "Account Balance",
Enc: encoding.ASCII,
Pref: BCDPrefixer.LLL,
}),
56: field.NewString(&field.Spec{
Length: 99,
Description: "Original Data Elements",
Enc: encoding.BCD,
Pref: BCDPrefixer.LL,
}),
100: field.NewString(&field.Spec{
Length: 99,
Description: "Receiving Code",
Enc: encoding.BCD,
Pref: BCDPrefixer.LL,
}),
102: field.NewString(&field.Spec{
Length: 99,
Description: "Source Account",
Enc: encoding.ASCII,
Pref: BCDPrefixer.LL,
}),
103: field.NewString(&field.Spec{
Length: 99,
Description: "Destination Account",
Enc: encoding.ASCII,
Pref: BCDPrefixer.LL,
}),
},
}
func (s *cortexSwitch) getMti(message Message, reversal bool) string {
if reversal {
return fmt.Sprintf("1%s", FinancialReversal)
}
switch message.Channel {
case MASTERCARD:
return fmt.Sprintf("1%s", FinancialRequestMasterVisa)
default:
return fmt.Sprintf("1%s", FinancialRequest)
}
}
func (s *cortexSwitch) getProcessCode(message Message) string {
var processCode string
transaction := message.Transaction
device := message.Device
switch transaction {
case PURCHASE, ELOAD:
processCode = "00"
case WITHDRAW:
processCode = "01"
case IBFTD:
processCode = "10"
case IBFTC:
if message.TargetBank == INTER_SYSTEM {
processCode = "26"
} else {
processCode = "21"
}
case BAL_INQ:
if device == ATM {
processCode = "30"
} else {
processCode = "31"
}
case FT:
processCode = "48"
case BILLS:
processCode = "51"
default:
panic("Unable to get Process Code")
}
return processCode
}
var cortex = &cortexSwitch{
spec: *fisGlobalSpec,
}
func (s *cortexSwitch) build(message *Message, reversal bool) {
originalMti := message.Mti
message.Mti = s.getMti(*message, reversal)
if reversal {
originalDataElements := s.serializeOriginalDataElements(originalMti, message.TraceNumber, message.LocalTransactionDateTime, padLeftWithZeros(message.AcquiringInstitutionCode, 10))
message.OriginalDataElements = originalDataElements
message.Transaction = "REVERSAL " + message.Transaction
return
}
message.TransmissionDateTime = generateTransmissionDateTime()
message.TraceNumber = generateStan()
message.Rrn = generateRrn()
message.LocalTransactionDateTime = generateLocalTransactionDateTime(message.TransmissionDateTime)
message.ProcessCode = s.getProcessCode(*message) + "0000"
}
func (s *cortexSwitch) pack(message Message) ([]byte, error) {
isoMesage := iso8583.NewMessage(fisGlobalSpec)
isoMesage.MTI(message.Mti)
if len(message.PrimaryAccountNumber) > 0 {
isoMesage.Field(2, message.PrimaryAccountNumber)
}
isoMesage.Field(3, message.ProcessCode)
isoMesage.Field(4, padLeftWithZeros(moveDecimalRight(message.TransactionAmount), fisGlobalSpec.Fields[4].Spec().Length))
isoMesage.Field(6, padLeftWithZeros(moveDecimalRight(message.TransactionAmount), fisGlobalSpec.Fields[6].Spec().Length))
isoMesage.Field(7, message.TransmissionDateTime)
isoMesage.Field(11, message.TraceNumber)
isoMesage.Field(12, message.LocalTransactionDateTime)
isoMesage.Field(26, string(message.Device))
isoMesage.Field(30, padLeftWithZeros(moveDecimalRight(message.TransactionAmount), fisGlobalSpec.Fields[4].Spec().Length))
isoMesage.Field(32, padLeftWithZeros(message.AcquiringInstitutionCode, 10))
isoMesage.Field(37, message.Rrn)
if len(message.TerminalID) > 0 {
isoMesage.Field(41, message.TerminalID)
}
if len(message.TerminalNameAndLocation) > 0 {
isoMesage.Field(43, string(message.TerminalNameAndLocation))
}
isoMesage.Field(46, serializeCortexFee(message.TransactionFee))
if len(message.CurrencyCode) > 0 {
isoMesage.Field(49, string(message.CurrencyCode))
}
isoMesage.Field(51, string(PHP))
if len(message.OriginalDataElements) > 0 {
isoMesage.Field(56, message.OriginalDataElements)
}
if len(message.ReceivingInstitutionCode) > 0 {
isoMesage.Field(100, message.ReceivingInstitutionCode)
}
if len(message.SourceAccount) > 0 {
isoMesage.Field(102, message.SourceAccount)
}
if len(message.DestinationAccount) > 0 {
isoMesage.Field(103, message.DestinationAccount)
}
keys := make([]int, 0, len(isoMesage.GetFields()))
for k := range isoMesage.GetFields() {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
val, _ := isoMesage.GetString(k)
log.Printf("Field %d: %s", k, val)
}
rawMessage, err := isoMesage.Pack()
if err != nil {
return nil, err
}
headerBytes := []byte(header)
result := append(headerBytes, rawMessage...)
originalLength := len(result)
lengthPrefix := make([]byte, 2)
lengthValue := int16(originalLength)
lengthPrefix[0] = byte(lengthValue >> 8)
lengthPrefix[1] = byte(lengthValue)
withPrefix := append(lengthPrefix, result...)
return withPrefix, err
}
func (s *cortexSwitch) unpack(r io.Reader) (AtmResponse, error) {
lengthBuffer := network.NewBinary2BytesHeader()
lengthBuffer.ReadFrom(r)
length := lengthBuffer.Len
response := make([]byte, length)
n, err := io.ReadFull(r, response)
if err != nil {
return AtmResponse{}, err
}
if n < 21 {
return AtmResponse{}, err
}
header := make([]byte, 21)
copy(header, response[:21])
responseMessage := iso8583.NewMessage(fisGlobalSpec)
unpacked := make([]byte, length)
copy(unpacked, response[21:])
responseMessage.Unpack(unpacked)
traceNumber, _ := responseMessage.GetField(11).String()
responseCode, _ := responseMessage.GetField(39).String()
rrn, _ := responseMessage.GetField(37).String()
balanceField, _ := responseMessage.GetField(54).String()
balance := balanceDeserializer(balanceField)
atmResponse := AtmResponse{
TraceNumber: traceNumber,
ResponseCode: responseCode,
RRN: rrn,
Balance: fmt.Sprintf("%.2f", balance),
}
keys := make([]int, 0, len(responseMessage.GetFields()))
for k := range responseMessage.GetFields() {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
val, _ := responseMessage.GetString(k)
log.Printf("Field %d: %s", k, val)
}
return atmResponse, nil
}
func serializeCortexFee(fee float64) string {
serializedFee := fmt.Sprint("00608D-", padLeftWithZeros(moveDecimalRight(fee), 7))
return addTrailingSpaces(serializedFee, 34)
}
func (s *cortexSwitch) serializeOriginalDataElements(mti string, traceNumber string, localTransactionDateTime string, acquiringCode string) string {
return fmt.Sprint(mti, traceNumber, localTransactionDateTime, "01", acquiringCode)
}
func (s *cortexSwitch) packEchoTest() ([]byte, error) {
return nil, errors.New("echo test not supported")
}