优化乐刷通知与钱包充值流程
This commit is contained in:
@@ -77,6 +77,7 @@ type VerifyNotifyResult struct {
|
||||
MatchedKey string
|
||||
Got string
|
||||
Expected map[string]string
|
||||
BaseString map[string]string
|
||||
ParamKeys []string
|
||||
}
|
||||
|
||||
@@ -185,19 +186,19 @@ func (c *Client) VerifyNotify(params map[string]string) bool {
|
||||
func (c *Client) VerifyNotifyDetail(params map[string]string) VerifyNotifyResult {
|
||||
got := strings.ToUpper(params["sign"])
|
||||
result := VerifyNotifyResult{
|
||||
Got: got,
|
||||
Expected: map[string]string{},
|
||||
ParamKeys: notifyParamKeys(params),
|
||||
Got: got,
|
||||
Expected: map[string]string{},
|
||||
BaseString: map[string]string{},
|
||||
ParamKeys: notifyParamKeys(params),
|
||||
}
|
||||
if got == "" {
|
||||
return result
|
||||
}
|
||||
for _, item := range c.notifyKeyCandidates() {
|
||||
expected := Sign(params, item.key, SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "sign"},
|
||||
})
|
||||
expected := Sign(params, item.key, notifySignOptions())
|
||||
baseString := SignBaseString(params, notifySignOptions())
|
||||
result.Expected[item.name] = expected
|
||||
result.BaseString[item.name] = baseString
|
||||
if got == expected {
|
||||
result.OK = true
|
||||
result.MatchedKey = item.name
|
||||
@@ -207,6 +208,13 @@ func (c *Client) VerifyNotifyDetail(params map[string]string) VerifyNotifyResult
|
||||
return result
|
||||
}
|
||||
|
||||
func notifySignOptions() SignOptions {
|
||||
return SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "leshua", "sign"},
|
||||
}
|
||||
}
|
||||
|
||||
type notifyKeyCandidate struct {
|
||||
name string
|
||||
key string
|
||||
@@ -217,16 +225,13 @@ func (c *Client) notifyKeyCandidates() []notifyKeyCandidate {
|
||||
if c.cfg.NotifyKey != "" {
|
||||
candidates = append(candidates, notifyKeyCandidate{name: "notify_key", key: c.cfg.NotifyKey})
|
||||
}
|
||||
if c.cfg.SignKey != "" && c.cfg.SignKey != c.cfg.NotifyKey {
|
||||
candidates = append(candidates, notifyKeyCandidate{name: "sign_key", key: c.cfg.SignKey})
|
||||
}
|
||||
return candidates
|
||||
}
|
||||
|
||||
func notifyParamKeys(params map[string]string) []string {
|
||||
keys := make([]string, 0, len(params))
|
||||
for key := range params {
|
||||
if key == "sign" || key == "error_code" {
|
||||
if key == "sign" || key == "error_code" || key == "leshua" {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, key)
|
||||
@@ -276,6 +281,16 @@ type SignOptions struct {
|
||||
}
|
||||
|
||||
func Sign(params map[string]string, key string, opts SignOptions) string {
|
||||
baseString := SignBaseString(params, opts)
|
||||
stringSignTemp := "key=" + key
|
||||
if baseString != "" {
|
||||
stringSignTemp = baseString + "&key=" + key
|
||||
}
|
||||
sum := md5.Sum([]byte(stringSignTemp))
|
||||
return strings.ToUpper(hex.EncodeToString(sum[:]))
|
||||
}
|
||||
|
||||
func SignBaseString(params map[string]string, opts SignOptions) string {
|
||||
excluded := map[string]bool{}
|
||||
for _, item := range opts.ExcludeKeys {
|
||||
excluded[item] = true
|
||||
@@ -298,9 +313,7 @@ func Sign(params map[string]string, key string, opts SignOptions) string {
|
||||
for _, name := range keys {
|
||||
parts = append(parts, name+"="+params[name])
|
||||
}
|
||||
parts = append(parts, "key="+key)
|
||||
sum := md5.Sum([]byte(strings.Join(parts, "&")))
|
||||
return strings.ToUpper(hex.EncodeToString(sum[:]))
|
||||
return strings.Join(parts, "&")
|
||||
}
|
||||
|
||||
func ParsePayload(body []byte) (map[string]string, error) {
|
||||
@@ -342,6 +355,7 @@ func parseXMLPayload(body []byte) (map[string]string, error) {
|
||||
decoder := xml.NewDecoder(bytes.NewReader(body))
|
||||
out := map[string]string{}
|
||||
var current string
|
||||
depth := 0
|
||||
for {
|
||||
token, err := decoder.Token()
|
||||
if err == io.EOF {
|
||||
@@ -352,14 +366,25 @@ func parseXMLPayload(body []byte) (map[string]string, error) {
|
||||
}
|
||||
switch item := token.(type) {
|
||||
case xml.StartElement:
|
||||
current = item.Name.Local
|
||||
depth++
|
||||
if depth > 1 {
|
||||
current = item.Name.Local
|
||||
if _, exists := out[current]; !exists {
|
||||
out[current] = ""
|
||||
}
|
||||
}
|
||||
case xml.CharData:
|
||||
value := strings.TrimSpace(string(item))
|
||||
if current != "" && current != "xml" && value != "" {
|
||||
if current != "" && value != "" {
|
||||
out[current] = value
|
||||
}
|
||||
case xml.EndElement:
|
||||
current = ""
|
||||
if current == item.Name.Local {
|
||||
current = ""
|
||||
}
|
||||
if depth > 0 {
|
||||
depth--
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
|
||||
Reference in New Issue
Block a user