标签:style blog http io os ar java for sp
1、下载ChinaPay的dll文件,解压放到BIN目录下,共4个文件。
2、引用ChinaPay_Net.dll到项目。
3、申请到的key放到网站,本例是放到根目录key文件夹下:
4、添加类:
using System; using System.Text; using System.Web; using ChinaPay_Net; /// <summary> ///Chinapay 的摘要说明 /// </summary> public class Chinapay { private static string _MerId = "808080*********";//商户号 private static string _Action = "https://payment.ChinaPay.com/pay/TransGet"; private static string _BgRetUrl = "http://www.XXX.com/Payment/ChinaPayBgRetUrl.aspx"; private static string _PageRetUrl = "http://www.XXX.com/Payment/ChinaPayPageRetUrl.aspx"; string strUrl = HttpContext.Current.Request.PhysicalApplicationPath; /// <summary> /// 创建提交表单 /// </summary> /// <param name="OrdId">订单号,长度11位</param> /// <param name="Amount">付款金额,最多保留两位有效数字</param> /// <returns></returns> public static string getChinaPayForm(string OrderId, decimal Amount, string PrivateValue) { string CuryId = "156"; string TransDate = DateTime.Now.ToString("yyyyMMdd"); string TransType = "0001"; StringBuilder sb = new StringBuilder(); if (OrderId.Length != 11) { sb.Append("订单号必须为长度11位"); } else if (Amount <= 0) { sb.Append("订单金额必须大于0"); } else { string OrdId = OrderId.Substring(0, 4) + _MerId.Substring(10, 5) + OrderId.Substring(4, 7); string TransAmt = "000000000000" + Math.Round(Amount, 2, MidpointRounding.AwayFromZero).ToString().Replace(".", ""); TransAmt = TransAmt.Substring(TransAmt.Length - 12, 12); Chinapay cp = new Chinapay(); string ChkValue = cp.getSign(_MerId, OrdId, TransAmt, CuryId, TransDate, TransType); sb.Append("<form action=\"" + _Action + "\" method=\"POST\">"); sb.Append("<input type=\"hidden\" name=\"MerId\" value=\"" + _MerId + "\" />"); sb.Append("<input type=\"hidden\" name=\"OrdId\" value=\"" + OrdId + "\" />"); sb.Append("<input type=\"hidden\" name=\"TransAmt\" value=\"" + TransAmt + "\" />"); sb.Append("<input type=\"hidden\" name=\"CuryId\" value=\"" + CuryId + "\" />"); sb.Append("<input type=\"hidden\" name=\"TransDate\" value=\"" + TransDate + "\" />"); sb.Append("<input type=\"hidden\" name=\"TransType\" value=\"" + TransType + "\" />"); sb.Append("<input type=\"hidden\" name=\"Version\" value=\"20040916\" />"); sb.Append("<input type=\"hidden\" name=\"BgRetUrl\" value=\"" + _BgRetUrl + "\" />"); sb.Append("<input type=\"hidden\" name=\"PageRetUrl\" value=\"" + _PageRetUrl + "\" />"); sb.Append("<input type=\"hidden\" name=\"GateId\" value=\"\">"); sb.Append("<input type=\"hidden\" name=\"Priv1\" value=\"" + PrivateValue + "\">"); sb.Append("<input type=\"hidden\" name=\"ChkValue\" value=\"" + ChkValue + "\">"); sb.Append("<input id=\"PostData\" type=\"submit\" value=\"Post\" style=\"display:none;\" />"); sb.Append("</form>"); sb.Append("<script type=\"text/javascript\">document.getElementById(\"PostData\").click();</script>"); } return sb.ToString(); } public string getSign(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType) { NetPayClientClass npc = new NetPayClientClass(); string path = strUrl + "key\\MerPrK.key"; npc.setMerKeyFile(path); string strChkValue = ""; strChkValue = npc.sign(MerId, OrdId, TransAmt, CuryId, TransDate, TransType); return strChkValue.Trim(); } public string signData(string MerId, string SignMsg) { NetPayClientClass npc = new NetPayClientClass(); npc.setMerKeyFile(strUrl + "key\\MerPrK.key"); string strChkValueData = ""; strChkValueData = npc.signData(MerId, SignMsg); return strChkValueData.Trim(); } public bool getCheck(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType, string OrderStatus, string CheckValue) { NetPayClientClass npc = new NetPayClientClass(); npc.setPubKeyFile(strUrl + "key\\PgPubk.key"); string strFlag = ""; bool bolFlag = false; strFlag = npc.check(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, CheckValue); if (strFlag == "0") { bolFlag = true; } return bolFlag; } public bool checkData(string PlainData, string CheckValue) { NetPayClientClass npc = new NetPayClientClass(); npc.setPubKeyFile(strUrl + "key\\PgPubk.key"); string strFlagData = ""; bool bolFlagData = false; strFlagData = npc.checkData(PlainData, CheckValue); if (strFlagData == "true") { bolFlagData = true; } return bolFlagData; } }
5、添加页面,后台代码如下:
using System; public partial class Payment_ChinaPayPost { private string OrderId = RequestString("OrderId");//必选 private decimal Amount = RequestDecimal("Amount");//必选 private string PrivateValue = RequestString("PrivateValue");//可选参数 protected void Page_Load(object sender, EventArgs e) { string postData = Chinapay.getChinaPayForm(OrderId, Amount, PrivateValue); ResponseWrite(postData); } }
6、接收返回值:
using System; /// <summary> /// ChinaPayModel 的摘要说明 /// </summary> public class ChinaPayModel1 { /// <summary> /// 商户号 /// </summary> public string merid { get; set; } /// <summary> /// 订单号 /// </summary> public string orderno { get; set; } /// <summary> /// 交易日期 /// </summary> public string transdate { get; set; } /// <summary> /// 金额 /// </summary> public string amount { get; set; } /// <summary> /// 币种 /// </summary> public string currencycode { get; set; } /// <summary> /// 类型 /// </summary> public string transtype { get; set; } /// <summary> /// 状态 /// </summary> public string status { get; set; } /// <summary> /// 签名 /// </summary> public string checkvalue { get; set; } /// <summary> /// 支付网关号 /// </summary> public string GateId { get; set; } /// <summary> /// 商户私有域 /// </summary> public string Priv1 { get; set; } } public class ChinaPayModel2 { /// <summary> /// 商户号 /// </summary> public string merid { get; set; } /// <summary> /// 订单号 /// </summary> public string orderno { get; set; } /// <summary> /// 交易日期 /// </summary> public DateTime transdate { get; set; } /// <summary> /// 交易金额 /// </summary> public decimal amount { get; set; } /// <summary> /// 币种,156为人民币 /// </summary> public int currencycode { get; set; } /// <summary> /// 类型:0001为付款,0002 /// </summary> public string transtype { get; set; } /// <summary> /// 状态:成功和失败 /// </summary> public bool status { get; set; } /// <summary> /// 签名是否正确 /// </summary> public bool checkvalue { get; set; } /// <summary> /// 支付网关号 /// </summary> public string GateId { get; set; } /// <summary> /// 商户私有域 /// </summary> public string Priv1 { get; set; } } public class ChinaPayMode { public string RequestForm { get; set; } public string RequestQueryString { get; set; } public ChinaPayModel1 Model1 { get; set; } public ChinaPayModel2 Model2 { get; set; } }
public static ChinaPayModel1 ChinaPayModel1() { Func<string, string> Request = RequestString; string merid = Request("merid"); string orderno = Request("orderno"); string transdate = Request("transdate"); string amount = Request("amount"); string currencycode = Request("currencycode"); string transtype = Request("transtype"); string status = Request("status"); string checkvalue = Request("checkvalue"); string GateId = Request("GateId"); string Priv1 = Request("Priv1"); return new ChinaPayModel1() { merid = merid, orderno = orderno, transdate = transdate, amount = amount, currencycode = currencycode, transtype = transtype, status = status, checkvalue = checkvalue, GateId = GateId, Priv1 = Priv1 }; } public static ChinaPayModel2 ChinaPayModel2() { Func<string, int> ParseInt = ParseInt; Func<string, decimal> ParseDecimal = PageBaseUser.ParseDecimal; ChinaPayModel1 model = ChinaPayModel1(); string orderno = ""; if (model.orderno.Length == 16) { orderno = model.orderno.Substring(0, 4) + model.orderno.Substring(9, 7); } DateTime dt = Convert.ToDateTime("1900-01-01"); if (model.transdate.Length == 8) { DateTime.TryParse(model.transdate.Substring(0, 4) + "-" + model.transdate.Substring(4, 2) + "-" + model.transdate.Substring(6, 2), out dt); } decimal amount = 0; if (model.amount.Length == 12) { amount = ParseDecimal(model.amount) / 100; } bool checkvalue = new Chinapay().getCheck(model.merid, model.orderno, model.amount, model.currencycode, model.transdate, model.transtype, model.status, model.checkvalue); return new ChinaPayModel2() { merid = model.merid, orderno = orderno, transdate = dt, amount = amount, currencycode = ParseInt(model.currencycode), transtype = model.transtype, status = ParseInt(model.status) == 1001, checkvalue = checkvalue, GateId = model.GateId, Priv1 = model.Priv1 }; }
//以下三个方法是处理请求的
/// <summary> /// 请求转Int型 /// </summary> /// <param name="QueryName"></param> /// <param name="DefaultValue"></param> /// <returns></returns> public static decimal RequestDecimal(string QueryName, decimal DefaultValue) { decimal value = DefaultValue; if (!string.IsNullOrEmpty(HttpContext.Current.Request[QueryName])) { decimal.TryParse(HttpUtility.HtmlDecode(HttpContext.Current.Request[QueryName].ToString().Trim()), out value); } return value; } /// <summary> /// 请求转String /// </summary> /// <param name="QueryName"></param> /// <param name="DefaultValue"></param> /// <returns></returns> public static string RequestString(string QueryName, string DefaultValue) { string value = DefaultValue; if (!string.IsNullOrEmpty(HttpContext.Current.Request[QueryName])) { value = HttpUtility.HtmlDecode(HttpContext.Current.Request[QueryName].ToString().Trim()); } return value; } /// <summary> /// 请求转Decimal型 /// </summary> /// <param name="QueryName"></param> /// <param name="DefaultValue"></param> /// <returns></returns> public static decimal RequestDecimal(string QueryName, decimal DefaultValue) { decimal value = DefaultValue; if (!string.IsNullOrEmpty(HttpContext.Current.Request[QueryName])) { decimal.TryParse(HttpUtility.HtmlDecode(HttpContext.Current.Request[QueryName].ToString().Trim()), out value); } return value; }
protected void Page_Load(object sender, EventArgs e) {
ChinaPayModel1 model1 = CommonHelper.ChinaPayModel1(); ChinaPayModel2 model2 = CommonHelper.ChinaPayModel2(); #region 测试时查看接收数据 string logMsg = JsonConvert.SerializeObject(new ChinaPayMode() { RequestForm = Request.Form.ToString(), RequestQueryString = Request.QueryString.ToString(), Model1 = model1, Model2 = model2 }); //省略 #endregion if (model2.checkvalue && model2.status) { }
}
标签:style blog http io os ar java for sp
原文地址:http://www.cnblogs.com/mydevin/p/4038283.html