优化乐刷通知与钱包充值流程
This commit is contained in:
@@ -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