예제 #1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var result  = string.Empty;
            var tradeNo = context.Request.QueryString["tradeNo"] ?? "201710110909";
            var outMess = string.Empty;

            try
            {
                var mess = CBCPayOnline.CheckOrder(tradeNo, tradeNo.Substring(0, 8), ConfigurationManager.AppSettings["icbc.merID"], ConfigurationManager.AppSettings["icbc.merAcct"], out outMess);
                result += mess + "<br/>";
                if (mess.Length > 5)//缴费成功,未返回错误编码,返回xml数据
                {
                    DataSet      myds      = new DataSet();
                    StringReader strReader = new StringReader(mess);
                    myds.ReadXml(strReader);
                    string stat = myds.Tables["out"].Rows[0]["tranStat"].ToString();
                    if (stat == "1" || stat == "0")
                    {
                        result += "工行查询支付成功";
                    }
                    else//支付失败
                    {
                        result += "工行查询支付失败";
                    }
                }
                else
                {
                    string pays = "";
                    switch (mess)
                    {
                    case "40972": pays = "API查询的订单不存在"; break;

                    case "40973": pays = "API查询过程中系统异常"; break;

                    case "40976": pays = "API查询系统异常"; break;

                    case "40977": pays = "商户证书信息错"; break;

                    case "40978": pays = "解包商户请求数据报错"; break;

                    case "40979": pays = "查询的订单不存在"; break;

                    case "40980": pays = "API查询过程中系统异常"; break;

                    case "40981": pays = "给商户打包返回数据错"; break;

                    case "40982": pays = "系统错误"; break;

                    case "40983": pays = "查询的订单不唯一"; break;

                    case "40987": pays = "商户代码或者商城账号有误"; break;

                    case "40947": pays = "给商户打包返回数据错"; break;

                    case "40948": pays = "商城状态非法"; break;

                    case "40949": pays = "商城类别非法"; break;

                    case "40950": pays = "商城应用类别非法"; break;

                    case "40951": pays = "商户证书id状态非法"; break;

                    case "40952": pays = "商户证书id未绑定"; break;

                    case "40953": pays = "商户id权限非法"; break;

                    case "40954": pays = "检查商户状态时数据库异常"; break;
                    }

                    result += tradeNo + ":工行查询支付发生错误, 错误编码:" + mess + "-" + pays;
                }
            }
            catch (Exception e)
            {
                result += tradeNo + ":工行查询支付发生错误:" + e.ToString();
            }


            context.Response.Write(result);
        }
