Jerry пре 6 година
родитељ
комит
b873ba58e5
2 измењених фајлова са 76 додато и 24 уклоњено
  1. 21 17
      README.md
  2. 55 7
      wechat_client_test.go

+ 21 - 17
README.md

@@ -44,8 +44,8 @@
 * gopay.GetAppPaySign() => 获取APP支付所需要的paySign
 * gopay.GetAppPaySign() => 获取APP支付所需要的paySign
 * gopay.ParseWeChatNotifyResultToBodyMap() => 解析微信支付异步通知的参数到BodyMap
 * gopay.ParseWeChatNotifyResultToBodyMap() => 解析微信支付异步通知的参数到BodyMap
 * gopay.ParseWeChatNotifyResult() => 解析微信支付异步通知的参数
 * gopay.ParseWeChatNotifyResult() => 解析微信支付异步通知的参数
-* gopay.VerifyWeChatResultSignByBodyMap() => 通过BodyMap验证微信支付异步通知的Sign值
-* gopay.VerifyWeChatResultSign() => 验证微信支付异步通知的Sign值
+* (Deprecated)gopay.VerifyWeChatResultSign() => 验证微信支付异步通知的Sign值
+* gopay.VerifyWeChatSign() => 验证微信API返回结果或异步通知结果的Sign值
 * gopay.Code2Session() => 登录凭证校验:获取微信用户OpenId、UnionId、SessionKey
 * gopay.Code2Session() => 登录凭证校验:获取微信用户OpenId、UnionId、SessionKey
 * gopay.GetAccessToken() => 获取小程序全局唯一后台接口调用凭据
 * gopay.GetAccessToken() => 获取小程序全局唯一后台接口调用凭据
 * gopay.GetPaidUnionId() => 微信小程序用户支付完成后,获取该用户的 UnionId,无需用户授权
 * gopay.GetPaidUnionId() => 微信小程序用户支付完成后,获取该用户的 UnionId,无需用户授权
@@ -227,15 +227,17 @@ if err != nil {
 }
 }
 fmt.Println("notifyRsp:", notifyRsp)
 fmt.Println("notifyRsp:", notifyRsp)
 
 
-//验证微信支付异步通知的Sign值
+//验证微信API返回结果或异步通知结果的Sign值
 //    apiKey:API秘钥值
 //    apiKey:API秘钥值
-//    signType:签名类型 MD5 或 HMAC-SHA256(默认请填写 MD5)
-//    notifyRsp:利用 gopay.ParseWeChatNotifyResult() 得到的结构体
-//    返回参数ok:是否验证通过
-//    返回参数sign:根据参数计算的sign值,非微信返回参数中的Sign
-ok, sign := gopay.VerifyWeChatResultSign("192006250b4c09247ec02edce69f6a2d", "MD5", notifyRsp)
-log.Println("ok:", ok)
-log.Println("sign:", sign)
+//    signType:签名类型(调用API方法时填写的类型)
+//    bean:微信API返回的结构体 wxRsp 或 异步通知解析的结构体 notifyReq
+//    返回参数ok:是否验签通过
+//    返回参数err:错误信息
+ok, err := gopay.VerifyWeChatSign("192006250b4c09247ec02edce69f6a2d", "MD5", notifyRsp)
+if err != nil {
+    fmt.Println("err:", err)
+}
+fmt.Println("ok:", ok)
 ```
 ```
 或者
 或者
 ```go
 ```go
@@ -249,15 +251,17 @@ if err != nil {
     return
     return
 }
 }
 
 
-//通过BodyMap验证微信支付异步通知的Sign值
+//验证微信API返回结果或异步通知结果的Sign值
 //    apiKey:API秘钥值
 //    apiKey:API秘钥值
