工行在线支付接口实战开发流程及源码

2018-06-22 07:34:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

作为刚毕业的我来说,第一次开发在线支付功能,还是工行的,真的是被坑死了,和工行的技术真的是太难沟通了,而且那个技术明显是他们接口问题还死不承认,总是让我细读开发文档,结果我代码什么都没改,他调试接口后可以运行支付了,他无话可说……希望这篇能让一些人少走一些弯路。进入正题:

一、  前期准备

  1. 支付组件注册(PS:当时工行技术根本没有给我这个组件,被坑了,当时还想骂那个技术一下,签名什么的,难道我随便按照自己的想法签一个也可以?后来终于给我了)

组件注册说明

一、API配置说明:

1.将ICBCEBankUtil.dll和infosecapi.dll两个dll文件拷贝到系统system32目录下(其中,如果是64位系统,则拷贝到SysWOW64目录下);

2.打开DOS窗口,进入system32目录/SysWOW64目录;

3.运行“regsvr32 ICBCEBankUtil.dll”命令注册控件;

 

二、函数说明:

1、初始化

init(

BSTR certFN,银行证书文件名

BSTR certFNM,商户证书文件名

BSTR keyFN,私钥文件名

BSTR key私钥保护口令

)

返回值:

-100:银行证书文件名传递错

-101:商户证书文件名传递错

-102:私钥文件名传递错

-103:私钥保护口令传递错

-110:打开银行证书文件错

-111:打开商户证书文件错

-112:打开私钥文件错

2、签名

signC(

BSTR src,明文字符串

     int srcLen,明文长度

     )

返回值:

如果成功则返回BASE64编码后的签名串,否则返回空串

如果返回空串,可调用getRC取得错误码

3、获取错误码(只在出错时有效)

getRC

返回值

        -100:明文字符串传递错误

        -1:明文错误

-2:私钥错 

-3:私钥解密错

-4:私钥保护口令错

4、验证签名

verifySignC(

BSTR src,明文字符串

int srcLen,明文字符串长度

BSTR sSrc,签名BASE64编码字符串

int sSrcLen签名BASE64编码字符串长度

)

返回值:

0:成功

-1:验证失败

-2:解码失败

-100:明文字符串传递有误

-101:签名字符串传递有误

5、获取证书

getCert(

int which证书类型:0银行证书,1商户证书

)

返回值:BASE64编码后的证书

注册好以后将infosecapi.dll引用到项目中。

2.证书引用

 然后将public.crt、商户.crt、商户.key 这三个文件放到项目某个文件夹下

二、      开发阶段

  1. 首先必须细读:《中国工商银行网上银行新B2C在线支付接口说明V1.0.0.11》这个文档(PS:工行人员当时没有给我这个文档,打电话过去问技术,技术让我细读这个文档,根本没有这个文档,还说他发给客户了的,问客户也说没有发过,当时真的是被坑好多,后来网上找到了一个)。
  2. 以下是源码:
