卡密类型处理

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"`
RechargeAccount string `json:"rechargeAccount"`
Status vo.OrderStatus `json:"status"`
CardCode string `json:"cardCode"` // 卡密才可能会有
Sign string `json:"sign" validate:"required"`
}

View File

@ -1,12 +1,8 @@
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"
@ -50,36 +46,13 @@ func verify(notify *po.Notify, appKey string) bool {
var kvStr []string
for _, kv := range kvRows {
if kv.Key != "sign" && kv.Key != "version" {
kvStr = append(kvStr, kv.Key+"="+kv.Value)
if kv.Key == "sign" || kv.Key == "version" {
continue
}
kvStr = append(kvStr, kv.Key+"="+kv.Value)
}
kvStr = append(kvStr, "key="+appKey)
signStr := strings.Join(kvStr, "&")
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(),
Queries: 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) {
got, err := zltx.Notify(context.Background(), in)