zoukankan      html  css  js  c++  java
  • PC端网站集成微信支付的关键代码

    目前来看,PC段实现微信支付,需要生成二维码,然后用微信扫码支付。

     
    微信支付官网DEMO没有ASP.NET的,只能自己从帮助文档里扣代码摸索。这里需要说明几点特别要注意的。
     
    看了下微信官网的文档,总结支付方式应该是两种:
     
    1、Navite 静态链接方式。
    2、JSAPI 方式。这种应该是手机端用到的。只支持微信内置浏览器。
     
    我使用的是第一种方式。
     
    但是据文档描述,这种方式还分为两种。请求流程不同,如下:
     
    1、发送支付请求到微信 -> 微信返回支付Prepay_ID -> 发送支付信息到微信 -> 返回支付URL -> 生成二维码 -> 支付
     
    2、发送支付信息到微信 -> 返回支付URL -> 生成二维码 -> 支付
     
    网上有不少例子是使用第一种方式,但是我试了好几次都没成功。第二种方式非常简单,返回的支付URL业很短。最后使用了这种方式。
     
    与支付宝不同的是,微信用POST提交方式,且需要提交XML,如下:
     
     1     public static string Get_Code_URL(SortedDictionary<string, string> sParaTemp)
     2     {
     3         StringBuilder submitStr = new StringBuilder(); //要提交的数据
     4         Dictionary<string, string> dPara = BuildRequestPara(sParaTemp);
     5         submitStr.Append("<xml>");
     6         foreach (KeyValuePair<string, string> kvp in dPara)
     7         {
     8             submitStr.Append("<" + kvp.Key + ">" + kvp.Value + "</" + kvp.Key + ">");
     9         }
    10         submitStr.Append("</xml>");
    11 
    12         //提交
    13         HttpWebRequest request = WebRequest.Create(PAY_URL) as HttpWebRequest;
    14         byte[] requestBytes = Encoding.GetEncoding("UTF-8").GetBytes(submitStr.ToString());
    15         request.Method = "POST";
    16         request.ContentType = "text/xml";
    17         request.ContentLength = requestBytes.Length;
    18         Stream requestStream = request.GetRequestStream();
    19         requestStream.Write(requestBytes, 0, requestBytes.Length);
    20         requestStream.Close();
    21 
    22         //获取结果
    23         HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    24         StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding("UTF-8"));
    25         string result = reader.ReadToEnd();
    26 
    27         //处理结果
    28         Dictionary<string, string> dicResult = ReceivePostXmlData(result);
    29         if (dicResult["return_code"] != null && dicResult["return_code"].Equals("SUCCESS"))
    30         {
    31             if (dicResult.Keys.Contains("code_url"))
    32             {
    33                 return dicResult["code_url"].ToString();
    34             }
    35             else {
    36                 return "";
    37             }
    38         }
    39         return "";
    40     }
    生成签名的方式跟支付宝一样,这里略过了。
     
    还有一点需要特别注意的是参数问题。微信一共给了4个参数
    APPID、MCH_ID、APPSECRET和KEY。
    这里需要特别注意。生成签名用的是APPID、MCH_ID和KEY。刚开始,管理微信的人给了APPSECRET,跟KEY一样都是32位的,怎么也提示签名错误。后来揍了他一顿,又扣出来个KEY,一次成功!!!浪费了两天时间。
     
    还有生成签名时需要注意。生成签名的字符串不要进行任务转码。
    如果生成签名的参数正确的话,还是报错,可通过 http://mch.weixin.qq.com/wiki/tools/signverify/ 验证下,以查找自己代码的问题。
     
    最后还是附上添加参数的代码:
     
     1      SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
     2         sParaTemp.Add("appid", WXConfig.APPID);
     3         sParaTemp.Add("mch_id", WXConfig.MCH_ID);
     4         sParaTemp.Add("trade_type", "NATIVE");
     5         sParaTemp.Add("body", cp[0].OrderItem);
     6         sParaTemp.Add("nonce_str", WXConfig.WX_Nonce_Str());
     7         sParaTemp.Add("notify_url", "http://my.test.sci99.com/wx_notify_url.aspx");
     8         sParaTemp.Add("out_trade_no", cp[0].PayID.ToString());
     9         sParaTemp.Add("spbill_create_ip", Page.Request.UserHostAddress);
    10         sParaTemp.Add("total_fee", (cp[0].CountMoney*10*10).ToString()); //单位分
    11         sParaTemp.Add("product_id", cp[0].PayID.ToString());
    12 
    13         string CODE_URL = WXConfig.Get_Code_URL(sParaTemp);
    这里需要注意,微信支付的金额单位是分。
     
    生成二维码用了Gma.QrCodeNet.Encoding.dll
     
  • 相关阅读:
    手动添加 memcached.jar包
    easyui返回数据类型
    负载均衡
    nginx负载均衡
    nginx配置文件详解
    Js操作Select大全(取值、设置选中)
    jQuery select的操作代码
    jQuery对Select操作大集合
    PHP+AJAX无刷新返回天气预报
    一个好用的PHP验证码类
  • 原文地址:https://www.cnblogs.com/chenliang-zibo/p/4337577.html
Copyright © 2011-2022 走看看