优化乐刷通知与钱包充值流程
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
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package leshua
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/config"
|
||||
@@ -22,6 +23,12 @@ func TestSignUsesASCIISortedNonEmptyParams(t *testing.T) {
|
||||
if got != want {
|
||||
t.Fatalf("Sign() = %s, want %s", got, want)
|
||||
}
|
||||
|
||||
baseString := SignBaseString(params, SignOptions{})
|
||||
wantBaseString := "amount=100&merchant_id=1234567890&nonce_str=abc&service=get_tdcode&third_order_id=NO1"
|
||||
if baseString != wantBaseString {
|
||||
t.Fatalf("SignBaseString() = %s, want %s", baseString, wantBaseString)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyNotifyIncludesEmptyAndExcludesErrorCode(t *testing.T) {
|
||||
@@ -33,11 +40,12 @@ func TestVerifyNotifyIncludesEmptyAndExcludesErrorCode(t *testing.T) {
|
||||
"amount": "100",
|
||||
"status": "2",
|
||||
"attach": "",
|
||||
"leshua": "",
|
||||
"error_code": "-20001",
|
||||
}
|
||||
params["sign"] = Sign(params, "notify-secret", SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "sign"},
|
||||
ExcludeKeys: []string{"error_code", "leshua", "sign"},
|
||||
})
|
||||
|
||||
if !client.VerifyNotify(params) {
|
||||
@@ -50,7 +58,7 @@ func TestVerifyNotifyIncludesEmptyAndExcludesErrorCode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyNotifyFallsBackToSignKey(t *testing.T) {
|
||||
func TestVerifyNotifyDoesNotFallBackToSignKey(t *testing.T) {
|
||||
client := NewClient(config.LeshuaPaymentConfig{
|
||||
NotifyKey: "wrong-notify-secret",
|
||||
SignKey: "sign-secret",
|
||||
@@ -67,12 +75,64 @@ func TestVerifyNotifyFallsBackToSignKey(t *testing.T) {
|
||||
ExcludeKeys: []string{"error_code", "sign"},
|
||||
})
|
||||
|
||||
result := client.VerifyNotifyDetail(params)
|
||||
if result.OK {
|
||||
t.Fatal("VerifyNotifyDetail().OK = true, want false")
|
||||
}
|
||||
if _, ok := result.Expected["sign_key"]; ok {
|
||||
t.Fatal("VerifyNotifyDetail() unexpectedly used sign_key fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyNotifyUsesDocumentedNotifySignature(t *testing.T) {
|
||||
client := NewClient(config.LeshuaPaymentConfig{NotifyKey: "notify-secret"})
|
||||
params := map[string]string{
|
||||
"merchant_id": "1234567890",
|
||||
"third_order_id": "NO1",
|
||||
"leshua_order_id": "LS1",
|
||||
"amount": "100",
|
||||
"status": "2",
|
||||
"sign_type": "MD5",
|
||||
}
|
||||
params["sign"] = Sign(params, "notify-secret", SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "leshua", "sign"},
|
||||
})
|
||||
|
||||
result := client.VerifyNotifyDetail(params)
|
||||
if !result.OK {
|
||||
t.Fatal("VerifyNotifyDetail().OK = false, want true")
|
||||
}
|
||||
if result.MatchedKey != "sign_key" {
|
||||
t.Fatalf("MatchedKey = %s, want sign_key", result.MatchedKey)
|
||||
if result.MatchedKey != "notify_key" {
|
||||
t.Fatalf("MatchedKey = %s, want notify_key", result.MatchedKey)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyNotifyKeepsEmptyXMLFieldsInSignature(t *testing.T) {
|
||||
client := NewClient(config.LeshuaPaymentConfig{NotifyKey: "notify-secret"})
|
||||
params, err := ParsePayload([]byte(`<leshua>
|
||||
<amount>100</amount>
|
||||
<goods_tag></goods_tag>
|
||||
<merchant_id>1234567890</merchant_id>
|
||||
<sign_type>MD5</sign_type>
|
||||
<status>2</status>
|
||||
<third_order_id>NO1</third_order_id>
|
||||
</leshua>`))
|
||||
if err != nil {
|
||||
t.Fatalf("ParsePayload(xml) error = %v", err)
|
||||
}
|
||||
params["sign"] = Sign(params, "notify-secret", SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "leshua", "sign"},
|
||||
})
|
||||
|
||||
result := client.VerifyNotifyDetail(params)
|
||||
if !result.OK {
|
||||
t.Fatal("VerifyNotifyDetail().OK = false, want true")
|
||||
}
|
||||
baseString := result.BaseString[result.MatchedKey]
|
||||
if !strings.Contains(baseString, "goods_tag=") {
|
||||
t.Fatalf("baseString = %s, want goods_tag included", baseString)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,11 +145,17 @@ func TestParsePayloadSupportsFormAndXML(t *testing.T) {
|
||||
t.Fatalf("ParsePayload(form) = %#v", form)
|
||||
}
|
||||
|
||||
xml, err := ParsePayload([]byte("<xml><third_order_id>NO2</third_order_id><status>6</status></xml>"))
|
||||
xml, err := ParsePayload([]byte("<xml><third_order_id>NO2</third_order_id><status>6</status><goods_tag></goods_tag><coupon/></xml>"))
|
||||
if err != nil {
|
||||
t.Fatalf("ParsePayload(xml) error = %v", err)
|
||||
}
|
||||
if xml["third_order_id"] != "NO2" || xml["status"] != "6" {
|
||||
t.Fatalf("ParsePayload(xml) = %#v", xml)
|
||||
}
|
||||
if value, ok := xml["goods_tag"]; !ok || value != "" {
|
||||
t.Fatalf("ParsePayload(xml).goods_tag = %q, exists=%v; want empty value", value, ok)
|
||||
}
|
||||
if value, ok := xml["coupon"]; !ok || value != "" {
|
||||
t.Fatalf("ParsePayload(xml).coupon = %q, exists=%v; want empty value", value, ok)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user