谁做过农业银行.net 微信支付接口口 .net

1914人阅读
C#、类库、方法(69)
知乎:显著提升程序员身心健康和工作效率的装备有哪些?
1.机械键盘:,,
2.静电容:
3.情怀插入:小黑USB键盘 ThinkPad USB Track Point 键盘 0B47082
显示器:Eizo,戴尔,三星,明基,22寸以上()
PC配置:i7,16G,SSD,双显示器
电脑椅:(个人经济承受能力下,最贵的)
萌妹纸一枚(多多益善,不过太多小心受用不起)
1.Win(Visual Studio)
2.Linux(Vim,其他真的还有必要说吗?)
3.Mac(Xcode,CodeRunner,SnippetsLab)
4.跨平台(Pycharm,UltraEdit,Eclipse,Source Insight,Codeblocks)
5.插件(Visual Assist X,Pydev)
1.听音乐:
2.玩游戏: v1,v2
微信刷卡支付接口
应用场景实例
收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,由商户收银台或者商户后台调用该接口发起支付。
微信提供5种开发语言的SDK,选择自己的开发语言下载,项目中会有很多示例。
简略版 数据需自行获取 
public class ToWeixinPayBLL
微信扫码支付
public static ResponseDTO GetWeixinPayRequestExecute(string osaleh_osalehID, string str_osaled_osaledID, string txtPaymentCode, string orderType, UsersEntity user)
ResponseDTO resp = new ResponseDTO();
decimal osaled_amount = 0;
IList&OsaledEntity& OsaledList = new List&OsaledEntity&();
#region 提交支付信息,并接收返回结果
WxPayData data = new WxPayData();
data.SetValue("auth_code", txtPaymentCode);
data.SetValue("body", Description);
Int32 totalAmount = (Int32)(Convert.ToDecimal(osaled_amount) * 100);
data.SetValue("total_fee", totalAmount);
catch (Exception ex)
ExceptionLog.LogExp(ex.Message);
string out_trade_no = GenerateOutTradeNo();
data.SetValue("out_trade_no", out_trade_no);
WxPayData result = Micropay(data, 10);
#endregion
#region 如果提交被扫支付接口调用失败,则抛异常提示
if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL")
string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToString() : "";
resp.Message = "微信支付接口调用失败,原因:" + returnM
resp.Status = false;
#endregion
result.CheckSign();
if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS")
#region 支付成功,业务处理
resp.Message = "支付成功";
resp.Status = true;
#endregion
if (result.GetValue("err_code").ToString() != "USERPAYING" && result.GetValue("err_code").ToString() != "SYSTEMERROR")
resp.Message = "支付失败,错误代码:" + result.GetValue("err_code").ToString() + "。描述:" + result.GetValue("err_code_des").ToString();
resp.Status = false;
#region 确认支付是否成功,每隔一段时间(5s)查询一次订单,共查询6次
int queryTimes = 6;
while (queryTimes-- & 0)
int succResult = 0;
WxPayData queryResult = Query(out_trade_no, out succResult);
if (succResult == 2)
Thread.Sleep(5000);
else if (succResult == 1)
支付成功,业务处理
resp.Message = "支付成功";
resp.Status = true;
#endregion 支付成功,处理交易流程
resp.Message = "支付失败,错误代码:" + result.GetValue("err_code").ToString() + "。描述:" + result.GetValue("err_code_des").ToString();
Cancel(out_trade_no);
catch (Exception ex)
resp.Message += "\n撤销订单异常:" + ex.M
ExceptionLog.ToWeixinPayLog(resp.Message);
resp.Status = false;
#endregion
Cancel(out_trade_no);
catch (Exception ex)
resp.Message = "支付超时,请重试。撤销订单异常:" + ex.Message + "。";
ExceptionLog.ToWeixinPayLog(resp.Message);
resp.Status = false;
resp.Message = "操作失败,未查询到订单信息,请联系管理员!";
resp.Status = false;
catch (Exception ex)
ExceptionLog.ToWeixinPayLog(ex.Message);
resp.Message = "支付失败," + ex.M
resp.Status = false;
public static WxPayData Micropay(WxPayData inputObj, int timeOut = 10)
string url = "https://api.mch./pay/micropay";
if (!inputObj.IsSet("body"))
ExceptionLog.ToWeixinPayLog("提交被扫支付API接口中,缺少必填参数body!");
else if (!inputObj.IsSet("out_trade_no"))
ExceptionLog.ToWeixinPayLog("提交被扫支付API接口中,缺少必填参数out_trade_no!");
else if (!inputObj.IsSet("total_fee"))
ExceptionLog.ToWeixinPayLog("提交被扫支付API接口中,缺少必填参数total_fee!");
else if (!inputObj.IsSet("auth_code"))
ExceptionLog.ToWeixinPayLog("提交被扫支付API接口中,缺少必填参数auth_code!");
inputObj.SetValue("spbill_create_ip", ConfigHelper.WinxinPay_AdressIP);
inputObj.SetValue("appid", ConfigHelper.WinxinPay_AppID);
inputObj.SetValue("mch_id", ConfigHelper.WWinxinPay_MCHID);
inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));
inputObj.SetValue("sign", inputObj.MakeSign());
string xml = inputObj.ToXml();
var start = DateTime.N
string response = HttpHelper.Post(xml, url, false, timeOut);
var end = DateTime.N
int timeCost = (int)((end - start).TotalMilliseconds);
WxPayData result = new WxPayData();
result.FromXml(response);
查询订单情况
@param string out_trade_no
商户订单号
@param int succCode
查询订单结果:0表示订单不成功,1表示订单成功,2表示继续查询
@return 订单查询接口返回的数据,参见协议接口
public static WxPayData Query(string out_trade_no, out int succCode)
WxPayData queryOrderInput = new WxPayData();
queryOrderInput.SetValue("out_trade_no", out_trade_no);
WxPayData result = OrderQuery(queryOrderInput);
if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS")
if (result.GetValue("trade_state").ToString() == "SUCCESS")
succCode = 1;
else if (result.GetValue("trade_state").ToString() == "USERPAYING")
succCode = 2;
if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "FAIL")
if (result.GetValue("err_code").ToString() == "ORDERNOTEXIST")
succCode = 0;
succCode = 2;
succCode = 3;
@param WxPayData inputObj 提交给查询订单API的参数
@param int timeOut 超时时间
@throws WxPayException
@return 成功时返回订单查询结果,其他抛异常
public static WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6)
string url = "https://api.mch./pay/orderquery";
if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
ExceptionLog.ToWeixinPayLog("订单查询接口中,out_trade_no、transaction_id至少填一个!");
inputObj.SetValue("appid", ConfigHelper.WinxinPay_AppID);
inputObj.SetValue("mch_id", ConfigHelper.WWinxinPay_MCHID);
inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));
inputObj.SetValue("sign", inputObj.MakeSign());
string xml = inputObj.ToXml();
string response = HttpHelper.Post(xml, url, false, timeOut);
WxPayData result = new WxPayData();
result.FromXml(response);
撤销订单API接口
@param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个
@param int timeOut 接口超时时间
@throws WxPayException
@return 成功时返回API调用结果,其他抛异常
public static WxPayData Reverse(WxPayData inputObj, int timeOut = 6)
string url = "https://api.mch./secapi/pay/reverse";
if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
ExceptionLog.ToWeixinPayLog("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
inputObj.SetValue("appid", ConfigHelper.WinxinPay_AppID);
inputObj.SetValue("mch_id", ConfigHelper.WWinxinPay_MCHID);
inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));
inputObj.SetValue("sign", inputObj.MakeSign());
string xml = inputObj.ToXml();
string response = HttpHelper.Post(xml, url, true, timeOut);
WxPayData result = new WxPayData();
result.FromXml(response);
撤销订单,如果失败会重复调用10次
@param string out_trade_no 商户订单号
@param depth 调用次数,这里用递归深度表示
@return false表示撤销失败,true表示撤销成功
public static bool Cancel(string out_trade_no, int depth = 0)
if (depth & 10) { return false; }
WxPayData reverseInput = new WxPayData();
reverseInput.SetValue("out_trade_no", out_trade_no);
WxPayData result = Reverse(reverseInput);
if (result.GetValue("return_code").ToString() != "SUCCESS")
return false;
if (result.GetValue("result_code").ToString() != "SUCCESS" && result.GetValue("recall").ToString() == "N")
return true;
else if (result.GetValue("recall").ToString() == "Y")
return Cancel(out_trade_no, ++depth);
return false;
public static string GenerateOutTradeNo()
var ran = new Random();
return string.Format("{0}{1}{2}", ConfigHelper.WWinxinPay_MCHID, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
public class WxPayData
public WxPayData()
private SortedDictionary&string, object& m_values = new SortedDictionary&string, object&();
public void SetValue(string key, object value)
m_values[key] = value;
public object GetValue(string key)
object o = null;
m_values.TryGetValue(key, out o);
public bool IsSet(string key)
object o = null;
m_values.TryGetValue(key, out o);
if (null != o)
return true;
return false;
public string ToXml()
if (0 == m_values.Count)
ExceptionLog.LogExp("WxPayData数据为空!");
string xml = "&xml&";
foreach (KeyValuePair&string, object& pair in m_values)
if (pair.Value == null)
ExceptionLog.LogExp("WxPayData内部含有值为null的字段!");
if (pair.Value.GetType() == typeof(int))
xml += "&" + pair.Key + "&" + pair.Value + "&/" + pair.Key + "&";
else if (pair.Value.GetType() == typeof(string))
xml += "&" + pair.Key + "&" + "&![CDATA[" + pair.Value + "]]&&/" + pair.Key + "&";
ExceptionLog.LogExp("WxPayData字段数据类型错误!");
xml += "&/xml&";
public SortedDictionary&string, object& FromXml(string xml)
if (string.IsNullOrEmpty(xml))
ExceptionLog.LogExp("将空的xml串转换为WxPayData不合法!");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode xmlNode = xmlDoc.FirstC
XmlNodeList nodes = xmlNode.ChildN
foreach (XmlNode xn in nodes)
XmlElement xe = (XmlElement)
m_values[xe.Name] = xe.InnerT
if (m_values["return_code"].ToString() != "SUCCESS")
CheckSign();
catch (Exception ex)
ExceptionLog.LogExp(ex.Message);
public string ToUrl()
string buff = "";
foreach (KeyValuePair&string, object& pair in m_values)
if (pair.Value == null)
ExceptionLog.LogExp("WxPayData内部含有值为null的字段!");
if (pair.Key != "sign" && pair.Value.ToString() != "")
buff += pair.Key + "=" + pair.Value + "&";
buff = buff.Trim('&');
public string ToPrintStr()
string str = "";
foreach (KeyValuePair&string, object& pair in m_values)
if (pair.Value == null)
ExceptionLog.LogExp("WxPayData内部含有值为null的字段!");
str += string.Format("{0}={1}&br&", pair.Key, pair.Value.ToString());
public string MakeSign()
string str = ToUrl();
str += "&key=" + ConfigHelper.WinxinPay_K
var md5 = MD5.Create();
var bs = puteHash(Encoding.UTF8.GetBytes(str));
var sb = new StringBuilder();
foreach (byte b in bs)
sb.Append(b.ToString("x2"));
return sb.ToString().ToUpper();
public bool CheckSign()
if (!IsSet("sign"))
ExceptionLog.LogExp("WxPayData签名存在但不合法!");
else if (GetValue("sign") == null || GetValue("sign").ToString() == "")
ExceptionLog.LogExp("WxPayData签名存在但不合法!");
string return_sign = GetValue("sign").ToString();
string cal_sign = MakeSign();
if (cal_sign == return_sign)
return true;
ExceptionLog.LogExp("WxPayData签名验证错误!");
return false;
public SortedDictionary&string, object& GetValues()
前端效果图
扫描枪自动提交,input输入框内“onkeyup=fun()”方法即可
知乎:显著提升程序员身心健康和工作效率的装备有哪些?
1.机械键盘:,,
2.静电容:
3.情怀插入:小黑USB键盘 ThinkPad USB Track Point 键盘 0B47082
显示器:Eizo,戴尔,三星,明基,22寸以上()
PC配置:i7,16G,SSD,双显示器
电脑椅:(个人经济承受能力下,最贵的)
萌妹纸一枚(多多益善,不过太多小心受用不起)
1.Win(Visual Studio)
2.Linux(Vim,其他真的还有必要说吗?)
3.Mac(Xcode,CodeRunner,SnippetsLab)
4.跨平台(Pycharm,UltraEdit,Eclipse,Source Insight,Codeblocks)
5.插件(Visual Assist X,Pydev)
1.听音乐:
2.玩游戏: v1,v2
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:333773次
积分:4922
积分:4922
排名:第5598名
原创:154篇
转载:15篇
评论:43条
(5)(1)(1)(2)(2)(2)(1)(1)(1)(1)(2)(7)(7)(4)(1)(3)(9)(1)(6)(7)(2)(2)(11)(5)(7)(6)(4)(14)(3)(5)(5)(3)(3)(12)(11)(9)(1)(1)(1)4707人阅读
Asp.Net 技术文章(97)
项目管理与测试(9)
中国农业银行的网上支付平台接口的安装还是有点复杂的,摸索了很久才搞定,总结了一下与大家共享。
所用到的文件及口令,农行那边会给你的,请看具体步骤:
(1)安装K宝驱动HDCSPSetup.exe
(2)确认WINDOWS 2003中.NET已经安装,windows 2003 sp2系统已经自带
(3)安装Microsoft WSE 2.0 SP2.msi
(4)设置系统环境变量:
变量名:TrustMerchantIniFile
变量值:C:\WINDOWS\system32\TrustMerchant.ini
环境变量在我的电脑-&属性-&高级-&环境变量-&系统变量中添加
(5)将以下文件
abc.truststore
TrustMerchant.ini
TrustPay.cer
TrustPayClient.dll
111.pfx (商户证书可能是其它文件名)
5个文件复制到指定目录下,例如c:\windows\system32目录下
在D盘建立D:\ABC\log目录用于存放交易日志
(6)注册.NET组件
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe /codebase c:\windows\system32\TrustPayClient.dll
(7)导入服务器证书
商户证书 111.pfx 需要在服务器导入
导入密码:XXXXXXXX (这个密码农行会给你的)
(8)修改C:\WINDOWS\system32\TrustMerchant.ini文件
#网上支付平台证书
TrustPayCertFile=c:\windows\system32\TrustPay.cer
#农行根证书文件
TrustStoreFile=c:\windows\system32\abc.truststore
#商户编号(这个编号农行会给你的)
MerchantID=XXXXXXXXXXXXXXX
#交易日志文件存放目录
LogPath=D:\ABC\log
#商户证书储存目录档名(当KeyStoreType=0时,必须设定)
MerchantCertFile=c:\windows\system32\111.pfx
#商户私钥加密密码(当KeyStoreType0时,必须设定)
MerchantCertPassword=XXXXXXXX(这个密码农行会给你的)
(9)重启服务器
重启的漫长过程中可以喝杯咖啡并访问一下我的专栏
(10)一些错误的处理:
(10.1)Server.CreateObject 失败
错误类型:
Server 对象, ASP 0FFFF)
/test.asp, 第 42 行
错误原因:Server.CreateObject 失败
详细描述:8000ffff
解决方法:
KEY_USERS\S-1-5-20\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
赋予IUSR_x帐号读权限
(10.2)无法读取证书文档
500 内部服务器错误
错误类型:
TrustPayClient (0x)
test.asp, 第 74 行
错误原因:无法读取证书文档
这个问题郁闷了很久,后来用filemonitor工具才发现w3wp进程在读这个目录下的文件:
C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys
我们只要赋予IUSR_x帐号写权限就可以了。
(10.3)日志文件无法写入
错误类型:
mscorlib (0x)
test.asp, 第 74 行
错误原因:Access to the path 'D:\ABC\log\TrxLog..log' is denied.
我们同样需要赋予IUSR_x帐号D:\ABC\log目录的写权限。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:525849次
积分:5767
积分:5767
排名:第4302名
原创:16篇
转载:247篇
评论:47条
(1)(4)(1)(2)(3)(3)(2)(1)(1)(1)(1)(2)(2)(1)(2)(7)(8)(5)(1)(2)(5)(2)(2)(2)(2)(1)(2)(1)(5)(1)(1)(2)(1)(1)(5)(4)(6)(8)(6)(3)(6)(16)(4)(3)(5)(3)(6)(10)(4)(5)(4)(5)(3)(1)(7)(1)(2)(16)(53)2684人阅读
所用到的文件及口令,农行那边会给你的,请看具体步骤:
(1)安装K宝驱动HDCSPSetup.exe
(2)确认WINDOWS 2003中.NET已经安装,windows 2003 sp2系统已经自带
(3)安装Microsoft WSE 2.0 SP2.msi
(4)设置系统环境变量:
变量名:TrustMerchantIniFile
变量值:C:\WINDOWS\system32\TrustMerchant.ini
环境变量在我的电脑-&属性-&高级-&环境变量-&系统变量中添加
(5)将以下文件
abc.truststore
TrustMerchant.ini
TrustPay.cer
TrustPayClient.dll
111.pfx (商户证书可能是其它文件名)
5个文件复制到指定目录下,例如c:\windows\system32目录下
在D盘建立D:\ABC\log目录用于存放交易日志
(6)注册.NET组件
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe /codebase c:\windows\system32\TrustPayClient.dll
(7)导入服务器证书
商户证书 111.pfx 需要在服务器导入
导入密码:XXXXXXXX (这个密码农行会给你的)
(8)修改C:\WINDOWS\system32\TrustMerchant.ini文件
#网上支付平台证书
TrustPayCertFile=c:\windows\system32\TrustPay.cer
#农行根证书文件
TrustStoreFile=c:\windows\system32\abc.truststore
#商户编号(这个编号农行会给你的)
MerchantID=XXXXXXXXXXXXXXX
#交易日志文件存放目录
LogPath=D:\ABC\log
#商户证书储存目录档名(当KeyStoreType=0时,必须设定)
MerchantCertFile=c:\windows\system32\111.pfx
#商户私钥加密密码(当KeyStoreType0时,必须设定)
MerchantCertPassword=XXXXXXXX(这个密码农行会给你的)
(9)重启服务器
重启的漫长过程中可以喝杯咖啡
(10)一些错误的处理:
(10.1)Server.CreateObject 失败
错误类型:
Server 对象, ASP 0FFFF)
/test.asp, 第 42 行
错误原因:Server.CreateObject 失败
详细描述:8000ffff
解决方法:
KEY_USERS\S-1-5-20\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
赋予IUSR_x帐号读权限
(10.2)无法读取证书文档
500 内部服务器错误
错误类型:
TrustPayClient (0x)
test.asp, 第 74 行
错误原因:无法读取证书文档
这个问题郁闷了很久,后来用filemonitor工具才发现w3wp进程在读这个目录下的文件:
C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys
我们只要赋予IUSR_x帐号写权限就可以了。
(10.3)日志文件无法写入
错误类型:
mscorlib (0x)
test.asp, 第 74 行
错误原因:Access to the path 'D:\ABC\log\TrxLog..log' is denied.
我们同样需要赋予IUSR_x帐号D:\ABC\log目录的写权限。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:47212次
排名:千里之外
转载:39篇
(1)(1)(11)(22)(11)

我要回帖

更多关于 asp.net 支付宝接口 的文章

 

随机推荐