From eb9a1323e5352bdbd2469353656ff91cfbd2bf97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Fri, 15 Nov 2024 14:27:58 +0800 Subject: [PATCH] CardCode --- dctw/v1/api/card/card.go | 20 +++++++++-------- dctw/v1/api/card/trans.go | 44 ++++++++++++++++++++++++++++++++++--- dctw/v1/api/direct/trans.go | 1 + 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/dctw/v1/api/card/card.go b/dctw/v1/api/card/card.go index bf7b5d8..5c8f8d4 100644 --- a/dctw/v1/api/card/card.go +++ b/dctw/v1/api/card/card.go @@ -28,32 +28,34 @@ func (c *Card) Order(ctx context.Context, request *Order) (*OrderResp, error) { return response, nil } -func (c *Card) Query(ctx context.Context, request *Query) (*QueryResp, error) { +func (c *Card) Query(ctx context.Context, request *Query) (*QueryResp, *CardCode, error) { result, err := c.Request(ctx, request, queryPath) if err != nil { - return nil, err + return nil, nil, err } var response *QueryResp if err = json.Unmarshal(result, &response); err != nil { - return nil, err + return nil, nil, err } - return response, nil + cardCode, err := response.Decode(c.Config.AppKey) + return response, cardCode, nil } -func (c *Card) Notify(_ context.Context, httpRequest *http.Request) (*Notify, error) { +func (c *Card) Notify(_ context.Context, httpRequest *http.Request) (*Notify, *CardCode, error) { body, err := ioutil.ReadAll(httpRequest.Body) if err != nil { - return nil, err + return nil, nil, err } if err = c.VerifyDctW(httpRequest.Header.Get(core.SignKeyName), body); err != nil { - return nil, err + return nil, nil, err } var response *Notify if err = json.Unmarshal(body, &response); err != nil { - return nil, err + return nil, nil, err } + cardCode, err := response.Decode(c.Config.AppKey) - return response, err + return response, cardCode, err } diff --git a/dctw/v1/api/card/trans.go b/dctw/v1/api/card/trans.go index 9270ae4..7817425 100644 --- a/dctw/v1/api/card/trans.go +++ b/dctw/v1/api/card/trans.go @@ -3,7 +3,9 @@ package card import ( "encoding/json" "fmt" + "gitea.cdlsxd.cn/sdk/plugin/utils" "github.com/go-playground/validator/v10" + "strings" ) type Order struct { @@ -17,7 +19,7 @@ type Order struct { } type OrderResp struct { - Code json.Number `json:"code"` + Code json.Number `json:"code"` // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal Message string `json:"message"` TradeNo string `json:"tradeNo"` } @@ -39,11 +41,17 @@ type QueryResp struct { type Notify struct { MerchantId int `json:"merchantId"` OutTradeNo string `json:"outTradeNo"` + TradeNo string `json:"tradeNo"` RechargeAccount string `json:"rechargeAccount"` Status OrderStatus `json:"status"` CardCode string `json:"cardCode"` } +type CardCode struct { + Number string `json:"number"` + Password string `json:"password"` +} + func (o *Order) Json() ([]byte, error) { b, err := json.Marshal(o) if err != nil { @@ -98,12 +106,42 @@ func (o *Notify) Validate() error { return nil } +func (o *Notify) Decode(appKey string) (*CardCode, error) { + s, err := utils.AesDecode(o.CardCode, []byte(appKey)) + if err != nil { + return nil, err + } + parts := strings.Split(s, "_") + car := &CardCode{} + if len(parts) > 1 { + car.Number = parts[0] + car.Password = parts[1] + } else { + car.Password = s + } + return car, err +} + func (o *OrderResp) GetCode() Code { - // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal return Code(o.Code) } func (o *QueryResp) GetCode() Code { - // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal return Code(o.Code) } + +func (o *QueryResp) Decode(appKey string) (*CardCode, error) { + s, err := utils.AesDecode(o.CardCode, []byte(appKey)) + if err != nil { + return nil, err + } + parts := strings.Split(s, "_") + car := &CardCode{} + if len(parts) > 1 { + car.Number = parts[0] + car.Password = parts[1] + } else { + car.Password = s + } + return car, err +} diff --git a/dctw/v1/api/direct/trans.go b/dctw/v1/api/direct/trans.go index 421e286..2c25152 100644 --- a/dctw/v1/api/direct/trans.go +++ b/dctw/v1/api/direct/trans.go @@ -41,6 +41,7 @@ type Notify struct { MerchantId int `json:"merchantId"` OutTradeNo string `json:"outTradeNo"` RechargeAccount string `json:"rechargeAccount"` + TradeNo string `json:"tradeNo"` Status OrderStatus `json:"status"` }