From a603101cee37f7917fea30202c6bf4b4754daacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Tue, 10 Sep 2024 16:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=A1=E5=AF=86=E7=B1=BB=E5=9E=8B=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/zltx/internal/po/notify.go | 9 ++++--- plugins/zltx/internal/po/po.go | 11 +++++++++ plugins/zltx/internal/po/query.go | 1 + plugins/zltx/internal/transform.go | 39 +++++++++++++++++++++++++++--- plugins/zltx/internal/util.go | 28 +++++++++++++++++++++ plugins/zltx/internal/zltx.go | 4 +-- 6 files changed, 82 insertions(+), 10 deletions(-) diff --git a/plugins/zltx/internal/po/notify.go b/plugins/zltx/internal/po/notify.go index 153ee77..7a4b2d3 100644 --- a/plugins/zltx/internal/po/notify.go +++ b/plugins/zltx/internal/po/notify.go @@ -3,9 +3,10 @@ package po import "plugins/zltx/internal/vo" type Notify struct { - MerchantId int `json:"merchantId" validate:"required"` - OutTradeNo string `json:"outTradeNo" validate:"required"` - RechargeAccount string `json:"rechargeAccount" validate:"required"` - Status vo.OrderStatus `json:"status" validate:"required"` + MerchantId int `json:"merchantId"` + OutTradeNo string `json:"outTradeNo"` + RechargeAccount string `json:"rechargeAccount"` + Status vo.OrderStatus `json:"status"` + CardCode string `json:"cardCode"` // 卡密才可能会有 Sign string `json:"sign" validate:"required"` } diff --git a/plugins/zltx/internal/po/po.go b/plugins/zltx/internal/po/po.go index 27f0736..3a6ece0 100644 --- a/plugins/zltx/internal/po/po.go +++ b/plugins/zltx/internal/po/po.go @@ -1,11 +1,22 @@ package po import ( + "encoding/json" "fmt" "github.com/go-playground/validator/v10" "plugins/zltx/internal/vo" ) +type Card struct { + Number string `json:"number"` + Password string `json:"password"` +} + +func (c *Card) ToString() string { + bytes, _ := json.Marshal(c) + return string(bytes) +} + type Req interface { Validate() error } diff --git a/plugins/zltx/internal/po/query.go b/plugins/zltx/internal/po/query.go index 87b5ead..f165ca2 100644 --- a/plugins/zltx/internal/po/query.go +++ b/plugins/zltx/internal/po/query.go @@ -15,4 +15,5 @@ type QueryResp struct { Status vo.OrderStatus `json:"status"` Message string `json:"message"` OutTradeNo string `json:"outTradeNo"` + CardCode string `json:"cardCode"` // 卡密才可能会有 } diff --git a/plugins/zltx/internal/transform.go b/plugins/zltx/internal/transform.go index 21abd52..e42d54e 100644 --- a/plugins/zltx/internal/transform.go +++ b/plugins/zltx/internal/transform.go @@ -4,6 +4,7 @@ import ( "encoding/json" "gitea.cdlsxd.cn/sdk/plugin/proto" "plugins/zltx/internal/po" + "strings" "time" ) @@ -59,28 +60,58 @@ func (c *Config) queryReq(in *proto.QueryRequest) *po.QueryReq { } } -func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryResponse { +func queryResp(request *proto.QueryRequest, resp po.QueryResp, appKey string) (*proto.QueryResponse, error) { data, _ := json.Marshal(resp) - return &proto.QueryResponse{ + pb := &proto.QueryResponse{ Result: &proto.Result{ Status: resp.Status.GetOrderStatus(), OrderNo: request.Order.OrderNo, TradeNo: request.Order.TradeNo, Message: resp.Message, Data: data, + Extra: nil, }, } + if resp.CardCode != "" { + s, err := cardCodeDecode(resp.CardCode, []byte(appKey)) + if err != nil { + return nil, err + } + parts := strings.Split(s, "_") + car := &po.Card{ + Number: parts[0], + Password: parts[1], + } + b, _ := json.Marshal(car) + pb.Result.Extra = b + } + return pb, nil } -func notifyResp(in po.Notify) *proto.NotifyResponse { +func notifyResp(in po.Notify, appKey string) (*proto.NotifyResponse, error) { data, _ := json.Marshal(in) - return &proto.NotifyResponse{ + pb := &proto.NotifyResponse{ Result: &proto.Result{ Status: in.Status.GetOrderStatus(), OrderNo: in.OutTradeNo, TradeNo: "", Message: "", Data: data, + Extra: nil, }, } + if in.CardCode != "" { + s, err := cardCodeDecode(in.CardCode, []byte(appKey)) + if err != nil { + return nil, err + } + parts := strings.Split(s, "_") + car := &po.Card{ + Number: parts[0], + Password: parts[1], + } + b, _ := json.Marshal(car) + pb.Result.Extra = b + } + return pb, nil } diff --git a/plugins/zltx/internal/util.go b/plugins/zltx/internal/util.go index 25b3b24..f27e113 100644 --- a/plugins/zltx/internal/util.go +++ b/plugins/zltx/internal/util.go @@ -1,8 +1,12 @@ package internal import ( + "crypto/aes" + "crypto/cipher" "crypto/md5" + "encoding/base64" "encoding/hex" + "fmt" "gitea.cdlsxd.cn/sdk/plugin/utils" "net/url" "plugins/zltx/internal/po" @@ -55,3 +59,27 @@ func verify(notify *po.Notify, appKey string) bool { return sign([]byte(signStr)) == notify.Sign } + +func cardCodeDecode(code string, key []byte) (string, error) { + decoded, err := base64.StdEncoding.DecodeString(code) + if err != nil { + return "", fmt.Errorf("base64 decode error: %v", err) + } + block, err := aes.NewCipher(key) + if err != nil { + return "", fmt.Errorf("aes new cipher error: %v", err) + } + + if len(decoded) < aes.BlockSize { + return "", fmt.Errorf("ciphertext too short") + } + + decrypted := make([]byte, len(decoded)) + mode := cipher.NewCBCDecrypter(block, make([]byte, aes.BlockSize)) + mode.CryptBlocks(decrypted, decoded) + + padding := decrypted[len(decrypted)-1] + decrypted = decrypted[:len(decrypted)-int(padding)] + + return string(decrypted), nil +} diff --git a/plugins/zltx/internal/zltx.go b/plugins/zltx/internal/zltx.go index 15e7532..75dfee3 100644 --- a/plugins/zltx/internal/zltx.go +++ b/plugins/zltx/internal/zltx.go @@ -69,7 +69,7 @@ func (p *ZLTXService) Query(ctx context.Context, request *proto.QueryRequest) (* return nil, errors.New("请求错误,msg:" + response.Message) } - return queryResp(request, response), nil + return queryResp(request, response, c.AppKey) } func (p *ZLTXService) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { @@ -91,5 +91,5 @@ func (p *ZLTXService) Notify(_ context.Context, request *proto.NotifyRequest) (* return nil, fmt.Errorf("验签不通过") } - return notifyResp(poReq), nil + return notifyResp(poReq, c.AppKey) }