From ebcb3a9d7fd571484b8e059097b61c93d0c63dbd Mon Sep 17 00:00:00 2001 From: wolter Date: Fri, 6 Dec 2024 20:42:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/front/payment_controller.go | 2 +- app/http/middlewares/base.go | 12 ++++++++++ app/http/requestmapping/front.go | 1 + app/services/thirdpay/wx.go | 22 ++++++++++--------- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/http/controllers/front/payment_controller.go b/app/http/controllers/front/payment_controller.go index fdde9be..39b7bfd 100644 --- a/app/http/controllers/front/payment_controller.go +++ b/app/http/controllers/front/payment_controller.go @@ -235,5 +235,5 @@ func WxJsApiPay(c *gin.Context) { req.ClientIp = c.ClientIP() rsp, code := thirdpay.WxJsApiPay(*req) - controllers.HandCodeRes(c, rsp, code) + controllers.ApiRes(c, rsp, code, rsp.ThirdMsg) } diff --git a/app/http/middlewares/base.go b/app/http/middlewares/base.go index b4c9eed..4145579 100644 --- a/app/http/middlewares/base.go +++ b/app/http/middlewares/base.go @@ -107,6 +107,7 @@ func ValidateRequest() gin.HandlerFunc { return func(c *gin.Context) { var path = c.FullPath() var handler func() interface{} + var logId int64 if strings.Index(path, "admin") >= 0 { handler = requestmapping.BackendRequestMap[path] @@ -126,6 +127,17 @@ func ValidateRequest() gin.HandlerFunc { c.Abort() } c.Set("request", v) + //表单验证 + logHandler := requestmapping.FrontRequestMap[path] + if logHandler != nil { + _, isSaveLog := logHandler() + if isSaveLog { + logContent, _ := json.Marshal(v) + //记录请求日志 + logId, _ = services.AddRequestLog(logContent, c.ClientIP(), path) + } + c.Set("log", logId) + } c.Next() } diff --git a/app/http/requestmapping/front.go b/app/http/requestmapping/front.go index a1bcfc5..2eab3e2 100644 --- a/app/http/requestmapping/front.go +++ b/app/http/requestmapping/front.go @@ -11,6 +11,7 @@ var FrontRequestMap = map[string]func() (validForm interface{}, isSaveLog bool){ common.FRONT_V1 + "/pay/refund": func() (interface{}, bool) { return new(front.RefundReqs), true }, common.FRONT_V1 + "/pay/query": func() (interface{}, bool) { return new(front.QueryReqs), false }, common.FRONT_V1 + "/pay/close": func() (interface{}, bool) { return new(front.CloseReqs), true }, + common.FRONT_V1 + "/wx/payurl": func() (interface{}, bool) { return new(front.WxJsApiPayRequest), true }, } var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{ diff --git a/app/services/thirdpay/wx.go b/app/services/thirdpay/wx.go index e9803c8..2db9e91 100644 --- a/app/services/thirdpay/wx.go +++ b/app/services/thirdpay/wx.go @@ -132,7 +132,7 @@ type wxJsapiPay struct { param front.WxJsApiPayRequest order *ordersmodel.Orders payChannel paychannelmodel.PayChannel - wxConfig map[string]interface{} + wxConfig paymentService.WxPay openId string // 用户openid } @@ -154,13 +154,13 @@ func (this *wxJsapiPay) getPayChannel() { func (this *wxJsapiPay) getOpenId() { var err error //// 配置支付的解析 - wxConfig := make(map[string]interface{}) + wxConfig := paymentService.WxPay{} err = json.Unmarshal([]byte(this.payChannel.ExtJson), &wxConfig) if err != nil { this.code = handErr(err) return } - sk := wxConfig["secret"].(string) + sk := wxConfig.Secret if sk == "" { this.code = errorcode.PayChannelConfigNotFound return @@ -193,6 +193,7 @@ func (this *wxJsapiPay) getOpenId() { } if rsp.Openid == "" { this.code = errorcode.WechatAuthFail + this.msg = string(response) return } this.openId = rsp.Openid @@ -213,11 +214,10 @@ func (this *wxJsapiPay) SHA256WithRsaBase64(origData string) (sign string, err e //var keypath = "商户证书私钥地址" //key, err := ioutil.ReadFile(keypath) var key []byte - if pri, ok := this.wxConfig["private_key"].(string); ok { - key = []byte(pri) - } else { + if this.wxConfig.PrivateKey == "" { return "", errors.New("配置的私钥不存在") } + key = []byte(this.wxConfig.PrivateKey) blocks, _ := pem.Decode(key) if blocks == nil || blocks.Type != "PRIVATE KEY" { @@ -255,8 +255,10 @@ func WxJsApiPay(param front.WxJsApiPayRequest) (response front.WxJsApiPayRespons // 2 通过code获取openid task.getOpenId() if task.code != errorcode.Success { + response.ThirdMsg = task.msg return response, task.code } + task.wxConfig.AppId = task.payChannel.AppId order := task.order // 通过openid,订单数据,请求微信下单, 获取prepay_id @@ -268,15 +270,15 @@ func WxJsApiPay(param front.WxJsApiPayRequest) (response front.WxJsApiPayRespons Amount: order.Amount, PayerClientIp: param.ClientIp, ReturnUrl: "", - //Wx: paymentService.WxPay{}, - //Ali: paymentService.AliPay{}, - OpenId: task.openId, + Wx: task.wxConfig, + Ali: paymentService.AliPay{}, + OpenId: task.openId, } res := paymentService.PaymentService(ctx, orderRequest) // 下单失败 if res.Code != payCommon.PAY_SUCCESS_CODE { task.code = errorcode.PrePayFail - task.msg = res.ErrorMsg + response.ThirdMsg = res.ErrorMsg return response, task.code } // 更新订单状态