/// <summary>

    /// 工商银行签名验证类

    /// </summary>

    public class ICBCTest

    {

        string strCertFN = @"f:\aa\ahhfycYQHLTEST.pfx";

        string strCertFNM = @"f:\aa\ahhfyc.crt";

        string strKeyFN = @"f:\aa\ahhfyc.key";

 

 

        //string strCertFN = System.Web.HttpContext.Current.Server.MapPath(@"user\user.crt");

        //string strCertFNM = System.Web.HttpContext.Current.Server.MapPath(@"user\user.crt");

        //string strKeyFN = System.Web.HttpContext.Current.Server.MapPath(@"user\user.key");

        //string strKey = "12345678";

        string strKey = "12345678";

 

        /// <summary>

        /// 获取工商银行验证信息

        /// </summary>

        /// <returns></returns>

        public ICBC GetCheckInfo(ICBC argIcbc)

        {

            string strMerSignMsg = string.Empty;

            B2CUtil icbcObj = new B2CUtil();

 

            if (icbcObj.init(strCertFN, strCertFNM, strKeyFN, strKey) == 0)

            {

                //对订单数据进行签名,得到订单签名数据 MerSignMsg

                argIcbc.MerSignMsg = icbcObj.signC(argIcbc.TranData, argIcbc.TranData.Length);

 

                //证书公钥   商户用二进制方式读取证书公钥文件后,进行BASE64编码后产生的字符串

                argIcbc.MerCert = icbcObj.getCert(1);

                argIcbc.TranData = Base64Encode(argIcbc.TranData);

            }

            else

            {

                return null;

            }

            return argIcbc;

        }

 

        /// <summary>

        /// 获取工商银行验证信息

        /// </summary>

        /// <returns></returns>

        public ICBC GetCheckReturnInfo(ICBC argIcbc)

        {

            string strMerSignMsg = string.Empty;

            B2CUtil icbcObj = new B2CUtil();

 

            if (icbcObj.init(strCertFN, strCertFNM, strKeyFN, strKey) == 0)

            {

                argIcbc.TranData = Base64Decode(argIcbc.TranData);

 

                if (icbcObj.verifySignC(argIcbc.TranData, argIcbc.TranData.Length, argIcbc.MerSignMsg, argIcbc.MerSignMsg.Length) == 0) //判断验证银行签名是否成功

                {

                    argIcbc.IsCheck = true;

                }

                else

                    argIcbc.IsCheck = true;

            }

            else

            {

                argIcbc.IsCheck = false;

            }

            return argIcbc;

        }

 

 

        ///<summary>

 

        ///转成Base64形式的System.String

 

        ///</summary>

 

        ///<param name="str"></param>

 

        ///<returns></returns>

 

        public static string Base64Encode(string str)

        {

 

            byte[] b = System.Text.Encoding.Default.GetBytes(str);

 

            //转成Base64形式的System.String

 

            return Convert.ToBase64String(b);

 

        }

 

        public static string Base64Encode(Byte[] b)

        {

 

            ///转成Base64形式的System.String

 

            return Convert.ToBase64String(b);

 

        }

 

        ///<summary>

 

        /// Base64转回到原来的 System.String

 

        ///</summary>

 

        ///<param name="str"></param>

 

        ///<returns></returns>

 

        public static string Base64Decode(string str)

        {

 

            byte[] c = Convert.FromBase64String(str);

 

            //转回到原来的 System.String

 

            return System.Text.Encoding.Default.GetString(c);

 

        }

    }

 

 

 

 

 

 

