/// <summary> /// 关闭订单 /// </summary> /// <param name="inputObj">提交给关闭订单API的参数</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> public static WxData CloseOrder(WxData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/closeorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WxException("关闭订单接口中,out_trade_no必填!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, timeOut); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 撤销订单API接口。成功时返回API调用结果,其他抛异常 /// </summary> /// <param name="inputObj">提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> public static WxData Reverse(WxData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; //请求开始时间 Log.Debug("WxPayApi", "撤销订单 request数据 : " + xml); string response = HttpService.Post(xml, url, true, timeOut); Log.Debug("WxPayApi", "撤销订单 response数据 : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 调用统一下单,获得下单结果 /// </summary> /// <returns></returns> public WxData GetUnifiedOrderResult() { //统一下单 WxData data = new WxData(); data.SetValue("body", body); data.SetValue("attach", attach); data.SetValue("out_trade_no", (string.IsNullOrEmpty(out_trade_no)) ? WxPayApi.GenerateOutTradeNo() : out_trade_no); data.SetValue("total_fee", total_fee); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("goods_tag", goods_tag); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", openid); data.SetValue("notify_url", notify_url); WxData result = WxPayApi.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxException("UnifiedOrder response error!"); } unifiedOrderResult = result; return(result); }
/// <summary> /// 转换短链接。 /// 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX), /// 减小二维码数据量,提升扫描速度和精确度。 /// </summary> /// <param name="inputObj">提交给转换短连接API的参数</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> public static WxData ShortUrl(WxData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/tools/shorturl"; //检测必填参数 if (!inputObj.IsSet("long_url")) { throw new WxException("需要转换的URL,签名用原串,传输需URL encode!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; //请求开始时间 Log.Debug("WxPayApi", "转换短链接 request数据 : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "转换短链接 response数据 : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 退款查询完整业务流程逻辑,退款查询结果(xml格式) /// </summary> /// <param name="refund_id">微信退款单号(优先使用)</param> /// <param name="out_refund_no">商户退款单号</param> /// <param name="transaction_id">微信订单号</param> /// <param name="out_trade_no">商户订单号</param> /// <returns></returns> public static string Run(string refund_id, string out_refund_no, string transaction_id, string out_trade_no) { Log.Info("RefundQuery", "RefundQuery is processing..."); WxData data = new WxData(); if (!string.IsNullOrEmpty(refund_id)) { data.SetValue("refund_id", refund_id);//微信退款单号,优先级最高 } else if (!string.IsNullOrEmpty(out_refund_no)) { data.SetValue("out_refund_no", out_refund_no);//商户退款单号,优先级第二 } else if (!string.IsNullOrEmpty(transaction_id)) { data.SetValue("transaction_id", transaction_id);//微信订单号,优先级第三 } else { data.SetValue("out_trade_no", out_trade_no);//商户订单号,优先级最低 } WxData result = WxPayApi.RefundQuery(data);//提交退款查询给API,接收返回数据 Log.Info("RefundQuery", "RefundQuery process complete, result : " + result.ToXml()); return(result.ToPrintStr()); }
/// <summary> /// 返回成功结果给微信 /// </summary> public void ReturnSuccessMsgToWx() { WxData res = new WxData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); ReturnMsgToWx(res); }
/// <summary> /// 返回失败结果给微信 /// </summary> /// <param name="errMsg"></param> public void ReturnErrorMsgToWx(string errMsg) { WxData res = new WxData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", errMsg); ReturnMsgToWx(res); }
/// <summary> /// 生成直接支付url,支付url有效期为2小时,模式二 /// </summary> /// <param name="productId">商品ID</param> /// <param name="total_fee">订单总金额(单位:分)</param> /// <returns></returns> public string GetPayUrl(string productId, int total_fee) { Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing..."); WxData data = new WxData(); data.SetValue("body", productId); //商品描述 data.SetValue("attach", ""); //附加数据 data.SetValue("out_trade_no", productId); //随机字符串 data.SetValue("total_fee", total_fee); //总金额 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); //交易起始时间 data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间 data.SetValue("goods_tag", ""); //商品标记 data.SetValue("trade_type", "NATIVE"); //交易类型 data.SetValue("product_id", productId); //商品ID WxData result = WxPayApi.UnifiedOrder(data); //调用统一下单接口 if (!result.IsSet("code_url")) { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxException("UnifiedOrder response error!"); } string url = result.GetValue("code_url").ToString(); //获得统一下单接口返回的二维码链接 Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url); return(url); }
/// <summary> /// 下载对账单完整业务流程逻辑,成功返回对账单结果(xml格式) /// </summary> /// <param name="bill_date">下载对账单的日期(格式:20140603,一次只能下载一天的对账单)</param> /// <param name="bill_type">账单类型(ALL,返回当日所有订单信息,默认值 ;SUCCESS,返回当日成功支付的订单;REFUND,返回当日退款订单;REVOKED,已撤销的订单;)</param> /// <returns></returns> public static string Run(string bill_date, string bill_type) { Log.Info("DownloadBill", "DownloadBill is processing..."); WxData data = new WxData(); data.SetValue("bill_date", bill_date); //账单日期 data.SetValue("bill_type", bill_type); //账单类型 WxData result = WxPayApi.DownloadBill(data); //提交下载对账单请求给API,接收返回结果 Log.Info("DownloadBill", "DownloadBill process complete, result : " + result.ToXml()); return(result.ToPrintStr()); }
/// <summary> /// 通过code换取网页授权access_token和openid的返回数据,正确时返回的JSON数据包如下: /// { /// "access_token":"ACCESS_TOKEN", /// "expires_in":7200, /// "refresh_token":"REFRESH_TOKEN", /// "openid":"OPENID", /// "scope":"SCOPE", /// "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" /// } /// 其中access_token可用于获取共享收货地址。 /// openid是微信支付jsapi支付接口统一下单时必须的参数。 /// 更详细的说明请参考网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html /// </summary> /// <param name="code"></param> public void GetOpenidAndAccessTokenFromCode(string code) { try { //构造获取openid及access_token的url WxData data = new WxData(); data.SetValue("appid", WxConfig.APPID); data.SetValue("secret", WxConfig.APPSECRET); data.SetValue("code", code); data.SetValue("grant_type", "authorization_code"); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl(); //请求url以获取数据 string result = HttpService.Get(url); Log.Debug(this.GetType().ToString(), "通过code换取网页授权(GetOpenidAndAccessTokenFromCode) response数据 : " + result); JsonData jd = JsonMapper.ToObject(result); string str_access_token = ""; try { str_access_token = (string)jd["access_token"]; if (!string.IsNullOrEmpty(str_access_token)) { //保存access_token,用于收货地址获取 access_token = (string)jd["access_token"]; //获取用户openid openid = (string)jd["openid"]; Log.Debug(this.GetType().ToString(), "Get openid : " + openid); Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token); } else { Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errcode : " + (string)jd["errcode"]); Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errmsg : " + (string)jd["errmsg"]); } } catch { Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errcode : " + (string)jd["errcode"]); Log.Error(this.GetType().ToString(), "通过code换取网页授权失败 errmsg : " + (string)jd["errmsg"]); } } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxException(ex.ToString()); } }
/// <summary> /// 申请退款,成功时返回接口调用结果,其他抛异常 /// </summary> /// <param name="inputObj">提交给申请退款API的参数</param> /// <param name="timeOut">超时时间</param> /// <returns></returns> public static WxData Refund(WxData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxException("退款申请接口中,out_trade_no、transaction_id至少填一个!"); } else if (!inputObj.IsSet("out_refund_no")) { throw new WxException("退款申请接口中,缺少必填参数out_refund_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxException("退款申请接口中,缺少必填参数total_fee!"); } else if (!inputObj.IsSet("refund_fee")) { throw new WxException("退款申请接口中,缺少必填参数refund_fee!"); } else if (!inputObj.IsSet("op_user_id")) { throw new WxException("退款申请接口中,缺少必填参数op_user_id!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; Log.Debug("WxPayApi", "退款申请 request数据 : " + xml); string response = HttpService.Post(xml, url, true, timeOut); //调用HTTP通信接口提交数据到API Log.Debug("WxPayApi", "退款申请 response数据 : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); //获得接口耗时 //将xml格式的结果转换为对象以返回 WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 提交刷卡支付API。成功时返回调用结果,其他抛异常。 /// 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台, /// 由商户收银台或者商户后台调用该接口发起支付。 /// </summary> /// <param name="inputObj">提交给被扫支付API的参数</param> /// <param name="timeOut">超时时间</param> /// <returns></returns> public static WxData Micropay(WxData inputObj, int timeOut = 10) { string url = "https://api.mch.weixin.qq.com/pay/micropay"; //检测必填参数 if (!inputObj.IsSet("body")) { throw new WxException("提交被扫支付API接口中,缺少必填参数body!"); } else if (!inputObj.IsSet("out_trade_no")) { throw new WxException("提交被扫支付API接口中,缺少必填参数out_trade_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxException("提交被扫支付API接口中,缺少必填参数total_fee!"); } else if (!inputObj.IsSet("auth_code")) { throw new WxException("提交被扫支付API接口中,缺少必填参数auth_code!"); } inputObj.SetValue("spbill_create_ip", WxConfig.IP); //终端ip inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; //请求开始时间 Log.Debug("WxPayApi", "刷卡支付 request数据 : " + xml); string response = HttpService.Post(xml, url, false, timeOut); //调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "刷卡支付 response数据 : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); //获得接口耗时 //将xml格式的结果转换为对象以返回 WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 /// 返回值:共享收货地址js函数需要的参数,json格式可以直接做参数使用 /// </summary> /// <returns></returns> public string GetEditAddressParameters() { string parameter = ""; try { string scheme = page.Request.Url.Scheme; string host = page.Request.Url.Host; string path = page.Request.Path; string queryString = page.Request.Url.Query; //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url string url = scheme + "://" + host + path + queryString; //构造需要用SHA1算法加密的数据 WxData signData = new WxData(); signData.SetValue("appid", WxConfig.APPID); signData.SetValue("url", url); signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp()); signData.SetValue("noncestr", WxPayApi.GenerateNonceStr()); signData.SetValue("accesstoken", access_token); string param = signData.ToUrl(); Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign); //获取收货地址js函数入口参数 WxData afterData = new WxData(); afterData.SetValue("appId", WxConfig.APPID); afterData.SetValue("scope", "jsapi_address"); afterData.SetValue("signType", "sha1"); afterData.SetValue("addrSign", addrSign); afterData.SetValue("timeStamp", signData.GetValue("timestamp")); afterData.SetValue("nonceStr", signData.GetValue("noncestr")); //转为json格式 parameter = afterData.ToJson(); Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter); } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxException(ex.ToString()); } return(parameter); }
/// <summary> /// 查询订单,判断订单真实性。 /// 1、判断成功返回true。 /// 2、判断失败返回false,并主动发消息告诉微信后台。 /// </summary> /// <param name="notifyData">微信支付后台发送过来的数据</param> /// <returns></returns> public bool IsTrueOrder(WxData notifyData) { //检查支付结果中transaction_id(微信支付订单号)是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 ReturnErrorMsgToWx("支付结果中微信订单号不存在"); } //微信支付订单号 string transaction_id = notifyData.GetValue("transaction_id").ToString(); WxData req = new WxData(); req.SetValue("transaction_id", transaction_id); WxData res = WxPayApi.OrderQuery(req); if (res.GetValue("return_code").ToString() == "SUCCESS" && res.GetValue("result_code").ToString() == "SUCCESS") { return(true); } else { ReturnErrorMsgToWx("订单查询失败!判断订单不存在!"); return(false); } }
/// <summary> /// 统一下单 /// </summary> /// <param name="openId"></param> /// <param name="productId"></param> /// <returns></returns> private WxData UnifiedOrder(string openId, string productId) { //统一下单 WxData req = new WxData(); req.SetValue("body", "test"); req.SetValue("attach", "test"); req.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); req.SetValue("total_fee", 1); req.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); req.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); req.SetValue("goods_tag", "test"); req.SetValue("trade_type", "NATIVE"); req.SetValue("openid", openId); req.SetValue("product_id", productId); WxData result = WxPayApi.UnifiedOrder(req); return(result); }
/// <summary> /// 订单查询完整业务流程逻辑,返回订单查询结果(xml格式) /// </summary> /// <param name="transaction_id">微信订单号(优先使用)</param> /// <param name="out_trade_no">商户订单号</param> /// <returns></returns> public static string Run(string transaction_id, string out_trade_no) { Log.Info("OrderQuery", "OrderQuery is processing..."); WxData data = new WxData(); if (!string.IsNullOrEmpty(transaction_id))//如果微信订单号存在,则以微信订单号为准 { data.SetValue("transaction_id", transaction_id); } else//微信订单号不存在,才根据商户订单号去查单 { data.SetValue("out_trade_no", out_trade_no); } WxData result = WxPayApi.OrderQuery(data); //提交订单查询请求给API,接收返回数据 Log.Info("OrderQuery", "OrderQuery process complete, result : " + result.ToXml()); return(result.ToPrintStr()); }
/// <summary> /// 查询订单,判断订单真实性。处理支付回调结果。 /// 1、判断成功,主动发消息告诉微信后台。 /// 2、判断失败,主动发消息告诉微信后台。 /// </summary> public override void ProcessNotify() { WxData notifyData = GetNotifyData(); //检查支付结果中transaction_id(微信支付订单号)是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 WxData res = new WxData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } //微信支付订单号 string transaction_id = notifyData.GetValue("transaction_id").ToString(); //查询订单,判断订单真实性 if (!QueryOrder(transaction_id)) { //若订单查询失败,则立即返回结果给微信支付后台 WxData res = new WxData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } //查询订单成功 else { WxData res = new WxData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } }
/// <summary> /// 申请退款完整业务流程逻辑,退款结果(xml格式) /// </summary> /// <param name="transaction_id">微信订单号(优先使用)</param> /// <param name="out_trade_no">商户订单号</param> /// <param name="total_fee">订单总金额</param> /// <param name="refund_fee">退款金额</param> /// <returns></returns> public static string Run(string transaction_id, string out_trade_no, string total_fee, string refund_fee) { Log.Info("Refund", "Refund is processing..."); WxData data = new WxData(); if (!string.IsNullOrEmpty(transaction_id))//微信订单号存在的条件下,则已微信订单号为准 { data.SetValue("transaction_id", transaction_id); } else//微信订单号不存在,才根据商户订单号去退款 { data.SetValue("out_trade_no", out_trade_no); } data.SetValue("total_fee", int.Parse(total_fee)); //订单总金额 data.SetValue("refund_fee", int.Parse(refund_fee)); //退款金额 data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo()); //随机生成商户退款单号 data.SetValue("op_user_id", WxConfig.MCHID); //操作员,默认为商户号 WxData result = WxPayApi.Refund(data); //提交退款申请给API,接收返回数据 Log.Info("Refund", "Refund process complete, result : " + result.ToXml()); return(result.ToPrintStr()); }
/// <summary> /// 通过OpenID获取微信用户信息 /// </summary> /// <returns></returns> private WXUserInfo GetUserInfo(string openid, string ACCESS_TOKEN) { try { WxData data = new WxData(); data.SetValue("access_token", ACCESS_TOKEN); data.SetValue("openid", openid); data.SetValue("lang", "zh_CN"); string url = "https://api.weixin.qq.com/cgi-bin/user/info?" + data.ToUrl(); //请求url以获取数据 string result = WebRequestHelper.Get(url); //保存用户信息 WXUserInfo wxuserinfo = JsonConvert.DeserializeObject <WXUserInfo>(result); DAL.Log.Instance.Write("请求:" + data.ToUrl() + ",返回:" + result, "获取微信用户信息"); return(wxuserinfo); } catch (Exception ex) { DAL.Log.Instance.Write(ex.ToString(), "获取微信用户信息错误"); throw new Exception(ex.ToString()); } }
/// <summary> /// 生成签名 /// </summary> /// <param name="noncestr">随机字符串</param> /// <param name="timestamp">时间戳</param> /// <param name="jsapi_ticket"></param> /// <param name="url">调用JS接口页面的完整URL</param> /// <returns></returns> public static string MakeSign(string noncestr, string timestamp, string jsapi_ticket, string url) { if (string.IsNullOrEmpty(noncestr) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(jsapi_ticket) || string.IsNullOrEmpty(url)) { return(""); } //构造需要用SHA1算法加密的数据 WxData signData = new WxData(); signData.SetValue("url", url); signData.SetValue("timestamp", timestamp); signData.SetValue("noncestr", noncestr); signData.SetValue("jsapi_ticket", jsapi_ticket); string param = signData.ToUrl(); Log.Debug("SHA1加密前参数", param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug("SHA1加密后参数", addrSign); return(addrSign); }
/// <summary> /// 查询退款,提交退款申请后,通过该接口查询退款状态。 /// 退款有一定延时,用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。 /// out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个 /// </summary> /// <param name="inputObj">提交给查询退款API的参数</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> public static WxData RefundQuery(WxData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/refundquery"; //检测必填参数 if (!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id")) { throw new WxException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; //请求开始时间 Log.Debug("WxPayApi", "退款查询 request数据 : " + xml); string response = HttpService.Post(xml, url, false, timeOut); //调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "退款查询 response数据 : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); //获得接口耗时 //将xml格式的结果转换为对象以返回 WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 下载对账单 /// </summary> /// <param name="inputObj">提交给下载对账单API的参数</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> public static WxData DownloadBill(WxData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/downloadbill"; //检测必填参数 if (!inputObj.IsSet("bill_date")) { throw new WxException("对账单接口中,缺少必填参数bill_date!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); Log.Debug("WxPayApi", "下载对账单 request数据 : " + xml); string response = HttpService.Post(xml, url, false, timeOut); //调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "下载对账单 result数据 : " + response); WxData result = new WxData(); //若接口调用失败会返回xml格式的结果 if (response.Substring(0, 5) == "<xml>") { result.FromXml(response); } //接口调用成功则返回非xml格式的数据 else { result.SetValue("result", response); } return(result); }
/// <summary> /// 网页授权获取用户基本信息的全部过程。 /// 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html。 /// 第一步:利用url跳转获取code。 /// 第二步:利用code去获取openid和access_token。 /// 第三步:拉取用户信息(scopeType=1时) /// </summary> /// <param name="scopeType">0:snsapi_base;1:snsapi_userinfo;</param> public void GetOpenidAndAccessToken(int scopeType) { if (!string.IsNullOrEmpty(page.Request.QueryString["code"])) { //获取code码,以获取openid和access_token string code = page.Request.QueryString["code"]; Log.Debug(this.GetType().ToString(), "网页授权获取用户基本信息Get code : " + code); GetOpenidAndAccessTokenFromCode(code); if (scopeType == 1) { GetUserInfoFromOpenidAndAccessToken(openid, access_token); } } else { //构造网页授权获取code的URL //取提交页面的完整URL,包括请求参数 string redirect_uri = HttpUtility.UrlEncode(page.Request.Url.ToString()); WxData data = new WxData(); data.SetValue("appid", WxConfig.APPID); data.SetValue("redirect_uri", redirect_uri); data.SetValue("response_type", "code"); data.SetValue("scope", (scopeType == 0) ? "snsapi_base" : "snsapi_userinfo"); data.SetValue("state", "STATE" + "#wechat_redirect"); string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl(); Log.Debug(this.GetType().ToString(), "网页授权获取用户基本信息跳转到URL : " + url); try { //触发微信返回code码 //Redirect函数会抛出ThreadAbortException异常,不用处理这个异常 page.Response.Redirect(url); } catch (System.Threading.ThreadAbortException ex) { } } }
/// <summary> /// 查询订单 /// </summary> /// <param name="transaction_id"></param> /// <returns></returns> private bool QueryOrder(string transaction_id) { WxData req = new WxData(); req.SetValue("transaction_id", transaction_id); WxData res = WxPayApi.OrderQuery(req); if (res.GetValue("return_code").ToString() == "SUCCESS" && res.GetValue("result_code").ToString() == "SUCCESS") { return(true); } else { return(false); } }
/// <summary> /// 测速上报接口实现。成功时返回测速上报接口返回的结果,其他抛异常 /// </summary> /// <param name="inputObj">提交给测速上报接口的参数</param> /// <param name="timeOut">测速上报接口超时时间(秒)</param> /// <returns></returns> public static WxData Report(WxData inputObj, int timeOut = 1) { string url = "https://api.mch.weixin.qq.com/payitil/report"; //检测必填参数 if (!inputObj.IsSet("interface_url")) { throw new WxException("接口URL,缺少必填参数interface_url!"); } if (!inputObj.IsSet("return_code")) { throw new WxException("返回状态码,缺少必填参数return_code!"); } if (!inputObj.IsSet("result_code")) { throw new WxException("业务结果,缺少必填参数result_code!"); } if (!inputObj.IsSet("user_ip")) { throw new WxException("访问接口IP,缺少必填参数user_ip!"); } if (!inputObj.IsSet("execute_time_")) { throw new WxException("接口耗时,缺少必填参数execute_time_!"); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("user_ip", WxConfig.IP); //终端ip inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss")); //商户上报时间 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); Log.Info("WxPayApi", "测试上报 request数据 : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Info("WxPayApi", "测试上报 response数据 : " + response); WxData result = new WxData(); result.FromXml(response); return(result); }
/// <summary> /// 生成扫描支付模式一URL /// </summary> /// <param name="productId">商品ID</param> /// <returns></returns> public string GetPrePayUrl(string productId) { Log.Info(this.GetType().ToString(), "Native pay mode 1 url is producing..."); WxData data = new WxData(); data.SetValue("appid", WxConfig.APPID); //公众帐号id data.SetValue("mch_id", WxConfig.MCHID); //商户号 data.SetValue("time_stamp", WxPayApi.GenerateTimeStamp()); //时间戳 data.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串 data.SetValue("product_id", productId); //商品ID data.SetValue("sign", data.MakeSign()); //签名 string str = ToUrlParams(data.GetValues()); //转换为URL串 string url = "weixin://wxpay/bizpayurl?" + str; Log.Info(this.GetType().ToString(), "Get native pay mode 1 url : " + url); return(url); }
/// <summary> /// 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数, /// 微信浏览器调起JSAPI时的输入参数格式如下: /// { /// "appId" : "wx2421b1c4370ec43b", //公众号名称,由商户传入 /// "timeStamp":" 1395712654", //时间戳,自1970年以来的秒数 /// "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串 /// "package" : "prepay_id=u802345jgfjsdfgsdg888", /// "signType" : "MD5", //微信签名方式: /// "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 /// } /// 返回值:微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用。 /// 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 /// </summary> /// <returns></returns> public string GetJsApiParameters() { Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing..."); WxData jsApiParam = new WxData(); jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string parameters = jsApiParam.ToJson(); Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); return(parameters); }
/// <summary> /// 微信访问入口页,获取用户UnionID和OpenID /// </summary> /// <param name="intVisit">访问:1首页;2定制;3我的;4管家主页;5商家主页</param> /// <param name="strTargetID">目标编码:4管家编码;5商家编码</param> /// <returns></returns> public ActionResult Index(int intVisit = 0, string strTargetID = "") { ViewBag.UnionID = ""; ViewBag.OpenID = ""; ViewBag.Visit = intVisit; ViewBag.TargetID = strTargetID; ViewBag.ErrorMessage = ""; string strWxCode = "", strVisitResult = "", strAccessToken = ""; Dictionary <string, object> dicResult = null; try { if (string.IsNullOrEmpty(Request.QueryString["code"])) { #region 获取Code string strMyUrl = "http://" + Request.Url.Host + Request.Path + "?intVisit=" + intVisit + "&strTargetID=" + strTargetID; WxData wxdCode = new WxData(); wxdCode.SetValue("appid", WxConfig.AppID); wxdCode.SetValue("redirect_uri", HttpUtility.UrlEncode(strMyUrl)); wxdCode.SetValue("response_type", "code"); //wxdAuthorize.SetValue("scope", "snsapi_base"); wxdCode.SetValue("scope", "snsapi_userinfo"); wxdCode.SetValue("state", "STATE" + "#wechat_redirect"); Response.Redirect(WxConfig.AuthorizeUrl + wxdCode.ToUrl()); #endregion 获取Code } else { strWxCode = Request.QueryString["code"].Trim(); JavaScriptSerializer jss = new JavaScriptSerializer(); #region 获取OpenID WxData wxdOpen = new WxData(); wxdOpen.SetValue("appid", WxConfig.AppID); wxdOpen.SetValue("secret", WxConfig.AppSecret); wxdOpen.SetValue("code", strWxCode); wxdOpen.SetValue("grant_type", "authorization_code"); strVisitResult = WxHttp.Get(WxConfig.AccessTokenUrl + wxdOpen.ToUrl()); dicResult = (Dictionary <string, object>)jss.Deserialize(strVisitResult, typeof(Dictionary <string, object>)); if (dicResult.ContainsKey("errcode")) { throw new Exception("获取网页用户授权出错:" + dicResult["errmsg"].ToString()); } strAccessToken = dicResult["access_token"].ToString(); ViewBag.OpenID = dicResult["openid"].ToString(); #endregion 获取OpenID #region 获取UnionID strVisitResult = WxHttp.Get(WxConfig.UserUrl + "access_token=" + strAccessToken + "&openid=" + ViewBag.OpenID + "&lang=zh_CN"); dicResult = (Dictionary <string, object>)jss.Deserialize(strVisitResult, typeof(Dictionary <string, object>)); if (dicResult.ContainsKey("errcode")) { throw new Exception("获取用户信息出错:" + dicResult["errmsg"].ToString()); } ViewBag.UnionID = dicResult["unionid"].ToString(); #endregion 获取UnionID } } catch (Exception ex) { WriteLog("Weixin", "Index", "Exception:" + ex.Message.ToString()); ViewBag.ErrorMessage = ex.Message.ToString(); } return(View()); }
public ActionResult WxJsapiAuthorize() { string strUrl = ""; string strGet = ""; string strAccessToken = ""; //令牌 string strJsapiTicket = ""; //票据 DateTime dtmTicketExpire = DefaultTime; //票据到期时间 string strResult = ""; JsonData jsdResult; JsonResult jsrReturn = new JsonResult(); string strNonce = ""; string strTimestamp = ""; string strSignature = ""; try { if (string.IsNullOrEmpty(Request.Form["strUrl"])) { throw new Exception("strUrl为空!"); } strUrl = Request.Form["strUrl"].Trim(); WriteLog("Weixin", "WxJsapiAuthorize", "strUrl:" + strUrl); Dictionary <string, object> dicTicket = new Dictionary <string, object>(); dicTicket = GetJsapiTicket(strUrl); if (dicTicket.Count <= 0) { #region 获得微信JS接口的临时票据 strGet = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WxConfig.AppID + "&secret=" + WxConfig.AppSecret; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strAccessToken = (string)jsdResult["access_token"]; WriteLog("Weixin", "WxJsapiAuthorize", "strAccessToken:" + strAccessToken); strGet = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strAccessToken + "&type=jsapi"; dtmTicketExpire = DateTime.Now; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strJsapiTicket = (string)jsdResult["ticket"]; WriteLog("Weixin", "WxJsapiAuthorize", "strJsapiTicket:" + strJsapiTicket); dtmTicketExpire = dtmTicketExpire.AddMinutes(110); dicTicket = new Dictionary <string, object>(); dicTicket.Add("Ticket", strJsapiTicket); dicTicket.Add("Expire", dtmTicketExpire); SetJsapiTicket(strUrl, dicTicket); #endregion 获得微信JS接口的临时票据 } else { if (dicTicket.ContainsKey("Ticket")) { strJsapiTicket = (string)dicTicket["Ticket"]; } if (dicTicket.ContainsKey("Expire")) { dtmTicketExpire = (DateTime)dicTicket["Expire"]; } if (dtmTicketExpire <= DateTime.Now || strJsapiTicket == "") { #region 获得微信JS接口的临时票据 strGet = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WxConfig.AppID + "&secret=" + WxConfig.AppSecret; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strAccessToken = (string)jsdResult["access_token"]; WriteLog("Weixin", "WxJsapiAuthorize", "strAccessToken:" + strAccessToken); strGet = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strAccessToken + "&type=jsapi"; dtmTicketExpire = DateTime.Now; strResult = WxHttp.Get(strGet); jsdResult = JsonMapper.ToObject(strResult); strJsapiTicket = (string)jsdResult["ticket"]; WriteLog("Weixin", "WxJsapiAuthorize", "strJsapiTicket:" + strJsapiTicket); dtmTicketExpire = dtmTicketExpire.AddMinutes(110); dicTicket = new Dictionary <string, object>(); dicTicket.Add("Ticket", strJsapiTicket); dicTicket.Add("Expire", dtmTicketExpire); SetJsapiTicket(strUrl, dicTicket); #endregion 获得微信JS接口的临时票据 } } WxData wxdTicket = new WxData(); strNonce = WxApi.GenerateNonceStr(); wxdTicket.SetValue("noncestr", strNonce); wxdTicket.SetValue("jsapi_ticket", strJsapiTicket); strTimestamp = WxApi.GenerateTimeStamp(); wxdTicket.SetValue("timestamp", strTimestamp); wxdTicket.SetValue("url", strUrl); string strParam = wxdTicket.ToUrl(); WriteLog("Weixin", "WxJsapiAuthorize", "strParam:" + strParam); strSignature = GetSwcSH1(strParam);// FormsAuthentication.HashPasswordForStoringInConfigFile(strParam, "SHA1"); WriteLog("Weixin", "WxJsapiAuthorize", "strSignature:" + strSignature); jsrReturn.Data = new { Result = 1, AppID = WxConfig.AppID, Timestamp = strTimestamp, Noncestr = strNonce, Signature = strSignature }; } catch (Exception ex) { WriteLog("Weixin", "WxJsapiAuthorize", "Exception:" + ex.Message.ToString()); jsrReturn.Data = new { Result = -99, Message = ex.Message.ToString() }; } return(jsrReturn); }
/// <summary> /// 通过OpenID和AccessToken拉取用户信息的返回数据,正确时返回的JSON数据包如下: /// { /// "openid":" OPENID", //用户的唯一标识 /// "nickname": NICKNAME, //用户昵称 /// "sex":"1",//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知 /// "province":"PROVINCE"//用户个人资料填写的省份 /// "city":"CITY",//普通用户个人资料填写的城市 /// "country":"COUNTRY",//国家,如中国为CN /// "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", //用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。 /// "privilege":["PRIVILEGE1","PRIVILEGE2"],//用户特权信息,json 数组,如微信沃卡用户为(chinaunicom) /// "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"//只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。 /// } /// 更详细的说明请参考:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html /// </summary> /// <param name="OpenID"></param> /// <param name="AccessToken"></param> public void GetUserInfoFromOpenidAndAccessToken(string OpenID, string AccessToken) { try { //构造拉取用户信息的url WxData data = new WxData(); data.SetValue("access_token", AccessToken); data.SetValue("openid", OpenID); data.SetValue("lang", "zh_CN"); string url = "https://api.weixin.qq.com/sns/userinfo?" + data.ToUrl(); //请求url以获取数据 string result = HttpService.Get(url); Log.Debug(this.GetType().ToString(), "通过OpenID和AccessToken拉取用户信息(GetUserInfoFromOpenidAndAccessToken) response数据 : " + result); JsonData jd = JsonMapper.ToObject(result); if (!string.IsNullOrEmpty((string)jd["openid"])) { UserInfoResult = new WxData(); UserInfoResult.SetValue("openid", (string)jd["openid"]); UserInfoResult.SetValue("nickname", (string)jd["nickname"]); UserInfoResult.SetValue("sex", (int)jd["sex"]); UserInfoResult.SetValue("language", (string)jd["language"]); UserInfoResult.SetValue("city", (string)jd["city"]); UserInfoResult.SetValue("province", (string)jd["province"]); UserInfoResult.SetValue("country", (string)jd["country"]); UserInfoResult.SetValue("headimgurl", (string)jd["headimgurl"]); if (jd["privilege"].IsArray) { string strArray = ""; JsonData jdItems = jd["privilege"]; for (int i = 0; i < jdItems.Count; i++) { strArray = strArray + (string)jdItems[i] + ","; } strArray = strArray.TrimEnd(','); UserInfoResult.SetValue("privilege", strArray); } if (((IDictionary)jd).Contains("unionid")) { UserInfoResult.SetValue("unionid", (string)jd["unionid"]); } Log.Debug(this.GetType().ToString(), "用户信息 openid: " + UserInfoResult.GetValue("openid")); Log.Debug(this.GetType().ToString(), "用户信息 nickname: " + UserInfoResult.GetValue("nickname")); Log.Debug(this.GetType().ToString(), "用户信息 sex: " + UserInfoResult.GetValue("sex")); Log.Debug(this.GetType().ToString(), "用户信息 language: " + UserInfoResult.GetValue("language")); Log.Debug(this.GetType().ToString(), "用户信息 city: " + UserInfoResult.GetValue("city")); Log.Debug(this.GetType().ToString(), "用户信息 province: " + UserInfoResult.GetValue("province")); Log.Debug(this.GetType().ToString(), "用户信息 country: " + UserInfoResult.GetValue("country")); Log.Debug(this.GetType().ToString(), "用户信息 headimgurl: " + UserInfoResult.GetValue("headimgurl")); Log.Debug(this.GetType().ToString(), "用户信息 privilege: " + UserInfoResult.GetValue("privilege")); if (UserInfoResult.IsSet("unionid")) { Log.Debug(this.GetType().ToString(), "用户信息 unionid: " + UserInfoResult.GetValue("unionid")); } } else { Log.Error(this.GetType().ToString(), "通过OpenID和AccessToken拉取用户信息失败 errcode : " + (string)jd["errcode"]); Log.Error(this.GetType().ToString(), "通过OpenID和AccessToken拉取用户信息失败 errmsg : " + (string)jd["errmsg"]); } } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxException(ex.ToString()); } }