卡密类型处理

This commit is contained in:
李子铭 2024-09-10 16:17:29 +08:00
parent ddc62688f2
commit a603101cee
6 changed files with 82 additions and 10 deletions

View File

@ -3,9 +3,10 @@ package po
import "plugins/zltx/internal/vo"
type Notify struct {
MerchantId int `json:"merchantId" validate:"required"`
OutTradeNo string `json:"outTradeNo" validate:"required"`
RechargeAccount string `json:"rechargeAccount" validate:"required"`
Status vo.OrderStatus `json:"status" validate:"required"`
MerchantId int `json:"merchantId"`
OutTradeNo string `json:"outTradeNo"`
RechargeAccount string `json:"rechargeAccount"`
Status vo.OrderStatus `json:"status"`
CardCode string `json:"cardCode"` // 卡密才可能会有
Sign string `json:"sign" validate:"required"`
}

View File

@ -1,11 +1,22 @@
package po
import (
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"plugins/zltx/internal/vo"
)
type Card struct {
Number string `json:"number"`
Password string `json:"password"`
}
func (c *Card) ToString() string {
bytes, _ := json.Marshal(c)
return string(bytes)
}
type Req interface {
Validate() error
}

View File

@ -15,4 +15,5 @@ type QueryResp struct {
Status vo.OrderStatus `json:"status"`
Message string `json:"message"`
OutTradeNo string `json:"outTradeNo"`
CardCode string `json:"cardCode"` // 卡密才可能会有
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"gitea.cdlsxd.cn/sdk/plugin/proto"
"plugins/zltx/internal/po"
"strings"
"time"
)
@ -59,28 +60,58 @@ func (c *Config) queryReq(in *proto.QueryRequest) *po.QueryReq {
}
}
func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryResponse {
func queryResp(request *proto.QueryRequest, resp po.QueryResp, appKey string) (*proto.QueryResponse, error) {
data, _ := json.Marshal(resp)
return &proto.QueryResponse{
pb := &proto.QueryResponse{
Result: &proto.Result{
Status: resp.Status.GetOrderStatus(),
OrderNo: request.Order.OrderNo,
TradeNo: request.Order.TradeNo,
Message: resp.Message,
Data: data,
Extra: nil,
},
}
if resp.CardCode != "" {
s, err := cardCodeDecode(resp.CardCode, []byte(appKey))
if err != nil {
return nil, err
}
parts := strings.Split(s, "_")
car := &po.Card{
Number: parts[0],
Password: parts[1],
}
b, _ := json.Marshal(car)
pb.Result.Extra = b
}
return pb, nil
}
func notifyResp(in po.Notify) *proto.NotifyResponse {
func notifyResp(in po.Notify, appKey string) (*proto.NotifyResponse, error) {
data, _ := json.Marshal(in)
return &proto.NotifyResponse{
pb := &proto.NotifyResponse{
Result: &proto.Result{
Status: in.Status.GetOrderStatus(),
OrderNo: in.OutTradeNo,
TradeNo: "",
Message: "",
Data: data,
Extra: nil,
},
}
if in.CardCode != "" {
s, err := cardCodeDecode(in.CardCode, []byte(appKey))
if err != nil {
return nil, err
}
parts := strings.Split(s, "_")
car := &po.Card{
Number: parts[0],
Password: parts[1],
}
b, _ := json.Marshal(car)
pb.Result.Extra = b
}
return pb, nil
}

View File

@ -1,8 +1,12 @@
package internal
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/utils"
"net/url"
"plugins/zltx/internal/po"
@ -55,3 +59,27 @@ func verify(notify *po.Notify, appKey string) bool {
return sign([]byte(signStr)) == notify.Sign
}
func cardCodeDecode(code string, key []byte) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(code)
if err != nil {
return "", fmt.Errorf("base64 decode error: %v", err)
}
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("aes new cipher error: %v", err)
}
if len(decoded) < aes.BlockSize {
return "", fmt.Errorf("ciphertext too short")
}
decrypted := make([]byte, len(decoded))
mode := cipher.NewCBCDecrypter(block, make([]byte, aes.BlockSize))
mode.CryptBlocks(decrypted, decoded)
padding := decrypted[len(decrypted)-1]
decrypted = decrypted[:len(decrypted)-int(padding)]
return string(decrypted), nil
}

View File

@ -69,7 +69,7 @@ func (p *ZLTXService) Query(ctx context.Context, request *proto.QueryRequest) (*
return nil, errors.New("请求错误msg:" + response.Message)
}
return queryResp(request, response), nil
return queryResp(request, response, c.AppKey)
}
func (p *ZLTXService) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
@ -91,5 +91,5 @@ func (p *ZLTXService) Notify(_ context.Context, request *proto.NotifyRequest) (*
return nil, fmt.Errorf("验签不通过")
}
return notifyResp(poReq), nil
return notifyResp(poReq, c.AppKey)
}