feat: update pay notify
This commit is contained in:
@@ -2,7 +2,6 @@ package cmux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"quyun/providers/grpc"
|
||||
"quyun/providers/http"
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package cmux
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"quyun/providers/grpc"
|
||||
"quyun/providers/http"
|
||||
|
||||
|
||||
@@ -2,9 +2,8 @@ package job
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"quyun/providers/postgres"
|
||||
"sync"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
@@ -5,12 +5,11 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"quyun/providers/req/cookiejar"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"quyun/providers/req/cookiejar"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/opt"
|
||||
|
||||
@@ -5,12 +5,11 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"quyun/pkg/oauth"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"quyun/pkg/oauth"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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, ¬ifyData)
|
||||
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 ¬ifyData, nil
|
||||
}
|
||||
|
||||
type BodyMap struct {
|
||||
bm gopay.BodyMap
|
||||
}
|
||||
|
||||
@@ -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), ¬ifyReq)
|
||||
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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user