最了写了一个asp.net的在线银联支付的功能,看了网上的半天才弄出来自已的。希望能对大家有所用。
1,先在线银联去申请,然后,他们那边会给你一个包,然后把包名字更改下key,放到你的Web项目的根目录中,跟其他的目录结构在同一级别上。
2,如果你是asp.net的项目,可以去网上去找这几个文件,或者直接像我要也可以的哈,
3,如下分别上传代码给大家展示下,
1usingSystem;2usingSystem.Data;3usingSystem.Configuration;4usingSystem.Web;5usingSystem.Web.Security;6usingSystem.Web.UI;7usingSystem.Web.UI.HtmlControls;8usingSystem.Web.UI.WebControls;9usingSystem.Web.UI.WebControls.WebParts;10usingChinaPay_JY;1112namespaceDTcms.Web.api.payment.chinapay13{14///<summary>15///Chinapay 的摘要说明16///</summary>17publicpartialclassChinapay :System.Web.UI.Page18{19stringstrUrl = HttpContext.Current.Request.PhysicalapplicationPath;//获取网站根目录物理路径20publicChinapay()21{22}23/////////////////////////////////////////////////////////////////////////////////////////24//二次开发 Eric Chen 2014.12.0525/////////////////////////////////////////////////////////////////////////////////////////2627///<summary>28///订单签名函数sign29///</summary>30///<param name="MerId">商户号,长度为15个字节的数字串,由ChinaPay或清算银行分配</param>31///<param name="OrdId">订单号,长度为16个字节的数字串,由用户系统/网站生成,失败的订单号允许重复支付</param>32///<param name="TransAmt">交易金额,长度为12个字节的数字串,例如:数字串"000000001234"表示12.34元</param>33///<param name="CuryId">货币代码, 长度为3个字节的数字串,目前只支持人民币,取值为"156"</param>34///<param name="TransDate">交易日期,长度为8个字节的数字串,表示格式为:YYYYMMDD</param>35///<param name="TransType">交易类型,长度为4个字节的数字串,取值范围为:"0001"和"0002", 其中"0001"表示消费交易,"0002"表示退货交易</param>36///<returns>string CheckValue[256] 即NetPayClient根据上述输入参数生成的商户数字签名,长度为256字节的字符串</returns>37publicstringgetSign(stringMerId,stringOrdId,stringTransAmt,stringCuryId,stringTransDate,stringTransType)38{39NetPayClientClass npc =newNetPayClientClass();//实例NetPay签名40//npc.setMerKeyFile("Bin/MerPRK.key");//设置商户密钥文件地址 d:\\MerPrK.key41npc.setMerKeyFile(strUrl +"\\key\\MerPrK.key");42stringstrChkValue ="";//chinapay返回的商户数字签名43strChkValue =npc.sign(MerId, OrdId, TransAmt, CuryId, TransDate, TransType);44returnstrChkValue.Trim();45}46///<summary>47///对一段字符进行签名 signData48///</summary>49///<param name="MerId">商户号,长度为15个字节的数字串,由ChinaPay分配</param>50///<param name="SignMsg">用于要签名的字符串</param>51///<returns>String CheckValue[256]即NetPayClient根据上述输入参数生成的商户数字签名,长度为256字节的字符串</returns>52publicstringsignData(stringMerId,stringSignMsg)53{54NetPayClientClass npc =newNetPayClientClass();//实例NetPay签名55//npc.setMerKeyFile("Bin/MerPrK.key");//设置商户密钥文件地址 d:\\MerPrK.key56npc.setMerKeyFile(strUrl +"\\key\\MerPrK.key");57stringstrChkValueData ="";58strChkValueData =npc.signData(MerId, SignMsg);59returnstrChkValueData.Trim();60}6162///<summary>63///验证交易应答函数check64///</summary>65///<param name="MerId">商户号,长度为15个字节的数字串,由ChinaPay分配</param>66///<param name="OrdId">订单号,长度为16个字节的数字串,由商户系统生成,失败的订单号允许重复支付</param>67///<param name="TransAmt">交易金额,长度为12个字节的数字串,例如:数字串"000000001234"表示12.34元</param>68///<param name="CuryId">货币代码, 长度为3个字节的数字串,目前只支持人民币,取值为"156"</param>69///<param name="TransDate">交易日期,长度为8个字节的数字串,表示格式为: YYYYMMDD</param>70///<param name="TransType">交易类型,长度为4个字节的数字串,取值范围为:"0001"和"0002", 其中"0001"表示消费交易,"0002"表示退货交易</param>71///<param name="OrderStatus">交易状态,长度为4个字节的数字串。详见交易状态码说明</param>72///<param name="CheckValue">校验值,即ChinaPay对交易应答的数字签名,长度为256字节的字符串</param>73///<returns>true 表示成功,即该交易应答为ChinaPay所发送,商户根据“交易状态”进行后续处理;否则表示失败,即无效应答,商户可忽略该应答</returns>74publicboolgetCheck(stringMerId,stringOrdId,stringTransAmt,stringCuryId,stringTransDate,stringTransType,stringOrderStatus,stringCheckValue)75{76NetPayClientClass npc =newNetPayClientClass();//实例NetPay签名77//npc.setPubKeyFile("Bin/PgPubk.key");//设置chinapay公共密钥文件地址 d:\\PgPubk.key78npc.setPubKeyFile(strUrl +"\\key\\PgPubk.key");79stringstrFlag ="";80boolbolFlag =false;81strFlag = npc.check(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, CheckValue);//ChkValue 为ChinaPay返回给商户的域段内容82if(strFlag =="0")//“0”表示验签成功83bolFlag =true;84returnbolFlag;85}86///<summary>87///对一段字符串进行签名验证 checkData88///</summary>89///<param name="PlainData">用于数字签名的字符串</param>90///<param name="CheckValue">校验值,要验证的字符串的数字签名,长度为256字节的字符串</param>91///<returns>true 表示验证通过成功;否则表示失败</returns>92publicboolcheckData(stringPlainData,stringCheckValue)93{94NetPayClientClass npc =newNetPayClientClass();//实例NetPay签名95//npc.setPubKeyFile("Bin/PgPubk.key");//设置chinapay公共密钥文件地址 d:\\PgPubk.key96npc.setPubKeyFile(strUrl +"\\key\\PgPubk.key");97stringstrFlagData ="";98boolbolFlagData =false;99strFlagData =npc.checkData(PlainData, CheckValue);100if(strFlagData =="true")101bolFlagData =true;102returnbolFlagData;103}104}105}
如需要更改的话,需要跟你的业务和代码来改写里面的部分代码即可,Ok,continue......
Chinapay_Send.cs
1usingSystem;2usingSystem.Collections;3usingSystem.Configuration;4usingSystem.Data;5usingSystem.Web;6usingSystem.Web.Security;7usingSystem.Web.UI;8usingSystem.Web.UI.HtmlControls;9usingSystem.Web.UI.WebControls;10usingSystem.Web.UI.WebControls.WebParts;11usingChinaPay_JY;12usingSystem.Data.SqlClient;13usingDTcms.Web.api.payment.chinapay;//这里引用app_code的chinapay命名空间14usingDTcms.Common;15usingDTcms.Model;16publicpartialclasspayonline_chinapay_Chinapay_send : System.Web.UI.Page17{181920publicintuserid;//登录用户id21protectedvoidPage_Load(objectsender, EventArgs e)22{2324//读取站点配置信息25DTcms.Model.siteconfig siteConfig =newDTcms.BLL.siteconfig().loadConfig();26//获取订单信息27stringorder_no = DTRequest.GetFormString("pay_order_no").ToUpper();//订单号28decimalorder_amount = DTRequest.GetFormDecimal("pay_order_amount",0);//订单金额29stringuser_name = DTRequest.GetFormString("pay_user_name");//付款用户名30stringsubject = DTRequest.GetFormString("pay_user_message");//备注说明或者是留言31stringtrans_type =string.Empty;//交易类型1实物2虚拟32if(order_no ==""|| order_amount ==0)33{34Response.Redirect(newDTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("对不起,你提交的参数有误")));35return;36}37if(order_no.StartsWith("R"))//R开头为在线充值订单38{39DTcms.Model.user_amount_log model =newDTcms.BLL.user_amount_log().GetModel(order_no);40if(model ==null)41{42Response.Redirect(newDTcms.Web.UI.BasePage().linkurl("errro","?msg="+ Utils.UrlEncode("对不起,你充值的订单号不存在或已删除")));43return;44}45if(model.value !=order_amount)46{47Response.Redirect(newDTcms.Web.UI.BasePage().linkurl("error","?msg="+ Utils.UrlEncode("对不起,你充值的订单金额与实际金额不一致!")));48return;49}50trans_type ="2";51}52else//B开头为商品订单53{54DTcms.Model.orders model =newDTcms.BLL.orders().GetModel(order_no);55if(model ==null)56{57Response.Redirect(newDTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("对不起,你支付的订单号不存在或已删除!")));58return;59}60if(model.order_amount !=order_amount)61{62Response.Redirect(newDTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("对不起,你支付的订单金额与实际金额不一致!")));63return;64}65trans_type ="1";6667}68if(user_name !="")69{70user_name ="支付会员:"+user_name;71}72else{73user_name ="匿名用户";74}75767778//if (Request.Cookies[ProductCMS.GetCookieName()] == null)79//{80//ChangeHope_fc.Show_Msg("请先登录", "../index.html");81//Response.End();82//}83//else84//userid = Convert.ToInt32(Request.Cookies[ProductCMS.GetCookieName()].Values["CH_userid"]);8586//int intOrderid = 0, intProid = 0;//订单id,商品id87//if (Request["oid"] != null && Request["oid"] != "")88//intOrderid = Convert.ToInt32(Request["oid"]);89//else90////ChangeHope_fc.Show_Msg("参数错误,请稍后再试!");91//if (Request["pid"] != null && Request["pid"] != "")92//intProid = Convert.ToInt32(Request["pid"]);93//else94//ChangeHope_fc.Show_Msg("参数错误,请稍后再试!");9596//YX_sql Exsql = new YX_sql();97//ChangeHope_CMS cms = new ChangeHope_CMS();98Chinapay cpy =newChinapay();99//获取传递给银联chinapay的各个参数-----------------------------------------------100//string cpyUrl = "http://payment-test.chinapay.com/pay/TransGet";//测试地址,测试的时候用这个地址,应用到网站时用下面那个地址101stringcpyUrl ="https://payment.ChinaPay.com/pay/TransGet";//支付地址102stringcpyMerId ="808080201303248";//ChinaPay统一分配给商户的商户号,15位长度,必填103stringcpyOrdId ="03248"+ order_no.Substring(8);//商户提交给ChinaPay的交易订单号,订单号的第五至第九位必须是商户号的最后五位,即“12345”;16位长度,必填 20141208TU33BK 201412080919280460802 .PadRight(11, '0')104stringcpyTransAmt = (Convert.ToDouble((order_amount)*100).ToString());//订单交易金额,12位长度,左补0,必填,单位为分,000000001234 表示 12.34 元105stringcpyCuryId ="156";//订单交易币种,3位长度,固定为人民币156,必填106stringcpyTransDate = DateTime.Now.ToString("yyyyMMdd");//订单交易日期,8位长度,必填,格式yyyyMMdd107stringcpyTransType ="0001";//交易类型,4位长度,必填,0001表示消费交易,0002表示退货交易108stringcpyVersion ="20040916";//支付接入版本号,808080开头的商户用此版本,必填,另一版本为"20070129" 20040916109stringcpyBgRetUrl ="/chinapay/Chinapay_Bgreturn.aspx";//cms.Hope_Sitwww + "/chinapay/Chinapay_Bgreturn.aspx";//后台交易接收URL,为后台接受应答地址,用于商户记录交易信息和处理,对于使用者是不可见的,长度不要超过80个字节,必填110stringcpyPageRetUrl ="/chinapay/Chinapay_Pgreturn.aspx";//cms.Hope_Sitwww + "/chinapay/Chinapay_Pgreturn.aspx";//页面交易接收URL,为页面接受应答地址,用于引导使用者返回支付后的商户网站页面,长度不要超过80个字节,必填111stringcpyGateId ="";//支付网关号,可选,参看银联网关类型,如填写GateId(支付网关号),则消费者将直接进入支付页面,否则进入网关选择页面112stringcpyPriv1 ="Memo";//"" 商户私有域,长度不要超过60个字节,商户通过此字段向Chinapay发送的信息,Chinapay依原样填充返回给商户113114stringsql ="", strCountMoney = cpyTransAmt, strOrdid ="";//sql语句,实付款,订单自增id115//sql = "select * from yx_orderform where yx_id=" + intOrderid;//查询用户订单116//SqlDataReader sdr = Exsql.Re_dr(sql);117//if (sdr.Read())118//{119//cpyPriv1 = strOrdid = sdr["YX_ID"].ToString().Trim();120//strCountMoney = Convert.ToDecimal(Convert.ToDecimal(sdr["YX_SumPrice"]) + Convert.ToDecimal(sdr["YX_DGoodsPrice"]) * Convert.ToDecimal(sdr["YX_Sumcount"]) - Convert.ToDecimal(sdr["YX_SumVouchers"])).ToString("f2");121//}122///订单时间必须为当前时间,且格式必须为yyyyMMdd123//cpyTransDate = Convert.ToDateTime(DateTime.Now).GetDateTimeFormats('D')[1].ToString().Trim().Replace("-", "");124///订单号,16位长度,左补0,0000 02449 0000000125126intintO =strOrdid.Length;127for(inti =0; i <7- intO; i++)128{129strOrdid ="0"+strOrdid;130}131//0000 B14 12 05 1559294 00000000132//201412051721046647112133//0000000000000064134135cpyOrdId ="0000"+ cpyOrdId;//银联支付id="0000"+银联商户后五位+补0后的订单自增id + strOrdid;136///订单交易金额,12位长度,左补0137strCountMoney = strCountMoney.Replace(".","");138intintM =strCountMoney.Length;139for(inti =0; i <12- intM; i++)140{141strCountMoney ="0"+strCountMoney;142}143cpyTransAmt = strCountMoney;//获取传递的实付款144145//////////////////////////////////////////////////////////////////////146stringstrChkValue ="";//256字节长的ASCII码,此次交易所提交的关键数据的数字签名,必填147strChkValue =cpy.getSign(cpyMerId, cpyOrdId, cpyTransAmt, cpyCuryId, cpyTransDate, cpyTransType);148149if(strChkValue !="")150{151Response.Write("<form name='chinapayForm' method='post' action='"+ cpyUrl +"'>");//支付地址152Response.Write("<input type='hidden' name='MerId' value='"+ cpyMerId +"' />");//商户号153Response.Write("<input type='hidden' name='OrdId' value='"+ cpyOrdId +"' />");//订单号154Response.Write("<input type='hidden' name='TransAmt' value='"+ cpyTransAmt +"' />");//支付金额155Response.Write("<input type='hidden' name='CuryId' value='"+ cpyCuryId +"' />");//交易币种156Response.Write("<input type='hidden' name='TransDate' value='"+ cpyTransDate +"' />");//交易日期157Response.Write("<input type='hidden' name='TransType' value='"+ cpyTransType +"' />");//交易类型158Response.Write("<input type='hidden' name='Version' value='"+ cpyVersion +"' />");//支付接入版本号159Response.Write("<input type='hidden' name='BgRetUrl' value='"+ cpyBgRetUrl +"' />");//后台接受应答地址160Response.Write("<input type='hidden' name='PageRetUrl' value='"+ cpyPageRetUrl +"' />");//为页面接受应答地址161Response.Write("<input type='hidden' name='GateId' value='"+ cpyGateId +"' />");//支付网关号162Response.Write("<input type='hidden' name='Priv1' value='"+ cpyPriv1 +"' />");//商户私有域,这里将订单自增编号放进去了163Response.Write("<input type='hidden' name='ChkValue' value='"+ strChkValue +"' />");//此次交易所提交的关键数据的数字签名164Response.Write("<script>");165Response.Write("document.chinapayForm.submit();");166Response.Write("</script></form>");167}168//else169//ChangeHope_fc.Show_Msg("参数错误,请稍后再试!");170}171}
View Code
Chinapay_Bgreturn.cs
1usingSystem;2usingSystem.Collections;3usingSystem.Configuration;4usingSystem.Data;5usingSystem.Web;6usingSystem.Web.Security;7usingSystem.Web.UI;8usingSystem.Web.UI.HtmlControls;9usingSystem.Web.UI.WebControls;10usingSystem.Web.UI.WebControls.WebParts;1112usingDTcms.Web.api.payment.chinapay;1314publicpartialclasspayonline_chinapay_Chinapay_Bgreturn : System.Web.UI.Page15{16protectedvoidPage_Load(objectsender, EventArgs e)17{18Chinapay cpy =newChinapay();19stringTransDate ="",MerId ="",OrdId ="",TransType ="",TransAmt ="",CuryId ="",ChkValue ="",OrderStatus ="",GateId ="",Priv1 ="";20boolbolCheck=false;2122TransDate = Request["transdate"].Trim();23MerId = Request["merid"].Trim();24OrdId = Request["orderno"].Trim();25TransType = Request["transtype"].Trim();26TransAmt = Request["amount"].Trim();27CuryId = Request["currencycode"].Trim();//交易币种28ChkValue = Request["checkvalue"].Trim();29OrderStatus = Request["status"].Trim();30GateId = Request["GateId"].Trim();//支付网关号31Priv1 = Request["Priv1"].Trim();//商户私有域32///检验是否是银联chinapay返回的交易数据33bolCheck =cpy.getCheck(MerId,OrdId,TransAmt,CuryId,TransDate,TransType,OrderStatus,ChkValue);34if(bolCheck)35Response.Write("<script>alert('后台接收到应答');</script>");36else37Response.Write("<script>alert('后台没有接收到应答');</script>");38}39}
View Code
Chinapay_Pgreturn
1usingSystem;2usingSystem.Collections;3usingSystem.Configuration;4usingSystem.Data;5usingSystem.Web;6usingSystem.Web.Security;7usingSystem.Web.UI;8usingSystem.Web.UI.HtmlControls;9usingSystem.Web.UI.WebControls;10usingSystem.Web.UI.WebControls.WebParts;1112usingSystem.Data.SqlClient;13usingDTcms.Web.api.payment.chinapay;14namespaceDTcms.Web.api.payment.balance15{16publicpartialclasspayonline_chinapay_Chinapay_Pgreturn : System.Web.UI.Page17{18protectedvoidPage_Load(objectsender, EventArgs e)19{20//YX_sql Exsql = new YX_sql();21//ChangHope_DB db = new ChangHope_DB();22Chinapay cpy =newChinapay();23stringTransDate ="", MerId ="", OrdId ="", TransType ="", TransAmt ="", CuryId ="", ChkValue ="", OrderStatus ="", GateId ="", Priv1 ="";24boolbolCheck =false;2526TransDate = Request["transdate"].Trim();27MerId = Request["merid"].Trim();28OrdId = Request["orderno"].Trim();29TransType = Request["transtype"].Trim();30TransAmt = Request["amount"].Trim();31CuryId = Request["currencycode"].Trim();//交易币种32ChkValue = Request["checkvalue"].Trim();33OrderStatus = Request["status"].Trim();34GateId = Request["GateId"].Trim();//支付网关号35Priv1 = Request["Priv1"].Trim();//商户私有域,内容是订单自增编号36///检验是否是银联chinapay返回的交易数据37bolCheck =cpy.getCheck(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, ChkValue);38if(bolCheck)//bolCheck=true,检测返回参数是银联发送的,进入处理流程39{40try41{42///********************************43///这里写成功接收到银联支付成功后你自己要处理的流程,比如修改买、卖家金额等,订单状态等44///********************************4546}47catch(Exception)48{49//ChangeHope_fc.Show_Msg("参数错误,请稍后再试!", db.System_Http() + "/vipuser/default_vip.aspx");50}51}52//else53//ChangeHope_fc.Show_Msg("参数错误,请稍后再试!", db.System_Http() + "/vipuser/default_vip.aspx");54}55}5657}
View Code
代码中有很多注释啦,我就不再熬熟啦。