Jerry 7 лет назад
Родитель
Сommit
eda660aec1
2 измененных файлов с 50 добавлено и 1 удалено
  1. 1 1
      wechat_client_test.go
  2. 49 0
      wechat_servier_api.go

+ 1 - 1
wechat_client_test.go

@@ -276,7 +276,7 @@ func TestMd5(t *testing.T) {
 }
 
 func TestCode2Session(t *testing.T) {
-	userIdRsp, err := Code2Session(AppID, APPSecret, "081fuZyJ1wqYE20z15yJ15T2zJ1fuZyd")
+	userIdRsp, err := Code2Session(AppID, APPSecret, "011EZg6p0VO47n1p2W4p0mle6p0EZg6u")
 	if err != nil {
 		fmt.Println("err:", err)
 		return

+ 49 - 0
wechat_servier_api.go

@@ -28,6 +28,55 @@ func HttpAgent() (agent *gorequest.SuperAgent) {
 	return
 }
 
+//验证支付成功后通知Sign值
+func VerifyPayResultSign(apiKey string, notifyRsp *WeChatNotifyRequest) (ok bool, sign string) {
+
+	body := make(BodyMap)
+	body.Set("return_code", notifyRsp.ReturnCode)
+	body.Set("return_msg", notifyRsp.ReturnMsg)
+	body.Set("appid", notifyRsp.Appid)
+	body.Set("mch_id", notifyRsp.MchId)
+	body.Set("device_info", notifyRsp.DeviceInfo)
+	body.Set("nonce_str", notifyRsp.NonceStr)
+	body.Set("sign_type", notifyRsp.SignType)
+	body.Set("result_code", notifyRsp.ResultCode)
+	body.Set("err_code", notifyRsp.ErrCode)
+	body.Set("err_code_des", notifyRsp.ErrCodeDes)
+	body.Set("openid", notifyRsp.Openid)
+	body.Set("is_subscribe", notifyRsp.IsSubscribe)
+	body.Set("trade_type", notifyRsp.TradeType)
+	body.Set("bank_type", notifyRsp.BankType)
+	body.Set("total_fee", notifyRsp.TotalFee)
+	body.Set("settlement_total_fee", notifyRsp.SettlementTotalFee)
+	body.Set("fee_type", notifyRsp.FeeType)
+	body.Set("cash_fee", notifyRsp.CashFee)
+	body.Set("cash_fee_type", notifyRsp.CashFeeType)
+	body.Set("coupon_fee", notifyRsp.CouponFee)
+	body.Set("coupon_count", notifyRsp.CouponCount)
+	body.Set("coupon_type_0", notifyRsp.CouponType0)
+	body.Set("coupon_id_0", notifyRsp.CouponId0)
+	body.Set("coupon_fee_$n", notifyRsp.CouponFee0)
+	body.Set("transaction_id", notifyRsp.TransactionId)
+	body.Set("out_trade_no", notifyRsp.OutTradeNo)
+	body.Set("attach", notifyRsp.Attach)
+	body.Set("time_end", notifyRsp.TimeEnd)
+
+	signStr := sortSignParams(apiKey, body)
+	var hashSign []byte
+	if notifyRsp.SignType == SignType_MD5 {
+		hash := md5.New()
+		hash.Write([]byte(signStr))
+		hashSign = hash.Sum(nil)
+	} else {
+		hash := hmac.New(sha256.New, []byte(apiKey))
+		hash.Write([]byte(signStr))
+		hashSign = hash.Sum(nil)
+	}
+	sign = strings.ToUpper(hex.EncodeToString(hashSign))
+	ok = sign == notifyRsp.SignType
+	return
+}
+
 //JSAPI支付,支付参数后,再次计算出小程序用的paySign
 func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string) (paySign string) {
 	buffer := new(bytes.Buffer)