- big big refactor.
This commit is contained in:
109
platformapi/mtpsapi/callback.go
Normal file
109
platformapi/mtpsapi/callback.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package mtpsapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
type CallbackResponse struct {
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
type CallbackCommonMsg struct {
|
||||
AppKey string
|
||||
Timestamp int64
|
||||
Sign string
|
||||
}
|
||||
|
||||
type CallbackOrderMsg struct {
|
||||
OrderInfoCommon
|
||||
CallbackCommonMsg
|
||||
Status int
|
||||
CancelReasonId int
|
||||
CancelReason string
|
||||
}
|
||||
|
||||
type CallbackOrderExceptionMsg struct {
|
||||
OrderInfoCommon
|
||||
CallbackCommonMsg
|
||||
ExceptionID int64
|
||||
ExceptionCode int
|
||||
ExceptionDescr string
|
||||
ExceptionTime int64
|
||||
}
|
||||
|
||||
var (
|
||||
SuccessResponse = &CallbackResponse{Code: 0}
|
||||
SignatureIsNotOk = &CallbackResponse{Code: -1}
|
||||
)
|
||||
|
||||
func (a *API) CheckCallbackValidation(request *http.Request) (callbackResponse *CallbackResponse) {
|
||||
request.ParseForm()
|
||||
sign := a.signParams(request.PostForm)
|
||||
if sign != request.FormValue(signKey) {
|
||||
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
|
||||
return SignatureIsNotOk
|
||||
}
|
||||
|
||||
for _, valueKey := range []string{"delivery_id", "mt_peisong_id", "order_id"} {
|
||||
baseapi.SugarLogger.Errorf("Missing mandatory param:%v", valueKey)
|
||||
if request.FormValue(valueKey) == "" {
|
||||
return &CallbackResponse{
|
||||
Code: -1,
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *API) GetOrderCallbackMsg(request *http.Request) (orderMsg *CallbackOrderMsg, callbackResponse *CallbackResponse) {
|
||||
if callbackResponse = a.CheckCallbackValidation(request); callbackResponse != nil {
|
||||
return nil, callbackResponse
|
||||
}
|
||||
orderMsg = &CallbackOrderMsg{
|
||||
OrderInfoCommon: OrderInfoCommon{
|
||||
DeliveryId: utils.Str2Int64(request.FormValue("delivery_id")),
|
||||
MtPeisongId: request.FormValue("mt_peisong_id"),
|
||||
OrderId: request.FormValue("order_id"),
|
||||
CourierName: request.FormValue("courier_name"),
|
||||
CourierPhone: request.FormValue("courier_phone"),
|
||||
},
|
||||
CallbackCommonMsg: CallbackCommonMsg{
|
||||
AppKey: request.FormValue("appkey"),
|
||||
Timestamp: utils.Str2Int64(request.FormValue("timestamp")),
|
||||
Sign: request.FormValue("sign"),
|
||||
},
|
||||
Status: int(utils.Str2Int64(request.FormValue("status"))),
|
||||
CancelReasonId: int(utils.Str2Int64(request.FormValue("cancel_reason_id"))),
|
||||
CancelReason: request.FormValue("cancel_reason"),
|
||||
}
|
||||
return orderMsg, nil
|
||||
}
|
||||
|
||||
func (a *API) GetOrderExceptionCallbackMsg(request *http.Request) (orderMsg *CallbackOrderExceptionMsg, callbackResponse *CallbackResponse) {
|
||||
if callbackResponse = a.CheckCallbackValidation(request); callbackResponse != nil {
|
||||
return nil, callbackResponse
|
||||
}
|
||||
orderMsg = &CallbackOrderExceptionMsg{
|
||||
OrderInfoCommon: OrderInfoCommon{
|
||||
DeliveryId: utils.Str2Int64(request.FormValue("delivery_id")),
|
||||
MtPeisongId: request.FormValue("mt_peisong_id"),
|
||||
OrderId: request.FormValue("order_id"),
|
||||
CourierName: request.FormValue("courier_name"),
|
||||
CourierPhone: request.FormValue("courier_phone"),
|
||||
},
|
||||
CallbackCommonMsg: CallbackCommonMsg{
|
||||
AppKey: request.FormValue("appkey"),
|
||||
Timestamp: utils.Str2Int64(request.FormValue("timestamp")),
|
||||
Sign: request.FormValue("sign"),
|
||||
},
|
||||
ExceptionID: utils.Str2Int64(request.FormValue("exception_id")),
|
||||
ExceptionCode: int(utils.Str2Int64(request.FormValue("exception_code"))),
|
||||
ExceptionDescr: request.FormValue("exception_descr"),
|
||||
ExceptionTime: utils.Str2Int64(request.FormValue("exception_time")),
|
||||
}
|
||||
|
||||
return orderMsg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user