-//    signType:签名类型 MD5 或 HMAC-SHA256(默认请填写 MD5)
-//    bm:通过 gopay.ParseWeChatNotifyResult() 得到的BodyMap
-//    返回参数ok:是否验证通过
-//    返回参数sign:计算出的sign值,非微信返回参数中的Sign
-ok, sign := gopay.VerifyWeChatResultSignByBodyMap("192006250b4c09247ec02edce69f6a2d", gopay.SignType_MD5, bm)
+//    signType:签名类型(调用API方法时填写的类型)
+//    bean:微信API返回的结构体 wxRsp 或 异步通知解析的结构体 notifyReq
+//    返回参数ok:是否验签通过
+//    返回参数err:错误信息
+ok, err := gopay.VerifyWeChatSign("192006250b4c09247ec02edce69f6a2d", gopay.SignType_MD5, bm)
+if err != nil {
+    fmt.Println("err:", err)
+}
 fmt.Println("ok:", ok)
 fmt.Println("ok:", ok)
-fmt.Println("sign:", sign)
 ```
 ```
 
 
 ### 加密数据,解密到指定结构体
 ### 加密数据,解密到指定结构体

+ 55 - 7
wechat_client_test.go

@@ -3,7 +3,7 @@ package gopay
 import (
 import (
 	"crypto/md5"
 	"crypto/md5"
 	"encoding/hex"
 	"encoding/hex"
-	"encoding/xml"
+	"encoding/json"
 	"fmt"
 	"fmt"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
@@ -18,17 +18,65 @@ func TestMd5(t *testing.T) {
 	fmt.Println(" ssad  ", upper)
 	fmt.Println(" ssad  ", upper)
 }
 }
 
 
+type Student struct {
+	Name  string `json:"name,omitempty"`
+	Age   int    `json:"age,omitempty"`
+	Sign  string `json:"sign,omitempty"`
+	Phone string `json:"phone,omitempty"`
+}
+
 func TestBodyMap_MarshalXML(t *testing.T) {
 func TestBodyMap_MarshalXML(t *testing.T) {
 
 
-	maps := make(BodyMap)
-	maps.Set("name", "jerry")
-	maps.Set("age", 28)
-	maps.Set("phone", "13212345678")
+	student := new(Student)
+	student.Name = "Jerry"
+	student.Age = 28
+	student.Phone = "18017448610"
 
 
-	bytes, err := xml.Marshal(&maps)
+	marshal, err := json.Marshal(student)
 	if err != nil {
 	if err != nil {
 		fmt.Println("err:", err)
 		fmt.Println("err:", err)
 	}
 	}
-	fmt.Println("ssss:", string(bytes))
 
 
+	fmt.Println("marshal:", string(marshal))
+
+	maps := make(map[string]interface{})
+
+	err = json.Unmarshal(marshal, &maps)
+	if err != nil {
+		fmt.Println("err2:", err)
+	}
+	fmt.Println("maps:", maps)
+
+	//maps := make(BodyMap)
+	//maps.Set("name", "jerry")
+	//maps.Set("age", 28)
+	//maps.Set("phone", "13212345678")
+	//
+	//bytes, err := xml.Marshal(&maps)
+	//if err != nil {
+	//	fmt.Println("err:", err)
+	//}
+	//fmt.Println("ssss:", string(bytes))
+
+}
+
+func TestVerifyWeChatResponseSign(t *testing.T) {
+	student := new(Student)
+	student.Name = "Jerry"
+	student.Age = 1
+	student.Sign = "544E55ED43B787B945FF0BF8344A4D69"
+	student.Phone = "18017448610"
+
+	maps := make(BodyMap)
+	maps["name"] = "Jerry"
+	maps["age"] = 1
+	maps["sign"] = "544E55ED43B787B945FF0BF8344A4D69"
+	maps["phone"] = "18017448610"
+
+	ok, err := VerifyWeChatSign("ABCDEFG", "MD5", student)
+	if err != nil {
+		fmt.Println("errrrr:", err)
+		return
+	}
+	fmt.Println("ok:", ok)
 }
 }