Skip to content

Commit

Permalink
add func GetH5PaySign
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry committed May 20, 2019
1 parent 0fd4baf commit 2b9a2e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions wechat_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions wechat_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2b9a2e9

Please sign in to comment.