예제 #2
0
        public static JObject createForm(PaymentType paymentType, string from, string fromNo, string fee, string subject, string installmentTimes, bool isPC = false)
        {
            ////////////////////////////////////////////请求参数////////////////////////////////////////////
            //商户订单号,商户网站订单系统中唯一订单号,必填
            var now          = DateTime.Now;
            var out_trade_no = now.ToString("yyyyMMddHHmmssfff") + CommonBusiness.CreateRandom();
            var orderDate    = now.ToString("yyyyMMddHHmmss");

            //订单名称,必填
            if (string.IsNullOrEmpty(subject))
            {
                subject = "工行支付";
            }

            //付款金额,必填
            var total_fee = (double.Parse(fee)).ToString();

            if (!isPC)
            {
                var tranData = CreateTranData(orderDate, out_trade_no, total_fee, installmentTimes);

                // 本地数据维护 支付宝单位为元 支付宝类型统一为alipay 不好区分
                var result = TradeBusiness.AddTrade(out_trade_no, from, fromNo, PaymentType.ICBC, total_fee, subject, string.Empty, "0");
                if (!(bool)result["Flg"])
                {
                    throw new Exception("本地数据维护失败");
                }

                var bytes           = Encoding.GetEncoding("gbk").GetBytes(tranData);
                var translateString = Convert.ToBase64String(bytes);
                var form            = new StringBuilder();
                var signs           = CBCPayOnline.GetCheckInfo(tranData);
                form.Append("<h3>页面跳转中。。。</h3>");
                //form.Append("<form style=\"display:none;\" id=\"payForm\" action=\"http://wap2.icbc.com.cn/servlet/ICBCWAPEBizServlet\" > ");
                form.Append("<form style=\"display:none;\" id=\"payForm\" action=\"https://mywap2.icbc.com.cn/ICBCWAPBank/servlet/ICBCWAPEBizServlet\" > ");
                form.Append("<INPUT NAME=\"interfaceName\" TYPE=\"text\" value=\"ICBC_WAPB_B2C\">");
                form.Append("<INPUT NAME=\"interfaceVersion\" TYPE=\"text\" value=\"1.0.0.6\">");
                form.Append("<INPUT NAME=\"tranData\" TYPE=\"text\" value=\"" + translateString + "\">");
                form.Append("<INPUT NAME=\"merSignMsg\" TYPE=\"text\" value=\"" + signs[0] + "\">");
                form.Append("<INPUT NAME=\"merCert\" TYPE=\"text\" value=\"" + signs[1] + "\">");
                form.Append("<INPUT NAME=\"clientType\" TYPE=\"text\" value=\"0\">");
                form.Append("</form>");
                form.Append("<script>document.forms['payForm'].submit();</script>");

                return(new JObject(
                           new JProperty("Trade", ((JObject)result["Object"])["result"]),
                           new JProperty("Content", form.ToString())));
            }
            else
            {
                var tranData = CreateTranData_PC(orderDate, out_trade_no, total_fee, installmentTimes);

                // 本地数据维护 支付宝单位为元 支付宝类型统一为alipay 不好区分
                var result = TradeBusiness.AddTrade(out_trade_no, from, fromNo, PaymentType.ICBC, total_fee, subject, string.Empty, "0");
                if (!(bool)result["Flg"])
                {
                    throw new Exception("本地数据维护失败");
                }

                var bytes           = Encoding.GetEncoding("gbk").GetBytes(tranData);
                var translateString = Convert.ToBase64String(bytes);
                var form            = new StringBuilder();
                var signs           = CBCPayOnline.GetCheckInfo(tranData);
                form.Append("<h3>页面跳转中。。。</h3>");
                //form.Append("<form style=\"display:none;\" id=\"payForm\" action=\"http://wap2.icbc.com.cn/servlet/ICBCWAPEBizServlet\" > ");
                form.Append("<form style=\"display:none;\" id=\"payForm\" action=\"https://B2C.icbc.com.cn/servlet/ICBCINBSEBusinessServlet\" > ");
                form.Append("<INPUT NAME=\"interfaceName\" TYPE=\"text\" value=\"ICBC_PERBANK_B2C\">");
                form.Append("<INPUT NAME=\"interfaceVersion\" TYPE=\"text\" value=\"1.0.0.11\">");
                form.Append("<INPUT NAME=\"tranData\" TYPE=\"text\" value=\"" + translateString + "\">");
                form.Append("<INPUT NAME=\"merSignMsg\" TYPE=\"text\" value=\"" + signs[0] + "\">");
                form.Append("<INPUT NAME=\"merCert\" TYPE=\"text\" value=\"" + signs[1] + "\">");
                //form.Append("<INPUT NAME=\"clientType\" TYPE=\"text\" value=\"0\">");
                form.Append("</form>");
                form.Append("<script>document.forms['payForm'].submit();</script>");

                return(new JObject(
                           new JProperty("Trade", ((JObject)result["Object"])["result"]),
                           new JProperty("Content", form.ToString())));
            }
        }
