- fix ebai callback response.
This commit is contained in:
@@ -4,38 +4,65 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
type CallbackResponse ResponseResult
|
||||
|
||||
type CallbackMsg struct {
|
||||
Cmd string
|
||||
TimeStamp int64
|
||||
Body map[string]interface{}
|
||||
type CallbackResponse struct {
|
||||
Cmd string `json:"cmd"`
|
||||
Sign string `json:"sign"`
|
||||
Source string `json:"source"`
|
||||
Ticket string `json:"ticket"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Version int `json:"version"`
|
||||
Body *ResponseResult `json:"body"`
|
||||
}
|
||||
|
||||
func Err2CallbackResponse(err error, data interface{}) *CallbackResponse {
|
||||
type CallbackMsg struct {
|
||||
Cmd string `json:"cmd"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
}
|
||||
|
||||
func (a *API) Err2CallbackResponse(cmd string, err error, data interface{}) *CallbackResponse {
|
||||
response := &CallbackResponse{
|
||||
Cmd: cmd,
|
||||
Source: a.source,
|
||||
Ticket: utils.GetUpperUUID(),
|
||||
Timestamp: utils.GetCurTimestamp(),
|
||||
Version: 3,
|
||||
}
|
||||
if err == nil {
|
||||
return &CallbackResponse{
|
||||
response.Body = &ResponseResult{
|
||||
Error: "success",
|
||||
ErrNo: 0,
|
||||
Data: data,
|
||||
}
|
||||
} else {
|
||||
response.Body = &ResponseResult{
|
||||
Error: fmt.Sprintf("error:%v, data:%v", err, data),
|
||||
ErrNo: -1,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
return &CallbackResponse{
|
||||
Error: fmt.Sprintf("error:%v, data:%v", err, data),
|
||||
ErrNo: -1,
|
||||
Data: data,
|
||||
params := url.Values{
|
||||
"cmd": []string{"resp." + cmd},
|
||||
"version": []string{utils.Int2Str(response.Version)},
|
||||
"timestamp": []string{utils.Int64ToStr(response.Timestamp)},
|
||||
"ticket": []string{response.Ticket},
|
||||
"source": []string{response.Source},
|
||||
"body": []string{string(utils.MustMarshal(response.Body))},
|
||||
}
|
||||
response.Sign = a.signParams(params)
|
||||
return response
|
||||
}
|
||||
|
||||
func (a *API) unmarshalData(data []byte, msg interface{}) (callbackResponse *CallbackResponse) {
|
||||
func (a *API) unmarshalData(cmd string, data []byte, msg interface{}) (callbackResponse *CallbackResponse) {
|
||||
err := utils.UnmarshalUseNumber(data, msg)
|
||||
if err != nil {
|
||||
return Err2CallbackResponse(err, nil)
|
||||
return a.Err2CallbackResponse(cmd, err, nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -45,7 +72,7 @@ func (a *API) CheckCallbackValidation(request *http.Request) (callbackResponse *
|
||||
if sign != request.FormValue(signKey) {
|
||||
msg := fmt.Sprintf("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
|
||||
baseapi.SugarLogger.Info(msg)
|
||||
return Err2CallbackResponse(errors.New(msg), nil)
|
||||
return a.Err2CallbackResponse(request.FormValue("cmd"), errors.New(msg), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -56,10 +83,10 @@ func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackR
|
||||
return nil, callbackResponse
|
||||
}
|
||||
msg = new(CallbackMsg)
|
||||
if callbackResponse = a.unmarshalData([]byte(request.FormValue("body")), &msg.Body); callbackResponse != nil {
|
||||
if callbackResponse = a.unmarshalData(request.FormValue("cmd"), []byte(request.FormValue("body")), &msg.Body); callbackResponse != nil {
|
||||
return nil, callbackResponse
|
||||
}
|
||||
msg.Cmd = request.FormValue("cmd")
|
||||
msg.TimeStamp = utils.MustInterface2Int64(request.FormValue("timestamp"))
|
||||
msg.Timestamp = utils.MustInterface2Int64(request.FormValue("timestamp"))
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user