/// <summary>

    /// 工行实体信息类

    /// </summary>

    public class ICBC

    {

        //private string _orderPostUrl = "https://mybank.icbc.com.cn/servlet/ICBCINBSEBusinessServlet";

        //private string _orderPostUrl = "https://mybank3.dccnet.com.cn/servlet/NewB2cMerPayReqServlet";

        private string _orderPostUrl = "https://myipad.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet";

        //private string _interfaceName = "ICBC_PERBANK_B2C";

        private string _interfaceName = "ICBC_PERBANK_B2C";

        private string _interfaceVersion = "1.0.0.11";

        private string _orderid;

        private string _amount;

        private string _curType = "001";

        //private string _merID = "1302EC23361694";

        private string _merID = "1302EC23928826";

        //private string _merAcct = "1302010119022149867";

        private string _merAcct = "1302010119022141605";

        private string _verifyJoinFlag = "0";

        private string _notifyType = "HS";

        private string _merURL;

        private string _resultType = "1";

        private string _orderDate = DateTime.Now.ToString("yyyyMMddHHmmss");

        private string _merSignMsg;

        private string _merCert;

        private string _goodsID = "10021";

        private string _goodsName = "XXXX";

        private string _goodsNum;

        private string _carriageAmt;

        private string _remark1;

        private string _remark2;

        private string _merHint;

        private string _tranData;

        private string _merReference = "www.XXX.com";

        //private string _merReference = "localhost";

 

        private bool _isCheck = false;

 

        /// <summary>

        /// 是否检测成功

        /// </summary>

        public bool IsCheck

        {

            get { return _isCheck; }

            set { _isCheck = value; }

        }

 

        public string MerReference

        {

            get { return _merReference; }

            set { _merReference = value; }

        }

 

        /// <summary>

        /// 报文数据

        /// </summary>

        public string TranData

        {

            get { return _tranData; }

            set { _tranData = value; }

        }

 

        /// <summary>

        /// 工商支付接口路径

        /// </summary>

        public string OrderPostUrl

        {

            get { return _orderPostUrl; }

            set { _orderPostUrl = value; }

        }

 

        /// <summary>

        /// 接口名称

        /// </summary>

        public string InterfaceName

        {

            get { return _interfaceName; }

            set { _interfaceName = value; }

        }

 

        /// <summary>

        /// 接口版本号

        /// </summary>

        public string InterfaceVersion

        {

            get { return _interfaceVersion; }

            set { _interfaceVersion = value; }

        }

 

        /// <summary>

        /// 订单号

        /// </summary>

        public string Orderid

        {

            get { return _orderid; }

            set { _orderid = value; }

        }

 

        /// <summary>

        /// 订单金额

        /// </summary>

        public string Amount

        {

            get { return _amount; }

            set { _amount = value; }

        }

 

        /// <summary>

        /// 支付币种 RMB:001

        /// </summary>

        public string CurType

        {

            get { return _curType; }

            set { _curType = value; }

        }

 

        /// <summary>

        /// 商户代码

        /// </summary>

        public string MerID

        {

            get { return _merID; }

            set { _merID = value; }

        }

 

        /// <summary>

        /// 商户账号         /// </summary>

        public string MerAcct

        {

            get { return _merAcct; }

            set { _merAcct = value; }

        }

 

        /// <summary>

        /// 检验联名标志

        /// 取值“1”:客户支付时,网银判断该客户是否与商户联名,是则按上送金额扣帐,否则展现未联名错误;

        /// 取值“0”:不检验客户是否与商户联名,按上送金额扣帐。

        /// </summary>

        public string VerifyJoinFlag

        {

            get { return _verifyJoinFlag; }

            set { _verifyJoinFlag = value; }

        }

 

        /// <summary>

        /// 通知类型

        /// 取值“HS”:在交易完成后实时将通知信息以HTTP协议POST方式,主动发送给商户,发送地址为商户端随订单数据提交的接收工行支付结果的URL即表单中的merURL字段;

        /// 取值“AG”:在交易完成后不通知商户。商户需使用浏览器登录工行的B2C商户服务网站,或者使用工行提供的客户端程序API主动获取通知信息。

        /// </summary>

        public string NotifyType

        {

            get { return _notifyType; }

            set { _notifyType = value; }

        }

 

        /// <summary>

        /// 返回商户URL

        /// </summary>

        public string MerURL

        {

            get { return _merURL; }

            set { _merURL = value; }

        }

 

        /// <summary>

        /// 结果发送类型

        /// </summary>

        public string ResultType

        {

            get { return _resultType; }

            set { _resultType = value; }

        }

 

        /// <summary>

        /// 交易日期时间

        /// </summary>

        public string OrderDate

        {

            get { return _orderDate; }

            set { _orderDate = value; }

        }

        /// <summary>

        /// 订单签名数据

        /// </summary>

        public string MerSignMsg

        {

            get { return _merSignMsg; }

            set { _merSignMsg = value; }

        }

 

        /// <summary>

        /// 商城证书公钥

        /// </summary>

        public string MerCert

        {

            get { return _merCert; }

            set { _merCert = value; }

        }

 

        /// <summary>

        /// 商品编号

        /// </summary>

        public string GoodsID

        {

            get { return _goodsID; }

            set { _goodsID = value; }

        }

 

        /// <summary>

        /// 商品名称

        /// </summary>

        public string GoodsName

        {

            get { return _goodsName; }

            set { _goodsName = value; }

        }

 

        /// <summary>

        /// 商品数量

        /// </summary>

        public string GoodsNum

        {

            get { return _goodsNum; }

            set { _goodsNum = value; }

        }

 

        /// <summary>

        /// 已含运费金额

        /// </summary>

        public string CarriageAmt

        {

            get { return _carriageAmt; }

            set { _carriageAmt = value; }

        }

 

        /// <summary>

        /// 备注字段1

        /// </summary>

        public string Remark1

        {

            get { return _remark1; }

            set { _remark1 = value; }

        }

 

        /// <summary>

        /// 备注字段2

        /// </summary>

        public string Remark2

        {

            get { return _remark2; }

            set { _remark2 = value; }

        }

 

        /// <summary>

        /// 商城提示

        /// </summary>

        public string MerHint

        {

            get { return _merHint; }

            set { _merHint = value; }

        }

    }

 

 

 

 

