From 583aee88e285acb764779996091c299847eee97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=AE=8F?= <389838709@qq.com> Date: Fri, 9 Aug 2024 11:41:08 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=AF=E4=BB=98=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/third/paymentService/ali_service.go | 52 ++++---------- app/third/paymentService/wechat_service.go | 83 ++++++++-------------- 2 files changed, 41 insertions(+), 94 deletions(-) diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index 48a74ad..76faa56 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -14,25 +14,16 @@ import ( "github.com/go-pay/gopay/alipay" "github.com/qit-team/snow-core/log/logger" "strconv" - "sync" -) - -var ( - aliClient *alipay.Client - aliClientErr error - aliOnce sync.Once ) // AliInitClient 使用提供的支付请求参数初始化支付宝客户端 -func AliInitClient(aliConfig AliPay) { +func AliInitClient(aliConfig AliPay) (*alipay.Client, error) { envConfig := config.GetConf() - aliOnce.Do(func() { - // 初始化支付宝客户端 - // appid:应用ID - // privateKey:应用私钥,支持PKCS1和PKCS8 - // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 - aliClient, aliClientErr = alipay.NewClient(aliConfig.AppId, aliConfig.PrivateKey, envConfig.PayService.IsProd) - }) + // 初始化支付宝客户端 + // appid:应用ID + // privateKey:应用私钥,支持PKCS1和PKCS8 + // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 + aliClient, aliClientErr := alipay.NewClient(aliConfig.AppId, aliConfig.PrivateKey, envConfig.PayService.IsProd) // 自定义配置http请求接收返回结果body大小,默认 10MB aliClient.SetBodySize(10) // 没有特殊需求,可忽略此配置 @@ -51,10 +42,6 @@ func AliInitClient(aliConfig AliPay) { // 证书内容 aliClientErr = aliClient.SetCertSnByContent([]byte(aliConfig.AppPublicCert), []byte(aliConfig.AlipayRootCert), []byte(aliConfig.AlipayPublicCert)) -} - -// GetAliClient 获取已经初始化的支付宝客户端 -func GetAliClient() (*alipay.Client, error) { if aliClient == nil { return nil, errors.New("client not initialized") } @@ -67,10 +54,7 @@ func GetAliClient() (*alipay.Client, error) { // ALiH5PayInfo 支付宝手机网站支付 func ALiH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, error) { // 初始化支付宝客户端 - AliInitClient(payOrderRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(payOrderRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return "", err @@ -147,14 +131,12 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error { // ALiOrderQuery 查询支付宝订单 func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOrderQueryInfo, error) { // 初始化支付宝客户端 - AliInitClient(aliConfig) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(aliConfig) if err != nil { fmt.Println("Failed to get client:", err) return PayOrderQueryInfo{}, err } + // 请求参数 bm := make(gopay.BodyMap) bm.Set("out_trade_no", OrderNo) @@ -210,14 +192,12 @@ func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOr // AliRefundOrder 支付宝退款申请 func AliRefundOrder(ctx context.Context, orderRefundRequest OrderRefundRequest) (OrderRefundInfo, error) { // 初始化支付宝客户端 - AliInitClient(orderRefundRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(orderRefundRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return OrderRefundInfo{}, err } + // 请求参数 refundAmount := float64(orderRefundRequest.RefundAmount) / 100.0 bm := make(gopay.BodyMap) @@ -251,10 +231,7 @@ func AliRefundOrder(ctx context.Context, orderRefundRequest OrderRefundRequest) // AliRefundOrderQuery 支付宝订单退款查询 func AliRefundOrderQuery(ctx context.Context, orderRefundQueryRequest OrderRefundQueryRequest) (OrderRefundInfo, error) { // 初始化支付宝客户端 - AliInitClient(orderRefundQueryRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(orderRefundQueryRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return OrderRefundInfo{}, err @@ -290,10 +267,7 @@ func AliRefundOrderQuery(ctx context.Context, orderRefundQueryRequest OrderRefun // AliCloseOrder 支付宝订单关闭 func AliCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (OrderCloseInfo, error) { // 初始化支付宝客户端 - AliInitClient(orderCloseRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(orderCloseRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return OrderCloseInfo{}, err diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 8cb27d0..1a33eba 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -14,62 +14,48 @@ import ( "github.com/go-pay/gopay/wechat/v3" "github.com/qit-team/snow-core/log/logger" "strconv" - "sync" "time" ) -var ( - wxClient *wechat.ClientV3 - clientErr error - once sync.Once -) - // InitClient 使用提供的支付请求参数初始化微信客户端 -func InitClient(wxConfig WxPay) { - once.Do(func() { - // NewClientV3 初始化微信客户端 v3 - // mchid:商户ID 或者服务商模式的 sp_mchid - // serialNo:商户证书的证书序列号 - // apiV3Key:apiV3Key,商户平台获取 - // privateKey:私钥 apiclient_key.pem 读取后的内容 - wxClient, clientErr = wechat.NewClientV3( - wxConfig.MchId, - wxConfig.SerialNo, - wxConfig.ApiV3Key, - wxConfig.PrivateKey, - ) - }) +func InitClient(wxConfig WxPay) (*wechat.ClientV3, error) { + // NewClientV3 初始化微信客户端 v3 + // mchid:商户ID 或者服务商模式的 sp_mchid + // serialNo:商户证书的证书序列号 + // apiV3Key:apiV3Key,商户平台获取 + // privateKey:私钥 apiclient_key.pem 读取后的内容 + wxClient, clientErr := wechat.NewClientV3( + wxConfig.MchId, + wxConfig.SerialNo, + wxConfig.ApiV3Key, + wxConfig.PrivateKey, + ) // 启用自动同步返回验签,并定时更新微信平台API证书(开启自动验签时,无需单独设置微信平台API证书和序列号) clientErr = wxClient.AutoVerifySign() - - // 自定义配置http请求接收返回结果body大小,默认 10MB - wxClient.SetBodySize(10) // 没有特殊需求,可忽略此配置 - - // 打开Debug开关,输出日志,默认是关闭的 - wxClient.DebugSwitch = gopay.DebugOn -} - -// GetClient 获取已经初始化的微信客户端 -func GetClient() (*wechat.ClientV3, error) { if wxClient == nil { return nil, errors.New("client not initialized") } if clientErr != nil { return nil, clientErr } + + // 自定义配置http请求接收返回结果body大小,默认 10MB + wxClient.SetBodySize(10) // 没有特殊需求,可忽略此配置 + + // 打开Debug开关,输出日志,默认是关闭的 + wxClient.DebugSwitch = gopay.DebugOn return wxClient, nil + } // WxH5PayInfo 微信H5支付 func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, error) { // 初始化微信客户端 - InitClient(payOrderRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(payOrderRequest.Wx) if err != nil { return "", err } + expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339) // 初始化 BodyMap envConfig := config.GetConf() @@ -105,10 +91,7 @@ func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, er // WxPayCallBack 微信支付回调 func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { // 初始化微信客户端 - InitClient(wxConfig) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(wxConfig) if err != nil { return err } @@ -181,10 +164,7 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { // WxOrderQuery 查询微信订单 func WxOrderQuery(ctx context.Context, wxConfig WxPay, orderNo string) (PayOrderQueryInfo, error) { // 初始化微信客户端 - InitClient(wxConfig) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(wxConfig) if err != nil { return PayOrderQueryInfo{}, err } @@ -228,13 +208,11 @@ func WxOrderQuery(ctx context.Context, wxConfig WxPay, orderNo string) (PayOrder // WxOrderRefund 微信退款申请 func WxOrderRefund(ctx context.Context, orderRefundRequest OrderRefundRequest) (OrderRefundInfo, error) { // 初始化微信客户端 - InitClient(orderRefundRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(orderRefundRequest.Wx) if err != nil { return OrderRefundInfo{}, err } + // 初始化 BodyMap bm := make(gopay.BodyMap) bm.Set("out_trade_no", strconv.FormatInt(orderRefundRequest.OrderId, 10)). @@ -288,10 +266,7 @@ func WxOrderRefund(ctx context.Context, orderRefundRequest OrderRefundRequest) ( // WxOrderRefundQuery 微信订单退款查询 func WxOrderRefundQuery(ctx context.Context, orderRefundQueryRequest OrderRefundQueryRequest) (OrderRefundInfo, error) { // 初始化微信客户端 - InitClient(orderRefundQueryRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(orderRefundQueryRequest.Wx) if err != nil { return OrderRefundInfo{}, err } @@ -329,13 +304,11 @@ func WxOrderRefundQuery(ctx context.Context, orderRefundQueryRequest OrderRefund // WxCloseOrder 微信订单关闭 func WxCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (OrderCloseInfo, error) { // 初始化微信客户端 - InitClient(orderCloseRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(orderCloseRequest.Wx) if err != nil { return OrderCloseInfo{}, err } + wxRsp, err := wxClient.V3TransactionCloseOrder(ctx, strconv.FormatInt(orderCloseRequest.OrderId, 10)) if err != nil { return OrderCloseInfo{}, err From 966aba1dc45e79b63203c5d84e7fd8ca0f922670 Mon Sep 17 00:00:00 2001 From: wolter Date: Fri, 9 Aug 2024 15:23:08 +0800 Subject: [PATCH 2/3] =?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, From b0be00741f68d8348edc5bd33a8fb0897b418df4 Mon Sep 17 00:00:00 2001 From: wolter Date: Fri, 9 Aug 2024 16:05:34 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=90=8E=E5=8F=B0,=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/order_request_log.go | 10 ++++++ app/http/controllers/backend/order.go | 39 +++++++++++++++++++--- app/http/entities/backend/order.go | 19 +++++++++++ app/http/requestmapping/backend.go | 6 ++-- app/http/routes/admin.go | 6 ++-- app/services/order.go | 47 +++++++++++++++++++-------- 6 files changed, 105 insertions(+), 22 deletions(-) diff --git a/app/data/order_request_log.go b/app/data/order_request_log.go index c0c0568..0fc2126 100644 --- a/app/data/order_request_log.go +++ b/app/data/order_request_log.go @@ -37,3 +37,13 @@ func (m *OrderRequestLogRepo) OrderRequestLogDelete(orderLog *orderrequestlogmod func (m *OrderRequestLogRepo) OrderRequestLogUpdate(orderLog *orderrequestlogmodel.OrderRequestLog, conn builder.Cond, columns ...string) (int64, error) { return m.repo.Where(conn).MustCols(columns...).Update(orderLog) } + +// 后台查询订单日志列表 +func (m *OrderRequestLogRepo) OrderRequestLogBackendList(conn builder.Cond, pageFilter entities.PageRequest, orderLogList *[]orderrequestlogmodel.OrderRequestLog) (int64, error) { + repo := m.repo.Where(conn) + if pageFilter.Page > 0 { + repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1)) + } + repo.Join("left", "orders", "orders.app_id = order_request_log.app_id and orders.out_trade_no = order_request_log.out_trade_no") + return repo.Desc("order_request_log.create_time").FindAndCount(orderLogList) +} diff --git a/app/http/controllers/backend/order.go b/app/http/controllers/backend/order.go index 15d82f0..352bcca 100644 --- a/app/http/controllers/backend/order.go +++ b/app/http/controllers/backend/order.go @@ -5,6 +5,7 @@ import ( "PaymentCenter/app/http/controllers" "PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities/backend" + "PaymentCenter/app/models/ordercallbacklogmodel" "PaymentCenter/app/models/orderrequestlogmodel" "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/models/orderthirdpaylogmodel" @@ -34,10 +35,10 @@ func OrderList(c *gin.Context) { controllers.HandCodeRes(c, data, code) } -func OrderLogsList(c *gin.Context) { +func OrderRequestLogsList(c *gin.Context) { req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest) req.SetDefault() - requestLog, thirdLog, code := services.OrderLogsList(*req) + requestLog, total, code := services.OrderRequestLogsList(*req) requestLogList := make([]backend.OrderRequestLogResponse, 0, len(requestLog)) linq.From(requestLog).SelectT(func(in orderrequestlogmodel.OrderRequestLog) (out backend.OrderRequestLogResponse) { @@ -45,14 +46,42 @@ func OrderLogsList(c *gin.Context) { return }).ToSlice(&requestLogList) + controllers.HandCodeRes(c, entities.PageRsp{ + Total: total, + Data: requestLogList, + }, code) +} + +func OrderThirdLogsList(c *gin.Context) { + req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest) + req.SetDefault() + thirdLog, total, code := services.OrderThirdLogsList(*req) + thirdLogList := make([]backend.OrderThirdLogResponse, 0, len(thirdLog)) linq.From(thirdLog).SelectT(func(in orderthirdpaylogmodel.OrderThirdPayLog) (out backend.OrderThirdLogResponse) { out.ResponseFromDb(in) return }).ToSlice(&thirdLogList) - controllers.HandCodeRes(c, gin.H{ - "request_log_list": requestLogList, - "third_log_list": thirdLogList, + controllers.HandCodeRes(c, entities.PageRsp{ + Total: total, + Data: thirdLogList, + }, code) +} + +func OrderCallbackLogsList(c *gin.Context) { + req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest) + req.SetDefault() + callbackLog, total, code := services.OrderCallbackLogsList(*req) + + callbackLogList := make([]backend.OrderCallbackLogResponse, 0, len(callbackLog)) + linq.From(callbackLog).SelectT(func(in ordercallbacklogmodel.OrderCallbackLog) (out backend.OrderCallbackLogResponse) { + out.ResponseFromDb(in) + return + }).ToSlice(&callbackLogList) + + controllers.HandCodeRes(c, entities.PageRsp{ + Total: total, + Data: callbackLogList, }, code) } diff --git a/app/http/entities/backend/order.go b/app/http/entities/backend/order.go index 3b030d6..b60f97e 100644 --- a/app/http/entities/backend/order.go +++ b/app/http/entities/backend/order.go @@ -2,6 +2,7 @@ package backend import ( "PaymentCenter/app/http/entities" + "PaymentCenter/app/models/ordercallbacklogmodel" "PaymentCenter/app/models/orderrequestlogmodel" "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/models/orderthirdpaylogmodel" @@ -149,3 +150,21 @@ func (o *OrderThirdLogResponse) ResponseFromDb(db orderthirdpaylogmodel.OrderThi o.MerchantCallback = db.MerchantCallback o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05") } + +type OrderCallbackLogResponse struct { + Id int64 `json:"id"` + OrderId int64 `json:"order_id"` + MerchantRequest string `json:"merchant_request"` + Status int `json:"status"` + MerchantResponse string `json:"merchant_response"` + CreateTime string `json:"create_time"` +} + +func (o *OrderCallbackLogResponse) ResponseFromDb(db ordercallbacklogmodel.OrderCallbackLog) { + o.Id = db.Id + o.OrderId = db.OrderId + o.MerchantRequest = db.MerchantRequest + o.Status = db.Status + o.MerchantResponse = db.MerchantResponse + o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05") +} diff --git a/app/http/requestmapping/backend.go b/app/http/requestmapping/backend.go index 5d76b3f..0e75471 100644 --- a/app/http/requestmapping/backend.go +++ b/app/http/requestmapping/backend.go @@ -25,6 +25,8 @@ var BackendRequestMap = map[string]func() interface{}{ common.ADMIN_V1 + "/app/delete": func() interface{} { return new(entities.IdRequest) }, common.ADMIN_V1 + "/app/decrypt": func() interface{} { return new(backend.GenerateDecryptKeyRequest) }, // 订单 - common.ADMIN_V1 + "/order/list": func() interface{} { return new(backend.OrderListRequest) }, - common.ADMIN_V1 + "/order/log/list": func() interface{} { return new(backend.OrderLogsListRequest) }, + common.ADMIN_V1 + "/order/list": func() interface{} { return new(backend.OrderListRequest) }, + common.ADMIN_V1 + "/order/log/request": func() interface{} { return new(backend.OrderLogsListRequest) }, + common.ADMIN_V1 + "/order/log/callback": func() interface{} { return new(backend.OrderLogsListRequest) }, + common.ADMIN_V1 + "/order/log/third": func() interface{} { return new(backend.OrderLogsListRequest) }, } diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index 9d81e75..a15c83d 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -48,8 +48,10 @@ func RegisterAdminRoute(router *gin.Engine) { // 订单 order := v1.Group("/order") - order.GET("/list", backend.OrderList) // 订单列表 - order.GET("/log/list", backend.OrderLogsList) // 订单日志列表 + order.GET("/list", backend.OrderList) // 订单列表 + order.GET("/log/request", backend.OrderRequestLogsList) // 请求日志列表 + order.GET("/log/callback", backend.OrderCallbackLogsList) // 回调日志列表 + order.GET("/log/third", backend.OrderThirdLogsList) // 三方日志列表 } diff --git a/app/services/order.go b/app/services/order.go index cf42fbb..59d53e4 100644 --- a/app/services/order.go +++ b/app/services/order.go @@ -4,6 +4,7 @@ import ( "PaymentCenter/app/constants/errorcode" "PaymentCenter/app/data" "PaymentCenter/app/http/entities/backend" + "PaymentCenter/app/models/ordercallbacklogmodel" "PaymentCenter/app/models/orderrequestlogmodel" "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/models/orderthirdpaylogmodel" @@ -52,27 +53,47 @@ func OrderList(req backend.OrderList) (result []ordersmodel.OrdersBackendList, t return orderList, count, code } -func OrderLogsList(req backend.OrderLogsListRequest) (requestLog []orderrequestlogmodel.OrderRequestLog, thirdLod []orderthirdpaylogmodel.OrderThirdPayLog, code int) { +// 请求日志列表 +func OrderRequestLogsList(req backend.OrderLogsListRequest) (requestLog []orderrequestlogmodel.OrderRequestLog, total int64, code int) { requestRepo := data.NewOrderRequestLogRepo(paychannelmodel.GetInstance().GetDb()) - thirdRepo := data.NewOrderThirdPayLogRepo(paychannelmodel.GetInstance().GetDb()) + conn := builder.NewCond() + if req.OrderId > 0 { + conn = conn.And(builder.Eq{"orders.id": req.OrderId}) + } + // 请求日志 + orderLogList := make([]orderrequestlogmodel.OrderRequestLog, 0) + total, err := requestRepo.OrderRequestLogBackendList(conn, req.PageRequest, &orderLogList) + code = handErr(err) + return orderLogList, total, code +} +// 回调日志列表 +func OrderCallbackLogsList(req backend.OrderLogsListRequest) (callback []ordercallbacklogmodel.OrderCallbackLog, total int64, code int) { + callbackRepo := data.NewOrderCallbackLogRepo(paychannelmodel.GetInstance().GetDb()) conn := builder.NewCond() if req.OrderId > 0 { conn = conn.And(builder.Eq{"order_id": req.OrderId}) } - // 请求日志 - orderLogList := make([]orderrequestlogmodel.OrderRequestLog, 0) - _, err := requestRepo.OrderRequestLogList(conn, req.PageRequest, &orderLogList) - if err != nil { - code = handErr(err) - return - } - // 三方日志 - thirdLogList := make([]orderthirdpaylogmodel.OrderThirdPayLog, 0) - _, err = thirdRepo.OrderThirdPayLogList(conn, req.PageRequest, &thirdLogList) + // 回调日志 + callbackLogList := make([]ordercallbacklogmodel.OrderCallbackLog, 0) + total, err := callbackRepo.OrderCallbackLogList(conn, req.PageRequest, &callbackLogList) code = handErr(err) - return orderLogList, thirdLogList, code + return callbackLogList, total, code +} + +// 三方日志列表 +func OrderThirdLogsList(req backend.OrderLogsListRequest) (thirdLod []orderthirdpaylogmodel.OrderThirdPayLog, total int64, code int) { + thirdRepo := data.NewOrderThirdPayLogRepo(paychannelmodel.GetInstance().GetDb()) + // 三方日志 + conn := builder.NewCond() + if req.OrderId > 0 { + conn = conn.And(builder.Eq{"order_id": req.OrderId}) + } + thirdLogList := make([]orderthirdpaylogmodel.OrderThirdPayLog, 0) + total, err := thirdRepo.OrderThirdPayLogList(conn, req.PageRequest, &thirdLogList) + code = handErr(err) + return thirdLogList, total, code } func OrderCreate(orderIn *ordersmodel.Orders) (orderOut *ordersmodel.Orders, code int) {