- dadaapi added.

- refactor.
This commit is contained in:
gazebo
2018-06-21 14:23:01 +08:00
parent e1b377086e
commit 851b2c1eeb
10 changed files with 424 additions and 70 deletions

View File

@@ -23,14 +23,14 @@ type ELMCallbackResponse struct {
}
type ELMCallbackMsg struct {
AppId int `json:"appId" structs:"appId"`
RequestId string `json:"requestId" structs:"requestId"`
Type int `json:"type" structs:"type"`
Message string `json:"message" structs:"message"`
ShopId int `json:"shopId" structs:"shopId"`
Timestamp int64 `json:"timestamp" structs:"timestamp"`
UserId int64 `json:"userId" structs:"userId"`
Signature string `json:"signature" structs:"signature"`
AppId int `json:"appId"`
RequestId string `json:"requestId"`
Type int `json:"type"`
Message string `json:"message"`
ShopId int `json:"shopId"`
Timestamp int64 `json:"timestamp"`
UserId int64 `json:"userId"`
Signature string `json:"signature"`
}
var (
@@ -47,23 +47,22 @@ func (e *ELMAPI) unmarshalData(data []byte, msg interface{}) (callbackResponse *
return nil
}
func (e *ELMAPI) CheckRequestValidation(mapData map[string]interface{}) (callbackResponse *ELMCallbackResponse) {
func (e *ELMAPI) CheckCallbackValidation(mapData map[string]interface{}) (callbackResponse *ELMCallbackResponse) {
sign := e.signParamsMap(mapData, "")
if remoteSign, ok := mapData[signKey].(string); ok && sign != remoteSign {
if remoteSign, _ := mapData[signKey].(string); sign != remoteSign {
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, remoteSign)
return &ELMCallbackResponse{Message: "signature is invalid"}
}
return nil
}
func (e *ELMAPI) GetMsgFromData(data []byte) (msg *ELMCallbackMsg, callbackResponse *ELMCallbackResponse) {
func (e *ELMAPI) GetCallbackMsg(data []byte) (msg *ELMCallbackMsg, callbackResponse *ELMCallbackResponse) {
msg = new(ELMCallbackMsg)
callbackResponse = e.unmarshalData(data, msg)
if callbackResponse != nil {
if callbackResponse = e.unmarshalData(data, msg); callbackResponse != nil {
return nil, callbackResponse
}
mapData := structs.Map(msg)
callbackResponse = e.CheckRequestValidation(mapData)
callbackResponse = e.CheckCallbackValidation(mapData)
return msg, callbackResponse
}

View File

@@ -20,10 +20,10 @@ func init() {
baseapi.Init(sugarLogger)
// sandbox
elmapi = NewELMAPI("2d2b583447b04b6bba5a6f3faed3559b", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
// elmapi = NewELMAPI("b4f7e424475c3758c111dc60ceec3e2a", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
// prod
// elmapi = NewELMAPI("bab2a27f99562f394b411dbb9a6214da", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
elmapi = NewELMAPI("bab2a27f99562f394b411dbb9a6214da", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
}
func TestTest(t *testing.T) {
@@ -57,7 +57,7 @@ func TestGetOrder(t *testing.T) {
func TestCallbackSign(t *testing.T) {
jsonStr := `{"requestId":"200016348669063447","type":18,"appId":78247922,"message":"{\"orderId\":\"3024923917769149510\",\"state\":\"settled\",\"shopId\":157492364,\"updateTime\":1529465510,\"role\":1}","shopId":157492364,"timestamp":1529465510255,"signature":"D65F917D93B4F599B85486C799599141","userId":336072266322770688}`
msg, response := elmapi.GetMsgFromData([]byte(jsonStr))
msg, response := elmapi.GetCallbackMsg([]byte(jsonStr))
if response != nil || msg == nil {
t.Fatal("Something wrong")
}