150 lines
3.5 KiB
Go
150 lines
3.5 KiB
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"github.com/go-playground/validator/v10"
|
|
"net/http"
|
|
"plugins/qixing_alipay_redpack/internal/po"
|
|
"plugins/utils/helper"
|
|
)
|
|
|
|
type Config struct {
|
|
NchName string `validate:"required" json:"mch_name"` // 商户名称
|
|
BasUrl string `validate:"required" json:"base_url"` // 请求地址
|
|
AppKey string `validate:"required" json:"app_key"` // appKey密钥
|
|
BatchId string `validate:"required" json:"batch_id"` // 批次账号
|
|
NotifyUrl string `validate:"required" json:"notify_url"` // 回调地址
|
|
}
|
|
|
|
func (c *Config) validate() error {
|
|
|
|
err := validator.New().Struct(c)
|
|
if err != nil {
|
|
for _, err = range err.(validator.ValidationErrors) {
|
|
return proto.ErrorConfigFail(fmt.Sprintf("配置参数有误:%s", err.Error()))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func transConfig(config []byte) (*Config, error) {
|
|
|
|
var c Config
|
|
|
|
if err := json.Unmarshal(config, &c); err != nil {
|
|
return nil, proto.ErrorConfigFail(fmt.Sprintf("配置参数解析失败: %v", err))
|
|
}
|
|
|
|
if err := c.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &c, nil
|
|
}
|
|
|
|
func (c *Config) orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (*po.OrderReq, error) {
|
|
|
|
//var productExtra struct {
|
|
// Wishing string `json:"wishing"`
|
|
//}
|
|
//if err := json.Unmarshal(product.Extra, &productExtra); err != nil {
|
|
// return nil, proto.ErrorParamFail(fmt.Sprintf("启星product拓展参数解析失败: %v", err))
|
|
//}
|
|
|
|
if !helper.IsPhoneNumber(order.Account) && !helper.IsEmail(order.Account) {
|
|
return nil, proto.ErrorParamFail("启星支付宝红包只支持账号领取")
|
|
}
|
|
|
|
var orderExtra struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.Unmarshal(order.Extra, &orderExtra); err != nil {
|
|
return nil, proto.ErrorConfigFail(fmt.Sprintf("支付宝红包order拓展参数解析失败: %v", err))
|
|
}
|
|
|
|
return &po.OrderReq{
|
|
Amount: order.Amount,
|
|
BatchId: c.BatchId,
|
|
BizNo: order.OrderNo,
|
|
|
|
Name: orderExtra.Name,
|
|
Phone: order.Account,
|
|
|
|
NotifyUrl: c.NotifyUrl,
|
|
|
|
Sign: "",
|
|
}, nil
|
|
}
|
|
|
|
func orderResp(request *proto.OrderRequest, resp *po.OrderResp, bodyBytes []byte) *proto.OrderResponse {
|
|
return &proto.OrderResponse{
|
|
Result: &proto.Result{
|
|
Status: resp.GetOrderStatus(),
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: "",
|
|
Message: resp.GetMsg(),
|
|
Data: bodyBytes,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Config) queryReq(in *proto.QueryRequest_Order) *po.QueryReq {
|
|
|
|
return &po.QueryReq{
|
|
BizNo: in.OrderNo,
|
|
BatchId: c.BatchId,
|
|
}
|
|
}
|
|
|
|
func queryResp(request *proto.QueryRequest, resp *po.QueryResp, bodyBytes []byte) *proto.QueryResponse {
|
|
|
|
return &proto.QueryResponse{
|
|
Result: &proto.Result{
|
|
OrderNo: request.Order.OrderNo,
|
|
TradeNo: "",
|
|
Status: resp.GetOrderStatus(),
|
|
Message: resp.Msg,
|
|
Data: bodyBytes,
|
|
},
|
|
}
|
|
}
|
|
|
|
func notifyReq(in *proto.NotifyRequest) (*po.Notify, error) {
|
|
|
|
var n *po.Notify
|
|
if err := json.Unmarshal(in.Body, &n); err != nil {
|
|
return nil, proto.ErrorParamFail(fmt.Sprintf("notify拓展参数解析失败: %v", err))
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
func notifyResp(n *po.Notify, data []byte) (*proto.NotifyResponse, error) {
|
|
|
|
pb := &proto.NotifyResponse{
|
|
Result: &proto.Result{
|
|
Status: n.Type.GetOrderType(),
|
|
OrderNo: n.BizNo,
|
|
TradeNo: "",
|
|
Message: n.FailMsg,
|
|
Data: data,
|
|
Extra: nil,
|
|
},
|
|
Return: `{"code":"success"}`,
|
|
}
|
|
|
|
headers := make(http.Header)
|
|
headers.Set("Content-Type", "application/json")
|
|
|
|
headersBytes, err := json.Marshal(headers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pb.Headers = string(headersBytes)
|
|
|
|
return pb, nil
|
|
}
|