package gopay import ( "bytes" "crypto/hmac" "crypto/md5" "crypto/sha256" "encoding/hex" "strings" ) //JSAPI支付,支付参数后,再次计算出小程序用的paySign func GetMiniPaySign(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 } //本地通过支付参数计算Sign值 func getLocalSign(secretKey string, signType string, body BodyMap) (sign string) { signStr := sortSignParams(secretKey, body) //fmt.Println("signStr:", signStr) 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) } sign = strings.ToUpper(hex.EncodeToString(hashSign)) return } //从微信提供的接口获取:SandboxSignKey func getSanBoxSign(mchId, nonceStr, secretKey, signType string) (key string, err error) { body := make(BodyMap) body.Set("mch_id", mchId) body.Set("nonce_str", nonceStr) //计算沙箱参数Sign sanboxSign := getLocalSign(secretKey, signType, body) //沙箱环境:获取key后,重新计算Sign key, err = getSanBoxSignKey(mchId, nonceStr, sanboxSign) if err != nil { return "", err } return } //生成请求XML的Body体 func generateXml(bm BodyMap) (reqXml string) { buffer := new(bytes.Buffer) buffer.WriteString("") for k, v := range bm { buffer.WriteString("<") buffer.WriteString(k) buffer.WriteString(">") } buffer.WriteString("") reqXml = buffer.String() return }