From 2b9a2e9e3e4230289a82d27bb00b23c71a859f0c Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 20 May 2019 17:42:10 +0800 Subject: [PATCH] add func GetH5PaySign --- wechat_client_test.go | 6 +++--- wechat_export.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/wechat_client_test.go b/wechat_client_test.go index b62f5e2e..2aa38217 100644 --- a/wechat_client_test.go +++ b/wechat_client_test.go @@ -18,7 +18,7 @@ func TestWeChatClient_UnifiedOrder(t *testing.T) { // mchID:商户ID // secretKey:Key值 // isProd:是否是正式环境 - client := NewWeChatClient(appID, mchID, secretKey, true) + client := NewWeChatClient(appID, mchID, secretKey, false) //初始化参数Map body := make(BodyMap) @@ -27,7 +27,7 @@ func TestWeChatClient_UnifiedOrder(t *testing.T) { number := GetRandomString(32) log.Println("Number:", number) body.Set("out_trade_no", number) - body.Set("total_fee", 1) + body.Set("total_fee", 10) body.Set("spbill_create_ip", "124.77.173.62") body.Set("notify_url", "http://www.igoogle.ink") body.Set("trade_type", TradeType_JsApi) @@ -46,7 +46,7 @@ func TestWeChatClient_UnifiedOrder(t *testing.T) { pac := "prepay_id=" + wxRsp.PrepayId paySign := GetMiniPaySign(appID, wxRsp.NonceStr, pac, SignType_MD5, timeStamp, secretKey) fmt.Println("paySign:", paySign) - //fmt.Println("Response:", wxRsp) + fmt.Println("Response:", wxRsp) } func TestWeChatClient_QueryOrder(t *testing.T) { diff --git a/wechat_export.go b/wechat_export.go index a986ae96..abe2a084 100644 --- a/wechat_export.go +++ b/wechat_export.go @@ -54,6 +54,43 @@ func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey st return } +//JSAPI支付,支付参数后,再次计算出微信内H5支付需要用的paySign +func GetH5PaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) { + buffer := new(bytes.Buffer) + buffer.WriteString("appId=") + buffer.WriteString(appId) + + buffer.WriteString("&nonceStr=") + buffer.WriteString(nonceStr) + + buffer.WriteString("&package=") + buffer.WriteString(prepayId) + + buffer.WriteString("&signType=") + buffer.WriteString(signType) + + buffer.WriteString("&timeStamp=") + buffer.WriteString(timeStamp) + + buffer.WriteString("&key=") + buffer.WriteString(secretKey) + + signStr := buffer.String() + + var hashSign []byte + if signType == SignType_MD5 { + hash := md5.New() + hash.Write([]byte(signStr)) + hashSign = hash.Sum(nil) + } else { + hash := hmac.New(sha256.New, []byte(secretKey)) + hash.Write([]byte(signStr)) + hashSign = hash.Sum(nil) + } + paySign = strings.ToUpper(hex.EncodeToString(hashSign)) + return +} + //获取微信用户的OpenId、SessionKey、UnionId func GetWeChatUserId(appId, secretKey, wxCode string) (userRsp *WeChatUserIdRsp, err error) { userRsp = new(WeChatUserIdRsp)