54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package payment
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// DecryptNotify 解密回调通知
|
|
func (c *Client) DecryptNotify(rawJson string) (string, error) {
|
|
return c.DecryptResponse(rawJson, true)
|
|
}
|
|
|
|
// VerifyNotifySignature 验证回调通知签名
|
|
func (c *Client) VerifyNotifySignature(content, signature string) bool {
|
|
return c.VerifySignature(content, signature)
|
|
}
|
|
|
|
// ParseNotify 解析回调通知
|
|
func (c *Client) ParseNotify(rawJson string) (*NotifyRequest, error) {
|
|
var req NotifyRequest
|
|
if err := json.Unmarshal([]byte(rawJson), &req); err != nil {
|
|
return nil, fmt.Errorf("解析通知失败: %v", err)
|
|
}
|
|
return &req, nil
|
|
}
|
|
|
|
// VerifyAndParseNotify 验签并解析回调通知
|
|
func (c *Client) VerifyAndParseNotify(rawJson string) (map[string]interface{}, error) {
|
|
var reqData map[string]string
|
|
if err := json.Unmarshal([]byte(rawJson), &reqData); err != nil {
|
|
return nil, fmt.Errorf("解析通知失败: %v", err)
|
|
}
|
|
|
|
signature := reqData["signature"]
|
|
delete(reqData, "signature")
|
|
|
|
content := MapToString(reqData)
|
|
if !c.VerifySignature(content, signature) {
|
|
return nil, fmt.Errorf("签名验证失败")
|
|
}
|
|
|
|
decrypted, err := c.DecryptNotify(rawJson)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("解密通知失败: %v", err)
|
|
}
|
|
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(decrypted), &result); err != nil {
|
|
return nil, fmt.Errorf("解析解密后数据失败: %v", err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|