Jerry 7 yıl önce
ebeveyn
işleme
0cb93b2513
2 değiştirilmiş dosya ile 22 ekleme ve 6 silme
  1. 19 1
      wechat_client_test.go
  2. 3 5
      wechat_params.go

+ 19 - 1
wechat_client_test.go

@@ -1,9 +1,14 @@
 package gopay
 package gopay
 
 
 import (
 import (
+	"crypto/md5"
+	"encoding/hex"
 	"fmt"
 	"fmt"
 	"log"
 	"log"
+	"strconv"
+	"strings"
 	"testing"
 	"testing"
+	"time"
 )
 )
 
 
 func TestWeChatClient_UnifiedOrder(t *testing.T) {
 func TestWeChatClient_UnifiedOrder(t *testing.T) {
@@ -36,7 +41,11 @@ func TestWeChatClient_UnifiedOrder(t *testing.T) {
 	if err != nil {
 	if err != nil {
 		fmt.Println("Error:", err)
 		fmt.Println("Error:", err)
 	}
 	}
-	fmt.Println("Response:", wxRsp)
+	timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
+	//获取小程序需要的paySign
+	paySign := GetMiniPaySign(appID, wxRsp.NonceStr, wxRsp.PrepayId, SignType_MD5, timeStamp, secretKey)
+	fmt.Println("paySign:", paySign)
+	//fmt.Println("Response:", wxRsp)
 }
 }
 
 
 func TestWeChatClient_QueryOrder(t *testing.T) {
 func TestWeChatClient_QueryOrder(t *testing.T) {
@@ -201,3 +210,12 @@ func TestWeChatClient_BatchQueryComment(t *testing.T) {
 	}
 	}
 	fmt.Println("Response:", wxRsp)
 	fmt.Println("Response:", wxRsp)
 }
 }
+
+func TestMd5(t *testing.T) {
+	st := "appid=wxdaa2ab9ef87b5497&nonceStr=9k20rM66parD2U49&package=prepay_id=wx29164301554772fbc70d1d793335446010&signType=MD5&timeStamp=1548751382&key=GFDS8j98rewnmgl45wHTt980jg543wmg"
+	hash := md5.New()
+	hash.Write([]byte(st))
+	sum := hash.Sum(nil)
+	upper := strings.ToUpper(hex.EncodeToString(sum))
+	fmt.Println(" ssad  ", upper)
+}

+ 3 - 5
wechat_params.go

@@ -6,21 +6,20 @@ import (
 	"crypto/md5"
 	"crypto/md5"
 	"crypto/sha256"
 	"crypto/sha256"
 	"encoding/hex"
 	"encoding/hex"
-	"log"
 	"strings"
 	"strings"
 )
 )
 
 
 //JSAPI支付,支付参数后,再次计算出小程序用的paySign
 //JSAPI支付,支付参数后,再次计算出小程序用的paySign
-func GetMiniPaySign(appId, timeStamp, nonceStr, packages, signType, secretKey string) (paySign string) {
+func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
 	buffer := new(bytes.Buffer)
 	buffer := new(bytes.Buffer)
-	buffer.WriteString("appId=")
+	buffer.WriteString("appid=")
 	buffer.WriteString(appId)
 	buffer.WriteString(appId)
 
 
 	buffer.WriteString("&nonceStr=")
 	buffer.WriteString("&nonceStr=")
 	buffer.WriteString(nonceStr)
 	buffer.WriteString(nonceStr)
 
 
 	buffer.WriteString("&package=")
 	buffer.WriteString("&package=")
-	buffer.WriteString("prepay_id=" + packages)
+	buffer.WriteString("prepay_id=" + prepayId)
 
 
 	buffer.WriteString("&signType=")
 	buffer.WriteString("&signType=")
 	buffer.WriteString(signType)
 	buffer.WriteString(signType)
@@ -35,7 +34,6 @@ func GetMiniPaySign(appId, timeStamp, nonceStr, packages, signType, secretKey st
 
 
 	var hashSign []byte
 	var hashSign []byte
 	if signType == SignType_MD5 {
 	if signType == SignType_MD5 {
-		log.Println("signStr:::", signStr)
 		hash := md5.New()
 		hash := md5.New()
 		hash.Write([]byte(signStr))
 		hash.Write([]byte(signStr))
 		hashSign = hash.Sum(nil)
 		hashSign = hash.Sum(nil)