111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
package mtpsapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
type MtpsCallbackResponse struct {
|
|
Code int `json:"code"`
|
|
}
|
|
|
|
type MtpsCallbackCommon struct {
|
|
AppKey string
|
|
Timestamp int64
|
|
Sign string
|
|
}
|
|
|
|
type MtpsCallbackOrderMsg struct {
|
|
MtpsOrderInfoCommon
|
|
MtpsCallbackCommon
|
|
Status int
|
|
CancelReasonId int
|
|
CancelReason string
|
|
}
|
|
|
|
type MtpsCallbackOrderExceptionMsg struct {
|
|
MtpsOrderInfoCommon
|
|
MtpsCallbackCommon
|
|
ExceptionId int64
|
|
ExceptionCode int
|
|
ExceptionDescr string
|
|
ExceptionTime int64
|
|
}
|
|
|
|
var (
|
|
SuccessResponse = &MtpsCallbackResponse{Code: 0}
|
|
SignatureIsNotOk = &MtpsCallbackResponse{Code: -1}
|
|
)
|
|
|
|
func (m *MTPSAPI) CheckRequestValidation(request *http.Request) (callbackResponse *MtpsCallbackResponse) {
|
|
request.ParseForm()
|
|
sign := m.signParams(request.PostForm)
|
|
if sign != request.FormValue(signKey) {
|
|
m.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"} {
|
|
m.sugarLogger.Errorf("Missing mandatory param:%v", valueKey)
|
|
if request.FormValue(valueKey) == "" {
|
|
return &MtpsCallbackResponse{
|
|
Code: -1,
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MTPSAPI) GetOrderCallbackMsg(request *http.Request) (orderMsg *MtpsCallbackOrderMsg, callbackResponse *MtpsCallbackResponse) {
|
|
callbackResponse = m.CheckRequestValidation(request)
|
|
if callbackResponse != nil {
|
|
return nil, callbackResponse
|
|
}
|
|
orderMsg = &MtpsCallbackOrderMsg{
|
|
MtpsOrderInfoCommon: MtpsOrderInfoCommon{
|
|
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"),
|
|
},
|
|
MtpsCallbackCommon: MtpsCallbackCommon{
|
|
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 (m *MTPSAPI) GetOrderExceptionCallbackMsg(request *http.Request) (orderMsg *MtpsCallbackOrderExceptionMsg, callbackResponse *MtpsCallbackResponse) {
|
|
callbackResponse = m.CheckRequestValidation(request)
|
|
if callbackResponse != nil {
|
|
return nil, callbackResponse
|
|
}
|
|
orderMsg = &MtpsCallbackOrderExceptionMsg{
|
|
MtpsOrderInfoCommon: MtpsOrderInfoCommon{
|
|
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"),
|
|
},
|
|
MtpsCallbackCommon: MtpsCallbackCommon{
|
|
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
|
|
}
|