微信异步通知:

[AcceptVerbs("POST")]
public void Notify()
{
//编码(101-登录无效,102-账号无效,200-成功,201-失败,202~299-其他原因1-99,300-无效提交方式,400-无效参数)
MessagesDataCodeModel json = new MessagesDataCodeModel(false, "无效参数", 401);
int notify_id = 0;
string result = "failed";
try
{
//得到微信推送来的xml数据
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
string postStr = Encoding.UTF8.GetString(b); SortedDictionary<string, string> requestXML = Common.TenpayUtil.GetInfoFromXml(postStr);
if (requestXML != null && requestXML.Count > 0)
{
Models.WeiXinNotifyRecords model = GetNotifyModel(requestXML);//将xml数据转换为Model
model.remark = "";//postStr;
model.CreateDate = DateTime.Now;
notify_id = WeiXinNotifyRecordsBLL.Append(model);//记录微信通知
model.ID = notify_id; #region 验签
//微信返回的签名字符串
string sign = requestXML["sign"];
requestXML.Remove("sign");
//待签名字符串
string signStr = AlipaySignature.GetSignContent(requestXML) + "&key=" + Common.ConfigApi.WeiXinPay_API_Key;//借用阿里的方法
string newsign = Utils.GetMD5(signStr).ToUpper();//MD5加密,转大写
bool ValidateSign = sign == newsign;//验证签名是否一致
#endregion #region 处理订单
if (ValidateSign)
{
Models.TradeInfo tradeInfo = TradeInfoBLL.GetEntityByTradeNo(model.out_trade_no);
if (tradeInfo != null && model.appid == ConfigApi.WeiXinPay_App_app_id && model.mch_id == ConfigApi.WeiXinPay_App_mch_id)
{
result = "success";//TODO:处理订单逻辑,完成后 result="success"
}
else
{
result = "TradeError";
logger.Error("WeiXinPayController.Notify【订单数据与通知数据不符】");
}
}
else
{
result = "CheckSignError";
logger.Error("WeiXinPayController.Notify【验签失败】");
}
#endregion
}
}
catch (Exception ex)
{
logger.Error("WeiXinPayController.Notify【程序异常】", ex);
result = "exception";
} string xmlstr = @"<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
if (result != "success")
{
xmlstr = @"<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[FAIL]]></return_msg></xml>";
}
HttpContext.Current.Response.Write(xmlstr);
HttpContext.Current.Response.End();
}

  

把XML数据转换为SortedDictionary<string, string>集合:

/// <summary>
/// 把XML数据转换为SortedDictionary<string, string>集合
/// </summary>
/// <param name="strxml"></param>
/// <returns></returns>
public static SortedDictionary<string, string> GetInfoFromXml(string xmlstring)
{
SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);
XmlElement root = doc.DocumentElement;
int len = root.ChildNodes.Count;
for (int i = 0; i < len; i++)
{
string name = root.ChildNodes[i].Name;
if (!sParams.ContainsKey(name))
{
sParams.Add(name.Trim(), root.ChildNodes[i].InnerText.Trim());
}
}
}
catch { }
return sParams;
}

  

把参数排序后拼接,得到签名字符串:

 public static string GetSignContent(IDictionary<string, string> parameters)
{
// 第一步:把字典按Key的字母顺序排序
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator(); // 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder("");
while (dem.MoveNext())
{
string key = dem.Current.Key;
string value = dem.Current.Value;
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
{
query.Append(key).Append("=").Append(value).Append("&");
}
}
string content = query.ToString().Substring(0, query.Length - 1); return content;
}

  

签名算法文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3

