Asp.net 微信H5唤起支付和支付回调
做任何商城类的项目都离不开支付这一环节,今天就记录一下在开发微信H5支付时的开发过程
在开发到订单这部分模块的时候终于遇到了微信开发第二个难题,微信支付!
首先请阅读 微信JSAPI支付文档 和 微信统一下单API 了解场景和参数
微信支付需要有 微信公众平台 和 微信商户平台 账号
在微信商户平台配置 API秘钥
配置 JSAPI域名
配置完这些之后 开始写 WechatHelper 所用到的类
目录
Data 用来存放需要用到的参数
public class WxPayData { public WxPayData() { } //采用排序的Dictionary的好处是方便对数据包进行签名,不用再签名之前再做一次排序 private SortedDictionary<string, object> m_values = new SortedDictionary<string, object>(); /** * 设置某个字段的值 * @param key 字段名 * @param value 字段值 */ public void SetValue(string key, object value) { m_values[key] = value; } /** * 根据字段名获取某个字段的值 * @param key 字段名 * @return key对应的字段值 */ public object GetValue(string key) { object o = null; m_values.TryGetValue(key, out o); return o; } /** * 判断某个字段是否已设置 * @param key 字段名 * @return 若字段key已被设置,则返回true,否则返回false */ public bool IsSet(string key) { object o = null; m_values.TryGetValue(key, out o); if (null != o) return true; else return false; } /** * @将Dictionary转成xml * @return 经转换得到的xml串 * @throws WxPayException **/ public string ToXml() { //数据为空时不能转化为xml格式 if (0 == m_values.Count) { throw new WxPayException("WxPayData数据为空!"); } string xml = ""; foreach (KeyValuePair<string, object> pair in m_values) { //字段值不能为null,会影响后续流程 if (pair.Value == null) { throw new WxPayException("WxPayData内部含有值为null的字段!"); } if (pair.Value.GetType() == typeof(int)) { xml += "<" + pair.Key + ">" + pair.Value + "" + pair.Key + ">"; } else if (pair.Value.GetType() == typeof(string)) { xml += "<" + pair.Key + ">" + "" + pair.Value + "]]>" + pair.Key + ">"; } else//除了string和int类型不能含有其他数据类型 { throw new WxPayException("WxPayData字段数据类型错误!"); } } xml += ""; return xml; } /** * @将xml转为WxPayData对象并返回对象内部的数据 * @param string 待转换的xml串 * @return 经转换得到的Dictionary * @throws WxPayException */ public SortedDictionary<string, object> FromXml(string xml) { if (string.IsNullOrEmpty(xml)) { throw new WxPayException("将空的xml串转换为WxPayData不合法!"); } XmlDocument xmlDoc = new Xml.XmlDocument_XxeFixed(); xmlDoc.LoadXml(xml); XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点 XmlNodeList nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中 } try { //2015-06-29 错误是没有签名 if (m_values["return_code"].ToString() != "SUCCESS") { return m_values; } CheckSign();//验证签名,不通过会抛异常 } catch (WxPayException ex) { throw new WxPayException(ex.Message); } return m_values; } /** * @Dictionary格式转化成url参数格式 * @ return url格式串, 该串不包含sign字段值 */ public string ToUrl() { string buff = ""; foreach (KeyValuePair<string, object> pair in m_values) { if (pair.Value == null) { throw new WxPayException("WxPayData内部含有值为null的字段!"); } if (pair.Key != "sign" && pair.Value.ToString() != "") { buff += pair.Key + "=" + pair.Value + "&"; } } buff = buff.Trim('&'); return buff; } /** * @Dictionary格式化成Json * @return json串数据 */ public string ToJson() { // string jsonStr = JsonMapper.ToJson(m_values); return ""; } /** * @values格式化成能在Web页面上显示的结果(因为web页面上不能直接输出xml格式的字符串) */ public string ToPrintStr() { string str = ""; foreach (KeyValuePair<string, object> pair in m_values) { if (pair.Value == null) { throw new WxPayException("WxPayData内部含有值为null的字段!"); } str += string.Format("{0}={1}
", pair.Key, pair.Value.ToString()); } return str; } /** * @生成签名,详见签名生成算法 * @return 签名, sign字段不参加签名 */ public string MakeSign() { //转url格式 string str = ToUrl(); //在string后加入API KEY str += "&key=" + WechatConfig.Key; //MD5加密 var md5 = MD5.Create(); var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); var sb = new StringBuilder(); foreach (byte b in bs) { sb.Append(b.ToString("x2")); } //所有字符转为大写 return sb.ToString().ToUpper(); } /** * * 检测签名是否正确 * 正确返回true,错误抛异常 */ public bool CheckSign() { //如果没有设置签名,则跳过检测 if (!IsSet("sign")) { throw new WxPayException("WxPayData签名存在但不合法!"); } //如果设置了签名但是签名为空,则抛异常 else if (GetValue("sign") == null || GetValue("sign").ToString() == "") { throw new WxPayException("WxPayData签名存在但不合法!"); } //获取接收到的签名 string return_sign = GetValue("sign").ToString(); //在本地计算新的签名 string cal_sign = MakeSign(); if (cal_sign == return_sign) { return true; } throw new WxPayException("WxPayData签名验证错误!"); } /** * @获取Dictionary */ public SortedDictionary<string, object> GetValues() { return m_values; } }
Exception
public class WxPayException : Exception { public WxPayException(string msg) : base(msg) { } }
HttpService
用于发送请求,访问微信统一下单API 和 查询订单接口
public class HttpService { public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } public static string Post(string xml, string url, bool isUseCert, int timeout) { GC.Collect();//垃圾回收,回收没有正常关闭的http连接 string result = "";//返回结果 HttpWebRequest request = null; HttpWebResponse response = null; Stream reqStream = null; try { //设置最大连接数 ServicePointManager.DefaultConnectionLimit = 200; //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } /*************************************************************** * 下面设置HttpWebRequest的相关属性 * ************************************************************/ request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeout * 1000; //设置代理服务器 //WebProxy proxy = new WebProxy(); //定义一个网关对象 //proxy.Address = new Uri(WxPayConfig.PROXY_URL); //网关服务器端口:端口 //request.Proxy = proxy; //设置POST的数据类型和长度 request.ContentType = "text/xml"; byte[] data = Encoding.UTF8.GetBytes(xml); request.ContentLength = data.Length; ////是否使用证书 //if (isUseCert) //{ // string path = HttpContext.Current.Request.PhysicalApplicationPath; // X509Certificate2 cert = new X509Certificate2(path + WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD); // request.ClientCertificates.Add(cert); //} //往服务器写入数据 reqStream = request.GetRequestStream(); reqStream.Write(data, 0, data.Length); reqStream.Close(); //获取服务端返回 response = (HttpWebResponse)request.GetResponse(); //获取服务端返回数据 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); result = sr.ReadToEnd().Trim(); sr.Close(); } catch (System.Threading.ThreadAbortException e) { System.Threading.Thread.ResetAbort(); } catch (WebException e) { throw new WxPayException(e.ToString()); } catch (Exception e) { throw new WxPayException(e.ToString()); } finally { //关闭连接和流 if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return result; } }
WechatConfig 微信支付统一下单所用到的参数 会读取 /App_Data/xml/payment.xml 文件里面的内容
appid:微信公众平台的开发者ID(AppID)
mchid:微信商户平台账号
key:在微信商户平台设置的API密钥
appsecret:在微信公众平台中设置的开发者密码(AppSecret)
ip:终端IP
notify_url:微信支付异步回调地址,在微信支付成功时会异步调用你的接口
gateway:微信支付统一下单API接口
queryorder:微信支付查询订单接口
sealed class WechatConfig { #region 字段 static string appid = string.Empty; static string mchid = string.Empty; static string key = string.Empty; static string appsecret = string.Empty; static string notify_url = string.Empty; static string gateway = string.Empty; static string queryorder = string.Empty; #endregion static WechatConfig() { XmlDocument xml = new Xml.XmlDocument_XxeFixed(); xml.XmlResolver = null; xml.Load(HttpContext.Current.Server.MapPath("/App_Data/xml/payment.xml")); var root = xml.SelectSingleNode("paymentMethod").ChildNodes; foreach (XmlNode xn in root) { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("id") == "wechatpay") { appid = xe.GetAttribute("appid"); mchid = xe.GetAttribute("mchid"); key = xe.GetAttribute("key"); appsecret = xe.GetAttribute("appsecret"); notify_url = xe.GetAttribute("notify_url"); gateway = xe.GetAttribute("gateway"); queryorder = xe.GetAttribute("queryorder"); break; } } } internal static string Gateway { get { return gateway; } } internal static string Appid { get { return appid; } } internal static string Mchid { get { return mchid; } } internal static string Appsecret { get { return appsecret; } } internal static string Key { get { return key; } } internal static string Queryorder { get { return queryorder; } } internal static string Notify_url { get { return notify_url; } } }
WechatHelper
在业务处理层调用 根据不同场景传入不同的参数,可用于一个项目多个支付场景
out_trade_no :商户支付的订单号由商户自定义生成,仅支持使用字母、数字、中划线-、下划线_、竖线|、星号*这些英文半角字符的组合,请勿使用汉字或全角等特殊字符。微信支付要求商户订单号保持唯一性(建议根据当前系统时间加随机序列来生成订单号)。重新发起一笔支付要使用原订单号,避免重复支付;已支付过或已调用关单、撤销的订单号不能重新发起支付。
total_fee:订单总金额,单位为分 这里在方法里对金额进行了处理 可直接传实际金额
attach:附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用。用于区分不同的支付场景,在回调中可以根据此来调用不同的方法
body:商品简单描述
ip:终端ip
trade_type:交易类型 JSAPI -JSAPI支付 NATIVE -Native支付 APP -APP支付
openId:此参数为微信用户在商户对应appid下的唯一标识
public sealed class WechatHelper { #region JSAPI Pay ////// JSAPI Pay /// /// 订单编号 /// 价格 /// 自定义参数,回调回传 /// public static ResultModel JSApiPay(string out_trade_no, decimal total_fee, string openid, string ip, string attach = "", string body = "购买商品") { WxPayData result = Pay(out_trade_no, total_fee, attach, body, ip, "JSAPI", openid); string return_code = result.GetValue("return_code").ToString();//返回状态码 SUCCESS/FAIL string result_code = result.GetValue("result_code").ToString();//返回状态码 SUCCESS/FAIL if (return_code == "SUCCESS" && result_code == "SUCCESS") { // string url = result.GetValue("prepay_id").ToString();//预付款订单prepay_id ResultModel rm = new ResultModel(); rm.appId = WechatConfig.Appid; rm.timeStamp = WxPayApi.GenerateTimeStamp(); rm.nonceStr = result.GetValue("nonce_str").ToString(); rm.package = "prepay_id=" + result.GetValue("prepay_id").ToString(); //设置支付参数 var paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appId", rm.appId); paySignReqHandler.SetParameter("timeStamp", rm.timeStamp); paySignReqHandler.SetParameter("nonceStr", rm.nonceStr); paySignReqHandler.SetParameter("package", rm.package); paySignReqHandler.SetParameter("signType", "MD5"); var paySign = paySignReqHandler.CreateMd5Sign("key", WechatConfig.Key); rm.paySign = paySign; return rm; } return null; } #endregion public static WxPayData Pay(string out_trade_no, decimal total_fee, string attach, string body, string ip, string trade_type, string openId = "") { var paymoney = Convert.ToInt32(total_fee * 100); WxPayData data = new WxPayData(); data.SetValue("body", body);//商品描述 data.SetValue("attach", attach);//附加数据,回调返回 data.SetValue("out_trade_no", out_trade_no);//订单编号 data.SetValue("total_fee", paymoney.ToString());//总金额 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", trade_type);//交易类型 if (trade_type == "JSAPI") { data.SetValue("openid", openId);//openId } data.SetValue("product_id", out_trade_no);//商品ID return WxPayApi.UnifiedOrder(data, ip);//调用统一下单接口 } public static WxPayData GetNotifyData(Stream InputStream) { //接收从微信后台POST过来的数据 Stream s = InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); //转换数据格式并验证签名 WxPayData data = new WxPayData(); try { data.FromXml(builder.ToString()); } catch (WxPayException ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); } return data; } //查询订单 public static bool QueryOrder(string transaction_id) { WxPayData req = new WxPayData(); req.SetValue("transaction_id", transaction_id); WxPayData res = WxPayApi.OrderQuery(req); if (res.GetValue("return_code").ToString() == "SUCCESS" && res.GetValue("result_code").ToString() == "SUCCESS") { return true; } else { return false; } } } public class ResultModel { public string appId { get; set; } public string timeStamp { get; set; } public string nonceStr { get; set; } public string package { get; set; } public string paySign { get; set; } }
WxPayApi
在wechathelper中拿到业务参数之后到wxpayapi中加入固定参数
public class WxPayApi { /** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData UnifiedOrder(WxPayData inputObj,string ip, int timeOut = 6) { //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WxPayException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WxPayException("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } inputObj.SetValue("notify_url", WechatConfig.Notify_url);//异步通知url inputObj.SetValue("appid", WechatConfig.Appid);//公众账号ID inputObj.SetValue("mch_id", WechatConfig.Mchid);//商户号 inputObj.SetValue("spbill_create_ip", ip);//终端ip inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); string response = HttpService.Post(xml, WechatConfig.Gateway, false, timeOut); WxPayData result = new WxPayData(); result.FromXml(response); //ReportCostTime(url, timeCost, result);//测速上报 return result; } /** * 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数 * @return 时间戳 */ public static string GenerateTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } /** * 生成随机串,随机串包含字母或数字 * @return 随机串 */ public static string GenerateNonceStr() { return Guid.NewGuid().ToString().Replace("-", ""); } /** * * 查询订单 * @param WxPayData inputObj 提交给查询订单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回订单查询结果,其他抛异常 */ public static WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/orderquery"; inputObj.SetValue("appid", WechatConfig.Appid);//公众账号ID inputObj.SetValue("mch_id", WechatConfig.Mchid);//商户号 inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now; string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据 var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的数据转化为对象以返回 WxPayData result = new WxPayData(); result.FromXml(response); // ReportCostTime(url, timeCost, result);//测速上报 return result; } /*** * 订单查询完整业务流程逻辑 * @param transaction_id 微信订单号(优先使用) * @param out_trade_no 商户订单号 * @return 订单查询结果(xml格式) */ public static WxPayData Run(string transaction_id, string out_trade_no) { WxPayData data = new WxPayData(); if (!string.IsNullOrEmpty(transaction_id))//如果微信订单号存在,则以微信订单号为准 { data.SetValue("transaction_id", transaction_id); } else//微信订单号不存在,才根据商户订单号去查单 { data.SetValue("out_trade_no", out_trade_no); } WxPayData result = OrderQuery(data);//提交订单查询请求给API,接收返回数据 return result; } }
这样一个完整的wechathelper类就搭建完成了
创建xml配置文件 payment.xml
<?xml version="1.0" encoding="utf-8" ?> <paymentMethod> <info id="wechatpay" appid="" mchid="" key="" appsecret="" ip="" notify_url="http://xxx/pay/WechatNotify" gateway="https://api.mch.weixin.qq.com/pay/unifiedorder" queryorder="https://api.mch.weixin.qq.com/pay/orderquery" /> paymentMethod>
在控制器中创建 WechatHelper 调用JSApiPay方法
public ActionResult PayView(){ WechatHelper wechatPay = new WechatHelper(); var pay = wechatPay.JSApiPay(OrderNum, Price, WxOpenId, "ip", "5"); var jssdkUiPackage = JSSDKHelper.GetJsSdkUiPackage(APPID, SECRET), Request.Url.AbsoluteUri); ViewBag.JssdkUiPackage = jssdkUiPackage; ViewBag.uid = user.Id; ViewBag.orderNum = order.OrderNum; JsSdkUiPackage jup = jssdkUiPackage as JsSdkUiPackage; return View(pay); }
创建支付页面
@{ var jssdk = ViewBag.JssdkUiPackage as Senparc.Weixin.MP.Helpers.JsSdkUiPackage; var uid = ViewBag.uid; var ordernum = ViewBag.orderNum; } DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>微信支付title> head> <body> <input type="hidden" id="appid" value="@Model.appId" /> <input type="hidden" id="timestamp" value="@Model.timeStamp" /> <input type="hidden" id="nonceStr" value="@Model.nonceStr" /> <input type="hidden" id="signature" value="@Model.paySign" /> <input type="hidden" id="wxpackage" value="@Model.package" /> <input type="hidden" id="uid" value="@uid" /> <input type="hidden" id="_timestamp" value="@jssdk.Timestamp" /> <input type="hidden" id="_nonceStr" value="@jssdk.NonceStr" /> <input type="hidden" id="_signature" value="@jssdk.Signature" /> <input type="hidden" id="ordernum" value="@ordernum" /> <input type="hidden" id="types" value="1" /> <script src="~/scripts/plug/jquery-1.9.1.min.js">script> <script src="~/scripts/plug/jweixin-1.2.0.js">script> <script src="~/scripts/plug/wechatpay.js">script> body> html>
wechatpay.js
var appid = $("#appid").val(); var timestamp = $("#timestamp").val(); var nonceStr = $("#nonceStr").val(); var signature = $("#signature").val(); var wxpackage = $("#wxpackage").val(); var uid = $("#uid").val(); var _timestamp = $("#_timestamp").val(); var _nonceStr = $("#_nonceStr").val(); var _signature = $("#_signature").val(); var types = $("#types").val(); wx.config({ debug: false, appId: appid, // 公众号的唯一标识 timestamp: _timestamp, //生成签名的时间戳 nonceStr: _nonceStr, // 生成签名的随机串 signature: _signature,// 签名 jsApiList: ['chooseWXPay', 'translateVoice'] }); wx.ready(function () { wx.chooseWXPay({ timestamp: timestamp, nonceStr: nonceStr, package: wxpackage, signType: 'MD5', paySign: signature, success: function (res) { if (res.errMsg == "chooseWXPay:ok") { window.location.href = "/pay/Pay_Success"; } else { window.location.href = "/pay/Pay_Error"; } }, fail: function (res) { window.location.href = "/pay/Pay_Error"; }, cancel: function (res) { window.location.href = "/pay/Pay_Error"; } }); });
微信支付异步回调
这里就是支付成功之后微信异步回调的接口方法
参数可以参考 微信API文档 支付结果通知
#region 微信支付异步回调 [HttpPost] [Transaction] public ActionResult WechatNotify() { try { WechatHelper wechatPay = new WechatHelper(); WxPayData notifyData = wechatPay.GetNotifyData(Request.InputStream); string attach = notifyData.GetValue("attach").ToString();string transaction_id = notifyData.GetValue("transaction_id").ToString(); if (!notifyData.IsSet("transaction_id")) { log("回调return_msg=支付结果中微信订单号不存在", "wechatpayer"); } if (!wechatPay.QueryOrder(transaction_id)) { log("回调return_msg=订单查询失败", "wechatpayer"); } else { log("回调return_msg=OK", "wechatpayer"); } var payxml = XDocument.Parse(notifyData.ToXml()); if (payxml.Element("xml").Element("return_code").Value == "SUCCESS") { if (payxml.Element("xml").Element("result_code").Value == "SUCCESS") { string out_trade_no = payxml.Element("xml").Element("out_trade_no").Value; } return Content(""); } return Content(" "); } catch (Exception ex) { log(ex.ToString(), "wechatpayer"); return Content(" "); } } #endregion
这样就完成了在微信H5中唤起微信支付 并且通过异步回调 完成一些业务
边记录边学习 欢迎各位一起探讨