以下为支付页面逻辑代码:

 

 

 

    

public partial class trainpay : System.Web.UI.Page

    {

        public ICBC icmcModel = new ICBC();

        ICBCTest it = new ICBCTest();

        BLL.PXBCourse bllPXBCourse = new BLL.PXBCourse();

        public string money;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (pxbID != 0 && courseID != "" && UserId != "")

            {

 

                //string ccid = Request.QueryString["cid"];

                //Random rnd = new Random();

                //int n = rnd.Next(1000, 9999);

                //string orderId = DateTime.Now.ToString("yyyyMMddHHmmss") + n.ToString();//订单号

                string datetime = DateTime.Now.AddDays(-1).ToString("yyyyMMddHHmmss");

               

 

                //总价

                try

                {

                    //string ii = bllPXBCourse.Getbymoney(courseID,pxbID);

                    string ii = "1";

 

                    if (ii != "")

                    {

                        //money = ii + "00";

                        money = ii;

                    }

                    else

                    {

                        //money = "0.01";

                        Response.Write("金额出错,请联系管理员!");

                        Response.End();

                    }

                }

                catch

                {

                    Response.Write("金额出错,请联系管理员!");

                    Response.End();

                }

                //数据处理

                StringBuilder strXml = new StringBuilder();

 

                strXml.Append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\"?>");

                strXml.Append("<B2CReq>");

                //接口名称

                strXml.Append("<interfaceName>" + icmcModel.InterfaceName + "</interfaceName>");

                //接口版本号

                strXml.Append("<interfaceVersion>" + icmcModel.InterfaceVersion + "</interfaceVersion>");

                //订单信息

                strXml.Append("<orderInfo>");

                //交易日期时间

                strXml.Append("<orderDate>" + icmcModel.OrderDate + "</orderDate>");

                //支付币种

                strXml.Append("<curType>" + icmcModel.CurType + "</curType>");

                //商户代码

                strXml.Append("<merID>" + icmcModel.MerID + "</merID>");

                //订单信息列表

                strXml.Append("<subOrderInfoList>");

                //订单信息

                strXml.Append("<subOrderInfo>");

                //订单编号

                strXml.Append("<orderid>" + OrderId + "</orderid>");

                //订单金额

                strXml.Append("<amount>" + money + "</amount>");

                //分期付款期数 1代表全额付款

                strXml.Append("<installmentTimes>1</installmentTimes>");

                //商户账号

                strXml.Append("<merAcct>" + icmcModel.MerAcct + "</merAcct>");

                //商品编号

                strXml.Append("<goodsID>" + DateTime.Now.ToString("yyyyMMddHHmmss").ToString() + "</goodsID>");

                //商品名称

                strXml.Append("<goodsName>" + GetPxbname(pxbID) + "</goodsName>");

                //商品数量

                strXml.Append("<goodsNum>1</goodsNum>");

                //已含运费金额

                strXml.Append("<carriageAmt>0</carriageAmt>");

                strXml.Append("</subOrderInfo>");

 

                strXml.Append("</subOrderInfoList>");

                strXml.Append("</orderInfo>");

                strXml.Append("<custom>");

                //检验联名标志 取值“1”:客户支付时,网银判断该客户是否与商户联名

                strXml.Append("<verifyJoinFlag>" + icmcModel.VerifyJoinFlag + "</verifyJoinFlag>");

                //语言版本 取值:“EN_US”为英文版;取值:“ZH_CN”或其他为中文版

                strXml.Append("<Language>ZH_CN</Language>");

                strXml.Append("</custom>");

                strXml.Append("<message>");

                //支持订单支付的银行卡种类

                strXml.Append("<creditType>2</creditType>");

                //通知类型

                strXml.Append("<notifyType>HS</notifyType>");

                //结果发送类型

                strXml.Append("<resultType>1</resultType>");

                //商户reference

                strXml.Append("<merReference>" + icmcModel.MerReference + "</merReference>");

                //客户端IP 当商户reference项送空时,该项必输

                strXml.Append("<merCustomIp></merCustomIp>");

                //虚拟商品/实物商品标志位 取值“0”:虚拟商品 取值“1”,实物商品

                strXml.Append("<goodsType>1</goodsType>");

 

                //买家用户号   

                strXml.Append("<merCustomID>" + UserId + "</merCustomID>");

                //买家联系电话

                strXml.Append("<merCustomPhone>110</merCustomPhone>");

                //收货地址

                strXml.Append("<goodsAddress></goodsAddress>");

                //订单备注

                strXml.Append("<merOrderRemark></merOrderRemark>");

                //商城提示

                strXml.Append("<merHint></merHint>");

                //备注字段1

                strXml.Append("<remark1></remark1>");

                //备注字段2

                strXml.Append("<remark2></remark2>");

                //返回商户URL

                strXml.Append("<merURL>http://www.XXX.com/pay/PayReturn.aspx</merURL>");

                //返回商户变量

                //strXml.Append("<merVAR>" + OrderId + "," + UserId + "," + pxbID + "," + courseID + "</merVAR>");

                strXml.Append("<merVAR>" + OrderId + "," + UserId + "</merVAR>");

                strXml.Append("</message>");

                strXml.Append("</B2CReq>");

 

 

 

              

 

                icmcModel.TranData = strXml.ToString();

                icmcModel = it.GetCheckInfo(icmcModel);

 

                //Payment paymo = new Payment();

                //PaymentBLL paybll = new PaymentBLL();

                //paymo.oid = OrderId;

                //paymo.uid = UserId;

                //paymo.pid = pxbID.ToString();

                //paymo.cid = courseID;

                //paymo.ispay = 0;//0初始化订单,1支付成功,2支付失败,3签名失败,4数据异常

                //paymo.nopay = 1;//线上1,线下2

                //paymo.ordertime = DateTime.Now;

                //paybll.Add(paymo);

 

            }

            else

            {

                Response.Write("数据非法提交,请正常付费!");

                Response.End();

            }

        }

 

 

        //用户

        public string UserId

        {

            get

            {

                VerifyUser vuser = new VerifyUser();

                return vuser.GetUserID;

            }

        }

 

        //订单号

        public string OrderId

        {

            get

            {

                string sid = string.Empty + Request.QueryString["oid"];

               

                return sid;

            }

        }

 

        private BLL.PaymentBLL paymentBll = new PaymentBLL();

        //培训班

        public int pxbID

        {

            get

            {

 

                //string sid = string.Empty + Request.QueryString["pid"];

                Payment payment = paymentBll.GetModelbyoid(OrderId);

                string sid = payment.pid.ToString();

                int id = 0;

                if (!int.TryParse(sid, out id))

                    return 0;

                return id;

            }

        }

        //课程编号

        public string courseID

        {

            get

            {

                //string cid = Request.QueryString["cid"];

                Payment payment = paymentBll.GetModelbyoid(OrderId);

                string cid = payment.cid;

                cid = cid.Replace("x", ",");

                return cid;

            }

        }

        //培训班名称

        public string GetPxbname(int pxbid)

        {

            Community.BLL.PXB bll = new BLL.PXB();

            string name = "";

            Community.Model.PXB model = bll.GetModel(pxbid);

            if (model != null)

            {

                name = model.Name;

            }

            return name;

        }

    }

 

 

 

