卡密类型处理

This commit is contained in:
李子铭 2024-09-12 09:30:13 +08:00
parent d20fd39530
commit 44780174fb
3 changed files with 4 additions and 32 deletions

View File

@ -7,6 +7,5 @@ type Notify struct {
OutTradeNo string `json:"outTradeNo"` OutTradeNo string `json:"outTradeNo"`
RechargeAccount string `json:"rechargeAccount"` RechargeAccount string `json:"rechargeAccount"`
Status vo.OrderStatus `json:"status"` Status vo.OrderStatus `json:"status"`
CardCode string `json:"cardCode"` // 卡密才可能会有
Sign string `json:"sign" validate:"required"` Sign string `json:"sign" validate:"required"`
} }

View File

@ -1,12 +1,8 @@
package internal package internal
import ( import (
"crypto/aes"
"crypto/cipher"
"crypto/md5" "crypto/md5"
"encoding/base64"
"encoding/hex" "encoding/hex"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/utils" "gitea.cdlsxd.cn/sdk/plugin/utils"
"net/url" "net/url"
"plugins/zltx/internal/po" "plugins/zltx/internal/po"
@ -50,36 +46,13 @@ func verify(notify *po.Notify, appKey string) bool {
var kvStr []string var kvStr []string
for _, kv := range kvRows { for _, kv := range kvRows {
if kv.Key != "sign" && kv.Key != "version" { if kv.Key == "sign" || kv.Key == "version" {
kvStr = append(kvStr, kv.Key+"="+kv.Value) continue
} }
kvStr = append(kvStr, kv.Key+"="+kv.Value)
} }
kvStr = append(kvStr, "key="+appKey) kvStr = append(kvStr, "key="+appKey)
signStr := strings.Join(kvStr, "&") signStr := strings.Join(kvStr, "&")
return sign([]byte(signStr)) == notify.Sign 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

@ -80,7 +80,7 @@ func TestNotify(t *testing.T) {
Config: config(), Config: config(),
Queries: nil, Queries: nil,
Headers: nil, Headers: nil,
Body: []byte(`{"merchantId":10, "outTradeNo":"123", "rechargeAccount":"1866666666", "status":"01", "sign":"sign"}`), Body: []byte(`{"merchantId":23329,"outTradeNo":"202409111714224026320002","rechargeAccount":"18512869479","sign":"474ACB521DEE99551153B6CE108FD06D","status":"01"}`),
} }
t.Run("TestNotify", func(t *testing.T) { t.Run("TestNotify", func(t *testing.T) {
got, err := zltx.Notify(context.Background(), in) got, err := zltx.Notify(context.Background(), in)