108 lines
2.3 KiB
Go
108 lines
2.3 KiB
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"plugins/zltx_card/internal/po"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
AppId string `json:"app_id"`
|
|
AppKey string `json:"app_key"`
|
|
BaseUri string `json:"base_uri"`
|
|
NotifyUrl string `json:"notify_url"`
|
|
}
|
|
|
|
func transConfig(config []byte) (*Config, error) {
|
|
var c Config
|
|
err := json.Unmarshal(config, &c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
func (c *Config) orderReq(in *proto.OrderRequest) *po.OrderReq {
|
|
p := &po.OrderReq{
|
|
MerchantId: c.AppId,
|
|
OutTradeNo: in.Order.OrderNo,
|
|
ProductId: in.Product.ProductNo,
|
|
Mobile: in.Order.Account,
|
|
Number: in.Order.Quantity,
|
|
Version: "1.0",
|
|
TimeStamp: time.Now().Unix(),
|
|
NotifyUrl: c.NotifyUrl,
|
|
}
|
|
return p
|
|
}
|
|
|
|
func orderResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse {
|
|
data, _ := json.Marshal(resp)
|
|
return &proto.OrderResponse{
|
|
Result: &proto.Result{
|
|
Status: proto.Status_ING,
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: "", // 下单无该值返回
|
|
Message: resp.Message,
|
|
Data: data,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Config) queryReq(in *proto.QueryRequest) *po.QueryReq {
|
|
return &po.QueryReq{
|
|
MerchantId: c.AppId,
|
|
OutTradeNo: in.Order.OrderNo,
|
|
TimeStamp: time.Now().Unix(),
|
|
Version: "1.0",
|
|
}
|
|
}
|
|
|
|
func queryResp(request *proto.QueryRequest, resp po.QueryResp, appKey string) (*proto.QueryResponse, error) {
|
|
data, _ := json.Marshal(resp)
|
|
pb := &proto.QueryResponse{
|
|
Result: &proto.Result{
|
|
Status: resp.Status.GetOrderStatus(),
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: request.Order.TradeNo,
|
|
Message: resp.Message,
|
|
Data: data,
|
|
Extra: nil,
|
|
},
|
|
}
|
|
return pb, nil
|
|
}
|
|
|
|
func notifyResp(in po.Notify, appKey string) (*proto.NotifyResponse, error) {
|
|
data, _ := json.Marshal(in)
|
|
pb := &proto.NotifyResponse{
|
|
Result: &proto.Result{
|
|
Status: in.Status.GetOrderStatus(),
|
|
OrderNo: in.OutTradeNo,
|
|
TradeNo: "",
|
|
Message: "",
|
|
Data: data,
|
|
Extra: nil,
|
|
},
|
|
}
|
|
if in.CardCode != "" {
|
|
s, err := cardCodeDecode(in.CardCode, []byte(appKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
parts := strings.Split(s, "_")
|
|
car := &po.Card{}
|
|
if len(parts) > 1 {
|
|
car.Number = parts[0]
|
|
car.Password = parts[1]
|
|
} else {
|
|
car.Password = s
|
|
}
|
|
b, _ := json.Marshal(car)
|
|
pb.Result.Extra = b
|
|
}
|
|
return pb, nil
|
|
}
|