Skip to content

Commit a84668d

Browse files
committed
add GetAppPaySign function
1 parent 31bad2d commit a84668d

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

wechat_servier_api.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func VerifyPayResultSign(apiKey string, signType string, notifyRsp *WeChatNotify
122122
return
123123
}
124124

125-
//JSAPI支付,支付参数后,再次计算出小程序用的paySign
125+
//JSAPI支付,统一下单获取支付参数后,再次计算出小程序用的paySign
126126
func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string) (paySign string) {
127127
buffer := new(bytes.Buffer)
128128
buffer.WriteString("appId=")
@@ -159,7 +159,7 @@ func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey strin
159159
return
160160
}
161161

162-
//JSAPI支付,支付参数后,再次计算出微信内H5支付需要用的paySign
162+
//JSAPI支付,统一下单获取支付参数后,再次计算出微信内H5支付需要用的paySign
163163
func GetH5PaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string) (paySign string) {
164164
buffer := new(bytes.Buffer)
165165
buffer.WriteString("appId=")
@@ -196,6 +196,46 @@ func GetH5PaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string)
196196
return
197197
}
198198

199+
//APP支付,统一下单获取支付参数后,再次计算APP支付所需要的的sign
200+
// signType:此处签名方式,务必与统一下单时用的签名方式一致
201+
func GetAppPaySign(appid, partnerid, noncestr, prepayid, signType, timestamp, apiKey string) (paySign string) {
202+
buffer := new(bytes.Buffer)
203+
buffer.WriteString("appid=")
204+
buffer.WriteString(appid)
205+
206+
buffer.WriteString("&nonceStr=")
207+
buffer.WriteString(noncestr)
208+
209+
buffer.WriteString("&package=Sign=WXPay")
210+
211+
buffer.WriteString("&partnerid=")
212+
buffer.WriteString(partnerid)
213+
214+
buffer.WriteString("&prepayid=")
215+
buffer.WriteString(prepayid)
216+
217+
buffer.WriteString("&timeStamp=")
218+
buffer.WriteString(timestamp)
219+
220+
buffer.WriteString("&key=")
221+
buffer.WriteString(apiKey)
222+
223+
signStr := buffer.String()
224+
225+
var hashSign []byte
226+
if signType == SignType_MD5 {
227+
hash := md5.New()
228+
hash.Write([]byte(signStr))
229+
hashSign = hash.Sum(nil)
230+
} else {
231+
hash := hmac.New(sha256.New, []byte(apiKey))
232+
hash.Write([]byte(signStr))
233+
hashSign = hash.Sum(nil)
234+
}
235+
paySign = strings.ToUpper(hex.EncodeToString(hashSign))
236+
return
237+
}
238+
199239
//解密开放数据
200240
// encryptedData:包括敏感数据在内的完整用户信息的加密数据
201241
// iv:加密算法的初始向量

0 commit comments

Comments
 (0)