예제 #3
0
        //public static JObject execute(string from, string out_trade_no, string trade_no, string refund_amount, JObject result)
        //{
        //    // 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。
        //    var amount = (double.Parse(refund_amount)).ToString();
        //    var now = DateTime.Now;
        //    var seqno = now.ToString("yyyyMMddHHmmssfff") + CommonBusiness.CreateRandom();
        //    var signTime = now.ToString("yyyyMMddHHmmssfff");
        //    var tranDate = now.ToString("yyyyMMdd");
        //    var tranTime = now.ToString("HHmmssfff");
        //    var sb = new StringBuilder();
        //    sb.Append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        //    sb.Append("<CMS>");
        //    sb.Append("<eb>");
        //    sb.Append("<pub>");
        //    sb.Append("<TransCode>EBUSCOM</TransCode>"); //交易代码
        //    sb.Append("<CIS>100190013673155</CIS>"); // 客户注册时的归属编码,集团CIS号
        //    sb.Append("<BankCode>102</BankCode>"); // 客户注册时的归属单位,归属银行编号
        //    sb.Append("<ID>Niuluo.y.1001</ID>	"); // 无证书客户可上送空
        //    sb.Append("<TranDate>" + tranDate + "</TranDate>"); // ERP系统产生的交易日期,格式是yyyyMMdd
        //    sb.Append("<TranTime>" + tranTime + "</TranTime>"); // ERP系统产生的交易时间,格式如HHmmssSSS,精确到毫秒
        //    sb.Append("<fSeqno>" + seqno + "</fSeqno>"); // ERP系统产生的指令包序列号,一个集团永远不能重复
        //    sb.Append("</pub>");
        //    sb.Append("<in>");
        //    sb.Append("<TranType>0</TranType>"); // 0:退货 1:返还  2:转付
        //    sb.Append("<ShopType>2</ShopType>"); // 1:B2B商城  2:B2C商城
        //    sb.Append("<ShopCode>" + ConfigurationManager.AppSettings["icbc.merID"] + "</ShopCode>"); // 商城代码
        //    sb.Append("<ShopAcct>" + ConfigurationManager.AppSettings["icbc.merAcct"] + "</ShopAcct>"); // 商城账号 银联订单不可以上送
        //    sb.Append("<OrderNum>" + out_trade_no + "</OrderNum>");
        //    sb.Append("<PayType></PayType>"); // 0:普通 1:加急(B2C交易可为空)
        //    sb.Append("<PayDate>" + out_trade_no.Substring(0, 8) + "</PayDate>"); // yyyyMMdd(支持14位日期,前8位生效)
        //    sb.Append("<TransferName></TransferName>"); // B2C转付操作时必输,其他情况为空
        //    sb.Append("<TransferAccNo></TransferAccNo>"); // B2C转付操作时必输,其他情况为空
        //    sb.Append("<PayAmt>" + amount + "</PayAmt>"); // 以分为单位,B2C转付交易时需控制单笔金额(<=5万元), 融e购订单复用为现金支付金额
        //    sb.Append("<SignTime>" + signTime + "</SignTime>"); // 格式是yyyyMMddHHmmssSSS
        //    sb.Append("<ReqReserved1></ReqReserved1>"); // 退货时,可输入退货原因
        //    sb.Append("<ReqReserved2></ReqReserved2>	"); // 融e购订单复用为退货积分值|退货积分等值金额|退货积分对应商户金额
        //    sb.Append("<AcctSeq></AcctSeq>	");
        //    sb.Append("</in>");
        //    sb.Append("</eb>");
        //    sb.Append("</CMS>");

        //    var queryParams = new StringBuilder();
        //    queryParams.Append("Version=0.0.1.0");
        //    queryParams.Append("&TransCode=EBUSCOM");
        //    queryParams.Append("&BankCode=102");
        //    queryParams.Append("&GroupCIS=100190013673155");
        //    queryParams.Append("&ID=Niuluo.y.1001"); // 客户的证书ID(无证书客户可空)
        //    queryParams.Append("&PackageID=" + seqno); // 客户的指令包序列号(由客户ERP系统产生,不可重复)
        //    queryParams.Append("&Cert=客户的证书公钥信息(进行BASE64编码)");
        //    queryParams.Append("&reqData=" + CBCPayOnline.Encode(sb.ToString())); // 客户的xml请求数据

        //    var errorInfo = string.Empty;
        //    CBCPayOnline.PostDataBySSL(queryParams.ToString(), "https://银行通讯前置的地址和加密端口号?请求数据", string.Empty, string.Empty, out errorInfo);
        //    WebApiConfig.log.Info("工行退费返回结果:" + errorInfo);
        //    if (errorInfo.IndexOf("errorCode=") > -1)
        //    {
        //        WebApiConfig.log.Error("工行退费失败:" + errorInfo);
        //        throw new Exception("工行退费失败");
        //    }
        //    else if (errorInfo.IndexOf("reqData=") > -1)
        //    {
        //        var returnStr = CBCPayOnline.Decode(errorInfo.Replace("reqData=", string.Empty), "gbk");
        //        WebApiConfig.log.Info("工行退费返回xml:" + returnStr);
        //        var doc = XDocument.Parse(returnStr);
        //        var orderNum = doc.Descendants("OrderNum").FirstOrDefault().Value;
        //        var resultCode = doc.Descendants("Result").FirstOrDefault().Value;
        //        var fee = doc.Descendants("PayAmt").FirstOrDefault().Value;
        //        var retCode = doc.Descendants("RetCode").FirstOrDefault().Value;
        //        if (orderNum != out_trade_no)
        //        {
        //            WebApiConfig.log.Error("工行退费订单号不匹配:" + orderNum + "&" + out_trade_no);
        //            throw new Exception("工行退费交易不成功");
        //        }
        //        else if (resultCode != "1")
        //        {
        //            WebApiConfig.log.Error("工行退费交易不成功:" + orderNum + "&" + resultCode);
        //            throw new Exception("工行退费交易不成功");
        //        }
        //        else
        //        {
        //            WebApiConfig.log.Error("工行退费成功:" + orderNum + "&" + fee);
        //            JObject localResult;
        //            if (!TradeBusiness.UpdateTradeStatusSeveralTimes(out_trade_no, retCode, PaymentType.ICBC, string.Empty, TradeStatus.REFUND, out localResult))
        //            {
        //                WebApiConfig.log.Error("工行退费成功,本地数据库状态维护失败:" + orderNum);
        //                return new JObject(new JProperty("Trade", result));
        //            }
        //            else
        //            {
        //                return new JObject(new JProperty("Trade", localResult));
        //            }
        //        }
        //    }
        //    else
        //    {
        //        WebApiConfig.log.Error("工行退费结果未知:" + errorInfo);
        //        throw new Exception("工行退费失败");
        //    }
        //}

        public static JObject execute(string from, string out_trade_no, string trade_no, string refund_amount, JObject result)
        {
            //return new JObject(new JProperty("Trade", result));

            // 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。
            var amount   = (double.Parse(refund_amount)).ToString();
            var now      = DateTime.Now;
            var seqno    = now.ToString("yyyyMMddHHmmssfff") + CommonBusiness.CreateRandom();
            var signTime = now.ToString("yyyyMMddHHmmssfff");
            var tranDate = now.ToString("yyyyMMdd");
            var tranTime = now.ToString("HHmmssfff");
            var sb       = new StringBuilder();

            sb.Append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            sb.Append("<CMS>");
            sb.Append("<eb>");
            sb.Append("<pub>");
            sb.Append("<TransCode>EBUSCOM</TransCode>");        //交易代码
            sb.Append("<CIS>100190013673155</CIS>");            // 客户注册时的归属编码,集团CIS号
            sb.Append("<BankCode>102</BankCode>");              // 客户注册时的归属单位,归属银行编号
            sb.Append("<ID>Niuluo.y.1001</ID>	"); // 无证书客户可上送空
            sb.Append("<TranDate>" + tranDate + "</TranDate>"); // ERP系统产生的交易日期,格式是yyyyMMdd
            sb.Append("<TranTime>" + tranTime + "</TranTime>"); // ERP系统产生的交易时间,格式如HHmmssSSS,精确到毫秒
            sb.Append("<fSeqno>" + seqno + "</fSeqno>");        // ERP系统产生的指令包序列号,一个集团永远不能重复
            sb.Append("</pub>");
            sb.Append("<in>");
            sb.Append("<TranType>0</TranType>");                                                        // 0:退货 1:返还  2:转付
            sb.Append("<ShopType>2</ShopType>");                                                        // 1:B2B商城  2:B2C商城
            sb.Append("<ShopCode>" + ConfigurationManager.AppSettings["icbc.merID"] + "</ShopCode>");   // 商城代码
            sb.Append("<ShopAcct>" + ConfigurationManager.AppSettings["icbc.merAcct"] + "</ShopAcct>"); // 商城账号 银联订单不可以上送
            sb.Append("<OrderNum>" + out_trade_no + "</OrderNum>");
            sb.Append("<PayType></PayType>");                                                           // 0:普通 1:加急(B2C交易可为空)
            sb.Append("<PayDate>" + out_trade_no.Substring(0, 8) + "</PayDate>");                       // yyyyMMdd(支持14位日期,前8位生效)
            sb.Append("<TransferName></TransferName>");                                                 // B2C转付操作时必输,其他情况为空
            sb.Append("<TransferAccNo></TransferAccNo>");                                               // B2C转付操作时必输,其他情况为空
            sb.Append("<PayAmt>" + amount + "</PayAmt>");                                               // 以分为单位,B2C转付交易时需控制单笔金额(<=5万元), 融e购订单复用为现金支付金额
            sb.Append("<SignTime>" + signTime + "</SignTime>");                                         // 格式是yyyyMMddHHmmssSSS
            sb.Append("<ReqReserved1></ReqReserved1>");                                                 // 退货时,可输入退货原因
            sb.Append("<ReqReserved2></ReqReserved2>	"); // 融e购订单复用为退货积分值|退货积分等值金额|退货积分对应商户金额
            sb.Append("<AcctSeq></AcctSeq>	");
            sb.Append("</in>");
            sb.Append("</eb>");
            sb.Append("</CMS>");

            try
            {
                // NC签名数据处理
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[]        data     = encoding.GetBytes(sb.ToString());

                WebRequest     webRequest  = WebRequest.Create("http://www.niuluo-tech.cn:95");
                HttpWebRequest httpRequest = webRequest as HttpWebRequest;
                httpRequest.KeepAlive     = true;
                httpRequest.UserAgent     = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
                httpRequest.ContentType   = "INFOSEC_SIGN/1.0";
                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();
                }

                WebApiConfig.log.Info("NC签名数据:" + stringResponse);
                var beginSign = 0;
                var endSign   = 0;
                try
                {
                    beginSign = stringResponse.IndexOf("<sign>") + 6;
                    endSign   = stringResponse.IndexOf("</sign>");
                    WebApiConfig.log.Info("NC签名结果:" + beginSign + "&" + endSign);
                }
                catch (Exception e)
                {
                    WebApiConfig.log.Error("!!!!!!!!!!接收签名数据失败,请检查nc设置!!!!!!!!!!");
                }

                // NC工行退款
                var repSignContent = stringResponse.Substring(beginSign, endSign - beginSign);
                var queryParams    = new StringBuilder();
                queryParams.Append("Version=0.0.1.0");
                queryParams.Append("&TransCode=EBUSCOM");
                queryParams.Append("&BankCode=102");
                queryParams.Append("&GroupCIS=100190013673155");
                queryParams.Append("&ID=Niuluo.y.1001");          // 客户的证书ID(无证书客户可空)
                queryParams.Append("&PackageID=" + seqno);        // 客户的指令包序列号(由客户ERP系统产生,不可重复)
                queryParams.Append("&Cert=");
                queryParams.Append("&reqData=" + repSignContent); // 客户的xml请求数据
                ASCIIEncoding  encoding2    = new ASCIIEncoding();
                byte[]         data2        = encoding.GetBytes(queryParams.ToString());
                WebRequest     webRequest2  = WebRequest.Create("http://www.niuluo-tech.cn:94/servlet/ICBCCMPAPIReqServlet?PackageID=" + seqno + "&SendTime=" + now.ToString("yyyyMMddHHmmss"));
                HttpWebRequest httpRequest2 = webRequest2 as HttpWebRequest;
                httpRequest2.KeepAlive     = true;
                httpRequest2.UserAgent     = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
                httpRequest2.ContentType   = "application/x-www-form-urlencoded";
                httpRequest2.Method        = "POST";
                httpRequest2.ContentLength = data2.Length;
                Stream requestStream2 = httpRequest2.GetRequestStream();
                requestStream2.Write(data2, 0, data2.Length);
                requestStream2.Close();
                Stream responseStream2 = null;
                responseStream2 = httpRequest2.GetResponse().GetResponseStream();
                string stringResponse2 = string.Empty;
                if (responseStream2 != null)
                {
                    using (StreamReader responseReader =
                               new StreamReader(responseStream2, Encoding.GetEncoding("GBK")))
                    {
                        stringResponse2 = responseReader.ReadToEnd();
                    }

                    responseStream2.Close();
                }

                WebApiConfig.log.Info("工行退费返回结果:" + stringResponse2);
                if (stringResponse2.IndexOf("errorCode=") > -1)
                {
                    WebApiConfig.log.Error("工行退费失败:" + stringResponse2);
                    throw new Exception("工行退费失败");
                }
                else if (stringResponse2.IndexOf("reqData=") > -1)
                {
                    var returnStr = CBCPayOnline.Decode(stringResponse2.Replace("reqData=", string.Empty), "gbk");
                    WebApiConfig.log.Info("工行退费返回xml:" + returnStr);
                    var doc        = XDocument.Parse(returnStr);
                    var orderNum   = doc.Descendants("OrderNum").FirstOrDefault().Value;
                    var resultCode = doc.Descendants("Result").FirstOrDefault().Value;
                    var fee        = doc.Descendants("PayAmt").FirstOrDefault().Value;
                    var retCode    = doc.Descendants("fSeqno").FirstOrDefault().Value;
                    if (orderNum != out_trade_no)
                    {
                        WebApiConfig.log.Error("工行退费订单号不匹配:" + orderNum + "&" + out_trade_no);
                        throw new Exception("工行退费交易不成功");
                    }
                    else if (resultCode != "1")
                    {
                        var retMsg = doc.Descendants("RetMsg").FirstOrDefault().Value;
                        WebApiConfig.log.Error("工行退费交易不成功:" + orderNum + "&" + resultCode + ":" + retMsg);
                        throw new Exception("工行退费交易不成功");
                    }
                    else
                    {
                        WebApiConfig.log.Info("工行退费成功:" + orderNum + "&" + fee + ":" + retCode);
                        JObject localResult;
                        if (!TradeBusiness.UpdateTradeStatusSeveralTimes(out_trade_no, result.GetValue("ActualTradeNo").ToString(), PaymentType.ICBC, string.Empty, TradeStatus.REFUND, out localResult))
                        {
                            WebApiConfig.log.Error("工行退费成功,本地数据库状态维护失败:" + orderNum);
                            return(new JObject(new JProperty("Trade", result)));
                        }
                        else
                        {
                            return(new JObject(new JProperty("Trade", localResult)));
                        }
                    }
                }
                else
                {
                    WebApiConfig.log.Error("工行退费结果未知:" + stringResponse2);
                    throw new Exception("工行退费失败");
                }
            }
            catch (Exception e)
            {
                WebApiConfig.log.Error(e.Message);
                WebApiConfig.log.Error("退费出错:" + e.ToString());
            }

            return(null);
        }