-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
412 lines (350 loc) · 11.4 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/j03hanafi/bankiso/iso20022/pacs"
"github.com/antchfx/xmlquery"
)
func main() {
file, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal("Found error in log ", err)
}
log.SetOutput(file)
log.Println("Log setup")
path := pathHandler()
address := ":6067"
log.Printf("Biller started at %v", address)
err = http.ListenAndServe(address, path)
if err != nil {
log.Fatal(err.Error())
}
}
func pathHandler() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/", biller).Methods("POST")
router.HandleFunc("/biller", networkManagement).Methods("POST")
//router.HandleFunc("/biller3", biller3).Methods("POST")
return router
}
func biller3(w http.ResponseWriter, r *http.Request) {
log.Println("New Request from BIFast Connector XML")
fmt.Println("New Request from BIFast Connector XML")
body, _ := ioutil.ReadAll(r.Body)
rawRequest := string(body)
log.Println(rawRequest)
request := BusMsg{}
err := xml.Unmarshal(body, &request)
if err != nil {
log.Printf("Error unmarshal XML: %s", err.Error())
}
fmt.Println(request)
appHdr := request.AppHdr
bizMsgIdr := fmt.Sprintf("%v", *appHdr.BusinessMessageIdentifier)
trxType := bizMsgIdr[16:19]
fmt.Println(trxType)
var fileName string
switch trxType {
case "510":
type Doc struct {
XMLName xml.Name `xml:"BusMsg"`
Document pacs.Document00800108 `xml:"Document"`
}
document := Doc{}
err := xml.Unmarshal(body, &document)
if err != nil {
log.Printf("Error unmarshal XML: %s", err.Error())
}
CrAccId := *document.Document.Message.CreditTransferTransactionInformation[0].CdtrAcct.Id.Other.Identification
fmt.Println(CrAccId)
fileName = "accEnqRes.xml"
}
fileName = "xml/" + fileName
fmt.Println("filename:", fileName)
file, _ := os.Open(fileName)
defer file.Close()
response, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
responseFormatter2(w, response, 200)
}
func biller(w http.ResponseWriter, r *http.Request) {
log.Println("New Request from BIFast Connector")
fmt.Println("New Request from BIFast Connector")
body, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(body))
requestRaw := BusMsg{}
err := xml.Unmarshal(body, &requestRaw)
if err != nil {
log.Printf("Error unmarshal JSON: %s", err.Error())
}
fmt.Println("request: ", requestRaw)
// var msgID string
var fileName string
bzMsgID := fmt.Sprintf("%v", *requestRaw.AppHdr.BusinessMessageIdentifier)
MsgDefIdr := *requestRaw.AppHdr.MessageDefinitionIdentifier
trxType := bzMsgID[16:19]
fmt.Println("trxType:", trxType)
fmt.Println("MsgDefIdr:", MsgDefIdr)
switch trxType {
case "000":
if MsgDefIdr != "pacs.028.001.04" {
fmt.Println("Network management body request : ")
payload := networkRequest(body)
responseFormatter(w, payload, 200)
} else {
print("ok")
fileName = "paymentStatusResponse.xml"
}
// ##################### Account Enquiry ##################################
case "510":
var CrAccId string
doc, err := xmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
panic(err)
}
root := xmlquery.FindOne(doc, "//ns:BusMsg")
if n := root.SelectElement("//ns:Document/ns:FIToFICstmrCdtTrf/ns2:CdtTrfTxInf/ns2:CdtrAcct/ns2:Id/ns2:Othr/ns2:Id"); n != nil {
fmt.Printf("Name #%s\n", n.InnerText())
CrAccId = n.InnerText()
}
switch CrAccId {
case "510654300":
fileName = "accountEnquiryResponse.xml"
fmt.Println(CrAccId)
case "511654182":
fileName = "accountEnquiryResponse2.xml"
fmt.Println(CrAccId)
case "0000000000":
fileName = "rejectMessage.xml"
}
//##################### Credit Transfer ###################################
case "010": // Credit Transfer
var CrAccId string
doc, err := xmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
panic(err)
}
root := xmlquery.FindOne(doc, "//ns:BusMsg")
if n := root.SelectElement("//ns:Document/ns:FIToFICstmrCdtTrf/ns2:CdtTrfTxInf/ns2:CdtrAcct/ns2:Id/ns2:Othr/ns2:Id"); n != nil {
fmt.Printf("Name #%s\n", n.InnerText())
CrAccId = n.InnerText()
}
switch CrAccId {
case "0102345600":
fileName = "creditTransferResponse.xml"
case "0102345184":
fileName = "creditTransferResponse2.xml"
case "0000000000":
fileName = "rejectMessage.xml"
}
case "012":
fileName = "sampleCreditTransferResponse012.json"
fmt.Println("012")
case "110":
fileName = "sampleCreditTransferResponsewithProxy.json"
fmt.Println("110")
//==========================================================================
case "019":
fileName = "sampleFItoFICreditTransfer.json"
fmt.Println("019")
case "011":
fileName = "creditTransferResponse.xml"
fmt.Println("011")
// ################# Proxy Resolution #####################################
case "610":
var PxValue string
doc, err := xmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
panic(err)
}
root := xmlquery.FindOne(doc, "//ns:BusMsg")
if n := root.SelectElement("//ns:Document/ns:PrxyLookUp/ns2:LookUp/ns2:PrxyOnly/ns2:PrxyRtrvl/ns2:Val"); n != nil {
fmt.Printf("Name #%s\n", n.InnerText())
PxValue = n.InnerText()
}
switch PxValue {
case "086102345000":
fileName = "proxyResolutionResponse.xml"
case "086112345101":
fileName = "proxyResolutionResponse2.xml"
case "086112345804":
fileName = "proxyResolutionResponse3.xml"
case "086132345600":
fileName = "proxyResolutionResponse4.xml"
case "086142345804":
fileName = "proxyResolutionResponse5.xml"
case "08615234804":
fileName = "proxyResolutionResponse6.xml"
case "08616234811":
fileName = "proxyResolutionResponse7.xml"
case "08617234805":
fileName = "proxyResolutionResponse8.xml"
case "0000000000":
fileName = "rejectMessage.xml"
}
case "611":
fileName = "sampleProxyResolution611.json"
fmt.Println("611")
case "612":
fileName = "sampleProxyResolution612.json"
fmt.Println(("612"))
// =========================================================================
// ################# Proxy Registration Inquiry ############################
case "620":
var PxRegId string
doc, err := xmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
panic(err)
}
root := xmlquery.FindOne(doc, "//ns:BusMsg")
if n := root.SelectElement("//ns:Document/ns:PrxyNqryReq/ns2:GrpHdr/ns2:MsgSndr/ns2:Acct/ns2:Id/ns2:Othr/ns2:Id"); n != nil {
fmt.Printf("Name #%s\n", n.InnerText())
PxRegId = n.InnerText()
}
fmt.Println(PxRegId)
switch PxRegId {
case "6202345600":
fileName = "proxyRegistrationResponse.xml"
case "6212345101":
fileName = "proxyRegistrationResponse2.xml"
case "6222345808":
fileName = "proxyRegistrationResponse3.xml"
case "6232345600":
fileName = "proxyRegistrationResponse4.xml"
case "6242345600":
fileName = "proxyRegistrationResponse5.xml"
case "6252345808":
fileName = "proxyRegistrationResponse6.xml"
case "6262345806":
fileName = "proxyRegistrationResponse7.xml"
case "0000000000":
fileName = "rejectMessage.xml"
}
case "621":
fileName = "sampleProxyRegistrationInquiry621.json"
fmt.Println("621")
case "622":
fileName = "sampleProxyRegistrationInquiry622.json"
fmt.Println("622")
// =========================================================================
case "710":
fileName = "sampleRegisterNewProxy.json"
fmt.Println("710")
//#################### Proxy Maintenance ###################################
case "720":
var PxValue string
doc, err := xmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
panic(err)
}
root := xmlquery.FindOne(doc, "//ns:BusMsg")
if n := root.SelectElement("//ns:Document/ns:PrxyRegn/ns2:Regn/ns2:Prxy/ns2:Val"); n != nil {
fmt.Printf("Name #%s\n", n.InnerText())
PxValue = n.InnerText()
}
switch PxValue {
case "7202345600":
fileName = "proxyMaintenanceResponse.xml"
case "7212345101":
fileName = "proxyMaintenanceResponse2.xml"
case "0000000000":
fileName = "rejectMessage.xml"
}
case "721":
fileName = "sampleProxyMaintenance721.json"
fmt.Println("721")
//============================================================================
default:
fileName = "rejectMessage.xml"
fmt.Println("default")
}
if MsgDefIdr != "admn.001.001.01" {
fileName = "xml/" + fileName
fmt.Println("filename:", fileName)
file, _ := os.Open(fileName)
defer file.Close()
response, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
responseFormatter(w, response, 200)
}
}
func responseFormatter(w http.ResponseWriter, data []byte, statusCode int) {
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(statusCode)
w.Write(data)
}
func responseFormatter2(w http.ResponseWriter, data []byte, statusCode int) {
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(statusCode)
w.Write(data)
}
func networkRequest(data []byte) []byte {
request := Admn1{}
err := xml.Unmarshal(data, &request)
if err != nil {
fmt.Println("error")
}
fmt.Println(request.Document.AdmnReq.AdmnTxInf.FnctnCd)
response := Admn2{}
response.Ns = "urn:iso"
response.Ns1 = "urn:iso:std:iso:20022:tech:xsd:head.001.001.01"
response.Ns2 = "urn:iso:std:iso:20022:tech:xsd:admn.002.001.01"
response.Xsi = "http://www.w3.org/2001/XMLSchema-instance"
response.SchemaLocation = "urn:iso ../../../../xsd/phase1/MainCIHub.xsd "
response.AppHdr.Fr.FIId.FinInstnId.Othr.ID = request.AppHdr.Fr.FIId.FinInstnId.Othr.ID
response.AppHdr.To.FIId.FinInstnId.Othr.ID = request.AppHdr.To.FIId.FinInstnId.Othr.ID
response.AppHdr.BizMsgIdr = request.AppHdr.BizMsgIdr
response.AppHdr.MsgDefIdr = "admn.002.001.01"
response.AppHdr.CreDt = time.Now().Format("2006-01-02T15:04:05.000")
response.Document.AdmnResp.GrpHdr.MsgId = time.Now().Format("2006-01-02T15:04:05Z07:00")[0:4] + time.Now().Format("2006-01-02T15:04:05Z07:00")[5:7] + time.Now().Format("2006-01-02T15:04:05Z07:00")[8:10] + ("INDOIDJA" + fmt.Sprint(rand.Intn(9)) + fmt.Sprint(rand.Intn(9)) + fmt.Sprint(rand.Intn(9)) + fmt.Sprint(rand.Intn(9)) +
fmt.Sprint(rand.Intn(9)) + fmt.Sprint(rand.Intn(9)) + fmt.Sprint(rand.Intn(9)) + fmt.Sprint(rand.Intn(9)))
response.Document.AdmnResp.GrpHdr.CreDtTm = time.Now().Format("2006-01-02T15:04:05.000")
response.Document.AdmnResp.AdmnResponse.InstgAgt.FinInstnId.Othr.ID = "NOBUIDJA"
response.Document.AdmnResp.AdmnResponse.OrgnlInstrId = request.Document.AdmnReq.AdmnTxInf.InstrId
response.Document.AdmnResp.AdmnResponse.FnctnCd = request.Document.AdmnReq.AdmnTxInf.FnctnCd
response.Document.AdmnResp.AdmnResponse.TxSts = "ACTC"
payload, err := xml.Marshal(response)
if err != nil {
fmt.Println("error")
}
fmt.Println("response network management: ", string(payload))
return payload
}
func networkManagement(w http.ResponseWriter, r *http.Request) {
fmt.Println("New Request from BIFast Connector")
body, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(body))
requestRaw := BusMsg{}
err := xml.Unmarshal(body, &requestRaw)
if err != nil {
fmt.Printf("Error unmarshal JSON: %s", err.Error())
}
fmt.Println("request: ", requestRaw)
// var msgID string
bzMsgID := fmt.Sprintf("%v", *requestRaw.AppHdr.BusinessMessageIdentifier)
trxType := bzMsgID[16:19]
fmt.Println("trxType:", trxType)
switch trxType {
case "000":
fmt.Println("Network management body request : ")
payload := networkRequest(body)
responseFormatter(w, payload, 200)
default:
fmt.Println("BusMsgId salah")
}
}
// func main() {
// tes()
// }