forked from rkbalgi/libiso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paysim_send_msg_handler.go
77 lines (61 loc) · 1.69 KB
/
paysim_send_msg_handler.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
package handlers
import (
"bytes"
"encoding/hex"
"encoding/json"
"github.com/rkbalgi/go/iso8583"
_ "github.com/rkbalgi/go/iso8583/services"
"github.com/rkbalgi/go/iso_host"
"log"
"net/http"
)
type SendMessageHandlerHandler struct {
}
func (handler *SendMessageHandlerHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
buf := make([]byte, 100)
jsonBuf := bytes.NewBufferString("")
for {
n, err := req.Body.Read(buf)
if n > 0 {
//we have good data
jsonBuf.Write(buf[:n])
}
if err != nil && n == 0 {
break
}
if err != nil && err.Error() == "EOF" {
//log.Println("error :: ", err)
break
} else if err != nil {
log.Println("error :: ", err)
return
}
}
log.Println("send msg request:: ", jsonBuf.String())
sndReq := iso8583.WebMsgData{Type: "Request"}
_ = json.Unmarshal(jsonBuf.Bytes(), &sndReq)
log.Println(sndReq.Spec, sndReq.DataArray)
//iso_msg_def:=iso8583.GetMessageDefByName(snd_req.Spec);
isoMsg := iso8583.NewIso8583Message(sndReq.Spec)
isoMsg.SetData(sndReq.DataArray)
log.Println("received request : ", isoMsg.Dump())
//handle the incoming message
reqData := isoMsg.Bytes()
log.Println("req: \n", hex.EncodeToString(reqData))
msgBuf := bytes.NewBuffer(reqData)
respIsoMsg, err := iso_host.Handle(sndReq.Spec, msgBuf)
if err != nil {
_, _ = w.Write([]byte("error: " + err.Error()))
return
}
log.Println("processed response: \n", respIsoMsg.Dump())
webMsgData := respIsoMsg.ToWebMsg(false)
jsonData, err := json.Marshal(webMsgData)
if err == nil {
log.Println("writing json ", string(jsonData))
_, _ = w.Write(jsonData)
} else {
log.Println("error marshalling json -", err.Error())
_, _ = w.Write([]byte("error"))
}
}