微信App支付通知验签的更多相关文章

  1. 支付宝支付集成中:refund_fastpay_by_platform_nopwd接口服务器通知验签不通过

    在做p2p配资平台,也就是公司的项目,遇到了一个问题:refund_fastpay_by_platform_nopwd接口服务器通知验签不通过 下面是实录: 通知服务器的POST过来的数据: 1.si ...

  2. 微信支付-微信公众号支付,微信H5支付,微信APP支付,微信扫码支付

    在支付前,如果使用第三方MVC框架,则使用重写模式,服务器也需要配置该项 if (!-e $request_filename){ rewrite ^/(.*)$ /index.php/$ last; ...

  3. 微信app支付 ci框架做的

    /**     * 组合微信app支付  获得prepayid     * @param int $order_num     */    private function _wxpay_reques ...

  4. .net 微信APP支付接口的开发流程以及坑

    流程 申请APP的微信支付 申请成功之后得到APPID 商户号 以及自己设置商户号的支付密码 这时就可以开发接口了 微信APP支付API:https://pay.weixin.qq.com/wiki/ ...

  5. 微信app支付android客户端以及.net服务端实现

    由于公司运营需要,需要在客户端(android/ios)增加微信以及支付宝支付,在调用微信app支付时遇到一些问题,也算是一些踩过的坑,记录下来 ,希望能对.net开发者服务端网站更快的集成微信app ...

  6. php开发微信APP支付接口

    之前在开发APP中用到了微信支付,因为是第一次用,所以中途也遇到了好多问题,通过查看文档和搜集资料,终于完成了该功能的实现.在这里简单分享一下后台php接口的开发实例. 原文地址:代码汇个人博客 ht ...

  7. Android版-微信APP支付

    首发地址: Android版-微信APP支付 欢迎留言.转发 微信极速开发系列文章(微信支付.授权获取用户信息等):点击这里 目录 1.注册账号.开发者认证 2.添加应用 3.申请微信支付 4.技术开 ...

  8. 微信APP支付 - C#

    最近挺忙的,没时间写东西.然后在弄微信APP支付,网上的搜索一趟,都比较凌乱,我也遇到一些坑,不过也算弄好了,记录分享一下. 1.准备各种调用接口需要的参数,配置app.config. <!-- ...

  9. 微信APP支付整体流程记录备忘

      支付整体流程见文档:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_3   商户系统和微信支付系统主要交互说明:     步骤1: ...

随机推荐

  1. 批量插入数据(基于Mybatis的实现-Oracle)

    前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. -------------------------------- ...

  2. ansible playbook 示例

    http://blog.keshi.org/hogememo/2015/12/07/exploiting-ansible-jinja2 http://blog.keshi.org/hogememo/2 ...

  3. My SQL外键约束

    外键约束对子表的含义:如果在父表中找不到对应的候选键,则不能对子表进行insert/update操作 外键约束对父表的含义:在父表上进行update/delete以更新或删除在子表中有一条或多条对应匹 ...

  4. Thoughtful function is also good for investigation

    Did you know how many friends in your IM? Some of them you are not familiar with, but your friends c ...

  5. jquery一些基本函数

    jquery.com1.x版本兼容ie2.x版本简化适合移动端 $('li:first') 第一个$('li:eq(2)') $('li:last') 最后一个$('li:odd') 偶数行 1 3$ ...

  6. YCSB测试Mysql,MongoDB,TokuMX,Couchbase性能

    测试是由同事完成的,这里只做收藏. 测试说明: 1.数据量为3kw记录,每条记录11个字段,一个为主键,主键为字符类型,类似:user****,后续为数值 其他10字段为字符类型,100字符,记录长度 ...

  7. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

  8. 艺术品照片融合到背景墙上效果及DEMO

    演示地址: 功能:图片按照角度增加阴影,比较复杂的功能就是当墙面是不规则的时候,贴的艺术品必须按照墙面的角度进行变形处理.

  9. a different object with the same identifier value was already associated with the session:错误;

    当出现a different object with the same identifier value was already associated with thesession时,一般是因为在h ...

  10. 代理服务器(Proxy)原理

    17.1 什么是代理服务器(Proxy)   以类似代理人的身份去取得用户所需要的数据就是了! 但是由于它的『代理』能力,使得我们可以透过代理服务器来达成防火墙功能与用户浏览数据的分析!   此外,也 ...