This commit is contained in:
李子铭 2024-11-15 14:47:01 +08:00
parent 8102c3ffcd
commit 35ccf00466
4 changed files with 29 additions and 27 deletions

View File

@ -21,10 +21,12 @@ func (c *Card) Order(ctx context.Context, request *Order) (*OrderResp, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var response *OrderResp var response *OrderResp
if err = json.Unmarshal(result, &response); err != nil { if err = json.Unmarshal(result, &response); err != nil {
return nil, err return nil, err
} }
return response, nil return response, nil
} }
@ -33,11 +35,13 @@ func (c *Card) Query(ctx context.Context, request *Query) (*QueryResp, *CardCode
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var response *QueryResp var response *QueryResp
if err = json.Unmarshal(result, &response); err != nil { if err = json.Unmarshal(result, &response); err != nil {
return nil, nil, err return nil, nil, err
} }
cardCode, err := response.Decode(c.Config.AppKey) cardCode, err := response.Decode(c.Config.AppKey)
return response, cardCode, nil return response, cardCode, nil
} }

View File

@ -59,7 +59,7 @@ func TestCard_Query(t *testing.T) {
OutTradeNo: "test_card_zltx_1", OutTradeNo: "test_card_zltx_1",
Version: "1.0", Version: "1.0",
} }
resp, err := a.Query(context.Background(), req) resp, cardCode, err := a.Query(context.Background(), req)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -69,6 +69,7 @@ func TestCard_Query(t *testing.T) {
return return
} }
t.Log(resp) t.Log(resp)
t.Log(cardCode)
assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功") assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功")
} }
@ -93,11 +94,12 @@ func TestCard_Notify(t *testing.T) {
Header: headers, Header: headers,
Body: ioutil.NopCloser(bytes.NewBuffer(body)), Body: ioutil.NopCloser(bytes.NewBuffer(body)),
} }
resp, err := a.Notify(context.Background(), httpRequest) resp, cardCode, err := a.Notify(context.Background(), httpRequest)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
} }
t.Log(resp) t.Log(resp)
t.Log(cardCode)
assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功") assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功")
} }

View File

@ -107,19 +107,8 @@ func (o *Notify) Validate() error {
} }
func (o *Notify) Decode(appKey string) (*CardCode, error) { 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{} car := &CardCode{}
if len(parts) > 1 { return car.Decode(o.CardCode, appKey)
car.Number = parts[0]
car.Password = parts[1]
} else {
car.Password = s
}
return car, err
} }
func (o *CardCode) Json() ([]byte, error) { func (o *CardCode) Json() ([]byte, error) {
@ -130,6 +119,24 @@ func (o *CardCode) Json() ([]byte, error) {
return b, nil return b, nil
} }
func (o *CardCode) Decode(carCode, appKey string) (*CardCode, error) {
if carCode == "" {
return o, nil
}
s, err := utils.AesDecode(carCode, []byte(appKey))
if err != nil {
return o, nil
}
parts := strings.Split(s, "_")
if len(parts) > 1 {
o.Number = parts[0]
o.Password = parts[1]
} else {
o.Password = s
}
return o, err
}
func (o *OrderResp) GetCode() Code { func (o *OrderResp) GetCode() Code {
return Code(o.Code) return Code(o.Code)
} }
@ -139,17 +146,6 @@ func (o *QueryResp) GetCode() Code {
} }
func (o *QueryResp) Decode(appKey string) (*CardCode, error) { 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{} car := &CardCode{}
if len(parts) > 1 { return car.Decode(o.CardCode, appKey)
car.Number = parts[0]
car.Password = parts[1]
} else {
car.Password = s
}
return car, err
} }

View File

@ -173,7 +173,7 @@ func (c *DctWServer) Request(_ context.Context, request DctWRequest, path string
fmt.Printf("url:%s\n", url) fmt.Printf("url:%s\n", url)
fmt.Printf("body:%s\n", string(body)) fmt.Printf("body:%s\n", string(body))
fmt.Printf("signStr:%s\n", signStr) fmt.Printf("signStr:%s\n", signStr)
fmt.Printf("Header:%+v\n", req.Header) fmt.Printf("Headers:%+v\n", req.Header)
} }
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)