93 lines
2.7 KiB
Go
93 lines
2.7 KiB
Go
package ebaiapi
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
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 {
|
|
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,
|
|
}
|
|
}
|
|
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(cmd string, data []byte, msg interface{}) (callbackResponse *CallbackResponse) {
|
|
err := utils.UnmarshalUseNumber(data, msg)
|
|
if err != nil {
|
|
return a.Err2CallbackResponse(cmd, err, nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *API) CheckCallbackValidation(request *http.Request) (callbackResponse *CallbackResponse) {
|
|
sign := a.signParams(request.PostForm)
|
|
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 a.Err2CallbackResponse(request.FormValue("cmd"), errors.New(msg), nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackResponse *CallbackResponse) {
|
|
request.ParseForm()
|
|
if callbackResponse = a.CheckCallbackValidation(request); callbackResponse != nil {
|
|
return nil, callbackResponse
|
|
}
|
|
msg = new(CallbackMsg)
|
|
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"))
|
|
return msg, nil
|
|
}
|