- big big refactor.
This commit is contained in:
69
platformapi/elmapi/callback.go
Normal file
69
platformapi/elmapi/callback.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package elmapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"github.com/fatih/structs"
|
||||
)
|
||||
|
||||
// https://open.shop.ele.me/openapi/documents/callback
|
||||
const (
|
||||
MsgTypeOrderValid = 10
|
||||
MsgTypeMerchantValid = 12
|
||||
MsgTypeOrderCanceled = 14
|
||||
MsgTypeMerchantInvalid = 15
|
||||
MsgTypeOrderForceInvalid = 17
|
||||
MsgTypeOrderFinished = 18
|
||||
)
|
||||
|
||||
type CallbackResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type CallbackMsg struct {
|
||||
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 (
|
||||
SuccessResponse = &CallbackResponse{"ok"}
|
||||
)
|
||||
|
||||
func (e *API) unmarshalData(data []byte, msg interface{}) (callbackResponse *CallbackResponse) {
|
||||
err := utils.UnmarshalUseNumber(data, msg)
|
||||
if err != nil {
|
||||
return &CallbackResponse{
|
||||
Message: fmt.Sprintf(platformapi.ErrStrUnmarshalError, data, err),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *API) CheckCallbackValidation(mapData map[string]interface{}) (callbackResponse *CallbackResponse) {
|
||||
sign := e.signParamsMap(mapData, "")
|
||||
if remoteSign, _ := mapData[signKey].(string); sign != remoteSign {
|
||||
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, remoteSign)
|
||||
return &CallbackResponse{Message: platformapi.ErrStrCallbackSignatureIsWrong}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *API) GetCallbackMsg(data []byte) (msg *CallbackMsg, callbackResponse *CallbackResponse) {
|
||||
msg = new(CallbackMsg)
|
||||
if callbackResponse = e.unmarshalData(data, msg); callbackResponse != nil {
|
||||
return nil, callbackResponse
|
||||
}
|
||||
|
||||
mapData := structs.Map(msg)
|
||||
callbackResponse = e.CheckCallbackValidation(mapData)
|
||||
return msg, callbackResponse
|
||||
}
|
||||
Reference in New Issue
Block a user