150 lines
3.6 KiB
Go
150 lines
3.6 KiB
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"github.com/go-playground/validator/v10"
|
|
"html"
|
|
"plugins/alipay_cpn/internal/po"
|
|
"plugins/alipay_cpn/internal/vo"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
AppId string `validate:"required" json:"app_id"`
|
|
Prk string `validate:"required" json:"prk"` // 私钥
|
|
Npk string `validate:"required" json:"npk"` // 回调公钥
|
|
}
|
|
|
|
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) paramReq(req po.Req, method string) (*po.Param, error) {
|
|
if err := req.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &po.Param{
|
|
Timestamp: time.Now().Format(time.DateTime),
|
|
Version: vo.Version,
|
|
AppId: c.AppId,
|
|
Method: method,
|
|
Format: vo.Format,
|
|
Charset: vo.Charset,
|
|
SignType: vo.SignType,
|
|
BizContent: string(req.ToJson()),
|
|
Sign: "",
|
|
}, nil
|
|
}
|
|
|
|
func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (*po.OrderReq, error) {
|
|
type Extra struct {
|
|
LogonId string `json:"logon_id"`
|
|
PhoneId string `json:"phone_id"`
|
|
}
|
|
var extra Extra
|
|
if order.Extra != nil {
|
|
err := json.Unmarshal(order.Extra, &extra)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("order拓展参数 json unmarshal error: %v", err)
|
|
}
|
|
}
|
|
o := &po.OrderReq{
|
|
UserId: order.Account,
|
|
LogonId: extra.LogonId,
|
|
PhoneId: extra.PhoneId,
|
|
OutBizNo: order.OrderNo,
|
|
ActivityId: product.ProductNo,
|
|
}
|
|
return o, nil
|
|
}
|
|
|
|
func orderResp(request *proto.OrderRequest, resp *po.OrderResp) *proto.OrderResponse {
|
|
data, _ := json.Marshal(resp)
|
|
return &proto.OrderResponse{
|
|
Result: &proto.Result{
|
|
Status: resp.GetOrderStatus(),
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: resp.Response.VoucherId,
|
|
Message: resp.GetMsg(),
|
|
Data: data,
|
|
},
|
|
}
|
|
}
|
|
|
|
func queryReq(in *proto.QueryRequest_Order) (*po.QueryReq, error) {
|
|
type Extra struct {
|
|
LogonId string `json:"logon_id"`
|
|
PhoneId string `json:"phone_id"`
|
|
ActivityID string `json:"activity_id"`
|
|
}
|
|
var extra Extra
|
|
if in.Extra != nil {
|
|
err := json.Unmarshal(in.Extra, &extra)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("order拓展参数 json unmarshal error: %v", err)
|
|
}
|
|
}
|
|
return &po.QueryReq{
|
|
ActivityOrderId: in.TradeNo,
|
|
OutBizNo: in.OrderNo,
|
|
UserId: in.Account,
|
|
LogonId: extra.LogonId,
|
|
PhoneId: extra.PhoneId,
|
|
ActivityID: extra.ActivityID,
|
|
}, nil
|
|
}
|
|
|
|
func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryResponse {
|
|
data, _ := json.Marshal(resp)
|
|
return &proto.QueryResponse{
|
|
Result: &proto.Result{
|
|
Status: resp.Data.Status.GetOrderStatus(),
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: request.Order.TradeNo,
|
|
Message: resp.Data.Msg,
|
|
Data: data,
|
|
},
|
|
}
|
|
}
|
|
|
|
func notifyReq(in *proto.NotifyRequest) *po.Notify {
|
|
var n *po.Notify
|
|
_ = json.Unmarshal(in.Queries, &n)
|
|
n.BizContent = html.UnescapeString(n.BizContent)
|
|
return n
|
|
}
|
|
|
|
func notifyResp(n *po.Notify) *proto.NotifyResponse {
|
|
var b po.NotifyBizContent
|
|
_ = json.Unmarshal([]byte(n.BizContent), &b)
|
|
return &proto.NotifyResponse{
|
|
Result: &proto.Result{
|
|
Status: b.BizType.GetOrderStatus(),
|
|
OrderNo: b.OrderID,
|
|
TradeNo: b.VoucherID,
|
|
Message: fmt.Sprintf("%s, %s", b.BizType.GetText(), b.VoucherStatus.GetText()),
|
|
},
|
|
Return: "success",
|
|
}
|
|
}
|