- add callback sign check.
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package mtpsapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
type MtpsCallbackResponse struct {
|
||||
Code string `json:"code"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
type MtpsCallbackCommon struct {
|
||||
@@ -35,14 +34,21 @@ type MtpsCallbackOrderExceptionMsg struct {
|
||||
}
|
||||
|
||||
var (
|
||||
SuccessResponse = &MtpsCallbackResponse{Code: "0"}
|
||||
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) {
|
||||
return SignatureIsNotOk
|
||||
}
|
||||
|
||||
for _, valueKey := range []string{"delivery_id", "mt_peisong_id", "order_id"} {
|
||||
if request.FormValue(valueKey) == "" {
|
||||
return &MtpsCallbackResponse{
|
||||
Code: fmt.Sprintf("missing param:%s", valueKey),
|
||||
Code: -1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,13 +24,56 @@ const (
|
||||
|
||||
const (
|
||||
mtpsAPIURL = "https://peisongopen.meituan.com/api"
|
||||
signKey = "sign"
|
||||
)
|
||||
|
||||
const (
|
||||
mtpsStatusSuccess = 0
|
||||
mtpsStatusSystemError = 1
|
||||
mtpsStatusMissingSystemParams = 2
|
||||
mtpsStatusMissingBusinessParams = 3
|
||||
OrderStatusWaitingForSchedule = 0
|
||||
OrderStatusAccepted = 20
|
||||
OrderStatusPickedUp = 30
|
||||
OrderStatusDeliverred = 50
|
||||
OrderStatusCanceled = 99
|
||||
)
|
||||
|
||||
const (
|
||||
DSCRapid = 4011
|
||||
DSCIntime = 4012
|
||||
DSCTogether = 4013
|
||||
)
|
||||
|
||||
const (
|
||||
PickupTypeClientSendToStation = 1
|
||||
PickupTypeMtPick = 2
|
||||
)
|
||||
|
||||
const (
|
||||
OrderTypeASAP = 0
|
||||
OrderTypeBook = 1
|
||||
)
|
||||
|
||||
const (
|
||||
CoordinateTypeMars = 0
|
||||
CoordinateTypeBaidu = 1
|
||||
)
|
||||
|
||||
// 错误码
|
||||
const (
|
||||
ResponseCodeSuccess = 0
|
||||
)
|
||||
|
||||
// 取消原因
|
||||
const (
|
||||
CancelReasonClientActive = 101
|
||||
CancelReasonClientChangeTimeOrAddress = 102
|
||||
CancelReasonGoodRelated = 103
|
||||
CancelReasonMerchantOther = 199
|
||||
|
||||
CancelReasonMtpsAttitude = 201
|
||||
CancelReasonRidderSendNotIntime = 202
|
||||
CancelReasonRideerGetGoodNotIntime = 203
|
||||
CancelReasonRideerMtpsOther = 299
|
||||
|
||||
CancelReasonRideerOther = 399
|
||||
)
|
||||
|
||||
type MtpsOrderInfoCommon struct {
|
||||
@@ -61,27 +104,6 @@ type MTPSResult struct {
|
||||
Data map[string]interface{} `json:"data"`
|
||||
}
|
||||
|
||||
const (
|
||||
DSCRapid = 4011
|
||||
DSCIntime = 4012
|
||||
DSCTogether = 403
|
||||
)
|
||||
|
||||
const (
|
||||
PickupTypeClientSendToStation = 1
|
||||
PickupTypeMtPick = 2
|
||||
)
|
||||
|
||||
const (
|
||||
OrderTypeASAP = 0
|
||||
OrderTypeBook = 1
|
||||
)
|
||||
|
||||
const (
|
||||
CoordinateTypeMars = 0
|
||||
CoordinateTypeBaidu = 1
|
||||
)
|
||||
|
||||
type MtpsCreateOrderByShopInfo struct {
|
||||
DeliveryId int64
|
||||
OrderId string
|
||||
@@ -118,17 +140,23 @@ func NewMTPSAPI(appKey, secret string, sugarLogger *zap.SugaredLogger) *MTPSAPI
|
||||
}
|
||||
|
||||
func (m *MTPSAPI) signParams(params url.Values) string {
|
||||
keyValues := make([]string, 0)
|
||||
for k, v := range params {
|
||||
valStr := strings.Join(v, "")
|
||||
if valStr != "" {
|
||||
keyValues = append(keyValues, k+valStr)
|
||||
keys := make([]string, 0)
|
||||
for k := range params {
|
||||
if k != signKey {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(keyValues)
|
||||
finalStr := m.secret + strings.Join(keyValues, "")
|
||||
// e.sugarLogger.Debugf("sign str:%v", finalStr)
|
||||
sort.Strings(keys)
|
||||
finalStr := m.secret
|
||||
for _, key := range keys {
|
||||
valStr := strings.Join(params[key], "")
|
||||
if valStr != "" {
|
||||
finalStr += key + valStr
|
||||
}
|
||||
}
|
||||
|
||||
// m.sugarLogger.Debug(finalStr)
|
||||
return fmt.Sprintf("%x", sha1.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
@@ -144,8 +172,8 @@ func (m *MTPSAPI) AccessMTPS(action string, params map[string]interface{}) (retV
|
||||
params2["appkey"] = []string{m.appKey}
|
||||
params2["timestamp"] = []string{utils.Int64ToStr(utils.GetCurTimestamp())}
|
||||
params2["version"] = []string{"1.0"}
|
||||
params2["sign"] = []string{m.signParams(params2)}
|
||||
|
||||
params2[signKey] = []string{m.signParams(params2)}
|
||||
// m.sugarLogger.Debug(params2.Encode())
|
||||
request, _ := http.NewRequest("POST", mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
apiAccess := &common.AccessPlatformAPIWithRetryParams{
|
||||
@@ -157,30 +185,38 @@ func (m *MTPSAPI) AccessMTPS(action string, params map[string]interface{}) (retV
|
||||
SugarLogger: m.sugarLogger,
|
||||
}
|
||||
|
||||
err = common.AccessPlatformAPIWithRetry(apiAccess, func(response *http.Response) (result int, err error) {
|
||||
err = common.AccessPlatformAPIWithRetry(apiAccess, func(response *http.Response) (result string, err error) {
|
||||
jsonResult1, err := utils.HttpResponse2Json(response)
|
||||
if err != nil {
|
||||
m.sugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
return 0, err
|
||||
return common.PAErrorLevelGeneralFail, err
|
||||
}
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
|
||||
retVal = &MTPSResult{
|
||||
Code: code,
|
||||
}
|
||||
m.sugarLogger.Debug(jsonResult1)
|
||||
if code == mtpsStatusSuccess {
|
||||
if code == ResponseCodeSuccess {
|
||||
if innerData, ok := jsonResult1["data"]; ok {
|
||||
retVal.Data, _ = innerData.(map[string]interface{})
|
||||
}
|
||||
return common.PAErrorLevelSuccess, nil
|
||||
}
|
||||
retVal.Message = jsonResult1["message"].(string)
|
||||
return common.PAErrorLevelFailed, nil
|
||||
return common.PAErrorLevelGeneralFail, utils.NewErrorIntCode(retVal.Message, code)
|
||||
})
|
||||
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (m *MTPSAPI) result2OrderResponse(result *MTPSResult) (order *MtpsOrderResponse) {
|
||||
order = new(MtpsOrderResponse)
|
||||
order.MtPeisongId = result.Data["mt_peisong_id"].(string)
|
||||
order.DeliveryId = utils.MustInterface2Int64(result.Data["delivery_id"])
|
||||
order.OrderId = result.Data["order_id"].(string)
|
||||
return order
|
||||
}
|
||||
|
||||
func (m *MTPSAPI) CreateOrderByShop(basicParams *MtpsCreateOrderByShopInfo, addParams map[string]interface{}) (order *MtpsOrderResponse, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["delivery_id"] = utils.Int64ToStr(basicParams.DeliveryId)
|
||||
@@ -207,13 +243,9 @@ func (m *MTPSAPI) CreateOrderByShop(basicParams *MtpsCreateOrderByShopInfo, addP
|
||||
}
|
||||
if result, err := m.AccessMTPS("order/createByShop", params); err != nil {
|
||||
m.sugarLogger.Debugf("result:%v", result)
|
||||
return nil, err
|
||||
return nil, utils.NewErrorIntCode(err.Error(), result.Code)
|
||||
} else {
|
||||
order = new(MtpsOrderResponse)
|
||||
order.MtPeisongId = result.Data["mt_peisong_id"].(string)
|
||||
order.DeliveryId = utils.MustInterface2Int64(result.Data["delivery_id"])
|
||||
order.OrderId = result.Data["order_id"].(string)
|
||||
return order, nil
|
||||
return m.result2OrderResponse(result), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,6 +261,22 @@ func (m *MTPSAPI) QueryOrderStatus(deliveryId int64, mtPeiSongId string) (retVal
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MTPSAPI) CancelOrder(deliveryId int64, mtPeiSongId string, cancelReasonId int, cancelReason string) (result *MtpsOrderResponse, err error) {
|
||||
params := map[string]interface{}{
|
||||
"delivery_id": deliveryId,
|
||||
"mt_peisong_id": mtPeiSongId,
|
||||
"cancel_reason_id": cancelReasonId,
|
||||
"cancel_reason": cancelReason,
|
||||
}
|
||||
if result, err := m.AccessMTPS("order/delete", params); err != nil {
|
||||
m.sugarLogger.Debugf("result:%v", result)
|
||||
return nil, err
|
||||
} else {
|
||||
return m.result2OrderResponse(result), nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *MTPSAPI) simulateOrderBehavior(action string, deliveryId int64, mtPeiSongId string) (err error) {
|
||||
params := map[string]interface{}{
|
||||
"delivery_id": deliveryId,
|
||||
|
||||
@@ -19,6 +19,12 @@ func init() {
|
||||
// mtpsapi = NewMTPSAPI("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE", sugarLogger)
|
||||
}
|
||||
|
||||
func handleError(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
sugarLogger.Debug(err)
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
func TestTest(t *testing.T) {
|
||||
sugarLogger.Debug(utils.GetCurTimeStr())
|
||||
}
|
||||
@@ -59,11 +65,39 @@ func TestCreateOrderByShop(t *testing.T) {
|
||||
}
|
||||
|
||||
order, err := mtpsapi.CreateOrderByShop(basicParams, nil)
|
||||
handleError(t, err)
|
||||
if order != nil {
|
||||
sugarLogger.Debugf("order:%v", order)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
sugarLogger.Debugf("err:%s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimulateArrange(t *testing.T) {
|
||||
err := mtpsapi.SimulateArrange(123456789, "1529387562097059")
|
||||
handleError(t, err)
|
||||
}
|
||||
|
||||
func TestSimulatePickup(t *testing.T) {
|
||||
err := mtpsapi.SimulatePickup(123456789, "1529387562097059")
|
||||
handleError(t, err)
|
||||
}
|
||||
|
||||
func TestSimulateRearrange(t *testing.T) {
|
||||
err := mtpsapi.SimulateRearrange(123456789, "1529387562097059")
|
||||
handleError(t, err)
|
||||
}
|
||||
|
||||
func TestSimulateDeliver(t *testing.T) {
|
||||
err := mtpsapi.SimulateDeliver(123456789, "1529387562097059")
|
||||
handleError(t, err)
|
||||
}
|
||||
|
||||
func TestSimulateReportException(t *testing.T) {
|
||||
err := mtpsapi.SimulateReportException(123456789, "1529387562097059")
|
||||
handleError(t, err)
|
||||
}
|
||||
|
||||
func TestCancelOrder(t *testing.T) {
|
||||
result, err := mtpsapi.CancelOrder(123456789, "1529387562097059", CancelReasonMerchantOther, "just a test")
|
||||
handleError(t, err)
|
||||
sugarLogger.Debug(result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user