122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/sdk/plugin/dctw/v1/api/card"
|
|
"gitea.cdlsxd.cn/sdk/plugin/dctw/v1/core"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type Config struct {
|
|
AppId string `json:"app_id" validate:"required"`
|
|
AppKey string `json:"app_key" validate:"required"`
|
|
BaseUri string `json:"base_uri" validate:"required"`
|
|
NotifyUrl string `json:"notify_url" validate:"required"`
|
|
MerchantId int64 `json:"merchant_id" validate:"required"`
|
|
}
|
|
|
|
func (c *Config) validate() error {
|
|
err := validator.New().Struct(c)
|
|
if err != nil {
|
|
for _, err = range err.(validator.ValidationErrors) {
|
|
return fmt.Errorf("配置参数有误:" + err.Error())
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func transConfig(config []byte) (*Config, error) {
|
|
var c Config
|
|
err := json.Unmarshal(config, &c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = c.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
func (c *Config) dctW() (*card.Card, error) {
|
|
server, err := core.NewDctWServer(
|
|
&core.DctWConfig{
|
|
AppId: c.AppId,
|
|
AppKey: c.AppKey,
|
|
BaseUri: c.BaseUri,
|
|
},
|
|
core.WithDebug(false),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &card.Card{DctWServer: server}, nil
|
|
}
|
|
|
|
func (c *Config) orderReq(in *proto.OrderRequest) *card.Order {
|
|
return &card.Order{
|
|
Number: in.Order.Quantity,
|
|
MerchantId: c.MerchantId,
|
|
OutTradeNo: in.Order.OrderNo,
|
|
ProductId: in.Product.ProductNo,
|
|
Mobile: in.Order.Account,
|
|
NotifyUrl: c.NotifyUrl,
|
|
Version: "1.0",
|
|
}
|
|
}
|
|
|
|
func orderResp(request *proto.OrderRequest, resp *card.OrderResp) *proto.OrderResponse {
|
|
data, _ := json.Marshal(resp)
|
|
return &proto.OrderResponse{
|
|
Result: &proto.Result{
|
|
Status: proto.Status_ING,
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: resp.TradeNo,
|
|
Message: resp.Message,
|
|
Data: data,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Config) queryReq(in *proto.QueryRequest) *card.Query {
|
|
return &card.Query{
|
|
MerchantId: c.MerchantId,
|
|
OutTradeNo: in.Order.OrderNo,
|
|
Version: "1.0",
|
|
}
|
|
}
|
|
|
|
func queryResp(request *proto.QueryRequest, resp *card.QueryResp, cardCode *card.CardCode) (*proto.QueryResponse, error) {
|
|
data, _ := json.Marshal(resp)
|
|
extra, _ := cardCode.Json()
|
|
pb := &proto.QueryResponse{
|
|
Result: &proto.Result{
|
|
Status: resp.Status.GetOrderStatus(),
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: request.Order.TradeNo,
|
|
Message: resp.Message,
|
|
Data: data,
|
|
Extra: extra,
|
|
},
|
|
}
|
|
return pb, nil
|
|
}
|
|
|
|
func notifyResp(resp *card.Notify, cardCode *card.CardCode) (*proto.NotifyResponse, error) {
|
|
data, _ := json.Marshal(resp)
|
|
extra, _ := cardCode.Json()
|
|
pb := &proto.NotifyResponse{
|
|
Result: &proto.Result{
|
|
Status: resp.Status.GetOrderStatus(),
|
|
OrderNo: resp.OutTradeNo,
|
|
TradeNo: resp.TradeNo,
|
|
Message: resp.Status.GetOrderStatusText(),
|
|
Data: data,
|
|
Extra: extra,
|
|
},
|
|
Return: "success",
|
|
}
|
|
return pb, nil
|
|
}
|