diff --git a/server/payserver/api/v1/mainservice/mainservice.go b/server/payserver/api/v1/mainservice/mainservice.go index 1ad2a8d..f2e0d19 100644 --- a/server/payserver/api/v1/mainservice/mainservice.go +++ b/server/payserver/api/v1/mainservice/mainservice.go @@ -495,7 +495,7 @@ func (this *MainServiceApi) WxPayNotify(c *gin.Context) { f5.GetSysLog().Debug("wx pay post data:%s", rawdata) rawstr := fmt.Sprintf("%s\n%s\n%s\n", paytimestamp, paynonce, rawdata) - if !service.Wxpay.VerifyPaySign(rawstr, paysign) { + if service.Wxpay.VerifyPaySign(rawstr, paysign) != nil { c.JSON(501, failrspobj) return } @@ -696,6 +696,7 @@ func (this *MainServiceApi) WxPayNotify(c *gin.Context) { } f5.GetSysLog().Debug("notify url:%d, %s", gameid, notifyurl) + fields := []string{"status", "sp_orderid"} if len(notifyurl) > 0 { goodsidstr := q5.SafeToString(orderModel.ItemId) totalamountstr := q5.SafeToString(resObj.Amount.Total) @@ -742,11 +743,12 @@ func (this *MainServiceApi) WxPayNotify(c *gin.Context) { }) } else { orderModel.Status = 1 + count, _ := service.Wxpay.GetGoodsCount(gameid, int64(orderModel.ItemId)) + orderModel.SpAmount = int32(count) + fields = append(fields, "sp_amount") } orderModel.SpOrderId = resObj.TransId - count, _ := service.Wxpay.GetGoodsCount(gameid, int64(orderModel.ItemId)) - orderModel.SpAmount = int32(count) - orderModel.UpdateFields([]string{"status", "sp_orderid", "sp_amount"}) + orderModel.UpdateFields(fields) c.String(200, "") } diff --git a/server/payserver/service/wxpay.go b/server/payserver/service/wxpay.go index 94d964e..f57d336 100644 --- a/server/payserver/service/wxpay.go +++ b/server/payserver/service/wxpay.go @@ -2,6 +2,7 @@ package service import ( "context" + "crypto/rsa" "encoding/json" "errors" "f5" @@ -32,6 +33,7 @@ type wxpay struct { ctx context.Context client *core.Client payhtmlstr string + mchpubkey *rsa.PublicKey } type WxQuery struct { diff --git a/server/payserver/service/wxpay_prepare.go b/server/payserver/service/wxpay_prepare.go index 1523c6e..f50d61b 100644 --- a/server/payserver/service/wxpay_prepare.go +++ b/server/payserver/service/wxpay_prepare.go @@ -2,6 +2,10 @@ package service import ( "context" + "crypto" + "crypto/rsa" + "crypto/sha256" + "encoding/base64" "f5" "fmt" "main/mt" @@ -43,8 +47,10 @@ func (wp *wxpay) initMch() { wp.client, err = core.NewClient(wp.ctx, opts...) if err != nil { f5.GetSysLog().Alert("new wechat pay client err:%s", err) + } else { + wp.mchpubkey = mchPublicKey } - + go wp.checkGameMediaId() wp.payhtmlstr, err = f5.ReadJsonFile("../config/payhtml.template") @@ -154,14 +160,20 @@ func (wp *wxpay) GetPrepayInfoStr(openid string, gameid int64, userip string, or return } -func (wp *wxpay) VerifyPaySign(rawdata string, signature string) bool { - sign, err := wp.client.Sign(wp.ctx, rawdata) - f5.GetSysLog().Debug("rawstr:%s\nverify pay sign:%s\nsignature:%s", rawdata, sign.Signature, signature) - if err != nil || sign.Signature != signature { - return false +func (wp *wxpay) VerifyPaySign(rawdata string, signaturebase64 string) error { + // 解码签名 + signature, err := base64.StdEncoding.DecodeString(signaturebase64) + if err != nil { + return err } - return true + // 计算数据的SHA256哈希 + hash := sha256.New() + hash.Write([]byte(rawdata)) + hashed := hash.Sum(nil) + + // 使用公钥验证签名 + return rsa.VerifyPKCS1v15(wp.mchpubkey, crypto.SHA256, hashed, signature) } func (wp *wxpay) DecryptPaydata(associatedData string, nonce string, clipherdata string) (plaindata string, err error) {