以下是支付页面代码:

<body>

    <form name="sendOrder" method="post" action="<%= icmcModel.OrderPostUrl %>" id="order">

    <input type="hidden" name="interfaceName" value="<%= icmcModel.InterfaceName %>">

    <input type="hidden" name="interfaceVersion" value="<%= icmcModel.InterfaceVersion %>">

    <input type="hidden" name="tranData" value="<%= icmcModel.TranData %>">

    <input type="hidden" name="merSignMsg" value="<%= icmcModel.MerSignMsg %>">

    <input type="hidden" name="merCert" value="<%= icmcModel.MerCert %>">

     </form>

    <script type="text/javascript">

        document.order.submit();

    </script>

</body>

 

 

 

 

 

以下是支付返回页面:

public partial class PayReturn : System.Web.UI.Page

    {

        public ICBC icmcModel = new ICBC();

        protected void Page_Load(object sender, EventArgs e)

        {

            ICBCTest it = new ICBCTest();

            if (Request.Form["notifyData"] != null)

            {

                Payment paymo = new Payment();

                PaymentBLL paybll = new PaymentBLL();

 

                try

                {

                    //PaymentBLL paybll = new PaymentBLL();

                    ICBCTest icbcCheck = new ICBCTest();

                    ICBC icbcInfo = new ICBC();

 

                    icbcInfo.TranData = Request.Form["notifyData"];

                    icbcInfo.MerSignMsg = Request.Form["signMsg"].ToString();

 

                    icbcInfo = icbcCheck.GetCheckReturnInfo(icbcInfo);

                    //自定义返回

                    string strOrderSN = Request.Form["merVAR"].ToString();

 

                    string[] arrorder = strOrderSN.Split(',');

 

                    string oid = arrorder[0];

                    string uid = arrorder[1];

                    int pid = Convert.ToInt32(arrorder[2]);

                    string cid = arrorder[3];

 

                    if (icbcInfo.IsCheck)//处理签名

                    {

                        DataSet myds = new DataSet();

                        StringReader strReader = new StringReader(icbcInfo.TranData);

                        myds.ReadXml(strReader);

                        DataTable mytable = new DataTable();

                        mytable = myds.Tables["bank"];

 

                        if (null != mytable && mytable.Rows.Count > 0)

                        {

                            if (mytable.Rows[0]["tranStat"].ToString().Trim() == "1")

                            {

 

                                //这里做成功操作

 

                                try

                                {

 

                                    //入库处理

                                    BLL.PXBCourse bllPXBCourse = new BLL.PXBCourse();

                                    BLL.PXBUsers pxbUser = new BLL.PXBUsers();

 

                                    string b = cid;

                                    string[] arrtemp = b.Split('x');

                                    for (int i = 0; i < arrtemp.Length; i++)

                                    {

                                        int courseid = int.Parse(arrtemp[i]);

 

                                        int addi = bllPXBCourse.Addcoursechoose(uid, courseid, pid);

                                    }

 

                                    //付费成功的同时也报名成功

                                    int res = pxbUser.GetRecordCount(" userid='" + uid + "' and pxbid=" + pid);

                                    if (res == 0)

                                    {

                                        Model.PXBUser user = new Model.PXBUser();

                                        user.PXBID = pid;

                                        user.UserID = uid;

                                        user.BYWay = 1;

                                        user.CJWay = 1;

                                        //如果当前人不在当前培训班,则报名

                                        pxbUser.Add(user);

 

                                    }

                                    // <param name="nopay">支付方式:1为线上支付,2为线下支付</param>

                                    // <param name="ispay">支付状态:0初始化订单,1支付成功,2支付失败,3签名失败,4数据异常,5支付中</param>

                                    // <param name="oid">订单号</param>

                                    paybll.Updatepay(1,1, oid);//支付成功

 

                                    Response.Write("http://122.225.101.115:444/FirstPage.aspx");

                                    Response.End();

                                }

                                catch

                                {

                                    paybll.Updatepay(1,4, oid);//数据异常

 

                                    Response.Write("数据异常,请联系管理员!");

                                    Response.End();

                                }

 

 

                            }

                            else

                            {

                                paybll.Updatepay(1,2, oid);//支付失败

                                Response.Write("支付失败,请联系管理员!");

                                Response.End();

                            }

                        }

                    }

                    else

                    {

                        paybll.Updatepay(1,3, oid);//签名失败

                        Response.Write("签名失败,请联系管理员!");

                        Response.End();

                    }

                }

                catch

                {

                    Response.Write("数据异常,请联系管理员!");

                    Response.End();

                }

            }

            else

            {

                Response.Write("返回失败,请联系管理员!");

                Response.End();

            }

        }

    }

 

 

 

 

 

