From 966aba1dc45e79b63203c5d84e7fd8ca0f922670 Mon Sep 17 00:00:00 2001 From: wolter Date: Fri, 9 Aug 2024 15:23:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4api=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controllers/api.go | 52 ++++++++++++++----- app/http/controllers/base.go | 9 ++++ app/http/controllers/front/api.go | 38 ++++++-------- app/http/entities/backend/order.go | 15 +++--- app/http/entities/front/pay.go | 6 +++ .../orderrequestlogmodel/order_request_log.go | 2 + app/models/ordersmodel/orders.go | 2 +- app/services/order.go | 4 +- app/services/thirdpay/api/api_res_data.go | 2 +- app/services/thirdpay/do/pay.go | 2 +- app/services/thirdpay/do/pay_check.go | 2 +- app/services/thirdpay/pay.go | 4 +- .../thirdpay/thirdpay_notify/notify.go | 4 +- 13 files changed, 89 insertions(+), 53 deletions(-) diff --git a/app/http/controllers/api.go b/app/http/controllers/api.go index ec1e2eb..bf0f1d7 100644 --- a/app/http/controllers/api.go +++ b/app/http/controllers/api.go @@ -3,45 +3,69 @@ package controllers import ( "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/http/entities/front" "PaymentCenter/app/models/orderrequestlogmodel" "PaymentCenter/app/services" + "PaymentCenter/app/services/thirdpay/api" "PaymentCenter/app/utils" "PaymentCenter/config" "encoding/json" + "fmt" "github.com/gin-gonic/gin" "net/http" ) func ApiRes(c *gin.Context, data interface{}, code int, msg ...string) { - var log_id int64 - message := "" + var logId int64 + var responseData interface{} + var message string + originData := "{}" + appCheckInfo := GetAppCheckInfo(c).(*services.AppCheck) + // 空数据 if utils.IsNil(data) { data = struct{}{} } + // 获取错误信息 if len(msg) > 0 { message = msg[0] } else { message = errorcode.GetMsg(code, "") } + // 获取日志ID log, exists := GetApiLogId(c) if exists { - log_id = log.(int64) - dataByte, _ := json.Marshal(data) - status := common.STATUS_ENABLE - if code == errorcode.Success { - status = common.STATUS_DISABLED + logId = log.(int64) + } + // 加密数据 + if code == errorcode.Success { + if apiRsp, ok := data.(front.ApiResponse); ok { + apiRsp.Order, code = api.EnCrypt(appCheckInfo.App, apiRsp.Order) + if code != errorcode.Success { + message = errorcode.GetMsg(code, "") + } + responseData = apiRsp + } else { + responseData = data } - services.RequestLogUpdate(&orderrequestlogmodel.OrderRequestLog{ - Id: log_id, - MerchantResponse: string(dataByte), - Status: status, - }) } if code == errorcode.Success { - ApiSuccess(c, data, log_id, message) + ApiSuccess(c, responseData, logId, message) + b, _ := json.Marshal(data) + originData = string(b) } else { - ApiError(c, code, log_id, message) + ApiError(c, code, logId, message) + originData = fmt.Sprintf("{\"code\":%d,\"message\":\"%s\"}", code, message) + } + // 记录日志 + if logId > 0 { + services.RequestLogUpdate(&orderrequestlogmodel.OrderRequestLog{ + Id: logId, + AppId: appCheckInfo.AppId, + OutTradeNo: GetOutTradeNo(c), + MerchantResponse: originData, + Status: common.STATUS_DISABLED, + }) } } diff --git a/app/http/controllers/base.go b/app/http/controllers/base.go index 40331da..a694571 100644 --- a/app/http/controllers/base.go +++ b/app/http/controllers/base.go @@ -203,6 +203,15 @@ func GetAppCheckInfo(c *gin.Context) interface{} { return request } +// 获取OutTradeNo +func GetOutTradeNo(c *gin.Context) string { + outTradeNo, _ := c.Get("OutTradeNo") + if outTradeNo == nil { + return "" + } + return outTradeNo.(string) +} + func GetApiLogId(c *gin.Context) (interface{}, bool) { request, exists := c.Get("log") return request, exists diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index aa9d660..4ffffc9 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -7,14 +7,15 @@ import ( "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/services" "PaymentCenter/app/services/thirdpay" - "PaymentCenter/app/services/thirdpay/api" "github.com/gin-gonic/gin" "xorm.io/builder" ) func PayUrl(c *gin.Context) { - var res front.PayUrlResp + var res front.ApiResponse req := controllers.GetRequest(c).(*front.PayReqs) + c.Set("OutTradeNo", req.OutTradeNo) + appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) check := thirdpay.ThirdPayInfoCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP()) if check.CheckCode != errorcode.Success { @@ -31,14 +32,8 @@ func PayUrl(c *gin.Context) { controllers.ApiRes(c, nil, pay.PayCode) return } - data := thirdpay.NewOrdersResp(pay.Order) - encryptData, errCode := api.EnCrypt(appCheckInfo.App, data) - if errCode != errorcode.Success { - controllers.ApiRes(c, nil, pay.PayCode) - return - } - res.Order = string(encryptData) + res.Order = thirdpay.NewOrdersResp(pay.Order) res.Url = pay.Url controllers.ApiRes(c, res, pay.PayCode) return @@ -46,18 +41,17 @@ func PayUrl(c *gin.Context) { func Refund(c *gin.Context) { req := controllers.GetRequest(c).(*front.RefundReqs) + c.Set("OutTradeNo", req.OutTradeNo) + appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) refund := thirdpay.ThirdPayRefund(c.Request.Context(), req, appCheckInfo, c.ClientIP()) if refund.PayCode != errorcode.Success { controllers.ApiRes(c, nil, refund.PayCode) } data := thirdpay.NewOrdersResp(refund.Order) - encryptData, errCode := api.EnCrypt(appCheckInfo.App, data) - if errCode != errorcode.Success { - controllers.ApiRes(c, nil, refund.PayCode) - return - } - controllers.ApiRes(c, encryptData, refund.PayCode) + var res front.ApiResponse + res.Order = data + controllers.ApiRes(c, res, refund.PayCode) return } @@ -65,26 +59,24 @@ func Refund(c *gin.Context) { // 查询订单 func QueryOrder(c *gin.Context) { req := controllers.GetRequest(c).(*front.QueryReqs) + c.Set("OutTradeNo", req.OutTradeNo) appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) // 查询订单表 order := ordersmodel.Orders{ - OutTreadNo: req.OutTradeNo, + OutTradeNo: req.OutTradeNo, AppId: req.AppId, MerchantId: appCheckInfo.App.MerchantId, } cond := builder.NewCond() - cond = cond.And(builder.Eq{"out_tread_no": order.OutTreadNo}, builder.Eq{"app_id": order.AppId}) + cond = cond.And(builder.Eq{"out_trade_no": order.OutTradeNo}, builder.Eq{"app_id": order.AppId}) _, code := services.OrderFindOne(&order, cond) if code != errorcode.OrdersExist { controllers.ApiRes(c, nil, code) return } data := thirdpay.NewOrdersResp(&order) - encryptData, errCode := api.EnCrypt(appCheckInfo.App, data) - if errCode != errorcode.Success { - controllers.ApiRes(c, nil, errCode) - return - } - controllers.ApiRes(c, encryptData, errorcode.Success) + var res front.ApiResponse + res.Order = data + controllers.ApiRes(c, res, errorcode.Success) } diff --git a/app/http/entities/backend/order.go b/app/http/entities/backend/order.go index f898bf2..3b030d6 100644 --- a/app/http/entities/backend/order.go +++ b/app/http/entities/backend/order.go @@ -16,7 +16,7 @@ type OrderListRequest struct { MerchantId int64 `json:"merchant_id" form:"merchant_id"` PayChannelId int64 `json:"pay_channel_id" form:"pay_channel_id"` AppId int64 `json:"app_id" form:"app_id"` - OutTreadNo string `json:"out_tread_no" form:"out_tread_no"` + OutTradeNo string `json:"out_trade_no" form:"out_trade_no"` Status int `json:"status" form:"status"` OrderType int `json:"order_type" form:"order_type"` StartTime string `json:"start_time" form:"start_time"` @@ -28,7 +28,7 @@ type OrderList struct { MerchantId int64 `json:"merchant_id"` PayChannelId int64 `json:"pay_channel_id"` AppId int64 `json:"app_id"` - OutTreadNo string `json:"out_tread_no"` + OutTradeNo string `json:"out_trade_no"` Status int `json:"status"` OrderType int `json:"order_type"` StartTime time.Time `json:"start_time"` @@ -56,7 +56,7 @@ func (o *OrderListRequest) ValidateRequest() (r OrderList, err error) { r.MerchantId = o.MerchantId r.PayChannelId = o.PayChannelId r.AppId = o.AppId - r.OutTreadNo = o.OutTreadNo + r.OutTradeNo = o.OutTradeNo r.Status = o.Status r.OrderType = o.OrderType r.PageRequest = o.PageRequest @@ -69,7 +69,7 @@ type OrdersResponse struct { MerchantId int64 `json:"merchant_id"` PayChannelId int64 `json:"pay_channel_id"` AppId int64 `json:"app_id"` - OutTreadNo string `json:"out_tread_no"` + OutTradeNo string `json:"out_trade_no"` Status int `json:"status"` OrderType int `json:"order_type"` Amount int `json:"amount"` @@ -87,7 +87,7 @@ func (o *OrdersResponse) ResponseFromDb(db ordersmodel.OrdersBackendList) { o.MerchantId = db.MerchantId o.PayChannelId = db.PayChannelId o.AppId = db.AppId - o.OutTreadNo = db.OutTreadNo + o.OutTradeNo = db.OutTradeNo o.Status = db.Status o.OrderType = db.OrderType o.Amount = db.Amount @@ -107,6 +107,8 @@ type OrderLogsListRequest struct { type OrderRequestLogResponse struct { Id int64 `json:"id"` + AppId int64 `json:"app_id"` + OutTradeNo string `json:"out_trade_no"` IpAddress string `json:"ip_address"` Url string `json:"url"` MerchantRequest string `json:"merchant_request"` @@ -117,7 +119,8 @@ type OrderRequestLogResponse struct { func (o *OrderRequestLogResponse) ResponseFromDb(db orderrequestlogmodel.OrderRequestLog) { o.Id = db.Id - + o.AppId = db.AppId + o.OutTradeNo = db.OutTradeNo o.IpAddress = db.IpAddress o.Url = db.URL o.Status = db.Status diff --git a/app/http/entities/front/pay.go b/app/http/entities/front/pay.go index 5e0ce7a..edde6be 100644 --- a/app/http/entities/front/pay.go +++ b/app/http/entities/front/pay.go @@ -40,3 +40,9 @@ type QueryReqs struct { ApiCommonBody OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"` } + +// api 接口返回数据, 统一返回结构, order数据会进行加密 +type ApiResponse struct { + Order interface{} `json:"order"` + Url string `json:"url,omitempty"` +} diff --git a/app/models/orderrequestlogmodel/order_request_log.go b/app/models/orderrequestlogmodel/order_request_log.go index 5254657..4486d02 100644 --- a/app/models/orderrequestlogmodel/order_request_log.go +++ b/app/models/orderrequestlogmodel/order_request_log.go @@ -14,6 +14,8 @@ var ( // 实体 type OrderRequestLog struct { Id int64 + AppId int64 `xorm:"'app_id' bigint(20)"` + OutTradeNo string `xorm:"'out_trade_no' varchar(50)"` IpAddress string `xorm:"'ip_address' varchar(16)"` URL string `xorm:"'url' varchar(100)"` MerchantRequest string `xorm:"'merchant_request' JSON"` diff --git a/app/models/ordersmodel/orders.go b/app/models/ordersmodel/orders.go index e75a1a7..3b956e7 100644 --- a/app/models/ordersmodel/orders.go +++ b/app/models/ordersmodel/orders.go @@ -17,7 +17,7 @@ type Orders struct { MerchantId int64 `xorm:"'merchant_id' bigint(20)"` PayChannelId int64 `xorm:"'pay_channel_id' bigint(20)"` AppId int64 `xorm:"'app_id' bigint(20)"` - OutTreadNo string `xorm:"'out_tread_no' varchar(50)"` + OutTradeNo string `xorm:"'out_trade_no' varchar(50)"` OrderType int `xorm:"'order_type' TINYINT"` RefundOrderId int64 `xorm:"'refund_order_id' bigint(20)"` Amount int `xorm:"'amount' int(11)"` diff --git a/app/services/order.go b/app/services/order.go index 90da97c..cf42fbb 100644 --- a/app/services/order.go +++ b/app/services/order.go @@ -29,8 +29,8 @@ func OrderList(req backend.OrderList) (result []ordersmodel.OrdersBackendList, t if req.AppId > 0 { conn = conn.And(builder.Eq{"orders.app_id": req.AppId}) } - if req.OutTreadNo != "" { - conn = conn.And(builder.Like{"orders.out_tread_no", req.OutTreadNo}) + if req.OutTradeNo != "" { + conn = conn.And(builder.Like{"orders.out_trade_no", req.OutTradeNo}) } if req.Status > 0 { conn = conn.And(builder.Eq{"orders.status": req.Status}) diff --git a/app/services/thirdpay/api/api_res_data.go b/app/services/thirdpay/api/api_res_data.go index d592269..0c39b38 100644 --- a/app/services/thirdpay/api/api_res_data.go +++ b/app/services/thirdpay/api/api_res_data.go @@ -3,7 +3,7 @@ package api type OrdersResp struct { OrderNo int64 `json:"order_no"` OrderType int `json:"order_type"` - OutTreadNo string `json:"out_tread_no"` + OutTradeNo string `json:"out_trade_no"` Amount int `json:"amount"` Desc string `json:"desc"` diff --git a/app/services/thirdpay/do/pay.go b/app/services/thirdpay/do/pay.go index 1942d65..40a88d0 100644 --- a/app/services/thirdpay/do/pay.go +++ b/app/services/thirdpay/do/pay.go @@ -60,7 +60,7 @@ func (w *Pay) CreateOrder(order_type int) { MerchantId: w.PayParam.Merchant.Id, PayChannelId: w.PayParam.Channel.Id, AppId: w.PayParam.App_id, - OutTreadNo: w.PayParam.OutTradeNo, + OutTradeNo: w.PayParam.OutTradeNo, OrderType: order_type, Amount: w.PayParam.Amount, ExtJson: w.PayParam.ExtJson, diff --git a/app/services/thirdpay/do/pay_check.go b/app/services/thirdpay/do/pay_check.go index 35a31d5..3490dcb 100644 --- a/app/services/thirdpay/do/pay_check.go +++ b/app/services/thirdpay/do/pay_check.go @@ -98,7 +98,7 @@ func (w *PayCheck) CheckOrderRefund() { func (w *PayCheck) GetOrder() { cond := builder.NewCond() - cond = cond.And(builder.Eq{"out_tread_no": w.Reqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId}) + cond = cond.And(builder.Eq{"out_trade_no": w.Reqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId}) order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond) if code == errorcode.SystemError { w.CheckCode = code diff --git a/app/services/thirdpay/pay.go b/app/services/thirdpay/pay.go index 824e6d1..c28c442 100644 --- a/app/services/thirdpay/pay.go +++ b/app/services/thirdpay/pay.go @@ -23,7 +23,7 @@ func ThirdPayUrl(check *thirdpay.PayCheck) *thirdpay.Pay { func NewOrdersResp(db *ordersmodel.Orders) *api.OrdersResp { return &api.OrdersResp{ OrderNo: db.Id, - OutTreadNo: db.OutTreadNo, + OutTradeNo: db.OutTradeNo, Status: db.Status, OrderType: db.OrderType, Amount: db.Amount, @@ -67,7 +67,7 @@ func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck * func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { pay = thirdpay.NewPayWithPayCheck(check) // 创建订单 - if &check.OldOrder != nil { + if check.OldOrder != nil { pay.Order = check.OldOrder } else { pay.CreateOrder(common.ORDER_TYPE_PAY) diff --git a/app/services/thirdpay/thirdpay_notify/notify.go b/app/services/thirdpay/thirdpay_notify/notify.go index f90677a..5a69010 100644 --- a/app/services/thirdpay/thirdpay_notify/notify.go +++ b/app/services/thirdpay/thirdpay_notify/notify.go @@ -38,7 +38,7 @@ type OrderNotifyResp struct { type OrderNotifySendContent struct { OrderId int64 `json:"order_id"` - OutTreadNo string `json:"out_tread_no"` + OutTradeNo string `json:"out_trade_no"` CompleteTime time.Time `json:"complete_time"` OrderType int `json:"order-type"` Status int `json:"status"` @@ -133,7 +133,7 @@ func (o *OrderNotify) sendNotify(body *OrderNotifySendContent) { func (o *OrderNotify) setBody() *OrderNotifySendContent { return &OrderNotifySendContent{ OrderId: o.OrderId, - OutTreadNo: o.order.OutTreadNo, + OutTradeNo: o.order.OutTradeNo, CompleteTime: o.CompleteTime, Status: o.order.Status, OrderType: o.order.OrderType,