From 7a2d9fa1d72f4618e1dda4b6eb92ae789968950d Mon Sep 17 00:00:00 2001 From: wolter <11@gmail> Date: Mon, 21 Apr 2025 15:30:09 +0800 Subject: [PATCH] feat: wxmini fix --- app/http/entities/front/wechat.go | 4 ++++ app/http/requestmapping/front.go | 1 + app/http/routes/route.go | 2 +- app/services/thirdpay/do/wx_mini.go | 3 +++ app/services/thirdpay/wx_mini.go | 24 +++++++++++++++++++----- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/http/entities/front/wechat.go b/app/http/entities/front/wechat.go index fe44094..02436c7 100644 --- a/app/http/entities/front/wechat.go +++ b/app/http/entities/front/wechat.go @@ -29,6 +29,10 @@ type GetWxAuthMiniResponse struct { Errmsg string `json:"errmsg"` } +func (this *GetWxAuthMiniResponse) IsSuccess() bool { + return this.Errcode == 0 +} + // jsapi支付 type WxJsApiPayRequest struct { Code string `json:"code" form:"code" ` diff --git a/app/http/requestmapping/front.go b/app/http/requestmapping/front.go index e0fc90f..14b37aa 100644 --- a/app/http/requestmapping/front.go +++ b/app/http/requestmapping/front.go @@ -30,6 +30,7 @@ var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{ common.FRONT_V1 + "/wx/getWxAuthMini": func() interface{} { return new(front.GetWxAuthRequest) }, common.FRONT_V1 + "/wx/getCode": func() interface{} { return new(front.GetWxAuthRequest) }, common.FRONT_V1 + "/wx/getScheme": func() interface{} { return new(front.GetWxMiniSchemeRequest) }, + common.FRONT_V1 + "/wx/wxMini": func() interface{} { return new(front.WxMiniPayRequest) }, common.FRONT_V1 + "/payPage/list": func() interface{} { return new(front.PayChannelListRequest) }, common.FRONT_V1 + "/payPage/submit": func() interface{} { return new(front.GetPayLinkRequest) }, diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 443aecc..7b75ef4 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -75,7 +75,7 @@ func RegisterRoute(router *gin.Engine) { wx.GET("/getCode", front.GetWxCode) // 接收微信code wx.POST("/getScheme", front.GetWxMiniScheme) // 小程序 获取加密scheme码 wx.GET("/getWxAuthMini", front.GetWxAuthMini) // 小程序 获取openId, 授权 - wx.POST("/wxMini", front.WxMiniPay) // 小程序 获取支付参数 + wx.GET("/wxMini", front.WxMiniPay) // 小程序 获取支付参数 } // 微信jsapi支付链接 diff --git a/app/services/thirdpay/do/wx_mini.go b/app/services/thirdpay/do/wx_mini.go index a2741dd..18fb5c2 100644 --- a/app/services/thirdpay/do/wx_mini.go +++ b/app/services/thirdpay/do/wx_mini.go @@ -203,6 +203,9 @@ func (wm *WxMini) GetWxAuthMini(param front.GetWxAuthRequest) (rsp front.GetWxAu // 解析返回数据 err = json.Unmarshal(response, &rsp) + if !rsp.IsSuccess() && rsp.Openid == "" { + err = fmt.Errorf("获取openId失败:%d,%s", rsp.Errcode, rsp.Errmsg) + } return } diff --git a/app/services/thirdpay/wx_mini.go b/app/services/thirdpay/wx_mini.go index a8be8f8..481140a 100644 --- a/app/services/thirdpay/wx_mini.go +++ b/app/services/thirdpay/wx_mini.go @@ -10,6 +10,7 @@ import ( "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/app/utils" "context" + "encoding/json" "strconv" "time" ) @@ -37,7 +38,10 @@ func GetWxMiniSchemeService(param front.GetWxMiniSchemeRequest) (scheme string, // 微信小程序支付 func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, code int) { - var ctx = context.Background() + var ( + ctx = context.Background() + wxConfig = paymentService.WxPay{} + ) task := newWxJsapiPay(param) // 1 获取订单 @@ -51,11 +55,22 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, if task.code != errorcode.Success { return response, task.code } + + // 配置支付的解析 + err := json.Unmarshal([]byte(task.payChannel.ExtJson), &wxConfig) + if err != nil { + task.code = handErr(err) + return + } + task.wxConfig = wxConfig + task.wxConfig.AppId = task.payChannel.AppId + // 获取下单的请求参数 task.getOrderRequestLogs() if task.code != errorcode.Success { return response, task.code } + // 2 通过code获取openid rsp, code := GetWxAuthMini(front.GetWxAuthRequest{ Code: param.Code, @@ -67,8 +82,6 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, } task.openId = rsp.Openid - task.wxConfig.AppId = task.payChannel.AppId - order := task.order // 通过openid,订单数据,请求微信下单, 获取prepay_id orderRequest := paymentService.PayOrderRequest{ @@ -83,12 +96,14 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, OpenId: task.openId, } res := paymentService.PaymentService(ctx, orderRequest) + // 下单失败 if res.Code != payCommon.PAY_SUCCESS_CODE { task.code = errorcode.PrePayFail response.ThirdMsg = res.ErrorMsg return response, task.code } + // 更新订单状态 order.Status = common.ORDER_STATUS_PAYING task.code = services.OrderUpdate(order, "status") @@ -103,8 +118,7 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, NonceStr: strconv.Itoa(int(time.Now().Unix())), Package: "prepay_id=" + res.Result, SignType: "RSA", - PaySign: "", - ThirdMsg: "", + ThirdMsg: res.ErrorMsg, } // 4 签名