This commit is contained in:
yangduo 2025-02-20 23:13:30 +08:00
parent d4456d8a52
commit a6a802b293
3 changed files with 27 additions and 11 deletions

View File

@ -495,7 +495,7 @@ func (this *MainServiceApi) WxPayNotify(c *gin.Context) {
f5.GetSysLog().Debug("wx pay post data:%s", rawdata) f5.GetSysLog().Debug("wx pay post data:%s", rawdata)
rawstr := fmt.Sprintf("%s\n%s\n%s\n", paytimestamp, paynonce, 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) c.JSON(501, failrspobj)
return return
} }
@ -696,6 +696,7 @@ func (this *MainServiceApi) WxPayNotify(c *gin.Context) {
} }
f5.GetSysLog().Debug("notify url:%d, %s", gameid, notifyurl) f5.GetSysLog().Debug("notify url:%d, %s", gameid, notifyurl)
fields := []string{"status", "sp_orderid"}
if len(notifyurl) > 0 { if len(notifyurl) > 0 {
goodsidstr := q5.SafeToString(orderModel.ItemId) goodsidstr := q5.SafeToString(orderModel.ItemId)
totalamountstr := q5.SafeToString(resObj.Amount.Total) totalamountstr := q5.SafeToString(resObj.Amount.Total)
@ -742,11 +743,12 @@ func (this *MainServiceApi) WxPayNotify(c *gin.Context) {
}) })
} else { } else {
orderModel.Status = 1 orderModel.Status = 1
}
orderModel.SpOrderId = resObj.TransId
count, _ := service.Wxpay.GetGoodsCount(gameid, int64(orderModel.ItemId)) count, _ := service.Wxpay.GetGoodsCount(gameid, int64(orderModel.ItemId))
orderModel.SpAmount = int32(count) orderModel.SpAmount = int32(count)
orderModel.UpdateFields([]string{"status", "sp_orderid", "sp_amount"}) fields = append(fields, "sp_amount")
}
orderModel.SpOrderId = resObj.TransId
orderModel.UpdateFields(fields)
c.String(200, "") c.String(200, "")
} }

View File

@ -2,6 +2,7 @@ package service
import ( import (
"context" "context"
"crypto/rsa"
"encoding/json" "encoding/json"
"errors" "errors"
"f5" "f5"
@ -32,6 +33,7 @@ type wxpay struct {
ctx context.Context ctx context.Context
client *core.Client client *core.Client
payhtmlstr string payhtmlstr string
mchpubkey *rsa.PublicKey
} }
type WxQuery struct { type WxQuery struct {

View File

@ -2,6 +2,10 @@ package service
import ( import (
"context" "context"
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"f5" "f5"
"fmt" "fmt"
"main/mt" "main/mt"
@ -43,6 +47,8 @@ func (wp *wxpay) initMch() {
wp.client, err = core.NewClient(wp.ctx, opts...) wp.client, err = core.NewClient(wp.ctx, opts...)
if err != nil { if err != nil {
f5.GetSysLog().Alert("new wechat pay client err:%s", err) f5.GetSysLog().Alert("new wechat pay client err:%s", err)
} else {
wp.mchpubkey = mchPublicKey
} }
go wp.checkGameMediaId() go wp.checkGameMediaId()
@ -154,14 +160,20 @@ func (wp *wxpay) GetPrepayInfoStr(openid string, gameid int64, userip string, or
return return
} }
func (wp *wxpay) VerifyPaySign(rawdata string, signature string) bool { func (wp *wxpay) VerifyPaySign(rawdata string, signaturebase64 string) error {
sign, err := wp.client.Sign(wp.ctx, rawdata) // 解码签名
f5.GetSysLog().Debug("rawstr:%s\nverify pay sign:%s\nsignature:%s", rawdata, sign.Signature, signature) signature, err := base64.StdEncoding.DecodeString(signaturebase64)
if err != nil || sign.Signature != signature { if err != nil {
return false 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) { func (wp *wxpay) DecryptPaydata(associatedData string, nonce string, clipherdata string) (plaindata string, err error) {