新春即将来临,首先给大家拜个早年,祝攻城狮们新年快乐、万事如意、合家欢乐、团团圆圆、幸福健康、来年更能大展宏图 实现各自的梦想! 同时预祝各大科技公司大佬们事业蒸蒸日上、公司转型突破创新、冲出突围带领员工们早日实现上市梦想!
今天研究了下银联在线支付功能,特地记录下以表码农们还在坚守岗位。此功能主要是一般的.NET实现的,有机会转为标准的MVC模式实现以及应用到asp.net core 中。
首先第一是支付首页代码:
PayIndex.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PayIndex.aspx.cs" Inherits="UnionPayNET.PayIndex" %>
第二是跳转到支付:UnionPay.aspx
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Dictionaryparam = new Dictionary (); //以下信息非特殊情况不需要改动 param["version"] = SDKConfig.Version;//版本号 param["encoding"] = "UTF-8";//编码方式 param["txnType"] = "01";//交易类型 param["txnSubType"] = "01";//交易子类 param["bizType"] = "000201";//业务类型 param["signMethod"] = SDKConfig.SignMethod;//签名方法 param["channelType"] = "08";//渠道类型 param["accessType"] = "0";//接入类型 param["frontUrl"] = SDKConfig.FrontUrl; //前台通知地址 param["backUrl"] = SDKConfig.BackUrl; //后台通知地址 param["currencyCode"] = "156";//交易币种 // 订单超时时间。 // 超过此时间后,除网银交易外,其他交易银联系统会拒绝受理,提示超时。 跳转银行网银交易如果超时后交易成功,会自动退款,大约5个工作日金额返还到持卡人账户。 // 此时间建议取支付时的北京时间加15分钟。 // 超过超时时间调查询接口应答origRespCode不是A6或者00的就可以判断为失败。 param["payTimeout"] = DateTime.Now.AddMinutes(15).ToString("yyyyMMddHHmmss"); //TODO 以下信息需要填写 param["merId"] = Request.Form["merId"].ToString();//商户号,请改自己的测试商户号,此处默认取demo演示页面传递的参数 param["orderId"] = Request.Form["orderId"].ToString();//商户订单号,8-32位数字字母,不能含“-”或“_”,此处默认取demo演示页面传递的参数,可以自行定制规则 param["txnTime"] = Request.Form["txnTime"].ToString();//订单发送时间,格式为YYYYMMDDhhmmss,取北京时间,此处默认取demo演示页面传递的参数,参考取法: DateTime.Now.ToString("yyyyMMddHHmmss") param["txnAmt"] = Request.Form["txnAmt"].ToString();//交易金额,单位分,此处默认取demo演示页面传递的参数 AcpService.Sign(param, System.Text.Encoding.UTF8); string html = AcpService.CreateAutoFormHtml(SDKConfig.FrontTransUrl, param, System.Text.Encoding.UTF8);// 将SDKUtil产生的Html文档写入页面,从而引导用户浏览器重定向 Response.ContentEncoding = Encoding.UTF8; // 指定输出编码 Response.Write(html); } }
第三是:前台通知地址,填写接收银联前台通知的地址
FrontRcvResponse
protected string html; protected void Page_Load(object sender, EventArgs e) { log4net.ILog log = log4net.LogManager.GetLogger(this.GetType()); if (Request.HttpMethod == "POST") { // 使用Dictionary保存参数 DictionaryresData = new Dictionary (); NameValueCollection coll = Request.Form; string[] requestItem = coll.AllKeys; for (int i = 0; i < requestItem.Length; i++) { resData.Add(requestItem[i], Request.Form[requestItem[i]]); } //商户端根据返回报文内容处理自己的业务逻辑 ,DEMO此处只输出报文结果 StringBuilder builder = new StringBuilder(); log.Info("receive front notify: " + SDKUtil.CreateLinkString(resData, false, true, System.Text.Encoding.UTF8)); builder.Append(" 商户端接收银联返回报文并按照表格形式输出结果"); for (int i = 0; i < requestItem.Length; i++) { builder.Append("" + requestItem[i] + "" + Request.Form[requestItem[i]] + ""); } if (AcpService.Validate(resData, System.Text.Encoding.UTF8)) { builder.Append("商户端验证银联返回报文结果验证签名成功."); string respcode = resData["respCode"]; //00、A6为成功,其余为失败。其他字段也可按此方式获取。 //如果卡号我们业务配了会返回且配了需要加密的话,请按此方法解密 //if(resData.ContainsKey("accNo")) //{ // string accNo = SecurityUtil.DecryptData(resData["accNo"], System.Text.Encoding.UTF8); //} //customerInfo子域的获取 if (resData.ContainsKey("customerInfo")) { Dictionary customerInfo = AcpService.ParseCustomerInfo(resData["customerInfo"], System.Text.Encoding.UTF8); if (customerInfo.ContainsKey("phoneNo")) { string phoneNo = customerInfo["phoneNo"]; //customerInfo其他子域均可参考此方式获取 } foreach (KeyValuePair pair in customerInfo) { builder.Append(pair.Key + "=" + pair.Value + " \n"); } } } else { builder.Append("商户端验证银联返回报文结果验证签名失败."); } html = builder.ToString(); } }
第四是:后台通知地址,填写后台接收银联后台通知的地址,必须外网能访问
BackRcvResponse
protected string html; protected void Page_Load(object sender, EventArgs e) { log4net.ILog log = log4net.LogManager.GetLogger(this.GetType()); // **************后台接收银联返回报文交易结果展示*********************** if (Request.HttpMethod == "POST") { // 使用Dictionary保存参数 DictionaryresData = new Dictionary (); NameValueCollection coll = Request.Form; string[] requestItem = coll.AllKeys; for (int i = 0; i < requestItem.Length; i++) { resData.Add(requestItem[i], Request.Form[requestItem[i]]); } //商户端根据返回报文内容处理自己的业务逻辑 , 处理订单状态相关业务数据处理 //DEMO此处只输出报文结果 StringBuilder builder = new StringBuilder(); log.Info("receive back notify: " + SDKUtil.CreateLinkString(resData, false, true, System.Text.Encoding.UTF8)); builder.Append(" 商户端接收银联返回报文并按照表格形式输出结果"); for (int i = 0; i < requestItem.Length; i++) { builder.Append("" + requestItem[i] + "" + Request.Form[requestItem[i]] + ""); } if (AcpService.Validate(resData, System.Text.Encoding.UTF8)) { builder.Append("商户端验证银联返回报文结果验证签名成功."); string respcode = resData["respCode"]; //00、A6为成功,其余为失败。其他字段也可按此方式获取。 //如果卡号我们业务配了会返回且配了需要加密的话,请按此方法解密 //if(resData.ContainsKey("accNo")) //{ // string accNo = SecurityUtil.DecryptData(resData["accNo"], System.Text.Encoding.UTF8); //} //customerInfo子域的获取 if (resData.ContainsKey("customerInfo")) { Dictionary customerInfo = AcpService.ParseCustomerInfo(resData["customerInfo"], System.Text.Encoding.UTF8); if (customerInfo.ContainsKey("phoneNo")) { string phoneNo = customerInfo["phoneNo"]; //customerInfo其他子域均可参考此方式获取 } foreach (KeyValuePair pair in customerInfo) { builder.Append(pair.Key + "=" + pair.Value + " \n"); } } } else { builder.Append("商户端验证银联返回报文结果验证签名失败."); } html = builder.ToString(); } }
第五是:web.config
第六是:引用两个重要的DLL
CSharpCode.SharpZipLib.dll
BouncyCastle.Crypto.dll
第七就是:照搬官网SDK的相关类即可
最终显示的支付效果页面是: