plugin/dctw/v1/api/card/trans.go

152 lines
3.4 KiB
Go
Raw Normal View History

2024-11-14 18:33:56 +08:00
package card
import (
"encoding/json"
"fmt"
2024-11-15 14:27:58 +08:00
"gitea.cdlsxd.cn/sdk/plugin/utils"
2024-11-14 18:33:56 +08:00
"github.com/go-playground/validator/v10"
2024-11-15 14:27:58 +08:00
"strings"
2024-11-14 18:33:56 +08:00
)
type Order struct {
Number uint32 `validate:"required" json:"number"`
MerchantId string `validate:"required" json:"merchantId"`
OutTradeNo string `validate:"required" json:"outTradeNo"`
ProductId string `validate:"required" json:"productId"`
Mobile string `json:"mobile"`
NotifyUrl string `validate:"required" json:"notifyUrl"`
Version string `validate:"required" json:"version"`
}
type OrderResp struct {
2024-11-15 14:27:58 +08:00
Code json.Number `json:"code"` // 不能直接定义值对象因为单独定义类型type code json.Number类型无法jsonUnmarshal
2024-11-15 10:34:36 +08:00
Message string `json:"message"`
TradeNo string `json:"tradeNo"`
2024-11-14 18:33:56 +08:00
}
type Query struct {
MerchantId string `validate:"required" json:"merchantId"`
OutTradeNo string `validate:"required" json:"outTradeNo"`
Version string `validate:"required" json:"version"`
}
type QueryResp struct {
2024-11-15 10:34:36 +08:00
Code json.Number `json:"code"`
2024-11-14 18:33:56 +08:00
Status OrderStatus `json:"status"`
Message string `json:"message"`
OutTradeNo string `json:"outTradeNo"`
CardCode string `json:"cardCode"`
}
type Notify struct {
MerchantId int `json:"merchantId"`
OutTradeNo string `json:"outTradeNo"`
2024-11-15 14:27:58 +08:00
TradeNo string `json:"tradeNo"`
2024-11-14 18:33:56 +08:00
RechargeAccount string `json:"rechargeAccount"`
Status OrderStatus `json:"status"`
CardCode string `json:"cardCode"`
}
2024-11-15 14:27:58 +08:00
type CardCode struct {
Number string `json:"number"`
Password string `json:"password"`
}
2024-11-14 18:33:56 +08:00
func (o *Order) Json() ([]byte, error) {
b, err := json.Marshal(o)
if err != nil {
return nil, err
}
return b, nil
}
func (o *Order) Validate() error {
err := validator.New().Struct(o)
if err != nil {
for _, err = range err.(validator.ValidationErrors) {
return fmt.Errorf("参数有误:" + err.Error())
}
}
return nil
}
func (o *Query) Json() ([]byte, error) {
b, err := json.Marshal(o)
if err != nil {
return nil, err
}
return b, nil
}
func (o *Query) Validate() error {
err := validator.New().Struct(o)
if err != nil {
for _, err = range err.(validator.ValidationErrors) {
return fmt.Errorf("参数有误:" + err.Error())
}
}
return nil
}
func (o *Notify) Json() ([]byte, error) {
b, err := json.Marshal(o)
if err != nil {
return nil, err
}
return b, nil
}
func (o *Notify) Validate() error {
err := validator.New().Struct(o)
if err != nil {
for _, err = range err.(validator.ValidationErrors) {
return fmt.Errorf("参数有误:" + err.Error())
}
}
return nil
}
2024-11-15 10:34:36 +08:00
2024-11-15 14:27:58 +08:00
func (o *Notify) Decode(appKey string) (*CardCode, error) {
car := &CardCode{}
2024-11-15 14:47:01 +08:00
return car.Decode(o.CardCode, appKey)
2024-11-15 14:27:58 +08:00
}
2024-11-15 14:29:41 +08:00
func (o *CardCode) Json() ([]byte, error) {
b, err := json.Marshal(o)
if err != nil {
return nil, err
}
return b, nil
}
2024-11-15 14:47:01 +08:00
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
}
2024-11-15 10:34:36 +08:00
func (o *OrderResp) GetCode() Code {
return Code(o.Code)
}
func (o *QueryResp) GetCode() Code {
return Code(o.Code)
}
2024-11-15 14:27:58 +08:00
func (o *QueryResp) Decode(appKey string) (*CardCode, error) {
car := &CardCode{}
2024-11-15 14:47:01 +08:00
return car.Decode(o.CardCode, appKey)
2024-11-15 14:27:58 +08:00
}