plugins/plugins/wechat_redpack/internal/transform.go

126 lines
3.7 KiB
Go

package internal
import (
"encoding/json"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/services/transferbatch"
"plugins/utils/wechat"
"plugins/wechat_redpack/internal/vo"
)
func transConfig(config []byte) (*wechat.Server, error) {
var c wechat.Server
err := json.Unmarshal(config, &c)
if err != nil {
return nil, proto.ErrorConfigFail(err.Error())
}
if err = c.Validate(); err != nil {
return nil, err
}
return &c, nil
}
type OrderExtra struct {
Appid string `json:"app_id"`
OutDetailNo string `json:"out_detail_no"`
}
type ProductExtra struct {
BatchName string `json:"batch_name"`
BatchRemark string `json:"batch_remark"`
}
func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (transferbatch.InitiateBatchTransferRequest, error) {
var orderExtra OrderExtra
err := json.Unmarshal(order.Extra, &orderExtra)
if err != nil {
return transferbatch.InitiateBatchTransferRequest{}, fmt.Errorf("order拓展参数 json unmarshal error[%+v]", err)
}
var productExtra ProductExtra
err = json.Unmarshal(product.Extra, &productExtra)
if err != nil {
return transferbatch.InitiateBatchTransferRequest{}, fmt.Errorf("product拓展参数json unmarshal error:[%+v], extra[%s]", err, string(product.Extra))
}
transferAmount := int64(order.Amount * 100)
return transferbatch.InitiateBatchTransferRequest{
Appid: core.String(orderExtra.Appid),
OutBatchNo: core.String(order.OrderNo), // 微信转账商家批次单号
BatchName: core.String(productExtra.BatchName),
BatchRemark: core.String(productExtra.BatchRemark),
TotalAmount: core.Int64(transferAmount), // 转账总金额,单位分 100=1元 1000=0.1(需要申请)
TotalNum: core.Int64(int64(order.Quantity)),
TransferDetailList: []transferbatch.TransferDetailInput{
{
OutDetailNo: core.String(orderExtra.OutDetailNo),
TransferAmount: core.Int64(transferAmount),
TransferRemark: core.String(productExtra.BatchRemark),
Openid: core.String(order.Account),
UserName: nil,
},
},
TransferSceneId: core.String("1000"), // 转账场景ID, 1000 - 现金营销
}, nil
}
func orderResp(order *proto.OrderRequest_Order, tradeNo string) *proto.OrderResponse {
return &proto.OrderResponse{
Result: &proto.Result{
Status: proto.Status_ING,
OrderNo: order.GetOrderNo(),
TradeNo: tradeNo,
Message: "成功",
Data: nil,
Extra: nil,
},
}
}
func queryReq(order *proto.QueryRequest_Order) (*transferbatch.GetTransferDetailByOutNoRequest, error) {
var orderExtra OrderExtra
if order.Extra != nil {
err := json.Unmarshal(order.Extra, &orderExtra)
if err != nil {
return nil, fmt.Errorf("order拓展参数 json unmarshal error: %v", err)
}
}
return &transferbatch.GetTransferDetailByOutNoRequest{
OutDetailNo: core.String(orderExtra.OutDetailNo),
OutBatchNo: core.String(order.OrderNo),
}, nil
}
func queryResp(request *proto.QueryRequest, resp *transferbatch.TransferDetailEntity) *proto.QueryResponse {
data, _ := resp.MarshalJSON()
status := vo.QueryStatus(*resp.DetailStatus)
msg := status.GetText()
if resp.FailReason != nil {
m, ok := vo.FailReasonMsg[*resp.FailReason]
if ok {
msg = fmt.Sprintf("Message:%s, FailReason:%s", msg, m)
} else {
msg = fmt.Sprintf("Message:%s, FailReason:%s", msg, *resp.FailReason)
}
}
return &proto.QueryResponse{
Result: &proto.Result{
Status: status.GetOrderStatus(),
OrderNo: request.Order.GetOrderNo(),
TradeNo: request.Order.TradeNo,
Message: msg,
Data: data,
Extra: nil,
},
}
}
func notifyResp() *proto.NotifyResponse {
return &proto.NotifyResponse{
Result: &proto.Result{
Status: proto.Status_ING,
Extra: nil,
},
}
}