Files
baseapi/platformapi/jdapi/callback.go
2018-06-22 22:26:12 +08:00

122 lines
3.9 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/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
type CallbackResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
type CallbackOrderMsg struct {
ID int `json:"-"` // 用于传递Jdorder的主键值减少一次读库操作
BillID string `json:"billId"`
StatusID string `json:"statusId"`
Timestamp string `json:"timestamp"`
Remark string `json:"remark"`
}
type CallbackDeliveryStatusMsg struct {
OrderID string `json:"orderId"`
DeliveryStatusTime string `json:"deliveryStatusTime"`
DeliveryManNo string `json:"deliveryManNo"`
DeliveryManName string `json:"deliveryManName"`
DeliveryManPhone string `json:"deliveryManPhone"`
DeliveryCarrierNo string `json:"deliveryCarrierNo"`
DeliveryCarrierName string `json:"deliveryCarrierName"`
DeliveryStatus int `json:"deliveryStatus"`
Remark string `json:"remark"`
FailType string `json:"failType"`
CreatePin string `json:"createPin"`
OpTime int64 `json:"opTime"`
InputTime string `json:"inputTime"`
}
var (
SuccessResponse = &CallbackResponse{Code: "0", Msg: "success", Data: ""}
)
func (a *API) unmarshalData(strData string, msg interface{}) (callbackResponse *CallbackResponse) {
err := utils.UnmarshalUseNumber([]byte(strData), msg)
if err != nil {
return &CallbackResponse{
Code: ResponseCodeAbnormalParam,
Msg: fmt.Sprintf(platformapi.ErrStrUnmarshalError, strData, err),
Data: strData,
}
}
return nil
}
func (a *API) CheckCallbackValidation(request *http.Request) (callbackResponse *CallbackResponse) {
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"] = a.appSecret
mapData["v"] = request.FormValue("v")
mapData[paramJson] = request.FormValue(paramJson)
sign := a.signParams(mapData)
if sign != request.FormValue(signKey) {
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
return &CallbackResponse{
Code: ResponseCodeInvalidSign,
Msg: platformapi.ErrStrCallbackSignatureIsWrong,
Data: string(utils.MustMarshal(mapData)),
}
}
return nil
}
func (a *API) getCommonOrderCallbackMsg(request *http.Request, msg interface{}, needDecode bool) (callbackResponse *CallbackResponse) {
if callbackResponse = a.CheckCallbackValidation(request); callbackResponse != nil {
return callbackResponse
}
jdParamJSON := request.FormValue(paramJson)
if needDecode {
if jdParamJSON2, err := url.QueryUnescape(jdParamJSON); err == nil {
jdParamJSON = jdParamJSON2
} else {
return &CallbackResponse{
Code: ResponseCodeAbnormalParam,
Msg: fmt.Sprintf(platformapi.ErrStrUnescapeError, jdParamJSON, err),
Data: jdParamJSON,
}
}
}
if callbackResponse = a.unmarshalData(jdParamJSON, msg); callbackResponse != nil {
return callbackResponse
}
return nil
}
func (a *API) GetOrderCallbackMsg(request *http.Request) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackOrderMsg)
callbackResponse = a.getCommonOrderCallbackMsg(request, msg, false)
return msg, callbackResponse
}
func (a *API) GetOrderApplyCancelCallbackMsg(request *http.Request) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackOrderMsg)
callbackResponse = a.getCommonOrderCallbackMsg(request, msg, true)
return msg, callbackResponse
}
func (a *API) GetOrderDeliveryCallbackMsg(request *http.Request) (msg *CallbackDeliveryStatusMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackDeliveryStatusMsg)
callbackResponse = a.getCommonOrderCallbackMsg(request, msg, true)
return msg, callbackResponse
}