然而,比较重要的一个还有支付查询页面,防止比如支付成功了,但由于网络异常等原因没有返回支付成功数据等情况下使用。

 

以下是支付查询页面逻辑代码:

 

public partial class openpay : System.Web.UI.Page

    {

        /// <summary>

        /// 银行证书文件地址

        /// </summary>

        static string strCertFN = System.Web.HttpContext.Current.Server.MapPath(@"user\user.crt");

 

        /// <summary>

        /// 商户证书文件地址

        /// </summary>

        static string strCertFNM = System.Web.HttpContext.Current.Server.MapPath(@"user\user.crt");

 

        /// <summary>

        /// 私钥文件名

        /// </summary>

        //static string strKeyFN = System.Web.HttpContext.Current.Server.MapPath(@"user\user.key");

        static string strKeyFN = System.Web.HttpContext.Current.Server.MapPath(@"user\kjks-sy.key");

 

        /// <summary>

        /// 私钥口令

        /// </summary>

        static string strKey = "12345678";

       // static string api_url = "https://corporbank.icbc.com.cn/servlet/ICBCINBSEBusinessServlet";

        //static string api_url = "https://corporbank3.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet";

        static string api_url = "https://myipad.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet";

        //static string post_params = "APIName=EAPI&APIVersion=001.001.002.001&MerReqData=";

        static string post_params = "APIName=kjks.y.1302&APIVersion=0.0.1.0&MerReqData=";

        static string cert_path = HttpContext.Current.Server.MapPath("~/..");

 

        protected void Page_Load(object sender, EventArgs e)

        {

            string outMess;

            string mess = CheckOrder("201408041357171136 ", "20140804", "1302EC23361694", "1302010119022149867", out outMess);

 

        }

        /// <summary>

        /// 查询订单

        /// </summary>

        /// <param name="strOrderNum">订单号</param>

        /// <param name="strTranDate">交易日期</param>

        /// <param name="strShopCode">商家代码</param>

        /// <param name="strShopAccount">商城账号</param>

        /// <param name="errInfo"></param>

        /// <returns></returns>

        public static string CheckOrder(string strOrderNum, string strTranDate, string strShopCode, string strShopAccount, out string errInfo)

        {

            try

            {

                errInfo = string.Empty;

                StringBuilder sb = new StringBuilder();

                sb.Append("<?xml  version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?><ICBCAPI><in><orderNum>");

                sb.Append(strOrderNum);

                sb.Append("</orderNum><tranDate>");

                sb.Append(strTranDate);

                sb.Append("</tranDate><ShopCode>");

                sb.Append(strShopCode);

                sb.Append("</ShopCode><ShopAccount>");

                sb.Append(strShopAccount);

                sb.Append("</ShopAccount></in></ICBCAPI>");

                string post_data = post_params + sb.ToString();

                string retruenstring = PostDataBySSL(post_data, api_url, cert_path, strKey, out errInfo);

                //var result = SpringFactory.BusinessFactory.GetBusinessAnonymousUser();

                //result.AddLogs("返回3:" + (retruenstring.Length > 400 ? retruenstring.Substring(0, 400) : retruenstring));

                if (retruenstring.Length <= 5)

                {

                    return retruenstring;

                }

                return HttpUtility.UrlDecode(retruenstring);

            }

            catch

            {

 

                errInfo = "查询缴费接口失败";

 

                return "99";

 

            }

        }

 

        /// <summary>

        /// 发送SSL加密请求

        /// </summary>

        /// <param name="post_data"></param>

        /// <param name="url"></param>

        /// <param name="cert_path"></param>

        /// <param name="cert_password"></param>

        /// <param name="errInfo"></param>

        /// <returns></returns>

        public static string PostDataBySSL(string post_data, string url, string cert_path, string cert_password, out string errInfo)

        {

            errInfo = string.Empty;

            try

            {

                ASCIIEncoding encoding = new ASCIIEncoding();

                byte[] data = encoding.GetBytes(post_data);

                if (cert_path != string.Empty)

                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

 

                WebRequest webRequest = WebRequest.Create(url);

                HttpWebRequest httpRequest = webRequest as HttpWebRequest;

 

                if (cert_path.ToLower().EndsWith(".cer"))

                {

                    httpRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(cert_path));

                }

 

                else

                {

                    //SpringFactory.BusinessFactory.GetBusinessAnonymousUser().AddLogs(cert_path);

                    httpRequest.ClientCertificates.Add(new X509Certificate2(cert_path, cert_password, X509KeyStorageFlags.MachineKeySet));

 

 

                }

                httpRequest.KeepAlive = true;

                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";

                httpRequest.ContentType = "application/x-www-form-urlencoded";

                httpRequest.Method = "POST";

 

                httpRequest.ContentLength = data.Length;

                Stream requestStream = httpRequest.GetRequestStream();

                requestStream.Write(data, 0, data.Length);

                requestStream.Close();

                Stream responseStream = null;

                responseStream = httpRequest.GetResponse().GetResponseStream();

                string stringResponse = string.Empty;

                if (responseStream != null)

                {

                    using (StreamReader responseReader =

                        new StreamReader(responseStream, Encoding.GetEncoding("GBK")))

                    {

                        stringResponse = responseReader.ReadToEnd();

                    }

                    responseStream.Close();

                }

                return stringResponse;

            }

            catch (Exception e)

            {

                errInfo = e.Message;

 

                // SpringFactory.BusinessFactory.GetBusinessAnonymousUser().AddLogs(e.Message);

                return string.Empty;

            }

        }

 

        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

        {

            return true;

        }

 

    }

 

 

 

