微信开发在c#关闭页面_c# 微信开发时 微信多次请求 - CSDN
  • 微信的WeixinJSBridge还是很牛的,比如自动关闭当前浏览器内置函数:WeixinJSBridge.call('closeWindow'); 直接调用即可。

    微信的WeixinJSBridge还是很牛的,比如自动关闭当前浏览器内置函数:WeixinJSBridge.call('closeWindow');

    直接调用即可。

    展开全文
  • 二、微信开发的基本配置 1.配置IP白名单 2.服务器配置 3.申请测试号 微信公众号开发系列点击查看所有开发文章 一、注册一个微信公众号 关于注册这点我就简单说一下,需要注意的就是,微信公众号一般用的...

    目录

    一、注册一个微信公众号

    二、微信开发的基本配置

     1.配置IP白名单

    2.服务器配置

    3.申请测试号

    微信公众号开发系列点击查看所有开发文章


     

    一、注册一个微信公众号

      关于注册这点我就简单说一下,需要注意的就是,微信公众号一般用的就是订阅号和企业号,订阅号不能认证,权限少,且不能就行二次开发。而企业号可以认证,但是认证只有一年期限,认证费用300元。当然了,为了让我们了解微信公众号能够开发什么,腾讯提供的微信公众号测试号,也就是说你不管注册的是什么都可以申请一个测试号进行开发,后面我所用到就是我的订阅号申请的测试号。


    二、微信开发的基本配置

     1.配置IP白名单

        IP白名单就是微信公众号允许放行IP地址,也就是说你服务器的IP地址,关于获取IP地址有很多网站都可以,腾讯以前也提供了相应的接口网站,但是现在关闭了,这里我就提供一个免费的网站http://pv.sohu.com/cityjson?ie=utf-8,运行截图如下:

    我们只需要把cip里的值添加到IP白名单就可以了。


    2.服务器配置

       

     如上图所示,需要配置的有URL、Token、密钥、加密方式。其中URL为你服务器处理程序的地址,Token可以随便写,单是要和你服务器代码中的一样,密钥可以点击自动生成,加密方式就选择明文就行。当然了,我这没有启用,因为我的是订阅号,不能进行开发,所以需要申请一个测试号,下面就讲测试号申请和微信服务响应。


    3.申请测试号

        1.找到开发者工具,选择公众平台测试账号(开发文档、接口测试是很好的东西可以多用)

      

    进入之后把你的URL和Token填写再接口配置信息里,点击提交,就可以了(前提是服务器中的处理程序已经写好了(*^_^*))

    这就微信公众号中的一些开发配置,关于处理程序和相应的代码在后面的文章贴出。 

    展开全文
  • 微信小程序支付功能的开发的时候坑比较多,不过对于钱的事谨慎也是好事。网上关于小程序支付的实例很多,但是大多多少有些问题,C#开发的更少。此篇文档的目的是讲开发过程中遇到的问题做一个备注,也方便其他开发的...

    微信小程序支付功能的开发的时候坑比较多,不过对于钱的事谨慎也是好事。网上关于小程序支付的实例很多,但是大多多少有些问题,C#开发的更少。此篇文档的目的是讲开发过程中遇到的问题做一个备注,也方便其他开发的同学作为参考!


           1、首先建议把官方文档支付部分看上三遍,每个细节都不要放过,因为任何一个点和微信要求不符都会导致支付不成功。https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=3_1

          2、经过验证的微信支付功能,会需要一些商户号、支付秘钥等,不要搞混。

         3、经常遇到的是“签名错误”,请仔细看需要传送的xml参数及取值规则是否符合微信规则。微信有个验证工具可以验证发送的xml字段是否合法。


    下面上代码:


    web.config

    [csharp] view plain copy
    1.   <add key="ConnectionString" value="server=127.0.0.1;database=;uid=sa;pwd="/>  
    2.   <add key="ConnectionString2" value="server=127.0.0.1;database=codematic2;uid=sa;pwd=1"/>  
    3.   <add key="appid" value=""/>//appid  
    4.   <add key="secret" value=""/>//小程序秘钥  
    5.   <add key="mch_id" value=""/>//商户号  
    6.   <add key="key" value=""/>//支付秘钥  
    7.   <add key="ip" value=""/>//服务器IP  
    8.   <add key="PayResulturl" value=""/>//微信返回接收信息的url地址  
    9. </appSettings>  
    支付后台xiadan.ashx

    [csharp] view plain copy
    1. <%@ WebHandler Language="C#" Class="xiadan" %>  
    2.   
    3. using System;  
    4. using System.Web;  
    5. using System.Net;  
    6. using System.IO;  
    7. using System.Configuration;  
    8. using Maticsoft.Model;  
    9. using Maticsoft.BLL;  
    10. using System.Security.Cryptography;  
    11. using System.Text;  
    12. using System.Xml.Serialization;  
    13. using System.Xml;  
    14. using System.Collections.Generic;  
    15. using System.Data;  
    16. using System.Net.Security;  
    17. using System.Security.Cryptography.X509Certificates;  
    18. using System.Linq;  
    19. using Newtonsoft.Json;  
    20.   
    21. public class xiadan : IHttpHandler  
    22. {  
    23.   
    24.     public void ProcessRequest(HttpContext context)  
    25.     {  
    26.         context.Response.ContentType = "text/plain";  
    27.         string openid = context.Request.Params["openid"];  
    28.         string ordertime = context.Request.Params["ordertime"];  
    29.   
    30.         string appid = ConfigurationManager.AppSettings["appid"];  
    31.         string secret = ConfigurationManager.AppSettings["secret"];  
    32.         string key = ConfigurationManager.AppSettings["key"];  
    33.         string mch_id = ConfigurationManager.AppSettings["mch_id"];  
    34.         string ip = ConfigurationManager.AppSettings["ip"];  
    35.         string PayResulturl = ConfigurationManager.AppSettings["PayResulturl"];  
    36.         string roomid = context.Request.Params["roomid"];  
    37.         string aa = "-押金";////商品描述交易字段格式根据不同的应用场景按照以下格式:APP——需传入应用市场上的APP名字-实际商品名称,天天爱消除-游戏充值。  
    38.   
    39.         string strcode = aa;  
    40.         byte[] buffer = Encoding.UTF8.GetBytes(strcode);  
    41.         string body = Encoding.UTF8.GetString(buffer, 0, buffer.Length);  
    42.         string totalfee = context.Request.Params["totalfee"];  
    43.         string output = "";  
    44.         if ((context.Request.Params["openid"] != null) && (context.Request.Params["openid"] != ""))  
    45.         {  
    46.             //OrderInfo order = new OrderInfo();  
    47.   
    48.             //order.appid = appid;  
    49.   
    50.             System.Random Random = new System.Random();  
    51.   
    52.   
    53.   
    54.             var dic = new Dictionary<stringstring>  
    55. {  
    56.     {"appid", appid},  
    57.     {"mch_id", mch_id},  
    58.     {"nonce_str", GetRandomString(20)/*Random.Next().ToString()*/},  
    59.     {"body",body},  
    60.     {"out_trade_no",roomid + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Random.Next(999).ToString()},//商户自己的订单号码  
    61.     {"total_fee",totalfee},  
    62.     {"spbill_create_ip",ip},//服务器的IP地址  
    63.     {"notify_url",PayResulturl},//异步通知的地址,不能带参数  
    64.     {"trade_type","JSAPI" },  
    65.     {"openid",openid}  
    66. };  
    67.       //加入签名  
    68.             dic.Add("sign", GetSignString(dic));  
    69.   
    70.             var sb = new StringBuilder();  
    71.             sb.Append("<xml>");  
    72.   
    73.   
    74.             foreach (var d in dic)  
    75.             {  
    76.                 sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">");  
    77.             }  
    78.             sb.Append("</xml>");  
    79.             var xml = new XmlDocument();  
    80.             //  xml.LoadXml(GetPostString("https://api.mch.weixin.qq.com/pay/unifiedorder", sb.ToString()));  
    81.             CookieCollection coo = new CookieCollection();  
    82.             Encoding en = Encoding.GetEncoding("UTF-8");  
    83.   
    84.             HttpWebResponse response = CreatePostHttpResponse("https://api.mch.weixin.qq.com/pay/unifiedorder", sb.ToString(), en);  
    85.             //打印返回值  
    86.             Stream stream = response.GetResponseStream();   //获取响应的字符串流  
    87.             StreamReader sr = new StreamReader(stream); //创建一个stream读取流  
    88.             string html = sr.ReadToEnd();   //从头读到尾,放到字符串html  
    89.                                             //Console.WriteLine(html);  
    90.             xml.LoadXml(html);  
    91.             //对请求返回值 进行处理  
    92.   
    93.             var root = xml.DocumentElement;  
    94.   
    95.             DataSet ds = new DataSet();  
    96.             StringReader stram = new StringReader(html);  
    97.             XmlTextReader reader = new XmlTextReader(stram);  
    98.             ds.ReadXml(reader);  
    99.             string return_code = ds.Tables[0].Rows[0]["return_code"].ToString();  
    100.             if (return_code.ToUpper() == "SUCCESS")  
    101.             {  
    102.                 //通信成功  
    103.                 string result_code = ds.Tables[0].Rows[0]["result_code"].ToString();//业务结果  
    104.                 if (result_code.ToUpper() == "SUCCESS")  
    105.                 {  
    106.                     var res = new Dictionary<stringstring>  
    107. {  
    108.     {"appId", appid},  
    109.     {"timeStamp", GetTimeStamp()},  
    110.     {"nonceStr", dic["nonce_str"]},  
    111.     {"package",  "prepay_id="+ds.Tables[0].Rows[0]["prepay_id"].ToString()},  
    112.     {"signType""MD5"}  
    113. };  
    114.   
    115.                     //在服务器上签名  
    116.                     res.Add("paySign", GetSignString(res));  
    117.                     // string signapp = res.ToString();  
    118.                     string signapp = JsonConvert.SerializeObject(res);  
    119.                     if ((context.Request.Params["openid"] != null) && (context.Request.Params["openid"] != ""))  
    120.                     {   
    121.                     //存储订单信息  
    122.                     Maticsoft.Model.order_history oh = new Maticsoft.Model.order_history();  
    123.                     //oh.shop_id =  
    124.                     oh.room_id = Convert.ToInt32(roomid);  
    125.                     oh.pay_price = Convert.ToDecimal(totalfee);  
    126.                     oh.out_trade_no = dic["out_trade_no"];  
    127.                     oh.order_timestart = Convert.ToDateTime(ordertime);  
    128.                     oh.openid = openid;  
    129.                     oh.creating_date = DateTime.Now;  
    130.   
    131.                     Maticsoft.BLL.order_history bll = new Maticsoft.BLL.order_history();  
    132.                     bll.Add(oh);  
    133.   
    134.                 }  
    135.                 context.Response.Write(signapp);  
    136.             }  
    137.         }  
    138.   
    139.   
    140.   
    141.   
    142.     }  
    143.     context.Response.Write(output);  
    144.     }  
    145.   
    146. public bool IsReusable  
    147. {  
    148.     get  
    149.     {  
    150.         return false;  
    151.     }  
    152. }  
    153.   
    154. public string GetMd5Hash(String input)  
    155. {  
    156.     if (input == null)  
    157.     {  
    158.         return null;  
    159.     }  
    160.   
    161.     MD5 md5Hash = MD5.Create();  
    162.   
    163.     // 将输入字符串转换为字节数组并计算哈希数据    
    164.     byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));  
    165.   
    166.     // 创建一个 Stringbuilder 来收集字节并创建字符串    
    167.     StringBuilder sBuilder = new StringBuilder();  
    168.   
    169.     // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串    
    170.     for (int i = 0; i < data.Length; i++)  
    171.     {  
    172.         sBuilder.Append(data[i].ToString());  
    173.     }  
    174.   
    175.     // 返回十六进制字符串    
    176.     return sBuilder.ToString();  
    177. }  
    178. /// <summary>    
    179. /// 对象序列化成 XML String    
    180. /// </summary>    
    181. public static string XmlSerialize<T>(T obj)  
    182. {  
    183.     string xmlString = string.Empty;  
    184.     XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));  
    185.     using (MemoryStream ms = new MemoryStream())  
    186.     {  
    187.         xmlSerializer.Serialize(ms, obj);  
    188.         xmlString = Encoding.UTF8.GetString(ms.ToArray());  
    189.     }  
    190.     return xmlString;  
    191. }  
    192. /// <summary>  
    193. /// 从字符串里随机得到,规定个数的字符串.  
    194. /// </summary>  
    195. /// <param name="allChar"></param>  
    196. /// <param name="CodeCount"></param>  
    197. /// <returns></returns>  
    198. public static string GetRandomString(int CodeCount)  
    199. {  
    200.     string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";  
    201.     string[] allCharArray = allChar.Split(',');  
    202.     string RandomCode = "";  
    203.     int temp = -1;  
    204.     Random rand = new Random();  
    205.     for (int i = 0; i < CodeCount; i++)  
    206.     {  
    207.         if (temp != -1)  
    208.         {  
    209.             rand = new Random(temp * i * ((int)DateTime.Now.Ticks));  
    210.         }  
    211.         int t = rand.Next(allCharArray.Length - 1);  
    212.         while (temp == t)  
    213.         {  
    214.             t = rand.Next(allCharArray.Length - 1);  
    215.         }  
    216.         temp = t;  
    217.         RandomCode += allCharArray[t];  
    218.     }  
    219.   
    220.     return RandomCode;  
    221. }  
    222.   
    223.   
    224. public static string GetWebClientIp()  
    225. {  
    226.     string userIP = "IP";  
    227.   
    228.     try  
    229.     {  
    230.         if (System.Web.HttpContext.Current == null  
    231.     || System.Web.HttpContext.Current.Request == null  
    232.     || System.Web.HttpContext.Current.Request.ServerVariables == null)  
    233.             return "";  
    234.   
    235.         string CustomerIP = "";  
    236.   
    237.         //CDN加速后取到的IP     
    238.         CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];  
    239.         if (!string.IsNullOrEmpty(CustomerIP))  
    240.         {  
    241.             return CustomerIP;  
    242.         }  
    243.   
    244.         CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    245.   
    246.   
    247.         if (!String.IsNullOrEmpty(CustomerIP))  
    248.             return CustomerIP;  
    249.   
    250.         if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)  
    251.         {  
    252.             CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    253.             if (CustomerIP == null)  
    254.                 CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    255.         }  
    256.         else  
    257.         {  
    258.             CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    259.   
    260.         }  
    261.   
    262.         if (string.Compare(CustomerIP, "unknown"true) == 0)  
    263.             return System.Web.HttpContext.Current.Request.UserHostAddress;  
    264.         return CustomerIP;  
    265.     }  
    266.     catch { }  
    267.   
    268.     return userIP;  
    269. }  
    270.   
    271.   
    272.   
    273.   
    274. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)  
    275. {  
    276.     return true//总是接受     
    277. }  
    278.   
    279. public static HttpWebResponse CreatePostHttpResponse(string url, string datas, Encoding charset)  
    280. {  
    281.     HttpWebRequest request = null;  
    282.     //HTTPSQ请求  
    283.     ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);  
    284.     request = WebRequest.Create(url) as HttpWebRequest;  
    285.     request.ProtocolVersion = HttpVersion.Version10;  
    286.     request.Method = "POST";  
    287.     request.ContentType = "application/x-www-form-urlencoded";  
    288.   
    289.     //如果需要POST数据     
    290.     //if (!(parameters == null || parameters.Count == 0))  
    291.     //{  
    292.     StringBuilder buffer = new StringBuilder();  
    293.     //int i = 0;  
    294.     //foreach (string key in parameters.Keys)  
    295.     //{  
    296.     //    if (i > 0)  
    297.     //    {  
    298.     //        buffer.AppendFormat("&{0}={1}", key, parameters[key]);  
    299.     //    }  
    300.     //    else  
    301.     //    {  
    302.     //        buffer.AppendFormat("{0}={1}", key, parameters[key]);  
    303.     //    }  
    304.     //    i++;  
    305.     //}  
    306.     buffer.AppendFormat(datas);  
    307.     byte[] data = charset.GetBytes(buffer.ToString());  
    308.     using (Stream stream = request.GetRequestStream())  
    309.     {  
    310.         stream.Write(data, 0, data.Length);  
    311.     }  
    312.     //}  
    313.     return request.GetResponse() as HttpWebResponse;  
    314. }  
    315.   
    316.   
    317. public string GetSignString(Dictionary<stringstring> dic)  
    318. {  
    319.     string key = System.Web.Configuration.WebConfigurationManager.AppSettings["key"].ToString();//商户平台 API安全里面设置的KEY  32位长度  
    320.                                                                                                 //排序  
    321.     dic = dic.OrderBy(d => d.Key).ToDictionary(d => d.Key, d => d.Value);  
    322.     //连接字段  
    323.     var sign = dic.Aggregate("", (current, d) => current + (d.Key + "=" + d.Value + "&"));  
    324.     sign += "key=" + key;  
    325.     //MD5  
    326.     // sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5").ToUpper();  
    327.     System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();  
    328.     sign = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(sign))).Replace("-"null);  
    329.     return sign;  
    330. }  
    331.   
    332.   
    333. /// <summary>    
    334. /// 获取时间戳    
    335. /// </summary>    
    336. /// <returns></returns>    
    337. public static string GetTimeStamp()  
    338. {  
    339.     TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);  
    340.     return Convert.ToInt64(ts.TotalSeconds).ToString();  
    341. }  
    342.   
    343. }  



          微信返回信息接收后台页面notify_url.ashx

    [csharp] view plain copy
    1. <%@ WebHandler Language="C#" Class="notify_url" %>  
    2.   
    3. using System;  
    4. using System.Web;  
    5. using System.Collections.Generic;  
    6. using System.Data;  
    7. using System.IO;  
    8. using System.Text;  
    9. using System.Xml;  
    10. using System.Net;  
    11. public class notify_url : IHttpHandler  
    12. {  
    13.     public string return_result = "";  
    14.     public void ProcessRequest(HttpContext context)  
    15.     {  
    16.         context.Response.ContentType = "text/plain";  
    17.         context.Response.Write("Hello World");  
    18.   
    19.   
    20.         String xmlData = getPostStr();//获取请求数据  
    21.         if (xmlData == "")  
    22.         {  
    23.   
    24.         }  
    25.         else  
    26.         {  
    27.             var dic = new Dictionary<stringstring>  
    28. {  
    29.     {"return_code""SUCCESS"},  
    30.     {"return_msg","OK"}  
    31.   
    32. };  
    33.             var sb = new StringBuilder();  
    34.             sb.Append("<xml>");  
    35.   
    36.   
    37.             foreach (var d in dic)  
    38.             {  
    39.                 sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">");  
    40.             }  
    41.             sb.Append("</xml>");  
    42.   
    43.   
    44.   
    45.   
    46.   
    47.             //把数据重新返回给客户端  
    48.             DataSet ds = new DataSet();  
    49.             StringReader stram = new StringReader(xmlData);  
    50.             XmlTextReader datareader = new XmlTextReader(stram);  
    51.             ds.ReadXml(datareader);  
    52.             if (ds.Tables[0].Rows[0]["return_code"].ToString() == "SUCCESS")  
    53.             {  
    54.   
    55.   
    56.                 string wx_appid = "";//微信开放平台审核通过的应用APPID  
    57.                 string wx_mch_id = "";//微信支付分配的商户号  
    58.   
    59.                 string wx_nonce_str = "";//     随机字符串,不长于32位  
    60.                 string wx_sign = "";//签名,详见签名算法  
    61.                 string wx_result_code = "";//SUCCESS/FAIL  
    62.   
    63.                 string wx_return_code = "";  
    64.                 string wx_openid = "";//用户在商户appid下的唯一标识  
    65.                 string wx_is_subscribe = "";//用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效  
    66.                 string wx_trade_type = "";//    APP  
    67.                 string wx_bank_type = "";//     银行类型,采用字符串类型的银行标识,银行类型见银行列表  
    68.                 string wx_fee_type = "";//  货币类型,符合ISO4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型  
    69.   
    70.   
    71.                 string wx_transaction_id = "";//微信支付订单号  
    72.                 string wx_out_trade_no = "";//商户系统的订单号,与请求一致。  
    73.                 string wx_time_end = "";//  支付完成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则  
    74.                 int wx_total_fee = -1;//    订单总金额,单位为分  
    75.                 int wx_cash_fee = -1;//现金支付金额订单现金支付金额,详见支付金额  
    76.  
    77.  
    78.                 #region  数据解析  
    79.                 //列 是否存在  
    80.                 string signstr = "";//需要前面的字符串  
    81.                                     //wx_appid  
    82.                 if (ds.Tables[0].Columns.Contains("appid"))  
    83.                 {  
    84.                     wx_appid = ds.Tables[0].Rows[0]["appid"].ToString();  
    85.                     if (!string.IsNullOrEmpty(wx_appid))  
    86.                     {  
    87.                         signstr += "appid=" + wx_appid;  
    88.                     }  
    89.                 }  
    90.   
    91.                 //wx_bank_type  
    92.                 if (ds.Tables[0].Columns.Contains("bank_type"))  
    93.                 {  
    94.                     wx_bank_type = ds.Tables[0].Rows[0]["bank_type"].ToString();  
    95.                     if (!string.IsNullOrEmpty(wx_bank_type))  
    96.                     {  
    97.                         signstr += "&bank_type=" + wx_bank_type;  
    98.                     }  
    99.                 }  
    100.                 //wx_cash_fee  
    101.                 if (ds.Tables[0].Columns.Contains("cash_fee"))  
    102.                 {  
    103.                     wx_cash_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["cash_fee"].ToString());  
    104.   
    105.                     signstr += "&cash_fee=" + wx_cash_fee;  
    106.                 }  
    107.   
    108.                 //wx_fee_type  
    109.                 if (ds.Tables[0].Columns.Contains("fee_type"))  
    110.                 {  
    111.                     wx_fee_type = ds.Tables[0].Rows[0]["fee_type"].ToString();  
    112.                     if (!string.IsNullOrEmpty(wx_fee_type))  
    113.                     {  
    114.                         signstr += "&fee_type=" + wx_fee_type;  
    115.                     }  
    116.                 }  
    117.   
    118.                 //wx_is_subscribe  
    119.                 if (ds.Tables[0].Columns.Contains("is_subscribe"))  
    120.                 {  
    121.                     wx_is_subscribe = ds.Tables[0].Rows[0]["is_subscribe"].ToString();  
    122.                     if (!string.IsNullOrEmpty(wx_is_subscribe))  
    123.                     {  
    124.                         signstr += "&is_subscribe=" + wx_is_subscribe;  
    125.                     }  
    126.                 }  
    127.   
    128.                 //wx_mch_id  
    129.                 if (ds.Tables[0].Columns.Contains("mch_id"))  
    130.                 {  
    131.                     wx_mch_id = ds.Tables[0].Rows[0]["mch_id"].ToString();  
    132.                     if (!string.IsNullOrEmpty(wx_mch_id))  
    133.                     {  
    134.                         signstr += "&mch_id=" + wx_mch_id;  
    135.                     }  
    136.                 }  
    137.   
    138.                 //wx_nonce_str  
    139.                 if (ds.Tables[0].Columns.Contains("nonce_str"))  
    140.                 {  
    141.                     wx_nonce_str = ds.Tables[0].Rows[0]["nonce_str"].ToString();  
    142.                     if (!string.IsNullOrEmpty(wx_nonce_str))  
    143.                     {  
    144.                         signstr += "&nonce_str=" + wx_nonce_str;  
    145.                     }  
    146.                 }  
    147.   
    148.                 //wx_openid  
    149.                 if (ds.Tables[0].Columns.Contains("openid"))  
    150.                 {  
    151.                     wx_openid = ds.Tables[0].Rows[0]["openid"].ToString();  
    152.                     if (!string.IsNullOrEmpty(wx_openid))  
    153.                     {  
    154.                         signstr += "&openid=" + wx_openid;  
    155.                     }  
    156.                 }  
    157.   
    158.                 //wx_out_trade_no  
    159.                 if (ds.Tables[0].Columns.Contains("out_trade_no"))  
    160.                 {  
    161.                     wx_out_trade_no = ds.Tables[0].Rows[0]["out_trade_no"].ToString();  
    162.                     if (!string.IsNullOrEmpty(wx_out_trade_no))  
    163.                     {  
    164.                         signstr += "&out_trade_no=" + wx_out_trade_no;  
    165.                     }  
    166.                 }  
    167.   
    168.                 //wx_result_code   
    169.                 if (ds.Tables[0].Columns.Contains("result_code"))  
    170.                 {  
    171.                     wx_result_code = ds.Tables[0].Rows[0]["result_code"].ToString();  
    172.                     if (!string.IsNullOrEmpty(wx_result_code))  
    173.                     {  
    174.                         signstr += "&result_code=" + wx_result_code;  
    175.                     }  
    176.                 }  
    177.   
    178.                 //wx_result_code   
    179.                 if (ds.Tables[0].Columns.Contains("return_code"))  
    180.                 {  
    181.                     wx_return_code = ds.Tables[0].Rows[0]["return_code"].ToString();  
    182.                     if (!string.IsNullOrEmpty(wx_return_code))  
    183.                     {  
    184.                         signstr += "&return_code=" + wx_return_code;  
    185.                     }  
    186.                 }  
    187.   
    188.                 //wx_sign   
    189.                 if (ds.Tables[0].Columns.Contains("sign"))  
    190.                 {  
    191.                     wx_sign = ds.Tables[0].Rows[0]["sign"].ToString();  
    192.                     //if (!string.IsNullOrEmpty(wx_sign))  
    193.                     //{  
    194.                     //    signstr += "&sign=" + wx_sign;  
    195.                     //}  
    196.                 }  
    197.   
    198.                 //wx_time_end  
    199.                 if (ds.Tables[0].Columns.Contains("time_end"))  
    200.                 {  
    201.                     wx_time_end = ds.Tables[0].Rows[0]["time_end"].ToString();  
    202.                     if (!string.IsNullOrEmpty(wx_time_end))  
    203.                     {  
    204.                         signstr += "&time_end=" + wx_time_end;  
    205.                     }  
    206.                 }  
    207.   
    208.                 //wx_total_fee  
    209.                 if (ds.Tables[0].Columns.Contains("total_fee"))  
    210.                 {  
    211.                     wx_total_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["total_fee"].ToString());  
    212.   
    213.                     signstr += "&total_fee=" + wx_total_fee;  
    214.                 }  
    215.   
    216.                 //wx_trade_type  
    217.                 if (ds.Tables[0].Columns.Contains("trade_type"))  
    218.                 {  
    219.                     wx_trade_type = ds.Tables[0].Rows[0]["trade_type"].ToString();  
    220.                     if (!string.IsNullOrEmpty(wx_trade_type))  
    221.                     {  
    222.                         signstr += "&trade_type=" + wx_trade_type;  
    223.                     }  
    224.                 }  
    225.   
    226.                 //wx_transaction_id  
    227.                 if (ds.Tables[0].Columns.Contains("transaction_id"))  
    228.                 {  
    229.                     wx_transaction_id = ds.Tables[0].Rows[0]["transaction_id"].ToString();  
    230.                     if (!string.IsNullOrEmpty(wx_transaction_id))  
    231.                     {  
    232.                         signstr += "&transaction_id=" + wx_transaction_id;  
    233.                     }  
    234.                 }  
    235.  
    236.                 #endregion  
    237.   
    238.                 //追加key 密钥  
    239.                 signstr += "&key=" + System.Web.Configuration.WebConfigurationManager.AppSettings["key"].ToString();  
    240.                 //签名正确  
    241.                 string orderStrwhere = "ordernumber='" + wx_out_trade_no + "'";  
    242.   
    243.   
    244.   
    245.                 if (wx_sign == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(signstr, "MD5").ToUpper())  
    246.                 {  
    247.                     //签名正确   处理订单操作逻辑  
    248.   
    249.   
    250.                 }  
    251.                 else  
    252.                 {  
    253.                     //追加备注信息  
    254.   
    255.                 }  
    256.   
    257.             }  
    258.             else  
    259.             {  
    260.                 // 返回信息,如非空,为错误原因  签名失败 参数格式校验错误  
    261.                 string return_msg = ds.Tables[0].Rows[0]["return_msg"].ToString();  
    262.   
    263.             }  
    264.   
    265.   
    266.             return_result = sb.ToString();  
    267.         }  
    268.     }  
    269.   
    270.   
    271.     public bool IsReusable  
    272.     {  
    273.         get  
    274.         {  
    275.             return false;  
    276.         }  
    277.     }  
    278.   
    279.     //获得Post过来的数据  
    280.     public string getPostStr()  
    281.     {  
    282.         Int32 intLen = Convert.ToInt32(System.Web.HttpContext.Current.Request.InputStream.Length);  
    283.         byte[] b = new byte[intLen];  
    284.         System.Web.HttpContext.Current.Request.InputStream.Read(b, 0, intLen);  
    285.         return System.Text.Encoding.UTF8.GetString(b);  
    286.     }  
    287. }  

    退款:

    [csharp] view plain copy
    1. <%@ WebHandler Language="C#" Class="jiesuan" %>  
    2.   
    3. using System;  
    4. using System.Web;  
    5. using System.Net;  
    6. using System.IO;  
    7. using System.Configuration;  
    8. using Maticsoft.Model;  
    9. using Maticsoft.BLL;  
    10. using System.Security.Cryptography;  
    11. using System.Text;  
    12. using System.Xml.Serialization;  
    13. using System.Xml;  
    14. using System.Collections.Generic;  
    15. using System.Data;  
    16. using System.Net.Security;  
    17. using System.Security.Cryptography.X509Certificates;  
    18. using System.Linq;  
    19. using Newtonsoft.Json;  
    20.   
    21. public class jiesuan : IHttpHandler  
    22. {  
    23.   
    24.     public void ProcessRequest(HttpContext context)  
    25.     {  
    26.         context.Response.ContentType = "text/plain";  
    27.         // string openid = context.Request.Params["openid"];  
    28.         // string ordertime = context.Request.Params["ordertime"];  
    29.         string orderid = context.Request.Params["order_id"];  
    30.         string appid = ConfigurationManager.AppSettings["appid"];  
    31.         string secret = ConfigurationManager.AppSettings["secret"];  
    32.         string key = ConfigurationManager.AppSettings["key"];  
    33.         string mch_id = ConfigurationManager.AppSettings["mch_id"];  
    34.         string ip = ConfigurationManager.AppSettings["ip"];  
    35.         string PayResulturl = ConfigurationManager.AppSettings["PayResulturl"];  
    36.         //string roomid = context.Request.Params["roomid"];  
    37.         string aa = "私密自助棋牌室-退款";////商品描述交易字段格式根据不同的应用场景按照以下格式:APP——需传入应用市场上的APP名字-实际商品名称,天天爱消除-游戏充值。  
    38.   
    39.         string strcode = aa;  
    40.         byte[] buffer = Encoding.UTF8.GetBytes(strcode);  
    41.         string body = Encoding.UTF8.GetString(buffer, 0, buffer.Length);  
    42.         DateTime timestart;  
    43.         int totalhour;  
    44.         float totalfee;  
    45.         float pay_price;  
    46.         float refund_fee;  
    47.         string out_refund_no;  
    48.         int roomid;  
    49.         string openid;  
    50.         //string totalfee = context.Request.Params["totalfee"];  
    51.         string output = "";  
    52.         string signapp;  
    53.         DataSet orderhistory;  
    54.         if ((context.Request.Params["order_id"] != null) && (context.Request.Params["order_id"] != ""))  
    55.         {  
    56.             Maticsoft.BLL.order_history oh = new Maticsoft.BLL.order_history();  
    57.             orderhistory = oh.GetList(" order_id=" + orderid + "");  
    58.             pay_price = Convert.ToSingle(orderhistory.Tables[0].Rows[0]["pay_price"]);  
    59.             roomid = Convert.ToInt32(orderhistory.Tables[0].Rows[0]["room_id"]);  
    60.             openid = orderhistory.Tables[0].Rows[0]["openid"].ToString();  
    61.             System.Random Random = new System.Random();  
    62.   
    63.   
    64.             timestart = Convert.ToDateTime(orderhistory.Tables[0].Rows[0]["order_timestart"]);//订单开始时间  
    65.   
    66.             TimeSpan ts = DateTime.Now - timestart; //时间差的绝对值    
    67.             totalhour = Convert.ToInt32(ts.Hours);//间隔小时数  
    68.   
    69.             totalfee = totalhour * Convert.ToSingle(orderhistory.Tables[0].Rows[0]["price"]);  
    70.             out_refund_no = roomid + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Random.Next(999).ToString();  
    71.             refund_fee = totalfee - pay_price;  
    72.             var dic = new Dictionary<stringstring>  
    73. {  
    74.     {"appid", appid},  
    75.     {"mch_id", mch_id},  
    76.     {"nonce_str", GetRandomString(20)/*Random.Next().ToString()*/},  
    77.      {"op_user_id",mch_id},  
    78.       {"out_refund_no",out_refund_no},//商户退款号  
    79.     {"out_trade_no",orderhistory.Tables[0].Rows[0]["out_trade_no"].ToString()},//商户自己的订单号码  
    80.     {"refund_fee",/*refund_fee.ToString()*/"1"},//退款金额  
    81.     {"total_fee",orderhistory.Tables[0].Rows[0]["pay_price"].ToString()},//订单金额  
    82.     
    83.   
    84. };  
    85.   
    86.             //加入签名  
    87.             dic.Add("sign", GetSignString(dic));  
    88.   
    89.             var sb = new StringBuilder();  
    90.             sb.Append("<xml>");  
    91.   
    92.   
    93.             foreach (var d in dic)  
    94.             {  
    95.                 sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">");  
    96.             }  
    97.             sb.Append("</xml>");  
    98.             var xml = new XmlDocument();  
    99.             //  xml.LoadXml(GetPostString("https://api.mch.weixin.qq.com/pay/unifiedorder", sb.ToString()));  
    100.             CookieCollection coo = new CookieCollection();  
    101.             Encoding en = Encoding.GetEncoding("UTF-8");  
    102.   
    103.             HttpWebResponse response = CreatePostHttpResponse("https://api.mch.weixin.qq.com/secapi/pay/refund", sb.ToString(), en);  
    104.             //打印返回值  
    105.             Stream stream = response.GetResponseStream();   //获取响应的字符串流  
    106.             StreamReader sr = new StreamReader(stream); //创建一个stream读取流  
    107.             string html = sr.ReadToEnd();   //从头读到尾,放到字符串html  
    108.                                             //Console.WriteLine(html);  
    109.             xml.LoadXml(html);  
    110.             //对请求返回值 进行处理  
    111.   
    112.             var root = xml.DocumentElement;  
    113.   
    114.             DataSet ds = new DataSet();  
    115.             StringReader stram = new StringReader(html);  
    116.             XmlTextReader reader = new XmlTextReader(stram);  
    117.             ds.ReadXml(reader);  
    118.             string return_code = ds.Tables[0].Rows[0]["return_code"].ToString();  
    119.             string return_mesage = ds.Tables[0].Rows[0]["return_msg"].ToString();  
    120.             if (return_code.ToUpper() == "SUCCESS")  
    121.             {  
    122.                 //通信成功  
    123.                 string result_code = ds.Tables[0].Rows[0]["result_code"].ToString();//业务结果  
    124.                 if (result_code.ToUpper() == "SUCCESS")  
    125.                 {  
    126.                     if ((context.Request.Params["order_id"] != null) && (context.Request.Params["order_id"] != ""))  
    127.                     {  
    128.                         //更新订单信息  
    129.                         Maticsoft.Model.pokerrooms model = new Maticsoft.Model.pokerrooms();  
    130.   
    131.                         Maticsoft.Model.order_history oh1 = new Maticsoft.Model.order_history();  
    132.                         Maticsoft.BLL.pokerrooms b = new Maticsoft.BLL.pokerrooms();  
    133.   
    134.                         Maticsoft.BLL.order_history bll = new Maticsoft.BLL.order_history();  
    135.                         model = b.GetModel(roomid);  
    136.                         oh1 = bll.GetModel(Convert.ToInt32(orderid));  
    137.   
    138.                         oh1.order_timeend = DateTime.Now;  
    139.                         oh1.out_refund_no = out_refund_no;  
    140.                         oh1.refund_fee = Convert.ToDecimal(refund_fee);  
    141.   
    142.                         bll.Update(oh1);  
    143.   
    144.                         model.attribute1 = "0";//0空闲  1 使用中  
    145.                         model.room_id =Convert.ToInt32(oh1.room_id);  
    146.   
    147.                         b.Update(model);//更新棋牌室使用状态  
    148.                         var res = new Dictionary<stringstring>  
    149. {  
    150.     {"out_refund_no", out_refund_no},  
    151.     {"return_mesage", return_mesage},  
    152.     {"return_code", return_code},  
    153.     {"refund_fee",refund_fee.ToString() },  
    154.     {"order_timeend", oh1.order_timeend.ToString() },  
    155.     {"order_id", oh1.order_id.ToString() }  
    156.   
    157. };  
    158.                         signapp = JsonConvert.SerializeObject(res);  
    159.                         context.Response.Write(signapp);  
    160.                     }  
    161.   
    162.                 }  
    163.             }  
    164.   
    165.   
    166.   
    167.   
    168.         }  
    169.       //  context.Response.Write(output);  
    170.     }  
    171.   
    172.     public bool IsReusable  
    173.     {  
    174.         get  
    175.         {  
    176.             return false;  
    177.         }  
    178.     }  
    179.   
    180.     public string GetMd5Hash(String input)  
    181.     {  
    182.         if (input == null)  
    183.         {  
    184.             return null;  
    185.         }  
    186.   
    187.         MD5 md5Hash = MD5.Create();  
    188.   
    189.         // 将输入字符串转换为字节数组并计算哈希数据    
    190.         byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));  
    191.   
    192.         // 创建一个 Stringbuilder 来收集字节并创建字符串    
    193.         StringBuilder sBuilder = new StringBuilder();  
    194.   
    195.         // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串    
    196.         for (int i = 0; i < data.Length; i++)  
    197.         {  
    198.             sBuilder.Append(data[i].ToString());  
    199.         }  
    200.   
    201.         // 返回十六进制字符串    
    202.         return sBuilder.ToString();  
    203.     }  
    204.     /// <summary>    
    205.     /// 对象序列化成 XML String    
    206.     /// </summary>    
    207.     public static string XmlSerialize<T>(T obj)  
    208.     {  
    209.         string xmlString = string.Empty;  
    210.         XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));  
    211.         using (MemoryStream ms = new MemoryStream())  
    212.         {  
    213.             xmlSerializer.Serialize(ms, obj);  
    214.             xmlString = Encoding.UTF8.GetString(ms.ToArray());  
    215.         }  
    216.         return xmlString;  
    217.     }  
    218.     /// <summary>  
    219.     /// 从字符串里随机得到,规定个数的字符串.  
    220.     /// </summary>  
    221.     /// <param name="allChar"></param>  
    222.     /// <param name="CodeCount"></param>  
    223.     /// <returns></returns>  
    224.     public static string GetRandomString(int CodeCount)  
    225.     {  
    226.         string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";  
    227.         string[] allCharArray = allChar.Split(',');  
    228.         string RandomCode = "";  
    229.         int temp = -1;  
    230.         Random rand = new Random();  
    231.         for (int i = 0; i < CodeCount; i++)  
    232.         {  
    233.             if (temp != -1)  
    234.             {  
    235.                 rand = new Random(temp * i * ((int)DateTime.Now.Ticks));  
    236.             }  
    237.             int t = rand.Next(allCharArray.Length - 1);  
    238.             while (temp == t)  
    239.             {  
    240.                 t = rand.Next(allCharArray.Length - 1);  
    241.             }  
    242.             temp = t;  
    243.             RandomCode += allCharArray[t];  
    244.         }  
    245.   
    246.         return RandomCode;  
    247.     }  
    248.   
    249.   
    250.     public static string GetWebClientIp()  
    251.     {  
    252.         string userIP = "IP";  
    253.   
    254.         try  
    255.         {  
    256.             if (System.Web.HttpContext.Current == null  
    257.         || System.Web.HttpContext.Current.Request == null  
    258.         || System.Web.HttpContext.Current.Request.ServerVariables == null)  
    259.                 return "";  
    260.   
    261.             string CustomerIP = "";  
    262.   
    263.             //CDN加速后取到的IP     
    264.             CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];  
    265.             if (!string.IsNullOrEmpty(CustomerIP))  
    266.             {  
    267.                 return CustomerIP;  
    268.             }  
    269.   
    270.             CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    271.   
    272.   
    273.             if (!String.IsNullOrEmpty(CustomerIP))  
    274.                 return CustomerIP;  
    275.   
    276.             if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)  
    277.             {  
    278.                 CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    279.                 if (CustomerIP == null)  
    280.                     CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    281.             }  
    282.             else  
    283.             {  
    284.                 CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    285.   
    286.             }  
    287.   
    288.             if (string.Compare(CustomerIP, "unknown"true) == 0)  
    289.                 return System.Web.HttpContext.Current.Request.UserHostAddress;  
    290.             return CustomerIP;  
    291.         }  
    292.         catch { }  
    293.   
    294.         return userIP;  
    295.     }  
    296.   
    297.   
    298.   
    299.   
    300.     private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)  
    301.     {  
    302.         return true//总是接受     
    303.     }  
    304.   
    305.     public static HttpWebResponse CreatePostHttpResponse(string url, string datas, Encoding charset)  
    306.     {  
    307.        // string cert = @"C:\apiclient_cert.p12";  
    308.        string cert =System.Web.HttpContext.Current.Server.MapPath("/apiclient_cert.p12");  //证书已上传到网站根目录,注意安全性问题  
    309.         string password = ConfigurationManager.AppSettings["mch_id"];  
    310.        
    311.      //   X509Certificate cer = new X509Certificate(cert, password);//经过测试,这个发布到IIS后,发起微信退款时会引起<span style="color:rgb(255,0,0);font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;font-size:13px;">"基础连接已经关闭: 连接被意外关闭"</span>  
    312.           
    313.   
    314.             X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);  
    315.   
    316.    
    317.   
    318.         HttpWebRequest request = null;  
    319.         //HTTPSQ请求  
    320.         ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);  
    321.         request = WebRequest.Create(url) as HttpWebRequest;  
    322.   
    323.         request.ProtocolVersion = HttpVersion.Version10;  
    324.         request.ClientCertificates.Add(cer);//add by sui 20170505  
    325.         request.Method = "POST";  
    326.         request.ContentType = "application/x-www-form-urlencoded";  
    327.   
    328.   
    329.         //如果需要POST数据     
    330.         //if (!(parameters == null || parameters.Count == 0))  
    331.         //{  
    332.         StringBuilder buffer = new StringBuilder();  
    333.         //int i = 0;  
    334.         //foreach (string key in parameters.Keys)  
    335.         //{  
    336.         //    if (i > 0)  
    337.         //    {  
    338.         //        buffer.AppendFormat("&{0}={1}", key, parameters[key]);  
    339.         //    }  
    340.         //    else  
    341.         //    {  
    342.         //        buffer.AppendFormat("{0}={1}", key, parameters[key]);  
    343.         //    }  
    344.         //    i++;  
    345.         //}  
    346.         buffer.AppendFormat(datas);  
    347.         byte[] data = charset.GetBytes(buffer.ToString());  
    348.         using (Stream stream = request.GetRequestStream())  
    349.         {  
    350.             stream.Write(data, 0, data.Length);  
    351.         }  
    352.         //}  
    353.   
    354.         return request.GetResponse() as HttpWebResponse;  
    355.   
    356.   
    357.     }  
    358.   
    359.   
    360.     public string GetSignString(Dictionary<stringstring> dic)  
    361.     {  
    362.         string key = System.Web.Configuration.WebConfigurationManager.AppSettings["key"].ToString();//商户平台 API安全里面设置的KEY  32位长度  
    363.                                                                                                     //排序  
    364.         dic = dic.OrderBy(d => d.Key).ToDictionary(d => d.Key, d => d.Value);  
    365.         //连接字段  
    366.         var sign = dic.Aggregate("", (current, d) => current + (d.Key + "=" + d.Value + "&"));  
    367.         sign += "key=" + key;  
    368.         //MD5  
    369.         // sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5").ToUpper();  
    370.         System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();  
    371.         sign = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(sign))).Replace("-"null);  
    372.         return sign;  
    373.     }  
    374.   
    375.   
    376.     /// <summary>    
    377.     /// 获取时间戳    
    378.     /// </summary>    
    379.     /// <returns></returns>    
    380.     public static string GetTimeStamp()  
    381.     {  
    382.         TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);  
    383.         return Convert.ToInt64(ts.TotalSeconds).ToString();  
    384.     }  
    385.   
    386. }  
    展开全文
  • 首先要理解微信支付的流程。需二次握手。先把订单信息,金额传给微信微信返回相应信息,再调用微信支付。 详细的查看微信公众号的相关资料?...

    首先要理解微信支付的流程。需二次握手。

    先把订单信息,金额传给微信,微信返回相应信息,再调用微信支付。

     详细的查看微信公众号的相关资料

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    /// <summary>
           /// 新建一个普通订单
           /// </summary>
           /// <returns></returns>
           public Entity NewOrder(Entity en)
           {
               string WeiXinOpenID = en.GetValue("WeiXinOpenID").TryString();
               string AddressID = en.GetValue("AddressID").TryString();
               string OrderMessage = en.GetValue("OrderMessage").TryString();
               int OrderStatus = (int)OrderEnum.OrderStatus.待付款;
               string CommodityList = en.GetValue("CommodityList").TryString();
               string CouponRecordGuid = en.GetValue("CouponRecordGuid").TryString(); //优惠券
               decimal ManorMoney = en.GetValue("ManorMoney").TryDecimal(); //庄园币
     
               string OrderGuid = System.Guid.NewGuid().ToString();
               try
               {
                   using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
                   {
                     
     
                       #region 支付信息
                       string PayRecordID = System.Guid.NewGuid().TryString();
                       int PayStatus = (int)OrderEnum.PayStatus.未支付;
                       string OrderNum = OrderServiceDA.NewOrder(OrderGuid, WeiXinOpenID, AddressID, OriginalTotalPrice, SaleTotalPrice, OrderMessage, OrderStatus);
     
                       //添加支付记录
                       OrderServiceDA.AddPayRecord(PayRecordID, SaleTotalPrice, WeiXinOpenID, OrderGuid, PayStatus);
         
     
     
                       decimal WeixinPayMoney = SaleTotalPrice - (ManorMoney + Couponvalue);
                       if (WeixinPayMoney > 0)
                       {
                           int PayType = (int)OrderEnum.PayType.微信支付;
                           OrderServiceDA.AddPayRecordWeixinDetail(PayRecordID, PayType, WeixinPayMoney, PayStatus);
                           OrderServiceDA.UpdatePayRecordPayType(PayRecordID, PayType);
                           WeixinPayService WeixinPayService = new WeixinPayService();
                           PaymentData payData = new PaymentData();
                           payData.SetValue("attach", OrderNum);
                           payData.SetValue("body", "微信在线支付");
                           payData.SetValue("out_trade_no", OrderNum);
                           payData.SetValue("trade_type", "JSAPI");
                           payData.SetValue("total_fee", (WeixinPayMoney * 100).ToInt32());//付款金额,单位分
                           payData.SetValue("openid", WeiXinOpenID);
                           PaymentData returnData = WeixinPayService.UnifiedOrder(payData);
     
                           string prepay_id = "";
                           string SignKey = "";
                           string TimeStamp = "";
                           string NonceStr = "";
                           string returnCode = returnData.GetValue("return_code").ToString();
                           if (returnCode.ToUpper().Equals("SUCCESS"))
                           {
                               #region 获取支付签名
                               //获取支付ID
                               prepay_id = returnData.GetValue("prepay_id").ToString();
                               PaymentData jsApiParam = new PaymentData();
                               NonceStr = WeixinPayService.GenerateNonceStr();
                               TimeStamp = WeixinPayService.GenerateTimeStamp();
                               jsApiParam.SetValue("appId", WeixinPayService.AppId);
                               jsApiParam.SetValue("timeStamp", TimeStamp);
                               jsApiParam.SetValue("nonceStr", NonceStr);
                               jsApiParam.SetValue("package", "prepay_id=" + prepay_id);
                               jsApiParam.SetValue("signType", "MD5");
                               SignKey = jsApiParam.MakeSign(WeixinPayService.SignKey);
                               #endregion
                           }
                           else
                           {
                               string ErrorMessage = returnData.GetValue("return_msg").ToString();
                               FaultException exception = new FaultException(ErrorMessage);
                               throw (exception);
                           }<br>               //保存信息,用于前端再进行调起支付。
                           OrderServiceDA.UpdateWeixinPayRecord(PayRecordID, prepay_id, SignKey, TimeStamp, NonceStr);
                       }
                       else
                       {
                           if (ManorMoney == 0)
                           {
                               int PayType = (int)OrderEnum.PayType.优惠券;
                               OrderServiceDA.UpdatePayRecordPayType(PayRecordID, PayType);
                           }
                           else
                           {
                               int PayType = (int)OrderEnum.PayType.庄园币;
                               OrderServiceDA.UpdatePayRecordPayType(PayRecordID, PayType);
                           }
                       }
                       #endregion
                       scope.Complete();
                   }
               }
               catch (Exception ex)
               {
                   Entity result = new Entity(new PropertyCollection());
                   result.AddSimple("Status", -10, typeof(int));
                   result.AddSimple("StatusText", ex.Message, typeof(string));
                   return result;
               }
     
     
               Entity resultEntity = new Entity(new PropertyCollection());
               resultEntity.AddSimple("Status", 115, typeof(int));
               resultEntity.AddSimple("StatusText", "操作成功.", typeof(string));
               resultEntity.AddSimple("OrderGuid", OrderGuid, typeof(string));
               return resultEntity;
           }

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using TalentCloud.Common;
    using TalentCloud.Base.Utils;
    using TalentCloud.Agriculture.Services;
    using System.Security.Cryptography;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using TalentCloud.Common.Log;
     
    namespace TalentCloud.Agriculture.Weixin.Services
    {
        public class WeixinPayService
        {
            /// <summary>
            /// 微信分配的AppId
            /// </summary>
            public string AppId = TCConfigManager.GetConfig("WeixinPayAppid").TryToString();
            /// <summary>
            /// 公众号的支付密钥appsecret
            /// </summary>
            public string AppSecret = TCConfigManager.GetConfig("WeixinPayAppsecret").TryToString();
            /// <summary>
            /// 当前用户IP
            /// </summary>
            public string Ip = WCFClientInfo.ClientIP;
            /// <summary>
            /// 商户号
            /// </summary>
            public string MchId = TCConfigManager.GetConfig("MchId").TryToString();
            /// <summary>
            /// 签名Key
            /// </summary>
            public string SignKey = TCConfigManager.GetConfig("SignKey").TryToString();
            //=======【证书路径设置】=====================================
            /* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要)
            */
            public string CertPath = TCConfigManager.GetConfig("certPath").TrySafeString();
            /// <summary>
            /// 证书密码,windows上可以直接双击导入系统,导入过程中会提示输入证书密码,证书密码默认为您的商户ID
            /// </summary>
            public string CertPassword = TCConfigManager.GetConfig("certpsd").TrySafeString();
            /// <summary>
            /// 支付结果通知回调url,用于商户接收支付结果
            /// </summary>
            public string NOTIFY_URL =TCConfigManager.GetConfig("WebSite").TryToString()+ "/Weixin/PayCallback.aspx";
     
            
            /// <summary>
            ///   生成随机串,随机串包含字母或数字 @return 随机串
            /// </summary>
            /// <returns></returns>
            public string GenerateNonceStr()
            {
                return Guid.NewGuid().ToString().Replace("-", "");
            }
     
            /// <summary>
            /// 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数
            /// </summary>
            /// <returns></returns>
            public string GenerateTimeStamp()
            {
                TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                return Convert.ToInt64(ts.TotalSeconds).ToString();
            }
     
            /// <summary>
            ///  * 统一下单
            ///* @param WxPaydata inputObj 提交给统一下单API的参数
            ///* @param int timeOut 超时时间
            ///* @throws WxPayException
            ///* @return 成功时返回,其他抛异常
            /// <returns></returns>
            public PaymentData UnifiedOrder(PaymentData inputObj, int timeOut = 15)
            {
                string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
                //检测必填参数
                if (!inputObj.IsSet("out_trade_no"))
                {
                    throw new Exception("缺少统一支付接口必填参数out_trade_no!");
                }
                else if (!inputObj.IsSet("body"))
                {
                    throw new Exception("缺少统一支付接口必填参数body!");
                }
                else if (!inputObj.IsSet("total_fee"))
                {
                    throw new Exception("缺少统一支付接口必填参数total_fee!");
                }
                else if (!inputObj.IsSet("trade_type"))
                {
                    throw new Exception("缺少统一支付接口必填参数trade_type!");
                }
     
                //关联参数
                if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
                {
                    throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
                }
                if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
                {
                    throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
                }
     
                //异步通知url未设置,则使用配置文件中的url
                if (!inputObj.IsSet("notify_url"))
                {
                    inputObj.SetValue("notify_url", NOTIFY_URL);//异步通知url
                }
     
     
                inputObj.SetValue("appid", AppId);//公众账号ID
                inputObj.SetValue("mch_id", MchId);//商户号
                inputObj.SetValue("spbill_create_ip", Ip);//终端ip           
                inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
                //签名
                inputObj.SetValue("sign", inputObj.MakeSign(SignKey));
                string xml = inputObj.ToXml();
                string response = HttpService.Post(xml, url, false, timeOut, "", "");
                PaymentData result = new PaymentData();
                result.FromXml(response);
                return result;
            }
     
            /// <summary>
            /// 申请退款
            /// </summary>
            /// <param name="inputObj">提交给申请退款API的参数</param>
            /// <param name="CertPath">证书路径</param>
            /// <param name="CertPassword">证书密码</param>
            /// <param name="timeOut">超时时间</param>
            /// <returns></returns>
            public PaymentData Refund(PaymentData 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 Exception("退款申请接口中,out_trade_no、transaction_id至少填一个!");
                }
                else if (!inputObj.IsSet("out_refund_no"))
                {
                    throw new Exception("退款申请接口中,缺少必填参数out_refund_no!");
                }
                else if (!inputObj.IsSet("total_fee"))
                {
                    throw new Exception("退款申请接口中,缺少必填参数total_fee!");
                }
                else if (!inputObj.IsSet("refund_fee"))
                {
                    throw new Exception("退款申请接口中,缺少必填参数refund_fee!");
                }
                else if (!inputObj.IsSet("op_user_id"))
                {
                    throw new Exception("退款申请接口中,缺少必填参数op_user_id!");
                }
     
                inputObj.SetValue("appid", AppId);//公众账号ID
                inputObj.SetValue("mch_id", MchId);//商户号
                inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
                inputObj.SetValue("sign", inputObj.MakeSign(SignKey));//签名
                string xml = inputObj.ToXml();
                string response = HttpService.Post(xml, url, true, timeOut, CertPath, CertPassword);
                //将xml格式的结果转换为对象以返回
                PaymentData result = new PaymentData();
                result.FromXml(response);
                return result;
            }
     
            /// <summary>
            /// 企业付款接口( 开通条件:
            /// 1、商户号已入驻90日
            /// 2、商户号有30天连续正常交易
            /// 登录微信支付商户平台-产品中心,开通企业付款。)
            /// </summary>
            /// <param name="inputObj">参数</param>
            /// <param name="CertPath">证书路径</param>
            /// <param name="CertPassword">证书密码</param>
            /// <param name="timeOut">超时时间</param>
            /// <returns></returns>
            public PaymentData Transfers(PaymentData inputObj, int timeOut = 6)
            {
                string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
     
                #region 检测必要参数
     
                //检测必填参数
                if (!inputObj.IsSet("partner_trade_no") && !inputObj.IsSet("partner_trade_no"))
                {
                    throw new Exception("企业付款接口中,缺少必填参数partner_trade_no!");
                }
                if (!inputObj.IsSet("openid"))
                {
                    throw new Exception("企业付款接口中,缺少必填参数openid!");
                }
                if (!inputObj.IsSet("check_name"))
                {
                    throw new Exception("企业付款接口中,缺少必填参数check_name!");
                }
                else
                {
                    string checkName = inputObj.GetValue("check_name").ToString();
                    switch (checkName)
                    {
                        case "FORCE_CHECK":
                        case "OPTION_CHECK":
                            if (!inputObj.IsSet("check_name"))
                            {
                                throw new Exception("企业付款接口中,缺少必填参数re_user_name!");
                            }
                            break;
                        default:
                            break;
                    }
                }
                if (!inputObj.IsSet("amount"))
                {
                    throw new Exception("企业付款接口中,缺少必填参数amount!");
                }
                if (!inputObj.IsSet("desc"))
                {
                    throw new Exception("企业付款接口中,缺少必填参数desc!");
                }
                if (!inputObj.IsSet("spbill_create_ip"))
                {
                    throw new Exception("企业付款接口中,缺少必填参数spbill_create_ip!");
                }
                #endregion
     
                #region 添加公共参数
                inputObj.SetValue("mch_appid", AppId);//公众账号ID
                inputObj.SetValue("mchid", MchId);//商户号
                inputObj.SetValue("spbill_create_ip", Ip);//随机字符串
                inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
                inputObj.SetValue("sign", inputObj.MakeSign(SignKey));//签名
                #endregion
                string xml = inputObj.ToXml();
                var start = DateTime.Now;
                string response = HttpService.Post(xml, url, true, timeOut, CertPath, CertPassword);
                LogHelper.WriteFileLog("TransfersData", response);
                PaymentData result = new PaymentData();
                result.FromXml(response);
     
                return result;
            }
     
     
            #region 微信发红发,未事项,签名失败问题,未解决
            /// <summary>
            /// 微信红包
            /// </summary>
            public class PayWeiXin
            {
                public string nonce_str { get; set; }
                public string sign { get; set; }
                public string mch_billno { get; set; }
                public string mch_id { get; set; }
                public string wxappid { get; set; }
                public string nick_name { get; set; }
                public string send_name { get; set; }
                public string re_openid { get; set; }
                public int total_amount { get; set; }
                public int min_value { get; set; }
                public int max_value { get; set; }
                public int total_num { get; set; }
                public string wishing { get; set; }
                public string client_ip { get; set; }
                public string act_id { get; set; }
                public string act_name { get; set; }
                public string remark { get; set; }
                public string logo_imgurl { get; set; }
                public string share_content { get; set; }
                public string share_url { get; set; }
                public string share_imgurl { get; set; }
     
            }
     
            /// <summary>
            /// 调用微信支付接口,发送红包
            /// </summary>
            /// <param name="payForWeiXin"></param>
            /// <returns></returns>
            public string PayRedBag(PaymentData inputObj)
            {   ////商户号
                inputObj.SetValue("mch_id", MchId);
                //商户 appid
                inputObj.SetValue("wxappid", AppId);
                inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
                //签名
                inputObj.SetValue("sign", inputObj.MakeSign(SignKey));
                string xml = inputObj.ToXml();
                string result = string.Empty;
                try
                {
                    result = PostPage("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", xml);
                    LogHelper.WriteFileLog("PayRedBagpostData", xml);
                    LogHelper.WriteFileLog("PayRedBag", result);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteFileLog("PayRedBagError", ex.Message);
                }
                return result;
            }
     
     
            /// <summary>
            /// post微信请求
            /// </summary>
            /// <param name="posturl"></param>
            /// <param name="postData"></param>
            /// <returns></returns>
            public string PostPage(string posturl, string postData)
            {
                Stream outstream = null;
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = Encoding.UTF8;
                byte[] data = encoding.GetBytes(postData);
                // 准备请求... 
                try
                {
                     
                    //CerPath证书路径
                    string certPath = TCConfigManager.GetConfig("certPath").TrySafeString();
                    //证书密码
                    string password = TCConfigManager.GetConfig("certpsd").TrySafeString();
                     
                    X509Certificate2 cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(certPath, password, X509KeyStorageFlags.MachineKeySet);
     
                    // 设置参数 
                    request = WebRequest.Create(posturl) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    request.Method = "POST";
                    request.ContentType = "text/xml";
                    request.ContentLength = data.Length;
                    request.ClientCertificates.Add(cert);
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();
                    //发送请求并获取相应回应数据 
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求 
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码 
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content;
     
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    return string.Empty;
                }
            }
     
            /// <summary>
            /// Md5加密
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static String Encrypt(String s)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
                bytes = md5.ComputeHash(bytes);
                md5.Clear();
                string ret = "";
                for (int i = 0; i < bytes.Length; i++)
                {
                    ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
                }
                return ret.PadLeft(32, '0');
            }
     
            public string RandomStr(string str, int Length)
            {
                string result = string.Empty;
                Random rd = new Random();
                for (int i = 0; i < Length; i++)
                {
                    result += str[rd.Next(str.Length)];
                }
                return result;
            }
            #endregion
        }
    }<br><br>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    //弹出微信支付页面
    WeixinJSBridge.invoke('getBrandWCPayRequest',
        {
            "appId": data.AppID,     //公众号名称,由商户传入
            "timeStamp": data.TimeStamp,         //时间戳,自1970年以来的秒数
            "nonceStr":  data.nonceStr, //随机串
            "package": "prepay_id="+data.prepay_id,
            "signType": "MD5",         //微信签名方式:
            "paySign":  data.SignKey //微信签名
        },
        function (res) {
            switch (res.err_msg) {
                case "get_brand_wcpay_request:ok":
                    layer.open({
                        content: '充值成功',
                        skin: 'msg',
                        time: 2 //2秒后自动关闭
                    });
                    browserHistory.push('/WeChat/dist/member/wallet/');
                    break;
                case "get_brand_wcpay_request:cancel":
                    break;
                case "get_brand_wcpay_request:fail":
                    layer.open({
                        content: '充值失败!',
                        skin: 'msg',
                        time: 2 //2秒后自动关闭
                    });
                    break;
                default:
            }
        });

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    public partial class PayCallback : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                StreamReader reader = new StreamReader(Request.InputStream, Encoding.UTF8);
                string postStr = reader.ReadToEnd();
                reader.Close();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(postStr);
                string ReturnCode = doc.SelectSingleNode("//result_code").InnerText;//获取支付结果代码
                StringBuilder xmlStr = new StringBuilder();
                LogHelper.WriteFileLog("PayCallback", ReturnCode);
                if (ReturnCode.ToUpper().Equals("SUCCESS"))
                {
                    string strOrderNo = doc.SelectSingleNode("//out_trade_no").InnerText;
                    //获取微信支付流水号
                    string weChatTransaction_Id = doc.SelectSingleNode("//transaction_id").InnerText;
                    string WeiXinOpenID = doc.SelectSingleNode("//openid").InnerText;
                    try
                    {
                        //获取订单号
                        WeiXinService WeiXinService = new WeiXinService();
                        Entity result= WeiXinService.PayOrder(WeiXinOpenID, strOrderNo, weChatTransaction_Id);
                        string Status = result.GetValue("Status").ToString();
                        string StatusText = result.GetValue("StatusText").ToString();
                        LogHelper.WriteFileLog("Pay", WeiXinOpenID + "," + strOrderNo + "," + weChatTransaction_Id + "," + Status + "," + StatusText);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteFileLog("PayError", ex.Message + "," + WeiXinOpenID + "," + strOrderNo + "," + weChatTransaction_Id);
                        //记录错误
                        throw ex;
                    }
     
                    #region 通知微信后台支付成功
                    Response.ContentType = "text/xml";
                    xmlStr.AppendLine("<xml>");
                    xmlStr.AppendFormat("<return_code><![CDATA[SUCCESS]]></return_code>");
                    xmlStr.AppendFormat("<return_msg><![CDATA[OK]]></return_msg>");
                    xmlStr.AppendFormat("</xml>");
                    #endregion
                }
                else
                {
                    #region 通知微信后台支付失败
                    Response.ContentType = "text/xml";
                    xmlStr.AppendLine("<xml>");
                    xmlStr.AppendFormat("<return_code><![CDATA[FAIL]]></return_code>");
                    xmlStr.AppendFormat("<return_msg><![CDATA[支付失败]]></return_msg>");
                    xmlStr.AppendFormat("</xml>");
                    #endregion
                }
                Response.Write(xmlStr.ToString());
            }

      

    展开全文
  • C#实现微信支付

    2018-05-24 08:28:09
    实现微信支付之前,需要到微信开发平台认证,这些认证和配置信息我就不多说了,这里主要从代码层面实现支付。 12345678910111213141516171819202122232425function onBridgeReady(){ Weix...

    C#实现微信支付

      最近在做微信公众号开发,在微信支付上遇到一些问题,困惑了3天,今天终于搞定。期间要感谢一些大神的帮助,趁热下面分享一下我的经验。

    在实现微信支付之前,

    需要到微信开发平台认证,这些认证和配置信息我就不多说了,这里主要从代码层面实现支付。

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    function onBridgeReady(){

       WeixinJSBridge.invoke(

           'getBrandWCPayRequest', {

               "appId" "wx2421b1c4370ec43b",     //公众号名称,由商户传入    

               "timeStamp"" 1395712654",         //时间戳,自1970年以来的秒数    

               "nonceStr" "e61463f8efa94090b1f366cccfbbb444", //随机串    

               "package" "prepay_id=u802345jgfjsdfgsdg888",     //统一订单号

               "signType" "MD5",         //微信签名方式:    

               "paySign" "70EA570631E4BB79628FBCA90534C63FF7FADD89" //支付签名

           },

           function(res){     <br>       alert(res.err_msg);

               if(res.err_msg == "get_brand_wcpay_request:ok" ) {}     // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回    ok,但并不保证它绝对可靠。

           }

       );

    }

    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{

       onBridgeReady();

    }

      上面的JS代码是官方文档上贴出来的,官方文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7。

    下面对JS中的几个参数做主要的讲解:

    在开发过程中,有4个非常重要的参数:一个是appid,AppSecret,apikey和商户号。上面js中的appID就是其中之一。timeStamp是一个时间戳,10位数,nonceStr是随机数,32位以内,这里最重要的两个参数,也是最容易出错的就是package和paySign了。我一一说来。先说package,这里需要用到prepay_id,这个参数是微信生成的订单号,需要我们调用统一订单接口来获取。官方文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1。可以从文档看到,要获取prepay_id,需要很多参数,这里面有一个非常重要的参数就是签名。注意:这里的签名和JS中的支付签名和不同的。 签名的算法文档里有,我只是强调两点。一个是参数的顺序,一定一定要按照ASCII从小到大拼接,二是apikey一定不能错,这个apikey在哪里呢?登入商户平台就可以设置了。

     

      拼接好签名需要的字符串后,进行MD5加密就可以得到签名了。然后把签名和前面所有的参数组合成xml格式的字符串,调用给的接口URL地址:
    https://api.mch.weixin.qq.com/pay/unifiedorder就可以返回一个xml结果,解析出其中的prepay_id,这样这个参数就成功获取到了。
       下面再说JS中的最后一个参数PaySign:支付签名。支付签名的算法也是一样,把JS中的其他5个参数按照顺序拼接,加上apikey,MD5加密,ok。
    签名的算法都是一样的,只是参数值不同而已。这里需要注意一点。在支付签名的时候,用到随机数nonceStr,时间戳timeStamp,而签名时使用的这两个参数
    和JS中的这两个参数的值要是一样的,是同一个随机数,同一个时间戳。为什么呢?虽然文档没说,但是我的理解是:支付签名是通过随机数和时间戳来生成的,
    然后在支付的时候,把随机数,时间戳,支付签名一起发送过去,那么在微信进行验证的时候,也会根基js中的随机数和时间戳生成签名和你发送的支付签名进行比较,
    如果你在js中重新获取新的随机数、时间戳,那么算出来的签名和你发送的签名就会不一样,就会报错:签名失败。

      到此,代码层面的注意点就这些,当然,还有其他的一些注意点。比如其中有用的openid,这个参数也是需要调用接口获取到的,还有支付授权的目录是否配置正确。
      最后一句总结:仔细看文档,最后问别人,可以找微信支付相关的群,里面很多大神的,我之前就是遇到一个问题纠结了2天没解决,(有时候光看文档也不行),
    然后怒加了8个微信开发的群,最终在高人的指点下终于搞出来了。再次表示感谢。程序员是一个热爱分享的群体,他们非常乐意把自己知道的东西分享出去。所以不懂的时候,多问问。



    微信开发(4)微信支付3.7C#版

    1.下载示例代码

    下载地址:http://dldx.csdn.net/fd.php?i=730282530419400&s=f205228286602140c31c30095f3d6f24
    不要下载官方示例代码,那个例子也跑不通。

    2、 解压缩

    打开code目录下的WeiPayDemo项目。项目代码是用老版本的VS做的,在VS013中打开时会进行一些转换。

    3、 打开PayConfig.cs修改如下参数:

    public static string MchId = "2222831601"; //修改为你的微信商户号
    public static string AppId = "wx11f796122a4333a4"; //修改为你的公众号的AppId
    public static string AppSecret = "555f4555cee55551439555bd7990555"; //你的AppSecret
    public static string AppKey = "1597777cceeccee5cccc77cbd7991111";//你的支付API密钥
    public static string SendUrl = "http://xxx.my3w.com/Send.aspx"; //发起支付页面
    public static string PayUrl = "http://xxx.my3w.com/WeiPay.aspx"; //支付页面
    public static