package weimobapi import ( "crypto/md5" "fmt" "strings" "time" "git.rosy.net.cn/baseapi/utils" ) const ( MsgTopicOrder = "ec_order" MsgTopicRights = "ec_rights" ) const ( MsgEventCreateOrder = "createOrder" MsgEventOrderStatusChange = "orderStatusChange" MsgEventCreateRights = "createRights" MsgEventCompleteRights = "completeRights" MsgEventCancelRights = "cancelRights" ) const ( ChannelTypeWeixinOpen = 0 ChannelTypeWeixinMini = 1 ChannelTypeH5 = 2 ChannelTypeQQ = 3 ChannelTypeWeibo = 4 ChannelTypeToutiao = 5 ChannelTypeAliPay = 6 ChannelTypeOffline = 7 ) type CallbackMsg struct { IsFake bool `json:"isFake"` // 是否自己生成的假消息 ID string `json:"id"` Topic string `json:"topic"` Event string `json:"event"` PublicAccountID string `json:"public_account_id"` BusinessID string `json:"business_id"` StoreID int64 `json:"storeId"` RightsID int64 `json:"rightsId"` OrderNo int64 `json:"orderNo"` StatusTime time.Time `json:"statusTime"` ChannelType int `json:"channelType"` } type ErrorInfo struct { ErrCode int `json:"errcode"` ErrMsg string `json:"errmsg"` } type CallbackResponse struct { Code ErrorInfo `json:"code"` } var ( SuccessResponse = &CallbackResponse{ Code: ErrorInfo{ ErrMsg: "success", }, } ) func Err2CallbackResponse(err error, data string) *CallbackResponse { if err == nil { return SuccessResponse } if data == "" { data = err.Error() } return &CallbackResponse{ Code: ErrorInfo{ ErrCode: -1, ErrMsg: data, }, } } 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{} ) err := utils.UnmarshalUseNumber(body, &mapMsg) if err != nil { return nil, Err2CallbackResponse(err, "") } msgBodyStr := mapMsg["msg_body"].(string) err = utils.UnmarshalUseNumber([]byte(msgBodyStr), &msgBody) if err != nil { return nil, Err2CallbackResponse(err, "") } msg = &CallbackMsg{ ID: utils.Interface2String(mapMsg["id"]), Topic: utils.Interface2String(mapMsg["topic"]), Event: 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.Event == MsgEventCreateOrder { msg.StatusTime = utils.Str2Time(strings.Replace(msgBody["createTime"].(string), ": ", ":", -1)) msg.ChannelType = int(utils.MustInterface2Int64(msgBody["channelType"])) } else { msg.StatusTime = time.Now() if msg.Topic == MsgTopicRights { msg.RightsID = utils.MustInterface2Int64(msgBody["rightsId"]) msg.StoreID = utils.MustInterface2Int64(msgBody["storeId"]) } } return msg, nil }