135 lines
3.8 KiB
Go
135 lines
3.8 KiB
Go
package internal
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
||
"plugins/union_pay_redpack/internal/po"
|
||
"plugins/union_pay_redpack/internal/vo"
|
||
"plugins/utils/union_pay"
|
||
)
|
||
|
||
type Config struct {
|
||
AppId string `json:"app_id"`
|
||
ChNlId string `json:"chnlId"` // 渠道方代码
|
||
IV string `json:"iv"` // 加密密钥
|
||
KEY string `json:"key"` // 加密密钥
|
||
Prk string `json:"prk"` // 私钥
|
||
Npk string `json:"npk"` // 回调公钥
|
||
|
||
PointId string `json:"point_id"` // 积分ID-积分类别代码,41开头的16位数字
|
||
InsAcctId string `json:"ins_acct_id"` // 机构账户代码
|
||
}
|
||
|
||
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, error) {
|
||
type OrderExtra struct {
|
||
TransDtTm string `json:"transDtTm"` // 交易日期时间
|
||
ValidEndDtTm string `json:"validEndDtTm"` // 有效截止时间
|
||
TransDigest string `json:"transDigest"` // 交易摘要
|
||
}
|
||
var e OrderExtra
|
||
err := json.Unmarshal(in.Order.Extra, &e)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("order extra json unmarshal error: %v", err)
|
||
}
|
||
|
||
mobile, err := union_pay.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &po.OrderReq{
|
||
ChNlId: c.ChNlId,
|
||
AccessId: vo.AccessId,
|
||
TransSeq: in.Order.OrderNo,
|
||
TransDtTm: e.TransDtTm,
|
||
InsAcctTp: vo.InsAcctTp,
|
||
InsAcctId: c.InsAcctId,
|
||
AcctEntityTp: vo.AcctEntityTp,
|
||
Mobile: mobile,
|
||
PointTp: vo.PointTp,
|
||
PointId: c.PointId,
|
||
PointAt: fmt.Sprintf("%f", in.Order.Amount*100),
|
||
DelayIn: vo.DelayIn,
|
||
TempIn: vo.TempIn,
|
||
InOutTransFlag: vo.InOutTransFlag,
|
||
TransDigest: e.TransDigest,
|
||
ValidEndDtTm: e.ValidEndDtTm,
|
||
}, nil
|
||
}
|
||
|
||
func orderResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse {
|
||
data, _ := json.Marshal(resp)
|
||
result := &proto.Result{
|
||
OrderNo: request.Order.OrderNo,
|
||
TradeNo: "",
|
||
Message: resp.Msg,
|
||
Data: data,
|
||
}
|
||
if !resp.Code.IsSuccess() {
|
||
result.Status = proto.Status_FAIL
|
||
result.Message = resp.GetMsg()
|
||
} else {
|
||
result.Status = proto.Status_ING
|
||
}
|
||
return &proto.OrderResponse{Result: result}
|
||
}
|
||
|
||
func (c *Config) queryReq(in *proto.QueryRequest, chNlId string) (*po.QueryReq, error) {
|
||
type OrderExtra struct {
|
||
TransSeq string `json:"transSeq"` // 当前交易号
|
||
TransDtTm string `json:"transDtTm"` // 交易日期时间
|
||
OrigTransDtTm string `json:"origTransDtTm"` // 原交易日期时间
|
||
}
|
||
var e OrderExtra
|
||
err := json.Unmarshal(in.Order.Extra, &e)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("order extra json unmarshal error: %v", err)
|
||
}
|
||
mobile, err := union_pay.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &po.QueryReq{
|
||
ChNlId: chNlId,
|
||
TransSeq: e.TransSeq, // 当前交易号
|
||
TransDtTm: e.TransDtTm, // 交易日期时间
|
||
OrigTransDtTm: e.OrigTransDtTm, // 原交易流水号
|
||
OrigTransSeq: in.Order.OrderNo, // 原交易流水号
|
||
AcctEntityTp: vo.AcctEntityTp,
|
||
Mobile: mobile,
|
||
}, nil
|
||
}
|
||
|
||
func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryResponse {
|
||
data, _ := json.Marshal(resp)
|
||
result := &proto.Result{
|
||
OrderNo: request.Order.OrderNo,
|
||
TradeNo: request.Order.TradeNo,
|
||
Status: resp.AcquireSt.GetOrderStatus(),
|
||
Message: resp.GetMsg(),
|
||
Data: data,
|
||
}
|
||
return &proto.QueryResponse{Result: result}
|
||
}
|
||
|
||
func notifyResp() *proto.NotifyResponse {
|
||
result := &proto.Result{
|
||
Status: proto.Status_ING,
|
||
OrderNo: "",
|
||
TradeNo: "",
|
||
Message: "无回调通知",
|
||
Data: nil,
|
||
Extra: nil,
|
||
}
|
||
return &proto.NotifyResponse{Result: result}
|
||
}
|