/// <summary> /// 获取响应结果 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="contentStr"></param> /// <param name="checkSign">是否检查返回签名,个别接口没有</param> /// <returns></returns> protected T GetRespResult <T>(string contentStr, bool checkSign = true) where T : WXPayBaseResp, new() { XmlDocument resultXml = null; var dics = WXXmlHelper.ChangXmlToDir(contentStr, ref resultXml); var t = new T { RespXml = resultXml }; t.FromResContent(dics); if (t.return_code.ToUpper() != "SUCCESS" || t.result_code.ToUpper() != "SUCCESS") { t.ret = (int)RespTypes.OperateFailed; t.msg = string.Concat(t.return_msg, t.err_code_des); return(t); } if (checkSign) { var sign = dics["sign"]?.ToString(); if (string.IsNullOrEmpty(sign) || sign != GetSign(GetSignContent(dics))) { t.ret = (int)RespTypes.ParaError; t.msg = "返回签名信息校验不正确!"; } } return(t); }
/// <summary> /// 获取app唤起客户端参数 /// </summary> /// <param name="prepayId">预支付交易Id</param> /// <returns></returns> public WXGetAppClientParaResp GetAppClientParaResp(string prepayId) { var appPara = new WXGetAppClientParaResp { app_id = ApiConfig.AppId, mch_id = ApiConfig.MchId, time_stamp = DateTime.Now.ToLocalSeconds().ToString(), nonce = WXXmlHelper.GenerateNonceStr(), prepay_id = prepayId, package = "Sign=WXPay" }; var dics = new SortedDictionary <string, object> { ["appid"] = appPara.app_id, ["partnerid"] = appPara.mch_id, ["timestamp"] = appPara.time_stamp, ["noncestr"] = appPara.nonce, ["package"] = appPara.package, ["prepayid"] = appPara.prepay_id }; appPara.sign = GetSign(GetSignContent(dics)); return(appPara); }
/// <summary> /// 获取企业付款信息 /// </summary> /// <param name="partner_trade_no"></param> /// <returns></returns> public async Task <WXPayGetTransferCashResp> GetTransferCashAsync(string partner_trade_no) { var urlStr = string.Concat(m_ApiUrl, "/mmpaymkttransfers/gettransferinfo"); var dics = new SortedDictionary <string, object> { ["nonce_str"] = WXXmlHelper.GenerateNonceStr(), ["partner_trade_no"] = partner_trade_no }; return(await PostApiAsync <WXPayGetTransferCashResp>(urlStr, dics, null, true, false)); }
/// <summary> /// 授权码查询OPENID接口 /// </summary> /// <param name="auth_code"></param> /// <returns></returns> public async Task <WXPayAuthCodeOpenIdResp> GetAuthCodeOpenIdAsync(string auth_code) { var url = string.Concat(m_ApiUrl, "/tools/authcodetoopenid"); var dics = new SortedDictionary <string, object> { ["nonce_str"] = WXXmlHelper.GenerateNonceStr(), ["auth_code"] = auth_code }; return(await PostApiAsync <WXPayAuthCodeOpenIdResp>(url, dics)); }
/// <summary> /// 查询红包 /// </summary> /// <param name="mch_billno">商户订单号</param> /// <param name="bill_type">订单类型</param> /// <returns></returns> public async Task <WXPayQueryRedResp> QueryRedAsync(string mch_billno, string bill_type = "MCHT") { var urlStr = string.Concat(m_ApiUrl, "/mmpaymkttransfers/gethbinfo"); var dics = new SortedDictionary <string, object> { ["nonce_str"] = WXXmlHelper.GenerateNonceStr(), ["mch_billno"] = mch_billno, ["bill_type"] = bill_type }; return(await PostApiAsync <WXPayQueryRedResp>(urlStr, dics, null, true)); }
/// <summary> /// 转换短链api /// </summary> /// <param name="long_url"></param> /// <returns></returns> public async Task <WXPayGetShortUrlResp> GetShortUrlAsync(string long_url) { var url = string.Concat(m_ApiUrl, "/tools/shorturl"); var dics = new SortedDictionary <string, object> { ["nonce_str"] = WXXmlHelper.GenerateNonceStr(), ["long_url"] = long_url }; return(await PostApiAsync <WXPayGetShortUrlResp>(url, dics, null, false, true, d => d["long_url"] = d["long_url"].ToString().UrlEncode())); }
/// <summary> /// 生成二维码地址(扫码支付模式一) /// </summary> /// <param name="product_id"></param> /// <returns></returns> public string CreateScanCode(string product_id) { var dics = new SortedDictionary <string, object> { ["time_stamp"] = DateTime.Now.ToLocalSeconds().ToString(), ["nonce_str"] = WXXmlHelper.GenerateNonceStr(), ["product_id"] = product_id, ["appid"] = ApiConfig.AppId, ["mch_id"] = ApiConfig.MchId }; var encStr = GetSignContent(dics); var sign = GetSign(encStr); return(string.Concat("weixin://wxpay/bizpayurl?", encStr, "&sign=", sign)); }
/// <summary> /// 获取js和小程序唤起客户端参数 /// </summary> /// <param name="prepayId">预支付交易Id</param> /// <returns></returns> public WXGetJsClientParaResp GetJsClientParaResp(string prepayId) { var jsPara = new WXGetJsClientParaResp { app_id = ApiConfig.AppId, time_stamp = DateTime.Now.ToLocalSeconds().ToString(), nonce = WXXmlHelper.GenerateNonceStr(), package = string.Concat("prepay_id=", prepayId) }; var dics = new SortedDictionary <string, object> { ["appId"] = jsPara.app_id, ["timeStamp"] = jsPara.time_stamp, ["nonceStr"] = jsPara.nonce, ["package"] = jsPara.package, ["signType"] = jsPara.sign_type }; jsPara.sign = GetSign(GetSignContent(dics)); return(jsPara); }
public WXPayBaseReq() { var nonceStr = WXXmlHelper.GenerateNonceStr(); _dics["nonce_str"] = nonceStr; }