feat: update pay notify

This commit is contained in:
Rogee
2025-04-30 20:54:37 +08:00
parent 67d6f3ec33
commit d6c5a2677c
48 changed files with 444 additions and 161 deletions

View File

@@ -1,6 +1,8 @@
package wepay
import (
"time"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
)
@@ -15,3 +17,25 @@ func DefaultProvider() container.ProviderContainer {
},
}
}
type PayNotify struct {
Mchid string `json:"mchid"`
Appid string `json:"appid"`
OutTradeNo string `json:"out_trade_no"`
TransactionID string `json:"transaction_id"`
TradeType string `json:"trade_type"`
TradeState string `json:"trade_state"`
TradeStateDesc string `json:"trade_state_desc"`
BankType string `json:"bank_type"`
Attach string `json:"attach"`
SuccessTime time.Time `json:"success_time"`
Payer struct {
Openid string `json:"openid"`
} `json:"payer"`
Amount struct {
Total int64 `json:"total"`
PayerTotal int64 `json:"payer_total"`
Currency string `json:"currency"`
PayerCurrency string `json:"payer_currency"`
} `json:"amount"`
}

View File

@@ -4,12 +4,16 @@ import (
"context"
"crypto/rsa"
"encoding/json"
"fmt"
"net/http"
"time"
w "quyun/providers/wechat"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/wechat/v3"
"github.com/go-pay/util/js"
"github.com/gofiber/fiber/v3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.ipao.vip/atom/container"
@@ -101,6 +105,42 @@ func (c *Client) V3TransactionJsapi(ctx context.Context, f func(*BodyMap)) (*Pre
}, nil
}
func (c *Client) ParseNotify(ctx fiber.Ctx) (*PayNotify, error) {
body := ctx.Body()
si := &wechat.SignInfo{
HeaderTimestamp: ctx.Get(wechat.HeaderTimestamp),
HeaderNonce: ctx.Get(wechat.HeaderNonce),
HeaderSignature: ctx.Get(wechat.HeaderSignature),
HeaderSerial: ctx.Get(wechat.HeaderSerial),
SignBody: string(body),
}
notifyReq := &wechat.V3NotifyReq{SignInfo: si}
if err := js.UnmarshalBytes(body, notifyReq); err != nil {
log.Errorf("json unmarshal error:%v", err)
return nil, ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": fmt.Sprintf("json unmarshal error:%v", err)})
}
// 获取微信平台证书
certMap := c.WxPublicKeyMap()
// 验证异步通知的签名
err := notifyReq.VerifySignByPKMap(certMap)
if err != nil {
log.Errorf("verify sign error:%v", err)
return nil, ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Invalid signature"})
}
var notifyData PayNotify
err = notifyReq.DecryptCipherTextToStruct(c.config.Pay.ApiV3Key, &notifyData)
if err != nil {
return nil, ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Invalid cipher text"})
}
log.Infof("Successfully decrypted cipher text for notify data: %+v", notifyData)
return &notifyData, nil
}
type BodyMap struct {
bm gopay.BodyMap
}

View File

@@ -3,11 +3,12 @@ package wepay
import (
"context"
"fmt"
"quyun/app/service/testx"
"testing"
"time"
"quyun/app/service/testx"
"github.com/go-pay/gopay/wechat/v3"
"github.com/go-pay/util/js"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/suite"
"go.ipao.vip/atom/contracts"
@@ -53,3 +54,22 @@ func (s *WePayTestSuite) Test_PrePay() {
})
})
}
func (s *WePayTestSuite) Test_parseNotify() {
Convey("parse notify", s.T(), func() {
Convey("prepay", func() {
content := `{"id":"43d17a94-eb1e-5641-bb11-f59e5b6e8749","summary":"支付成功","resource":{"nonce":"avbpSc2seCN5","algorithm":"AEAD_AES_256_GCM","ciphertext":"20VGA2uItmbqFvGBxBug2K3eORRyy/xYswoDA7v4+Yi2ArHnXCXzScVn6kD3ZVpKLiFY7zcTPpTxk2JFJF3vG/6WGG7uuD8DDK7keJk0PZoAfvmSPskQzieOVz3Tgmqp3SkE74mJHX1MeMZHMXMmzMJ4Mp1OmYD2YpiWsF7jlAtiGqxHSC//YlKGaJ/9r0QG4TwZcFpm+X4qkdBNX+DcSCjYeXGyWIm2bVujj63rO43DEA5x0nytdBSrpup/T85khZzNVue1EcyF5XY7PguePU3Q2o+e1c/LnoL9nN7S+n2ljm+nN3uCAhz8eqkPn4uowiq37Tw4JZ2rx2rXCb9jYKmt+I8JHpOij4SgX6oQd7fLeZHsbHC/05s0A1qdLzeF5AKgrAOQT/T1yQ+LsWTnY2ftXAP6mnqGE8Z+vQm5PGo8xsQ8AycVaAhwaRLFvn/XtwlkumfuduAojimFRSNElWwHcApnT+ekqzBrKnAvKo8hdeygf9QWHENcNWVwqwjUWIHe/fGWgJbc6u595bEHb4MkcI8ESD/6bpay/Wk6SyvZCJHqS1WWaPaU0xh9","original_type":"transaction","associated_data":"transaction"},"event_type":"TRANSACTION.SUCCESS","create_time":"2025-04-30T19:25:51+08:00","resource_type":"encrypt-resource"}`
var notifyReq wechat.V3NotifyReq
err := js.UnmarshalBytes([]byte(content), &notifyReq)
So(err, ShouldBeNil)
s.T().Logf("notifyReq: %+v", notifyReq)
var obj struct{}
err = notifyReq.DecryptCipherTextToStruct("5UBDkxVDY44AKafkqN6YgYxgtkXP6Mw6", &obj)
So(err, ShouldBeNil)
s.T().Logf("Decrypted object: %+v", obj)
})
})
}