Files
baseapi/platform/jdapi/callback.go

122 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package jdapi
import (
"fmt"
"net/http"
"net/url"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platform/common"
"git.rosy.net.cn/baseapi/utils"
)
type JDCallbackResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
type JDOrderMsg struct {
Id int `json:"-"` // 用于传递Jdorder的主键值减少一次读库操作
BillId string `json:"billId"`
StatusId string `json:"statusId"`
Timestamp string `json:"timestamp"`
Remark string `json:"remark"`
}
type JDDeliveryStatusMsg struct {
OrderId string
DeliveryStatusTime string
DeliveryManNo string
DeliveryManName string
DeliveryManPhone string
DeliveryCarrierNo string
DeliveryCarrierName string
DeliveryStatus int
Remark string
FailType string
CreatePin string
OpTime int64
InputTime string
}
var (
SuccessResponse = &JDCallbackResponse{Code: "0", Msg: "success", Data: ""}
)
func (j *JDAPI) unmarshalData(strData string, msg interface{}) (callbackResponse *JDCallbackResponse) {
err := utils.UnmarshalUseNumber([]byte(strData), msg)
if err != nil {
return &JDCallbackResponse{
Code: JDerrorCodeAbnormalParam,
Msg: fmt.Sprintf(common.CBErrMsgUnmarshal, strData, err),
Data: strData,
}
}
return nil
}
func (j *JDAPI) CheckCallbackValidation(request *http.Request) (callbackResponse *JDCallbackResponse) {
mapData := make(map[string]interface{})
mapData["token"] = request.FormValue("token")
mapData["app_key"] = request.FormValue("app_key")
mapData["timestamp"] = request.FormValue("timestamp")
mapData["format"] = request.FormValue("format")
mapData["app_secret"] = j.appSecret
mapData["v"] = request.FormValue("v")
mapData[JD_PARAM_JSON] = request.FormValue(JD_PARAM_JSON)
sign := j.signParams(mapData)
if sign != request.FormValue(signKey) {
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
return &JDCallbackResponse{
Code: JDerrorCodeInvalidSign,
Msg: "signature is invalid",
Data: string(utils.MustMarshal(mapData)),
}
}
return nil
}
func (j *JDAPI) getCommonOrderCallbackMsg(request *http.Request, msg interface{}, needDecode bool) (callbackResponse *JDCallbackResponse) {
if callbackResponse = j.CheckCallbackValidation(request); callbackResponse != nil {
return callbackResponse
}
jdParamJSON := request.FormValue(JD_PARAM_JSON)
if needDecode {
if jdParamJSON2, err := url.QueryUnescape(jdParamJSON); err == nil {
jdParamJSON = jdParamJSON2
} else {
return &JDCallbackResponse{
Code: JDerrorCodeAbnormalParam,
Msg: fmt.Sprintf(common.CBErrMsgUnescape, jdParamJSON, err),
Data: jdParamJSON,
}
}
}
if callbackResponse = j.unmarshalData(jdParamJSON, msg); callbackResponse != nil {
return callbackResponse
}
return nil
}
func (j *JDAPI) GetOrderCallbackMsg(request *http.Request) (msg *JDOrderMsg, callbackResponse *JDCallbackResponse) {
msg = new(JDOrderMsg)
callbackResponse = j.getCommonOrderCallbackMsg(request, msg, false)
return msg, callbackResponse
}
func (j *JDAPI) GetOrderApplyCancelCallbackMsg(request *http.Request) (msg *JDOrderMsg, callbackResponse *JDCallbackResponse) {
msg = new(JDOrderMsg)
callbackResponse = j.getCommonOrderCallbackMsg(request, msg, true)
return msg, callbackResponse
}
func (j *JDAPI) GetOrderDeliveryCallbackMsg(request *http.Request) (msg *JDDeliveryStatusMsg, callbackResponse *JDCallbackResponse) {
msg = new(JDDeliveryStatusMsg)
callbackResponse = j.getCommonOrderCallbackMsg(request, msg, true)
return msg, callbackResponse
}