feat: 微信支付fix
This commit is contained in:
		
							parent
							
								
									81dfd00cf9
								
							
						
					
					
						commit
						ebcb3a9d7f
					
				| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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{}{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
	}
 | 
			
		||||
	// 更新订单状态
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue