plugins/plugins/alipay_redpack/internal/transform.go

148 lines
3.6 KiB
Go

package internal
import (
"encoding/json"
"fmt"
"gitea.cdlsxd.cn/BaseSystem/plugin/proto"
"html"
"plugins/alipay_redpack/internal/po"
"plugins/alipay_redpack/internal/vo"
"plugins/utils/alipay"
"time"
)
type Config struct {
AppId string `json:"app_id"`
BaseUri string `json:"base_uri"`
Prk string `json:"prk"` // 私钥
PukPath string `json:"npk"` // 验签公钥
MchCertPath string `json:"mch_cert_path"`
RootCertPath string `json:"root_cert_path"`
}
func transConfig(config []byte) (*Config, error) {
var c Config
err := json.Unmarshal(config, &c)
if err != nil {
return nil, err
}
if c.BaseUri == "" {
c.BaseUri = baseUri
}
return &c, nil
}
func (c *Config) paramReq(req po.Req, method string) (*po.Param, error) {
if err := req.Validate(); err != nil {
return nil, err
}
cert, err := alipay.GetCert(c.MchCertPath, c.RootCertPath, c.PukPath, c.AppId)
if err != nil {
return nil, err
}
return &po.Param{
AlipayRootCertSn: cert.RootCertSN,
AppCertSn: cert.MchCertSN,
AppId: c.AppId,
Method: method,
Format: vo.Format,
Charset: vo.Charset,
SignType: vo.SignType,
Timestamp: time.Now().Format(time.DateTime),
Version: vo.Version,
BizContent: string(req.ToJson()),
Sign: "",
}, nil
}
func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (*po.OrderReq, error) {
type Extra struct {
Wishing string `json:"wishing"`
}
var extra Extra
if product.Extra != nil {
err := json.Unmarshal(product.Extra, &extra)
if err != nil {
return nil, fmt.Errorf("product extra json unmarshal error: %v", err)
}
}
payeeInfo := &po.PayeeInfo{
Identity: order.Account,
IdentityType: "ALIPAY_USER_ID",
}
if isValidPhoneNumber(order.Account) {
payeeInfo.IdentityType = "ALIPAY_LOGON_ID"
}
businessParams := &po.BusinessParams{
SubBizScene: "REDPACKET",
}
o := &po.OrderReq{
OutBizNo: order.OrderNo,
TransAmount: fmt.Sprintf("%.2f", product.Price),
ProductCode: product.ProductNo,
BizScene: "DIRECT_TRANSFER",
OrderTitle: extra.Wishing,
Remark: extra.Wishing,
PayeeInfo: payeeInfo,
BusinessParams: businessParams.ToString(),
}
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.OrderId,
Message: resp.GetMsg(),
Data: data,
},
}
}
func queryReq(in *proto.QueryRequest_Order) (*po.QueryReq, error) {
return &po.QueryReq{
OutBizNo: in.OrderNo,
OrderId: in.TradeNo,
ProductCode: "STD_RED_PACKET",
BizScene: "DIRECT_TRANSFER",
}, 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.Status.GetOrderStatus(),
OrderNo: b.OutBizNo,
TradeNo: b.OrderId,
Message: b.Status.GetText(),
},
}
}