From 507e120fc31e52800947dbcdd800ec32f6abe293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Tue, 7 Jan 2025 10:59:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/zltx_v2/internal/zltx_v2.go | 21 +++++++------ plugins/zltx_v2/internal/zltx_v2_test.go | 38 +++++------------------- 2 files changed, 18 insertions(+), 41 deletions(-) diff --git a/plugins/zltx_v2/internal/zltx_v2.go b/plugins/zltx_v2/internal/zltx_v2.go index b1bd87d..f6c4aa0 100644 --- a/plugins/zltx_v2/internal/zltx_v2.go +++ b/plugins/zltx_v2/internal/zltx_v2.go @@ -10,7 +10,6 @@ import ( "gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify" "io" "net/http" - "strings" ) // 插件通信信息,若不对应则会报错panic @@ -28,14 +27,17 @@ func (p *ZLTXV2Service) Order(ctx context.Context, request *proto.OrderRequest) if err != nil { return nil, err } + client, err := c.client() if err != nil { return nil, err } + response, errResp, err := api.OrderCreate(ctx, client, c.AppId, c.orderReq(request)) if err != nil { return nil, proto.ErrorRequestFail(fmt.Sprintf("请求异常[%v]", err)) } + if errResp != nil { return nil, proto.ErrorRequestFail(fmt.Sprintf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message)) } @@ -48,6 +50,7 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest) if err != nil { return nil, err } + client, err := c.client() if err != nil { return nil, err @@ -57,6 +60,7 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest) if err != nil { return nil, proto.ErrorRequestFail(fmt.Sprintf("请求异常[%v]", err)) } + if errResp != nil { return nil, proto.ErrorRequestFail(fmt.Sprintf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message)) } @@ -65,26 +69,21 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest) } func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { - req := &http.Request{ - Header: nil, - Body: nil, - } httpHeaders := make(http.Header) if err := json.Unmarshal(request.Headers, &httpHeaders); err != nil { return nil, proto.ErrorParamFail(fmt.Sprintf("headers Unmarshal err [%v]", err)) } - newHeaders := make(http.Header) - for key, values := range httpHeaders { - newKey := strings.ToTitle(strings.ToLower(key)) - newHeaders[newKey] = values + + req := &http.Request{ + Header: httpHeaders, + Body: io.NopCloser(bytes.NewBuffer(request.Body)), } - req.Header = newHeaders - req.Body = io.NopCloser(bytes.NewBuffer(request.Body)) c, err := transConfig(request.Config) if err != nil { return nil, err } + n, err := notify.NewNotify(c.AppId, c.AppKey).ParseAndVerify(req) if err != nil { return nil, proto.ErrorSignFail(err.Error()) diff --git a/plugins/zltx_v2/internal/zltx_v2_test.go b/plugins/zltx_v2/internal/zltx_v2_test.go index b78f4f1..4d70d32 100644 --- a/plugins/zltx_v2/internal/zltx_v2_test.go +++ b/plugins/zltx_v2/internal/zltx_v2_test.go @@ -6,7 +6,6 @@ import ( "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" "github.com/stretchr/testify/assert" - "google.golang.org/grpc/metadata" "net/http" "testing" ) @@ -36,7 +35,7 @@ func TestOrder(t *testing.T) { request := &proto.OrderRequest{ Config: config(), Order: &proto.OrderRequest_Order{ - OrderNo: "test_zltx_v2_6", + OrderNo: "test_zltx_v2_7", Account: "18666173766", Quantity: 1, Extra: nil, @@ -62,8 +61,8 @@ func TestQuery(t *testing.T) { request := &proto.QueryRequest{ Config: config(), Order: &proto.QueryRequest_Order{ - OrderNo: "test_zltx_v2_3", - TradeNo: "", + OrderNo: "test_zltx_v2_7", + TradeNo: "", // 731095414466813953 Account: "", Extra: nil, }, @@ -80,39 +79,18 @@ func TestQuery(t *testing.T) { } func TestNotify(t *testing.T) { - jsonData := []byte(`{ - "tradeStatus": "SUCCESS", - "orderNo": "C2081410801687044096", - "tradeStateDesc": "成功", - "cards": [ - {"no":"*","pwd":"*","deadline":"2024-07-03 23:59:59","cardType":1} - ], - "mchId": 23329, - "outTradeNo": "20240802212326527830", - "rechargeAccount": "17384082748", - "unitPrice":102.5 - }`) - md := metadata.New(map[string]string{ - "Authorization": "292e21f3683219369cf68dede0d45730", - "Content-Type": "application/json", - }) - ctx := metadata.NewIncomingContext(context.Background(), md) - headerData, ok := metadata.FromIncomingContext(ctx) - if !ok { - t.Error("无法获取 Header 数据") - return - } + bodyBytes := []byte(`{"tradeStatus":"SUCCESS","orderNo":"731095414466813953","tradeStateDesc":"成功","mchId":23329,"outTradeNo":"test_zltx_v2_7","rechargeAccount":"18666173766","unitPrice":0.1,"attach":""}`) + headerBytes := []byte(`{"Accept-Encoding":["gzip"],"Authorization":["39859e5d71a1e7a9f69a6d00cfe79a7a"],"Connection":["close"],"Content-Length":["186"],"Content-Type":["application/json"],"User-Agent":["GuzzleHttp/6.5.5 curl/7.69.1 PHP/7.2.34"],"X-Envoy-Attempt-Count":["1"],"X-Envoy-External-Address":["120.79.35.82"],"X-Forwarded-Client-Cert":["By=spiffe://cluster.local/ns/gopro/sa/default;Hash=7e17458f89c270f74ca6039bfd8c091de2924d2f95f3e415f9c6f95e67932798;Subject=\"\";URI=spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-go"],"X-Forwarded-For":["120.79.35.82, 112.124.3.46"],"X-Forwarded-Proto":["https"],"X-Real-Ip":["112.124.3.46"],"X-Remoteaddr":["172.21.0.1"],"X-Request-Id":["69a03321-e633-466b-808e-7e822232cf12"]}`) - HeaderBytes, _ := json.Marshal(headerData) in := &proto.NotifyRequest{ Config: config(), Queries: nil, - Headers: HeaderBytes, - Body: jsonData, + Headers: headerBytes, + Body: bodyBytes, } t.Run("TestNotify", func(t *testing.T) { - got, err := zltx.Notify(ctx, in) + got, err := zltx.Notify(context.Background(), in) if !assert.Nil(t, err) { t.Errorf("Notify() error = %v", err) return