feat: 微信支付fix
This commit is contained in:
		
							parent
							
								
									81dfd00cf9
								
							
						
					
					
						commit
						ebcb3a9d7f
					
				|  | @ -235,5 +235,5 @@ func WxJsApiPay(c *gin.Context) { | ||||||
| 	req.ClientIp = c.ClientIP() | 	req.ClientIp = c.ClientIP() | ||||||
| 
 | 
 | ||||||
| 	rsp, code := thirdpay.WxJsApiPay(*req) | 	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) { | 	return func(c *gin.Context) { | ||||||
| 		var path = c.FullPath() | 		var path = c.FullPath() | ||||||
| 		var handler func() interface{} | 		var handler func() interface{} | ||||||
|  | 		var logId int64 | ||||||
| 
 | 
 | ||||||
| 		if strings.Index(path, "admin") >= 0 { | 		if strings.Index(path, "admin") >= 0 { | ||||||
| 			handler = requestmapping.BackendRequestMap[path] | 			handler = requestmapping.BackendRequestMap[path] | ||||||
|  | @ -126,6 +127,17 @@ func ValidateRequest() gin.HandlerFunc { | ||||||
| 			c.Abort() | 			c.Abort() | ||||||
| 		} | 		} | ||||||
| 		c.Set("request", v) | 		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() | 		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/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/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 + "/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{}{ | var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{ | ||||||
|  |  | ||||||
|  | @ -132,7 +132,7 @@ type wxJsapiPay struct { | ||||||
| 	param      front.WxJsApiPayRequest | 	param      front.WxJsApiPayRequest | ||||||
| 	order      *ordersmodel.Orders | 	order      *ordersmodel.Orders | ||||||
| 	payChannel paychannelmodel.PayChannel | 	payChannel paychannelmodel.PayChannel | ||||||
| 	wxConfig   map[string]interface{} | 	wxConfig   paymentService.WxPay | ||||||
| 
 | 
 | ||||||
| 	openId string // 用户openid
 | 	openId string // 用户openid
 | ||||||
| } | } | ||||||
|  | @ -154,13 +154,13 @@ func (this *wxJsapiPay) getPayChannel() { | ||||||
| func (this *wxJsapiPay) getOpenId() { | func (this *wxJsapiPay) getOpenId() { | ||||||
| 	var err error | 	var err error | ||||||
| 	//// 配置支付的解析
 | 	//// 配置支付的解析
 | ||||||
| 	wxConfig := make(map[string]interface{}) | 	wxConfig := paymentService.WxPay{} | ||||||
| 	err = json.Unmarshal([]byte(this.payChannel.ExtJson), &wxConfig) | 	err = json.Unmarshal([]byte(this.payChannel.ExtJson), &wxConfig) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		this.code = handErr(err) | 		this.code = handErr(err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	sk := wxConfig["secret"].(string) | 	sk := wxConfig.Secret | ||||||
| 	if sk == "" { | 	if sk == "" { | ||||||
| 		this.code = errorcode.PayChannelConfigNotFound | 		this.code = errorcode.PayChannelConfigNotFound | ||||||
| 		return | 		return | ||||||
|  | @ -193,6 +193,7 @@ func (this *wxJsapiPay) getOpenId() { | ||||||
| 	} | 	} | ||||||
| 	if rsp.Openid == "" { | 	if rsp.Openid == "" { | ||||||
| 		this.code = errorcode.WechatAuthFail | 		this.code = errorcode.WechatAuthFail | ||||||
|  | 		this.msg = string(response) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	this.openId = rsp.Openid | 	this.openId = rsp.Openid | ||||||
|  | @ -213,11 +214,10 @@ func (this *wxJsapiPay) SHA256WithRsaBase64(origData string) (sign string, err e | ||||||
| 	//var keypath = "商户证书私钥地址"
 | 	//var keypath = "商户证书私钥地址"
 | ||||||
| 	//key, err := ioutil.ReadFile(keypath)
 | 	//key, err := ioutil.ReadFile(keypath)
 | ||||||
| 	var key []byte | 	var key []byte | ||||||
| 	if pri, ok := this.wxConfig["private_key"].(string); ok { | 	if this.wxConfig.PrivateKey == "" { | ||||||
| 		key = []byte(pri) |  | ||||||
| 	} else { |  | ||||||
| 		return "", errors.New("配置的私钥不存在") | 		return "", errors.New("配置的私钥不存在") | ||||||
| 	} | 	} | ||||||
|  | 	key = []byte(this.wxConfig.PrivateKey) | ||||||
| 
 | 
 | ||||||
| 	blocks, _ := pem.Decode(key) | 	blocks, _ := pem.Decode(key) | ||||||
| 	if blocks == nil || blocks.Type != "PRIVATE KEY" { | 	if blocks == nil || blocks.Type != "PRIVATE KEY" { | ||||||
|  | @ -255,8 +255,10 @@ func WxJsApiPay(param front.WxJsApiPayRequest) (response front.WxJsApiPayRespons | ||||||
| 	// 2 通过code获取openid
 | 	// 2 通过code获取openid
 | ||||||
| 	task.getOpenId() | 	task.getOpenId() | ||||||
| 	if task.code != errorcode.Success { | 	if task.code != errorcode.Success { | ||||||
|  | 		response.ThirdMsg = task.msg | ||||||
| 		return response, task.code | 		return response, task.code | ||||||
| 	} | 	} | ||||||
|  | 	task.wxConfig.AppId = task.payChannel.AppId | ||||||
| 
 | 
 | ||||||
| 	order := task.order | 	order := task.order | ||||||
| 	// 通过openid,订单数据,请求微信下单, 获取prepay_id
 | 	// 通过openid,订单数据,请求微信下单, 获取prepay_id
 | ||||||
|  | @ -268,15 +270,15 @@ func WxJsApiPay(param front.WxJsApiPayRequest) (response front.WxJsApiPayRespons | ||||||
| 		Amount:        order.Amount, | 		Amount:        order.Amount, | ||||||
| 		PayerClientIp: param.ClientIp, | 		PayerClientIp: param.ClientIp, | ||||||
| 		ReturnUrl:     "", | 		ReturnUrl:     "", | ||||||
| 		//Wx:            paymentService.WxPay{},
 | 		Wx:            task.wxConfig, | ||||||
| 		//Ali:           paymentService.AliPay{},
 | 		Ali:           paymentService.AliPay{}, | ||||||
| 		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 | ||||||
| 		task.msg = res.ErrorMsg | 		response.ThirdMsg = res.ErrorMsg | ||||||
| 		return response, task.code | 		return response, task.code | ||||||
| 	} | 	} | ||||||
| 	// 更新订单状态
 | 	// 更新订单状态
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue