69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package elmapi
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.rosy.net.cn/baseapi/platform/common"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"github.com/fatih/structs"
|
|
)
|
|
|
|
const (
|
|
OrderValid = 10
|
|
MerchantValid = 12
|
|
OrderCanceled = 14
|
|
MerchantInvalid = 15
|
|
OrderForceInvalid = 17
|
|
OrderFinished = 18
|
|
)
|
|
|
|
type ELMCallbackResponse struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
var (
|
|
SuccessResponse = &ELMCallbackResponse{"ok"}
|
|
)
|
|
|
|
func (e *ELMAPI) unmarshalData(data []byte, msg interface{}) (callbackResponse *ELMCallbackResponse) {
|
|
err := utils.UnmarshalUseNumber(data, msg)
|
|
if err != nil {
|
|
return &ELMCallbackResponse{
|
|
Message: fmt.Sprintf(common.CBErrMsgUnmarshal, data, err),
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *ELMAPI) CheckRequestValidation(mapData map[string]interface{}) (callbackResponse *ELMCallbackResponse) {
|
|
sign := e.signParamsMap(mapData, "")
|
|
if remoteSign, ok := mapData[signKey].(string); ok && sign != remoteSign {
|
|
e.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) {
|
|
msg = new(ELMCallbackMsg)
|
|
callbackResponse = e.unmarshalData(data, msg)
|
|
if callbackResponse != nil {
|
|
return nil, callbackResponse
|
|
}
|
|
|
|
mapData := structs.Map(msg)
|
|
callbackResponse = e.CheckRequestValidation(mapData)
|
|
return msg, callbackResponse
|
|
}
|