标签:des style blog http io ar color os 使用
最了写了一个asp.net的在线银联支付的功能,看了网上的半天才弄出来自已的。希望能对大家有所用。
1,先在线银联去申请,然后,他们那边会给你一个包,然后把包名字更改下key,放到你的Web项目的根目录中,跟其他的目录结构在同一级别上。
2,如果你是asp.net的项目,可以去网上去找这几个文件,或者直接像我要也可以的哈,
3,如下分别上传代码给大家展示下,
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.HtmlControls; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using ChinaPay_JY; 11 12 namespace DTcms.Web.api.payment.chinapay 13 { 14 /// <summary> 15 ///Chinapay 的摘要说明 16 /// </summary> 17 public partial class Chinapay :System.Web.UI.Page 18 { 19 string strUrl = HttpContext.Current.Request.PhysicalApplicationPath; //获取网站根目录物理路径 20 public Chinapay() 21 { 22 } 23 ///////////////////////////////////////////////////////////////////////////////////////// 24 // 二次开发 Eric Chen 2014.12.05 25 ///////////////////////////////////////////////////////////////////////////////////////// 26 27 /// <summary> 28 /// 订单签名函数sign 29 /// </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> 37 public string getSign(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType) 38 { 39 NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名 40 //npc.setMerKeyFile("Bin/MerPrK.key"); //设置商户密钥文件地址 d:\\MerPrK.key 41 npc.setMerKeyFile(strUrl + "\\key\\MerPrK.key"); 42 string strChkValue = ""; //chinapay返回的商户数字签名 43 strChkValue = npc.sign(MerId, OrdId, TransAmt, CuryId, TransDate, TransType); 44 return strChkValue.Trim(); 45 } 46 /// <summary> 47 /// 对一段字符进行签名 signData 48 /// </summary> 49 /// <param name="MerId">商户号,长度为15个字节的数字串,由ChinaPay分配</param> 50 /// <param name="SignMsg">用于要签名的字符串</param> 51 /// <returns>String CheckValue[256]即NetPayClient根据上述输入参数生成的商户数字签名,长度为256字节的字符串</returns> 52 public string signData(string MerId, string SignMsg) 53 { 54 NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名 55 //npc.setMerKeyFile("Bin/MerPrK.key"); //设置商户密钥文件地址 d:\\MerPrK.key 56 npc.setMerKeyFile(strUrl + "\\key\\MerPrK.key"); 57 string strChkValueData = ""; 58 strChkValueData = npc.signData(MerId, SignMsg); 59 return strChkValueData.Trim(); 60 } 61 62 /// <summary> 63 /// 验证交易应答函数check 64 /// </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> 74 public bool getCheck(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType, string OrderStatus, string CheckValue) 75 { 76 NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名 77 //npc.setPubKeyFile("Bin/PgPubk.key"); //设置chinapay公共密钥文件地址 d:\\PgPubk.key 78 npc.setPubKeyFile(strUrl + "\\key\\PgPubk.key"); 79 string strFlag = ""; 80 bool bolFlag = false; 81 strFlag = npc.check(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, CheckValue); // ChkValue 为ChinaPay返回给商户的域段内容 82 if (strFlag == "0") //“0”表示验签成功 83 bolFlag = true; 84 return bolFlag; 85 } 86 /// <summary> 87 /// 对一段字符串进行签名验证 checkData 88 /// </summary> 89 /// <param name="PlainData">用于数字签名的字符串</param> 90 /// <param name="CheckValue">校验值,要验证的字符串的数字签名,长度为256字节的字符串</param> 91 /// <returns>true 表示验证通过成功;否则表示失败</returns> 92 public bool checkData(string PlainData, string CheckValue) 93 { 94 NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名 95 //npc.setPubKeyFile("Bin/PgPubk.key"); //设置chinapay公共密钥文件地址 d:\\PgPubk.key 96 npc.setPubKeyFile(strUrl + "\\key\\PgPubk.key"); 97 string strFlagData = ""; 98 bool bolFlagData = false; 99 strFlagData = npc.checkData(PlainData, CheckValue); 100 if (strFlagData == "true") 101 bolFlagData = true; 102 return bolFlagData; 103 } 104 } 105 }
如需要更改的话,需要跟你的业务和代码来改写里面的部分代码即可,Ok,continue......
Chinapay_Send.cs
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 using ChinaPay_JY; 12 using System.Data.SqlClient; 13 using DTcms.Web.api.payment.chinapay; //这里引用app_code的chinapay命名空间 14 using DTcms.Common; 15 using DTcms.Model; 16 public partial class payonline_chinapay_Chinapay_send : System.Web.UI.Page 17 { 18 19 20 public int userid; //登录用户id 21 protected void Page_Load(object sender, EventArgs e) 22 { 23 24 //读取站点配置信息 25 DTcms.Model.siteconfig siteConfig = new DTcms.BLL.siteconfig().loadConfig(); 26 //获取订单信息 27 string order_no = DTRequest.GetFormString("pay_order_no").ToUpper();//订单号 28 decimal order_amount = DTRequest.GetFormDecimal("pay_order_amount", 0);//订单金额 29 string user_name = DTRequest.GetFormString("pay_user_name");//付款用户名 30 string subject = DTRequest.GetFormString("pay_user_message");//备注说明或者是留言 31 string trans_type = string.Empty;//交易类型1实物2虚拟 32 if (order_no == "" || order_amount == 0) 33 { 34 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("对不起,你提交的参数有误"))); 35 return; 36 } 37 if (order_no.StartsWith("R"))//R开头为在线充值订单 38 { 39 DTcms.Model.user_amount_log model = new DTcms.BLL.user_amount_log().GetModel(order_no); 40 if (model == null) 41 { 42 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("errro", "?msg=" + Utils.UrlEncode("对不起,你充值的订单号不存在或已删除"))); 43 return; 44 } 45 if (model.value != order_amount) 46 { 47 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起,你充值的订单金额与实际金额不一致!"))); 48 return; 49 } 50 trans_type = "2"; 51 } 52 else//B开头为商品订单 53 { 54 DTcms.Model.orders model = new DTcms.BLL.orders().GetModel(order_no); 55 if (model == null) 56 { 57 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("对不起,你支付的订单号不存在或已删除!"))); 58 return; 59 } 60 if (model.order_amount != order_amount) 61 { 62 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("对不起,你支付的订单金额与实际金额不一致!"))); 63 return; 64 } 65 trans_type = "1"; 66 67 } 68 if (user_name != "") 69 { 70 user_name = "支付会员:" + user_name; 71 } 72 else { 73 user_name = "匿名用户"; 74 } 75 76 77 78 //if (Request.Cookies[ProductCMS.GetCookieName()] == null) 79 //{ 80 // ChangeHope_fc.Show_Msg("请先登录", "../index.html"); 81 // Response.End(); 82 //} 83 //else 84 // userid = Convert.ToInt32(Request.Cookies[ProductCMS.GetCookieName()].Values["CH_userid"]); 85 86 //int intOrderid = 0, intProid = 0; //订单id,商品id 87 //if (Request["oid"] != null && Request["oid"] != "") 88 // intOrderid = Convert.ToInt32(Request["oid"]); 89 //else 90 // //ChangeHope_fc.Show_Msg("参数错误,请稍后再试!"); 91 //if (Request["pid"] != null && Request["pid"] != "") 92 // intProid = Convert.ToInt32(Request["pid"]); 93 //else 94 //ChangeHope_fc.Show_Msg("参数错误,请稍后再试!"); 95 96 //YX_sql Exsql = new YX_sql(); 97 //ChangeHope_CMS cms = new ChangeHope_CMS(); 98 Chinapay cpy = new Chinapay(); 99 //获取传递给银联chinapay的各个参数----------------------------------------------- 100 //string cpyUrl = "http://payment-test.chinapay.com/pay/TransGet"; //测试地址,测试的时候用这个地址,应用到网站时用下面那个地址 101 string cpyUrl = "https://payment.ChinaPay.com/pay/TransGet"; //支付地址 102 string cpyMerId = "808080201303248"; //ChinaPay统一分配给商户的商户号,15位长度,必填 103 string cpyOrdId = "03248" + order_no.Substring(8); //商户提交给ChinaPay的交易订单号,订单号的第五至第九位必须是商户号的最后五位,即“12345”;16位长度,必填 20141208TU33BK 201412080919280460802 .PadRight(11, ‘0‘) 104 string cpyTransAmt = (Convert.ToDouble((order_amount)*100).ToString()); //订单交易金额,12位长度,左补0,必填,单位为分,000000001234 表示 12.34 元 105 string cpyCuryId = "156"; //订单交易币种,3位长度,固定为人民币156,必填 106 string cpyTransDate = DateTime.Now.ToString("yyyyMMdd"); //订单交易日期,8位长度,必填,格式yyyyMMdd 107 string cpyTransType = "0001"; //交易类型,4位长度,必填,0001表示消费交易,0002表示退货交易 108 string cpyVersion = "20040916"; //支付接入版本号,808080开头的商户用此版本,必填,另一版本为"20070129" 20040916 109 string cpyBgRetUrl = "/chinapay/Chinapay_Bgreturn.aspx";//cms.Hope_Sitwww + "/chinapay/Chinapay_Bgreturn.aspx"; //后台交易接收URL,为后台接受应答地址,用于商户记录交易信息和处理,对于使用者是不可见的,长度不要超过80个字节,必填 110 string cpyPageRetUrl = "/chinapay/Chinapay_Pgreturn.aspx";// cms.Hope_Sitwww + "/chinapay/Chinapay_Pgreturn.aspx"; //页面交易接收URL,为页面接受应答地址,用于引导使用者返回支付后的商户网站页面,长度不要超过80个字节,必填 111 string cpyGateId = ""; //支付网关号,可选,参看银联网关类型,如填写GateId(支付网关号),则消费者将直接进入支付页面,否则进入网关选择页面 112 string cpyPriv1 = "Memo"; //"" 商户私有域,长度不要超过60个字节,商户通过此字段向Chinapay发送的信息,Chinapay依原样填充返回给商户 113 114 string sql = "", strCountMoney = cpyTransAmt, strOrdid = ""; //sql语句,实付款,订单自增id 115 //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 ///订单时间必须为当前时间,且格式必须为yyyyMMdd 123 //cpyTransDate = Convert.ToDateTime(DateTime.Now).GetDateTimeFormats(‘D‘)[1].ToString().Trim().Replace("-", ""); 124 ///订单号,16位长度,左补0,0000 02449 0000000 125 126 int intO = strOrdid.Length; 127 for (int i = 0; i < 7 - intO; i++) 128 { 129 strOrdid = "0" + strOrdid; 130 } 131 //0000 B14 12 05 1559294 00000000 132 //201412051721046647112 133 //0000000000000064 134 135 cpyOrdId = "0000" + cpyOrdId; //银联支付id="0000"+银联商户后五位+补0后的订单自增id + strOrdid; 136 ///订单交易金额,12位长度,左补0 137 strCountMoney = strCountMoney.Replace(".", ""); 138 int intM = strCountMoney.Length; 139 for (int i = 0; i < 12 - intM; i++) 140 { 141 strCountMoney = "0" + strCountMoney; 142 } 143 cpyTransAmt = strCountMoney; //获取传递的实付款 144 145 ////////////////////////////////////////////////////////////////////// 146 string strChkValue = ""; //256字节长的ASCII码,此次交易所提交的关键数据的数字签名,必填 147 strChkValue = cpy.getSign(cpyMerId, cpyOrdId, cpyTransAmt, cpyCuryId, cpyTransDate, cpyTransType); 148 149 if (strChkValue != "") 150 { 151 Response.Write("<form name=‘chinapayForm‘ method=‘post‘ action=‘" + cpyUrl + "‘>"); //支付地址 152 Response.Write("<input type=‘hidden‘ name=‘MerId‘ value=‘" + cpyMerId + "‘ />"); //商户号 153 Response.Write("<input type=‘hidden‘ name=‘OrdId‘ value=‘" + cpyOrdId + "‘ />"); //订单号 154 Response.Write("<input type=‘hidden‘ name=‘TransAmt‘ value=‘" + cpyTransAmt + "‘ />"); //支付金额 155 Response.Write("<input type=‘hidden‘ name=‘CuryId‘ value=‘" + cpyCuryId + "‘ />"); //交易币种 156 Response.Write("<input type=‘hidden‘ name=‘TransDate‘ value=‘" + cpyTransDate + "‘ />"); //交易日期 157 Response.Write("<input type=‘hidden‘ name=‘TransType‘ value=‘" + cpyTransType + "‘ />"); //交易类型 158 Response.Write("<input type=‘hidden‘ name=‘Version‘ value=‘" + cpyVersion + "‘ />"); //支付接入版本号 159 Response.Write("<input type=‘hidden‘ name=‘BgRetUrl‘ value=‘" + cpyBgRetUrl + "‘ />"); //后台接受应答地址 160 Response.Write("<input type=‘hidden‘ name=‘PageRetUrl‘ value=‘" + cpyPageRetUrl + "‘ />"); //为页面接受应答地址 161 Response.Write("<input type=‘hidden‘ name=‘GateId‘ value=‘" + cpyGateId + "‘ />"); //支付网关号 162 Response.Write("<input type=‘hidden‘ name=‘Priv1‘ value=‘" + cpyPriv1 + "‘ />"); //商户私有域,这里将订单自增编号放进去了 163 Response.Write("<input type=‘hidden‘ name=‘ChkValue‘ value=‘" + strChkValue + "‘ />"); //此次交易所提交的关键数据的数字签名 164 Response.Write("<script>"); 165 Response.Write("document.chinapayForm.submit();"); 166 Response.Write("</script></form>"); 167 } 168 //else 169 // ChangeHope_fc.Show_Msg("参数错误,请稍后再试!"); 170 } 171 }
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 12 using DTcms.Web.api.payment.chinapay; 13 14 public partial class payonline_chinapay_Chinapay_Bgreturn : System.Web.UI.Page 15 { 16 protected void Page_Load(object sender, EventArgs e) 17 { 18 Chinapay cpy = new Chinapay(); 19 string TransDate = "",MerId = "",OrdId = "",TransType = "",TransAmt = "",CuryId = "",ChkValue = "",OrderStatus = "",GateId = "",Priv1 = ""; 20 bool bolCheck=false; 21 22 TransDate = Request["transdate"].Trim(); 23 MerId = Request["merid"].Trim(); 24 OrdId = Request["orderno"].Trim(); 25 TransType = Request["transtype"].Trim(); 26 TransAmt = Request["amount"].Trim(); 27 CuryId = Request["currencycode"].Trim(); //交易币种 28 ChkValue = Request["checkvalue"].Trim(); 29 OrderStatus = Request["status"].Trim(); 30 GateId = Request["GateId"].Trim(); //支付网关号 31 Priv1 = Request["Priv1"].Trim(); //商户私有域 32 ///检验是否是银联chinapay返回的交易数据 33 bolCheck = cpy.getCheck(MerId,OrdId,TransAmt,CuryId,TransDate,TransType,OrderStatus,ChkValue); 34 if (bolCheck) 35 Response.Write("<script>alert(‘后台接收到应答‘);</script>"); 36 else 37 Response.Write("<script>alert(‘后台没有接收到应答‘);</script>"); 38 } 39 }
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 12 using System.Data.SqlClient; 13 using DTcms.Web.api.payment.chinapay; 14 namespace DTcms.Web.api.payment.balance 15 { 16 public partial class payonline_chinapay_Chinapay_Pgreturn : System.Web.UI.Page 17 { 18 protected void Page_Load(object sender, EventArgs e) 19 { 20 //YX_sql Exsql = new YX_sql(); 21 //ChangHope_DB db = new ChangHope_DB(); 22 Chinapay cpy = new Chinapay(); 23 string TransDate = "", MerId = "", OrdId = "", TransType = "", TransAmt = "", CuryId = "", ChkValue = "", OrderStatus = "", GateId = "", Priv1 = ""; 24 bool bolCheck = false; 25 26 TransDate = Request["transdate"].Trim(); 27 MerId = Request["merid"].Trim(); 28 OrdId = Request["orderno"].Trim(); 29 TransType = Request["transtype"].Trim(); 30 TransAmt = Request["amount"].Trim(); 31 CuryId = Request["currencycode"].Trim(); //交易币种 32 ChkValue = Request["checkvalue"].Trim(); 33 OrderStatus = Request["status"].Trim(); 34 GateId = Request["GateId"].Trim(); //支付网关号 35 Priv1 = Request["Priv1"].Trim(); //商户私有域,内容是订单自增编号 36 ///检验是否是银联chinapay返回的交易数据 37 bolCheck = cpy.getCheck(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, ChkValue); 38 if (bolCheck) //bolCheck=true,检测返回参数是银联发送的,进入处理流程 39 { 40 try 41 { 42 ///******************************** 43 ///这里写成功接收到银联支付成功后你自己要处理的流程,比如修改买、卖家金额等,订单状态等 44 ///******************************** 45 46 } 47 catch (Exception) 48 { 49 //ChangeHope_fc.Show_Msg("参数错误,请稍后再试!", db.System_Http() + "/vipuser/default_vip.aspx"); 50 } 51 } 52 //else 53 //ChangeHope_fc.Show_Msg("参数错误,请稍后再试!", db.System_Http() + "/vipuser/default_vip.aspx"); 54 } 55 } 56 57 }
代码中有很多注释啦,我就不再熬熟啦。
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/qiankundai/p/4150921.html