This commit is contained in:
李子铭 2024-11-15 14:27:58 +08:00
parent 1b3162df5d
commit eb9a1323e5
3 changed files with 53 additions and 12 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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"`
}