asp.net 微信支付 实例代码

    xiaoxiao2021-04-18  75

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="jkmobile.wx_pay.index" %> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no" /> <meta charset="utf-8" /> <meta name="viewport"> <title>专家咨询支付页面</title> <link href="../Content/css.css" rel="stylesheet" /> <link href="../Content/bootstrap.css" rel="stylesheet" /> <script src="../Scripts/jquery-1.10.2.js"></script> <script src="../Scripts/bootstrap.js"></script> <script src="../Scripts/layer_mobile/layer.js"></script> <style> .enter { width: 30px; } .enter2 { height: 40px; } .font_yahei { font-family: 'Microsoft YaHei'; } h3 { text-align: center; } .content_backgroundColor { background-color: #F5F3F3; } </style> </head> <body> <div class="container body-content"> <div class="row baseGreen"> <h3>专家咨询支付页面</h3> </div> <div class="row"> <div style=" height:20px;"> </div> </div> <div class="row"> <div class="col-xs-2 col-sm-2"> </div> <div class="col-xs-10 col-sm-10"> <h4> 产品名称:专家咨询</h4> </div> </div> <div class="row" style="margin-bottom:30px;"> <div class="col-xs-2 col-sm-2"> </div> <div class="col-xs-10 col-sm-10"> <h4> 合计金额:<%=str_order_money %>(元)</h4> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12" style="text-align:right ;"> <button type="button" class="btn btn-warning btn-block btn-lg" οnclick="fCharge()">付款</button> </div> </div> <div class="row"> <div style=" height:10px;"> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12"> <button type="button" class="btn btn-info btn-block" οnclick="fBackHome()">返回首页</button> </div> </div> <div class="row"> <div style=" height:20px;"> </div> </div> </div> <input id="order_money" type="hidden" value="<%=order_money %>" /> <input id="openid" type="hidden" value="<%=openid %>" /> <input id="order_id" type="hidden" value="<%=order_id %>" /> <input id="order_number" type="hidden" value="<%=order_number %>" /> <script type="text/javascript"> //获取url的参数 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } //初始化微信支付环境 function fCharge() { //fPostCharge(); if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } } else { fPostCharge(); } } //提交充值数据 function fPostCharge() { var vChargeVal = $("#order_money").val(); vChargeVal = parseFloat(vChargeVal); if (vChargeVal > 0) { layer.open({ type: 2, content: '正在启动微信支付...' }); $.ajax({ type: "post", data: { step: "wx_pay", order_id: $("#order_id").val(), order_number: $("#order_number").val(), openid: $("#openid").val(), totalfee: vChargeVal }, url: "/wx_pay/wx_pay.ashx", success: function (data) { layer.closeAll();//记得关闭 var json = eval("(" + data + ")");//转换后的JSON对象 onBridgeReady(json); }, error: function () { layer.closeAll(); layer.open({ content: '调用微信支付模块失败,请稍后再试!', btn: '确定' }); } }) } else { layer.open({ content: '付款金额不能小于等于0!', btn: '确定' }); } } //调用微信支付模块 function onBridgeReady(json) { WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId": json.appId, //公众号名称,由商户传入 "timeStamp": json.timeStamp, //时间戳,自1970年以来的秒数 "nonceStr": json.nonceStr, //随机串 "package": json.packageValue, "signType": "MD5", //微信签名方式: "paySign": json.paySign //微信签名 }, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { layer.closeAll(); //自定义结果页面 window.location.href = "http://*********"; } else { location.href = "/wx_pay/pay_error.aspx"; } // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。 }); } function fBackHome() { //自定义结果页面 location.href = "http://******"; } </script> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace jkmobile.wx_pay { public partial class index : System.Web.UI.Page { public string order_id = "", order_number = "", openid = "", str_order_money = "0.00"; public int order_money = 0; protected void Page_Load(object sender, EventArgs e) { Mobile_Healthy.Log.PublicLog.WriteLog("请求支付地址及参数:", "1", Request.Url.ToString()); try { if (Request.QueryString["openid"] != null) { openid = Request.QueryString["openid"]; } if (Request.QueryString["returnValue"] != null && !string.IsNullOrEmpty(Request.QueryString["returnValue"])) { string[] array_value = Request.QueryString["returnValue"].ToString().Split('|'); if (array_value.Length > 0) { order_id = array_value[0]; str_order_money = (Convert.ToDecimal(array_value[1]) / 100).ToString("#0.00"); order_money = Convert.ToInt32(array_value[1]); order_number = array_value[2]; } } } catch (Exception ex){ Mobile_Healthy.Log.PublicLog.WriteLog("请求支付异常详情:","4", ex.ToString()); } } } }

    wx_pay.ashx.cs代码

    using jkmobile.TenPayLibV3; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Web; namespace jkmobile.wx_pay { /// <summary> /// wx_pay 的摘要说明 /// </summary> public class wx_pay : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request.Params["step"] != null) { JsApiPay jsApiPay = new JsApiPay(); if (context.Request.Params["step"] == "wx_pay") { #region 购买专家咨询 object objResult = ""; //金额单位分 string strFee = context.Request.Params["totalfee"]; string order_id = context.Request.Params["order_id"]; string order_number = context.Request.Params["order_number"]; //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数 jsApiPay.openid = context.Request.Params["openid"]; jsApiPay.total_fee = int.Parse(strFee); //JSAPI支付预处理 try { string strBody = "健康项目-专家咨询购买";//商品描述 WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(strBody, order_id, jsApiPay.openid, order_number, WxPayConfig.NOTIFY_URL); WxPayData wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数 ModelForOrder aOrder = new ModelForOrder() { appId = wxJsApiParam.GetValue("appId").ToString(), nonceStr = wxJsApiParam.GetValue("nonceStr").ToString(), packageValue = wxJsApiParam.GetValue("package").ToString(), paySign = wxJsApiParam.GetValue("paySign").ToString(), timeStamp = wxJsApiParam.GetValue("timeStamp").ToString(), msg = "成功下单,正在唤起微信支付." }; objResult = aOrder; } catch (Exception ex) { ModelForOrder aOrder = new ModelForOrder() { appId = "", nonceStr = "", packageValue = "", paySign = "", timeStamp = "", msg = "下单失败,请重试,多次失败,请联系管理员." }; objResult = aOrder; } string strJson = JsonConvert.SerializeObject(objResult); context.Response.Write(strJson); #endregion } } context.Response.Flush(); context.Response.End(); } public bool IsReusable { get { return false; } } } } /** * 调用统一下单,获得下单结果 * @return 统一下单结果 * @失败时抛异常WxPayException */ /// <summary> /// 调用统一下单,获得下单结果 /// </summary> /// <param name="strBody">商品描述</param> /// <param name="out_trade_no">商户订单号</param> /// <param name="openid2">微信id</param> /// <param name="order_number">产品购买明细id</param> /// /// <param name="notify_url">通知地址</param> /// <returns></returns> public WxPayData GetUnifiedOrderResult(string strBody, string out_trade_no, string openid2, string order_number,string notify_url) { try { Mobile_Healthy.Log.PublicLog.WriteLog("统一下单回调地址:", "1", JsonConvert.SerializeObject(notify_url)); } catch { } //统一下单 WxPayData data = new WxPayData(); data.SetValue("body", strBody); data.SetValue("attach", openid2 + "|" + order_number); data.SetValue("out_trade_no", 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", "test"); data.SetValue("trade_type", "JSAPI"); data.SetValue("notify_url", notify_url); data.SetValue("openid", openid2); WxPayData 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 WxPayException("UnifiedOrder response error!"); } unifiedOrderResult = result; return result; }

      /**         *         * 统一下单         * @param WxPaydata inputObj 提交给统一下单API的参数         * @param int timeOut 超时时间         * @throws WxPayException         * @return 成功时返回,其他抛异常         */

            /// <summary>         /// 统一下单         /// </summary>         /// <param name="inputObj"></param>         /// <param name="timeOut"></param>         /// <returns></returns>         public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6)         {             string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";             //检测必填参数             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为必填参数!");             }

                //异步通知url未设置,则使用配置文件中的url             //if (inputObj.IsSet("notify_url"))             //{             //    inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//异步通知url             //}

                inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID             inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号             inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip                    inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串

                //签名             inputObj.SetValue("sign", inputObj.MakeSign());             string xml = inputObj.ToXml();

                var start = DateTime.Now;

                Log.Debug("WxPayApi", "UnfiedOrder request : " + xml);             string response = HttpService.Post(xml, url, false, timeOut);             Log.Debug("WxPayApi", "UnfiedOrder response : " + response);

                var end = DateTime.Now;             int timeCost = (int)((end - start).TotalMilliseconds);

                WxPayData result = new WxPayData();             result.FromXml(response);

                ReportCostTime(url, timeCost, result);//测速上报

                return result;         }

     /**      *      * 测速上报      * @param string interface_url 接口URL      * @param int timeCost 接口耗时      * @param WxPayData inputObj参数数组      */         private static void ReportCostTime(string interface_url, int timeCost, WxPayData inputObj)      {       //如果不需要进行上报       if(WxPayConfig.REPORT_LEVENL == 0)             {        return;       }

          //如果仅失败上报       if(WxPayConfig.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" &&     inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS")             {         return;       }          //上报逻辑       WxPayData data = new WxPayData();             data.SetValue("interface_url",interface_url);       data.SetValue("execute_time_",timeCost);       //返回状态码       if(inputObj.IsSet("return_code"))             {        data.SetValue("return_code",inputObj.GetValue("return_code"));       }       //返回信息             if(inputObj.IsSet("return_msg"))             {        data.SetValue("return_msg",inputObj.GetValue("return_msg"));       }       //业务结果             if(inputObj.IsSet("result_code"))             {        data.SetValue("result_code",inputObj.GetValue("result_code"));       }       //错误代码             if(inputObj.IsSet("err_code"))             {        data.SetValue("err_code",inputObj.GetValue("err_code"));       }       //错误代码描述             if(inputObj.IsSet("err_code_des"))             {        data.SetValue("err_code_des",inputObj.GetValue("err_code_des"));       }       //商户订单号             if(inputObj.IsSet("out_trade_no"))             {        data.SetValue("out_trade_no",inputObj.GetValue("out_trade_no"));       }       //设备号             if(inputObj.IsSet("device_info"))             {        data.SetValue("device_info",inputObj.GetValue("device_info"));       }          try             {        Report(data);       }             catch (WxPayException ex)             {        //不做任何处理       }      }

     

      /**         *          * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数,         * 微信浏览器调起JSAPI时的输入参数格式如下:         * {         *   "appId" : "wx2421b1c4370ec43b",     //公众号名称,由商户传入             *   "timeStamp":" 1395712654",         //时间戳,自1970年以来的秒数             *   "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串             *   "package" : "prepay_id=u802345jgfjsdfgsdg888",             *   "signType" : "MD5",         //微信签名方式:            *   "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名         * }         * @return string 微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用         * 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7         *         */         public WxPayData GetJsApiParameters()         {             Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing...");

                WxPayData jsApiParam = new WxPayData();             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 jsApiParam;         }

     

    WxPayData.cs 微信支付协议接口数据类,所有的API接口通信都依赖这个数据结构,

    using System; using System.Collections.Generic; using System.Web; using System.Xml; using System.Security.Cryptography; using System.Text; using LitJson; namespace jkmobile.TenPayLibV3 { /// <summary> /// 微信支付协议接口数据类,所有的API接口通信都依赖这个数据结构, /// 在调用接口之前先填充各个字段的值,然后进行接口通信, /// 这样设计的好处是可扩展性强,用户可随意对协议进行更改而不用重新设计数据结构, /// 还可以随意组合出不同的协议数据包,不用为每个协议设计一个数据包结构 /// </summary> 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) { Log.Error(this.GetType().ToString(), "WxPayData数据为空!"); throw new WxPayException("WxPayData数据为空!"); } string xml = "<xml>"; foreach (KeyValuePair<string, object> pair in m_values) { //字段值不能为null,会影响后续流程 if (pair.Value == null) { Log.Error(this.GetType().ToString(), "WxPayData内部含有值为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 + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">"; } else//除了string和int类型不能含有其他数据类型 { Log.Error(this.GetType().ToString(), "WxPayData字段数据类型错误!"); throw new WxPayException("WxPayData字段数据类型错误!"); } } xml += "</xml>"; return xml; } /** * @将xml转为WxPayData对象并返回对象内部的数据 * @param string 待转换的xml串 * @return 经转换得到的Dictionary * @throws WxPayException */ public SortedDictionary<string, object> FromXml(string xml) { if (string.IsNullOrEmpty(xml)) { Log.Error(this.GetType().ToString(), "将空的xml串转换为WxPayData不合法!"); throw new WxPayException("将空的xml串转换为WxPayData不合法!"); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml> 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) { Log.Error(this.GetType().ToString(), "WxPayData内部含有值为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 jsonStr; } /** * @values格式化成能在Web页面上显示的结果(因为web页面上不能直接输出xml格式的字符串) */ public string ToPrintStr() { string str = ""; foreach (KeyValuePair<string, object> pair in m_values) { if (pair.Value == null) { Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!"); throw new WxPayException("WxPayData内部含有值为null的字段!"); } str += string.Format("{0}={1}<br>", pair.Key, pair.Value.ToString()); } Log.Debug(this.GetType().ToString(), "Print in Web Page : " + str); return str; } /** * @生成签名,详见签名生成算法 * @return 签名, sign字段不参加签名 */ public string MakeSign() { //转url格式 string str = ToUrl(); //在string后加入API KEY str += "&key=" + WxPayConfig.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")) { Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!"); throw new WxPayException("WxPayData签名存在但不合法!"); } //如果设置了签名但是签名为空,则抛异常 else if (GetValue("sign") == null || GetValue("sign").ToString() == "") { Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!"); throw new WxPayException("WxPayData签名存在但不合法!"); } //获取接收到的签名 string return_sign = GetValue("sign").ToString(); //在本地计算新的签名 string cal_sign = MakeSign(); if (cal_sign == return_sign) { return true; } Log.Error(this.GetType().ToString(), "WxPayData签名验证错误!"); throw new WxPayException("WxPayData签名验证错误!"); } /** * @获取Dictionary */ public SortedDictionary<string, object> GetValues() { return m_values; } } }

     

    转载请注明原文地址: https://ju.6miu.com/read-675160.html

    最新回复(0)