feat: wxmini fix

This commit is contained in:
wolter 2025-04-21 15:30:09 +08:00
parent 8ce5ead203
commit 7a2d9fa1d7
5 changed files with 28 additions and 6 deletions

View File

@ -29,6 +29,10 @@ type GetWxAuthMiniResponse struct {
Errmsg string `json:"errmsg"` Errmsg string `json:"errmsg"`
} }
func (this *GetWxAuthMiniResponse) IsSuccess() bool {
return this.Errcode == 0
}
// jsapi支付 // jsapi支付
type WxJsApiPayRequest struct { type WxJsApiPayRequest struct {
Code string `json:"code" form:"code" ` Code string `json:"code" form:"code" `

View File

@ -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/getWxAuthMini": func() interface{} { return new(front.GetWxAuthRequest) },
common.FRONT_V1 + "/wx/getCode": 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/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/list": func() interface{} { return new(front.PayChannelListRequest) },
common.FRONT_V1 + "/payPage/submit": func() interface{} { return new(front.GetPayLinkRequest) }, common.FRONT_V1 + "/payPage/submit": func() interface{} { return new(front.GetPayLinkRequest) },

View File

@ -75,7 +75,7 @@ func RegisterRoute(router *gin.Engine) {
wx.GET("/getCode", front.GetWxCode) // 接收微信code wx.GET("/getCode", front.GetWxCode) // 接收微信code
wx.POST("/getScheme", front.GetWxMiniScheme) // 小程序 获取加密scheme码 wx.POST("/getScheme", front.GetWxMiniScheme) // 小程序 获取加密scheme码
wx.GET("/getWxAuthMini", front.GetWxAuthMini) // 小程序 获取openId, 授权 wx.GET("/getWxAuthMini", front.GetWxAuthMini) // 小程序 获取openId, 授权
wx.POST("/wxMini", front.WxMiniPay) // 小程序 获取支付参数 wx.GET("/wxMini", front.WxMiniPay) // 小程序 获取支付参数
} }
// 微信jsapi支付链接 // 微信jsapi支付链接

View File

@ -203,6 +203,9 @@ func (wm *WxMini) GetWxAuthMini(param front.GetWxAuthRequest) (rsp front.GetWxAu
// 解析返回数据 // 解析返回数据
err = json.Unmarshal(response, &rsp) err = json.Unmarshal(response, &rsp)
if !rsp.IsSuccess() && rsp.Openid == "" {
err = fmt.Errorf("获取openId失败%d,%s", rsp.Errcode, rsp.Errmsg)
}
return return
} }

View File

@ -10,6 +10,7 @@ import (
"PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/app/third/paymentService/payCommon"
"PaymentCenter/app/utils" "PaymentCenter/app/utils"
"context" "context"
"encoding/json"
"strconv" "strconv"
"time" "time"
) )
@ -37,7 +38,10 @@ func GetWxMiniSchemeService(param front.GetWxMiniSchemeRequest) (scheme string,
// 微信小程序支付 // 微信小程序支付
func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, code int) { func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, code int) {
var ctx = context.Background() var (
ctx = context.Background()
wxConfig = paymentService.WxPay{}
)
task := newWxJsapiPay(param) task := newWxJsapiPay(param)
// 1 获取订单 // 1 获取订单
@ -51,11 +55,22 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse,
if task.code != errorcode.Success { if task.code != errorcode.Success {
return response, task.code 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() task.getOrderRequestLogs()
if task.code != errorcode.Success { if task.code != errorcode.Success {
return response, task.code return response, task.code
} }
// 2 通过code获取openid // 2 通过code获取openid
rsp, code := GetWxAuthMini(front.GetWxAuthRequest{ rsp, code := GetWxAuthMini(front.GetWxAuthRequest{
Code: param.Code, Code: param.Code,
@ -67,8 +82,6 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse,
} }
task.openId = rsp.Openid task.openId = rsp.Openid
task.wxConfig.AppId = task.payChannel.AppId
order := task.order order := task.order
// 通过openid订单数据请求微信下单, 获取prepay_id // 通过openid订单数据请求微信下单, 获取prepay_id
orderRequest := paymentService.PayOrderRequest{ orderRequest := paymentService.PayOrderRequest{
@ -83,12 +96,14 @@ func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse,
OpenId: task.openId, OpenId: task.openId,
} }
res := paymentService.PaymentService(ctx, orderRequest) res := paymentService.PaymentService(ctx, orderRequest)
// 下单失败 // 下单失败
if res.Code != payCommon.PAY_SUCCESS_CODE { if res.Code != payCommon.PAY_SUCCESS_CODE {
task.code = errorcode.PrePayFail task.code = errorcode.PrePayFail
response.ThirdMsg = res.ErrorMsg response.ThirdMsg = res.ErrorMsg
return response, task.code return response, task.code
} }
// 更新订单状态 // 更新订单状态
order.Status = common.ORDER_STATUS_PAYING order.Status = common.ORDER_STATUS_PAYING
task.code = services.OrderUpdate(order, "status") 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())), NonceStr: strconv.Itoa(int(time.Now().Unix())),
Package: "prepay_id=" + res.Result, Package: "prepay_id=" + res.Result,
SignType: "RSA", SignType: "RSA",
PaySign: "", ThirdMsg: res.ErrorMsg,
ThirdMsg: "",
} }
// 4 签名 // 4 签名