首页 > 代码库 > 简单记录:银联在线支付

简单记录:银联在线支付

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 = http://www.mamicode.com/cp.getSign(_MerId, OrdId, TransAmt, CuryId, TransDate, TransType);"<form action=\"" + _Action + "\" method=\"POST\">");            sb.Append("<input type=\"hidden\" name=\"MerId\" value=http://www.mamicode.com/"" + _MerId + "\" />");            sb.Append("<input type=\"hidden\" name=\"OrdId\" value=http://www.mamicode.com/"" + OrdId + "\" />");            sb.Append("<input type=\"hidden\" name=\"TransAmt\" value=http://www.mamicode.com/"" + TransAmt + "\" />");            sb.Append("<input type=\"hidden\" name=\"CuryId\" value=http://www.mamicode.com/"" + CuryId + "\" />");            sb.Append("<input type=\"hidden\" name=\"TransDate\" value=http://www.mamicode.com/"" + TransDate + "\" />");            sb.Append("<input type=\"hidden\" name=\"TransType\" value=http://www.mamicode.com/"" + TransType + "\" />");            sb.Append("<input type=\"hidden\" name=\"Version\" value=http://www.mamicode.com/"20040916\" />");            sb.Append("<input type=\"hidden\" name=\"BgRetUrl\" value=http://www.mamicode.com/"" + _BgRetUrl + "\" />");            sb.Append("<input type=\"hidden\" name=\"PageRetUrl\" value=http://www.mamicode.com/"" + _PageRetUrl + "\" />");            sb.Append("<input type=\"hidden\" name=\"GateId\" value=http://www.mamicode.com/"\">");            sb.Append("<input type=\"hidden\" name=\"Priv1\" value=http://www.mamicode.com/"" + PrivateValue + "\">");            sb.Append("<input type=\"hidden\" name=\"ChkValue\" value=http://www.mamicode.com/"" + ChkValue + "\">");            sb.Append("<input id=\"PostData\" type=\"submit\" value=http://www.mamicode.com/"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 strChkValuehttp://www.mamicode.com/= "";        strChkValue = http://www.mamicode.com/npc.sign(MerId, OrdId, TransAmt, CuryId, TransDate, TransType);"key\\MerPrK.key");        string strChkValueDatahttp://www.mamicode.com/= "";        strChkValueData = http://www.mamicode.com/npc.signData(MerId, SignMsg);"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 strFlagDatahttp://www.mamicode.com/= "";        bool bolFlagData = http://www.mamicode.com/false;"true")        {            bolFlagData = http://www.mamicode.com/true;>

5、添加页面,后台代码如下:

using System;public partial class Payment_ChinaPayPost{    private string OrderId = RequestString("OrderId");//必选    private decimal Amount = RequestDecimal("Amount");//必选    private string PrivateValue = http://www.mamicode.com/RequestString("PrivateValue");//可选参数    protected void Page_Load(object sender, EventArgs e)    {        string postData = http://www.mamicode.com/Chinapay.getChinaPayForm(OrderId, Amount, PrivateValue);>

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 = http://www.mamicode.com/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 = http://www.mamicode.com/checkvalue,"";        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 = http://www.mamicode.com/new Chinapay().getCheck(model.merid, model.orderno, model.amount, model.currencycode, model.transdate, model.transtype, model.status, model.checkvalue);>  //以下三个方法是处理请求的
/// <summary> /// 请求转Int型 /// </summary> /// <param name="QueryName"></param> /// <param name="DefaultValue"></param> /// <returns></returns> public static decimal RequestDecimal(string QueryName, decimal DefaultValue) { decimal value = http://www.mamicode.com/DefaultValue;"QueryName"></param> /// <param name="DefaultValue"></param> /// <returns></returns> public static string RequestString(string QueryName, string DefaultValue) { string value = http://www.mamicode.com/DefaultValue;"QueryName"></param> /// <param name="DefaultValue"></param> /// <returns></returns> public static decimal RequestDecimal(string QueryName, decimal DefaultValue) { decimal value = http://www.mamicode.com/DefaultValue;>
   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)        {         }
}

  

简单记录:银联在线支付