- weimob callback msg format

This commit is contained in:
gazebo
2019-01-20 10:09:25 +08:00
parent bc9707b8b6
commit 7f5c14b3b1
6 changed files with 89 additions and 20 deletions

View File

@@ -1,6 +1,8 @@
package weimobapi
import (
"crypto/md5"
"fmt"
"strings"
"time"
@@ -24,12 +26,15 @@ const (
)
type CallbackMsg struct {
ID string `json:"id"`
MsgEvent string `json:"msgEvent"`
PublicAccountID string `json:"public_account_id"`
OrderNo int64 `json:"orderNo"`
StatusTime time.Time `json:"statusTime"`
ChannelType int `json:"channelType"`
IsFake bool `json:"isFake"` // 是否自己生成的假消息
ID string `json:"id"`
MsgEvent string `json:"msgEvent"`
PublicAccountID string `json:"public_account_id"`
BusinessID string `json:"business_id"`
OrderNo int64 `json:"orderNo"`
StatusTime time.Time `json:"statusTime"`
ChannelType int `json:"channelType"`
}
type ErrorInfo struct {
@@ -63,6 +68,17 @@ func Err2CallbackResponse(err error, data string) *CallbackResponse {
}
}
func (a *API) CheckCallbackValidation(sign, msgSignature, id, msgBody string) bool {
localSign := fmt.Sprintf("%x", md5.Sum([]byte(a.appID+id+a.appSecret)))
// baseapi.SugarLogger.Debug(sign, ":", localSign)
if localSign == sign {
localSign := fmt.Sprintf("%x", md5.Sum([]byte(a.appID+id+msgBody+a.appSecret)))
// baseapi.SugarLogger.Debug(msgSignature, ":", localSign)
return localSign == msgSignature
}
return false
}
func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *CallbackResponse) {
var (
mapMsg, msgBody map[string]interface{}
@@ -71,7 +87,8 @@ func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *C
if err != nil {
return nil, Err2CallbackResponse(err, "")
}
err = utils.UnmarshalUseNumber([]byte(mapMsg["msg_body"].(string)), &msgBody)
msgBodyStr := mapMsg["msg_body"].(string)
err = utils.UnmarshalUseNumber([]byte(msgBodyStr), &msgBody)
if err != nil {
return nil, Err2CallbackResponse(err, "")
}
@@ -79,13 +96,19 @@ func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *C
ID: utils.Interface2String(mapMsg["id"]),
MsgEvent: utils.Interface2String(mapMsg["event"]),
PublicAccountID: utils.Interface2String(mapMsg["public_account_id"]),
BusinessID: utils.Interface2String(mapMsg["business_id"]),
OrderNo: utils.MustInterface2Int64(msgBody["orderNo"]),
}
sign := utils.Interface2String(mapMsg["sign"])
msgSignature := utils.Interface2String(mapMsg["msgSignature"])
if !a.CheckCallbackValidation(sign, msgSignature, msg.ID, msgBodyStr) {
return nil, Err2CallbackResponse(fmt.Errorf("sign is not match"), "")
}
if msg.MsgEvent == MsgEventCreateOrder {
msg.StatusTime = utils.Str2Time(strings.Replace(msgBody["createTime"].(string), ": ", ":", -1))
msg.ChannelType = int(utils.MustInterface2Int64(msgBody["channelType"]))
} else {
msg.StatusTime = time.Now()
}
return nil, SuccessResponse
return msg, nil
}