三、注意事项

根据本人被坑死的经历,告诉大家注意以下事项:

1.在支付发送数据时,支付金额应该是以“分”为单位,也就是金额是一个整数。

2.在进行测试的时候,订单提交时间必须和他们服务器时间一致,而他们,工行测试服务器时间是被他们设置了的,根本不是正常时间,最重要的一点是,时间每天都在变化,比如今天是2016年8月22号,明天可能是2016年8月26号之类的,之前他们没有直接诶告诉我,被坑死了。时间不对,支付不了哦。

3.在测试时,支付成功后不会跳转到我们的支付返回地址,这个也必须注意一下,这也是他们测试服务器的原因,本人在这里也是被坑了,这样的话我们也无法更新本地订单支付状态表。

      本来很久之前就该写好的了,当时技术经理让我做完这个功能写一篇技术文档给后来人借鉴下的,可惜一直比较懒,现在我敬爱的技术经理都走了,我遇到的第一位温柔可亲的还对我这么好的技术经理居然都走了(PS:是个男的哦),真的很不舍。我多希望我们部门能尽快找到一位新的技术经理,带我飞,至少我才踏入这个社会,很多东西需要学习,有人给我引路,我会少走很多弯路,进步更快一些。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Redis命令拾遗四(集合类型)—包含简单搜索筛选商品设计实例。

下一篇:IQueryable与IEnumerable