插件-微信立减金,回调验签,回调是直连天下那边

This commit is contained in:
李子铭 2024-08-19 17:49:13 +08:00
parent 512976c0c2
commit b78c50e72e
5 changed files with 70 additions and 21 deletions

View File

@ -11,8 +11,8 @@ import (
"plugins/weixin_cpn/internal/vo" "plugins/weixin_cpn/internal/vo"
) )
func transConfig(config []byte) (*weixin.Config, error) { func transConfig(config []byte) (*weixin.Server, error) {
var c weixin.Config var c weixin.Server
err := json.Unmarshal(config, &c) err := json.Unmarshal(config, &c)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -6,7 +6,7 @@ import (
"plugins/utils/weixin" "plugins/utils/weixin"
) )
func getApiSrv(ctx context.Context, c *weixin.Config) (*cashcoupons.CouponApiService, error) { func getApiSrv(ctx context.Context, c *weixin.Server) (*cashcoupons.CouponApiService, error) {
client, err := weixin.New(ctx, c) client, err := weixin.New(ctx, c)
if err != nil { if err != nil {
return nil, err return nil, err
@ -14,3 +14,11 @@ func getApiSrv(ctx context.Context, c *weixin.Config) (*cashcoupons.CouponApiSer
svc := cashcoupons.CouponApiService{Client: client} svc := cashcoupons.CouponApiService{Client: client}
return &svc, nil return &svc, nil
} }
func verify(ctx context.Context, c *weixin.Server) error {
err := c.Verify(ctx, "", "")
if err != nil {
return err
}
return nil
}

View File

@ -59,6 +59,13 @@ func (p *WeiXinCpnService) Query(ctx context.Context, request *proto.QueryReques
return queryResp(request, resp), nil return queryResp(request, resp), nil
} }
func (p *WeiXinCpnService) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { func (p *WeiXinCpnService) Notify(ctx context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
config, err := transConfig(request.Config)
if err != nil {
return nil, err
}
if err = verify(ctx, config); err != nil {
return nil, err
}
return notifyResp(), nil return notifyResp(), nil
} }

View File

@ -13,7 +13,7 @@ import (
var server = &WeiXinCpnService{} var server = &WeiXinCpnService{}
func config() []byte { func config() []byte {
c := &weixin.Config{ c := &weixin.Server{
MchID: "1605446142", MchID: "1605446142",
MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76", MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76",
MchAPIv3Key: "ChengDuLanSeXiongDi1234567890123", MchAPIv3Key: "ChengDuLanSeXiongDi1234567890123",
@ -74,7 +74,33 @@ func TestNotify(t *testing.T) {
Config: config(), Config: config(),
Queries: []byte(``), Queries: []byte(``),
Headers: []byte(``), Headers: []byte(``),
Body: []byte(``), Body: []byte(`{
"id": "240fd59e-13a3-5e8b-84a8-6608d535f606",
"create_time": "2024-08-19T17:29:15+08:00",
"resource_type": "encrypt-resource",
"event_type": "COUPON.USE",
"summary": "代金券核销通知",
"original_type": "coupon",
"associated_data": "coupon",
"plain_text": {
"stock_creator_mchid": "1652465541",
"stock_id": "19345605",
"coupon_id": "71916331907",
"coupon_name": "三湘银行200元立减",
"description": "",
"status": "USED",
"create_time": "2024-08-19T17:28:11+08:00",
"coupon_type": "NORMAL",
"no_cash": false,
"singleitem": false,
"consume_information": {
"consume_time": "2024-08-19T17:29:15+08:00",
"consume_mchid": "1319713401",
"transaction_id": "4200002399202408192628783906"
}
}
}
`),
} }
t.Run("TestNotify", func(t *testing.T) { t.Run("TestNotify", func(t *testing.T) {
got, err := server.Notify(context.Background(), in) got, err := server.Notify(context.Background(), in)

View File

@ -4,12 +4,14 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/auth"
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
"github.com/wechatpay-apiv3/wechatpay-go/core/option" "github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/utils" "github.com/wechatpay-apiv3/wechatpay-go/utils"
"sync" "sync"
) )
type Config struct { type Server struct {
MchID string `json:"mch_id"` MchID string `json:"mch_id"`
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"` MchCertificateSerialNumber string `json:"mch_certificate_serial_number"`
MchAPIv3Key string `json:"mch_ap_iv_3_key"` MchAPIv3Key string `json:"mch_ap_iv_3_key"`
@ -31,7 +33,22 @@ func init() {
} }
} }
func New(ctx context.Context, c *Config) (*core.Client, error) { func client(ctx context.Context, c *Server) (*core.Client, error) {
mchPrivateKey, err := utils.LoadPrivateKey(c.PrivateKey)
if err != nil {
return nil, fmt.Errorf("MchID[%s] load merchant private key error:%v", c.MchID, err)
}
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(c.MchID, c.MchCertificateSerialNumber, mchPrivateKey, c.MchAPIv3Key),
}
cl, err := core.NewClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("MchID[%s] new wechat pay client err:%v", c.MchID, err)
}
return cl, nil
}
func New(ctx context.Context, c *Server) (*core.Client, error) {
instance.mutex.Lock() instance.mutex.Lock()
defer instance.mutex.Unlock() defer instance.mutex.Unlock()
if instance.clients[c.MchID] != nil { if instance.clients[c.MchID] != nil {
@ -47,17 +64,8 @@ func New(ctx context.Context, c *Config) (*core.Client, error) {
return instance.clients[c.MchID], nil return instance.clients[c.MchID], nil
} }
func client(ctx context.Context, c *Config) (*core.Client, error) { func (s *Server) Verify(ctx context.Context, message, signature string) error {
mchPrivateKey, err := utils.LoadPrivateKey(c.PrivateKey) var verifier auth.Verifier
if err != nil { verifier = verifiers.NewSHA256WithRSAVerifier(nil)
return nil, fmt.Errorf("MchID[%s] load merchant private key error:%v", c.MchID, err) return verifier.Verify(ctx, s.MchCertificateSerialNumber, message, signature)
}
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(c.MchID, c.MchCertificateSerialNumber, mchPrivateKey, c.MchAPIv3Key),
}
cl, err := core.NewClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("MchID[%s] new wechat pay client err:%v", c.MchID, err)
}
return cl, nil
} }