微信异步通知:

[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. iOS--KVO的实现原理与具体应用

    本文分为2个部分:概念与应用. 概念部分旨在剖析KVO这一设计模式的实现原理,应用部分通过创建的项目,以说明KVO技术在iOS开发中所带来的作用: 如果是作为是刚接触KVO的初学者,可以在了解基本原理 ...

  2. go read text file into string array

    http://stackoverflow.com/questions/5884154/golang-read-text-file-into-string-array-and-write 方法一 pac ...

  3. Retina屏实现1px边框

    问题描述 通常我们实现边框的方法都是设置1px的边框,但是在retina屏上因为设备像素比的不同,边框在移动设备上的表现也不相同,例如在devicePixelRatio = 2的retina屏下会显示 ...

  4. hibernate 左链接查询

    select pro from Provide as pro left join pro.labels as la left join pro.city as c where 1=1

  5. wordpress woodstock主题导入demo xml文件 execution time out

    1.已设置php.ini max_execution_time = 240 导入显示设置60 2.wp-config.php 添加 set_time_limit(600); 无效 3. .htacce ...

  6. 在xilinxFPGA上使用microblaze及自写GPIO中断

    很久很久没有更新过博客了,今天来扒一扒FPGA上CPU软核的使用. 主要完成的功能:使用的开发板是nexys 4 DDR,板上有16个switch以及16个LED,需要完成microblaze对led ...

  7. GPS开发之知识储备(NMEA0183)

    GPS是英文Global Positioning System(全球定位系统)的简称. NMEA0183(http://files.cnblogs.com/files/libra13179/NMEA0 ...

  8. 块级格式化上下文(block formatting context)

    在CSS2.1中,有三种定位方案--普通流.浮动和绝对定位: 普通流:元素按照先后位置自上而下布局,inline元素水平排列,直到行被占满后换行,block元素则被渲染为完整的一行,除非指定,所有元素 ...

  9. 初尝 JFinal 项目(二)

    这里以Roles角色表修改功能做一个例子 RolesController /** * 角色管理控制类 * @author 御手洗红豆 */public class RolesController ex ...

  10. mac 查看无线wifi的密码

    finder->应用程序->实用工具->钥匙串访问->右上角输入wifi名查找->显示密码(需要管理员账号)