152 lines
3.4 KiB
Go
152 lines
3.4 KiB
Go
package card
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"gitea.cdlsxd.cn/sdk/plugin/utils"
|
||
"github.com/go-playground/validator/v10"
|
||
"strings"
|
||
)
|
||
|
||
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 {
|
||
Code json.Number `json:"code"` // 不能直接定义值对象,因为单独定义类型type code json.Number类型无法jsonUnmarshal
|
||
Message string `json:"message"`
|
||
TradeNo string `json:"tradeNo"`
|
||
}
|
||
|
||
type Query struct {
|
||
MerchantId string `validate:"required" json:"merchantId"`
|
||
OutTradeNo string `validate:"required" json:"outTradeNo"`
|
||
Version string `validate:"required" json:"version"`
|
||
}
|
||
|
||
type QueryResp struct {
|
||
Code json.Number `json:"code"`
|
||
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"`
|
||
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 {
|
||
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
|
||
}
|
||
|
||
func (o *Notify) Decode(appKey string) (*CardCode, error) {
|
||
car := &CardCode{}
|
||
return car.Decode(o.CardCode, appKey)
|
||
}
|
||
|
||
func (o *CardCode) Json() ([]byte, error) {
|
||
b, err := json.Marshal(o)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
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)
|
||
}
|
||
|
||
func (o *QueryResp) GetCode() Code {
|
||
return Code(o.Code)
|
||
}
|
||
|
||
func (o *QueryResp) Decode(appKey string) (*CardCode, error) {
|
||
car := &CardCode{}
|
||
return car.Decode(o.CardCode, appKey)
|
||
}
|