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 {
return nil, err
}
var response *OrderResp
if err = json.Unmarshal(result, &response); err != nil {
return nil, err
}
return response, nil
}
@ -33,11 +35,13 @@ func (c *Card) Query(ctx context.Context, request *Query) (*QueryResp, *CardCode
if err != nil {
return nil, nil, err
}
var response *QueryResp
if err = json.Unmarshal(result, &response); err != nil {
return nil, nil, err
}
cardCode, err := response.Decode(c.Config.AppKey)
return response, cardCode, nil
}

View File

@ -59,7 +59,7 @@ func TestCard_Query(t *testing.T) {
OutTradeNo: "test_card_zltx_1",
Version: "1.0",
}
resp, err := a.Query(context.Background(), req)
resp, cardCode, err := a.Query(context.Background(), req)
if err != nil {
t.Error(err)
return
@ -69,6 +69,7 @@ func TestCard_Query(t *testing.T) {
return
}
t.Log(resp)
t.Log(cardCode)
assert.Equal(t, resp.Status.IsSuccess(), true, "充值是否成功")
}
@ -93,11 +94,12 @@ func TestCard_Notify(t *testing.T) {
Header: headers,
Body: ioutil.NopCloser(bytes.NewBuffer(body)),
}
resp, err := a.Notify(context.Background(), httpRequest)
resp, cardCode, err := a.Notify(context.Background(), httpRequest)
if err != nil {
t.Error(err)
return
}
t.Log(resp)
t.Log(cardCode)
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) {
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
return car.Decode(o.CardCode, appKey)
}
func (o *CardCode) Json() ([]byte, error) {
@ -130,6 +119,24 @@ func (o *CardCode) Json() ([]byte, error) {
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 {
return Code(o.Code)
}
@ -139,17 +146,6 @@ func (o *QueryResp) GetCode() 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
return car.Decode(o.CardCode, appKey)
}

View File

@ -173,7 +173,7 @@ func (c *DctWServer) Request(_ context.Context, request DctWRequest, path string
fmt.Printf("url:%s\n", url)
fmt.Printf("body:%s\n", string(body))
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)