当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 97

开发团队 / 微信Go SDK
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
service_sign.go 2.43 KB
一键复制 编辑 原始数据 按行查看 历史
shallot 提交于 2019-09-04 14:49 . 更新接口格式和说明文档。
package wechat
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
)
// JSAPI支付,统一下单获取支付参数后,再次计算出小程序用的paySign
func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string) (paySign string) {
// 原始字符串
raw := fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%s&key=%s",
appId, nonceStr, prepayId, signType, timeStamp, apiKey)
buffer := new(bytes.Buffer)
buffer.WriteString(raw)
signStr := buffer.String()
// 加密签名
var hashSign []byte
if signType == SignTypeHmacSHA256 {
hash := hmac.New(sha256.New, []byte(apiKey))
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
} else {
hash := md5.New()
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
}
paySign = strings.ToUpper(hex.EncodeToString(hashSign))
return
}
// JSAPI支付,统一下单获取支付参数后,再次计算出微信内H5支付需要用的paySign
func GetH5PaySign(appId, nonceStr, packages, signType, timeStamp, apiKey string) (paySign string) {
// 原始字符串
raw := fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%s&key=%s",
appId, nonceStr, packages, signType, timeStamp, apiKey)
buffer := new(bytes.Buffer)
buffer.WriteString(raw)
signStr := buffer.String()
// 加密签名
var hashSign []byte
if signType == SignTypeHmacSHA256 {
hash := hmac.New(sha256.New, []byte(apiKey))
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
} else {
hash := md5.New()
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
}
paySign = strings.ToUpper(hex.EncodeToString(hashSign))
return
}
// APP支付,统一下单获取支付参数后,再次计算APP支付所需要的的sign
func GetAppPaySign(appId, nonceStr, partnerId, prepayId, signType, timeStamp, apiKey string) (paySign string) {
// 原始字符串
raw := fmt.Sprintf("appId=%s&nonceStr=%s&package==Sign=WXPay&partnerid=%s&prepayid=%s&timeStamp=%s&key=%s",
appId, nonceStr, partnerId, prepayId, timeStamp, apiKey)
buffer := new(bytes.Buffer)
buffer.WriteString(raw)
// 加密签名
signStr := buffer.String()
var hashSign []byte
if signType == SignTypeHmacSHA256 {
hash := hmac.New(sha256.New, []byte(apiKey))
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
} else {
hash := md5.New()
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
}
paySign = strings.ToUpper(hex.EncodeToString(hashSign))
return
}
Go
1
https://gitee.com/OAGroup/wechat.git
git@gitee.com:OAGroup/wechat.git
OAGroup
wechat
微信Go SDK
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891