Ejemplo n.º 1
0
        //功能函数。将变量值不为空的参数组成字符串。结束
        /// <summary>
        /// 接收返回的信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ReceivePki()
        {
            // payState & strPayMsg & strNumber 记录快钱支付结果
            bool payState = false;              // 快钱支付状态
            string strPayMsg = string.Empty;    // 支付结果
            string strNumber = string.Empty;    // 订单号
            try
            {
                //获取人民币网关账户号
                string merchantAcctId = Request["merchantAcctId"].ToString().Trim();

                //获取网关版本.固定值
                ///快钱会根据版本号来调用对应的接口处理程序。
                ///本代码版本号固定为v2.0
                string version = Request["version"].ToString().Trim();

                //获取语言种类.固定选择值。
                ///只能选择1、2、3
                ///1代表中文;2代表英文
                ///默认值为1
                string language = Request["language"].ToString().Trim();

                //签名类型.固定值
                ///1代表MD5签名
                ///当前版本固定为1
                string signType = Request["signType"].ToString().Trim();

                //获取支付方式
                ///值为:10、11、12、13、14
                ///00:组合支付(网关支付页面显示快钱支持的各种支付方式,推荐使用)10:银行卡支付(网关支付页面只显示银行卡支付).11:电话银行支付(网关支付页面只显示电话支付).12:快钱账户支付(网关支付页面只显示快钱账户支付).13:线下支付(网关支付页面只显示线下支付方式).14:B2B支付(网关支付页面只显示B2B支付,但需要向快钱申请开通才能使用)
                string payType = Request["payType"].ToString().Trim();

                //获取银行代码
                ///参见银行代码列表
                string bankId = Request["bankId"].ToString().Trim();

                //获取商户订单号
                string orderId = Request["orderId"].ToString().Trim();

                //获取订单提交时间
                ///获取商户提交订单时的时间.14位数字。年[4位]月[2位]日[2位]时[2位]分[2位]秒[2位]
                ///如:20080101010101
                string orderTime = Request["orderTime"].ToString().Trim();

                //获取原始订单金额
                ///订单提交到快钱时的金额,单位为分。
                ///比方2 ,代表0.02元
                string orderAmount = Request["orderAmount"].ToString().Trim();

                //获取快钱交易号
                ///获取该交易在快钱的交易号
                string dealId = Request["dealId"].ToString().Trim();

                //获取银行交易号
                ///如果使用银行卡支付时,在银行的交易号。如不是通过银行支付,则为空
                string bankDealId = Request["bankDealId"].ToString().Trim();

                //获取在快钱交易时间
                ///14位数字。年[4位]月[2位]日[2位]时[2位]分[2位]秒[2位]
                ///如;20080101010101
                string dealTime = Request["dealTime"].ToString().Trim();

                //获取实际支付金额
                ///单位为分
                ///比方 2 ,代表0.02元
                string payAmount = Request["payAmount"].ToString().Trim();

                //获取交易手续费
                ///单位为分
                ///比方 2 ,代表0.02元
                string fee = Request["fee"].ToString().Trim();

                //获取扩展字段1
                string ext1 = Request["ext1"].ToString().Trim();

                //获取扩展字段2
                string ext2 = Request["ext2"].ToString().Trim();

                //获取处理结果
                ///10代表 成功; 11代表 失败
                string payResult = Request["payResult"].ToString().Trim();

                //获取错误代码
                ///详细见文档错误代码列表
                string errCode = Request["errCode"].ToString().Trim();

                //获取加密签名串
                string signMsg = Request["signMsg"].ToString().Trim();

                //生成加密串。必须保持如下顺序。
                string merchantSignMsgVal = "";
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "merchantAcctId", merchantAcctId);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "version", version);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "language", language);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "signType", signType);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "payType", payType);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "bankId", bankId);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "orderId", orderId);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "orderTime", orderTime);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "orderAmount", orderAmount);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "dealId", dealId);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "bankDealId", bankDealId);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "dealTime", dealTime);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "payAmount", payAmount);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "fee", fee);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "ext1", ext1);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "ext2", ext2);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "payResult", payResult);
                merchantSignMsgVal = appendParam(merchantSignMsgVal, "errCode", errCode);

                strNumber = orderId;
                //商家进行数据处理,并跳转会商家显示支付结果的页面
                ///首先进行签名字符串验证

                string pubkey_path = this.Server.MapPath(@"/99bill/99bill.cert.rsa.20140728.cer");//快钱公钥证书路径
                string CertificatePW = "gou19jiu19wang19";//存放公钥的证书密码
                if (CerRSAVerifySignature(merchantSignMsgVal, signMsg, pubkey_path, CertificatePW, 2))
                {
                    switch (payResult)
                    {
                        case "10":
                            /*
                             ' 商户网站逻辑处理,比方更新订单支付状态为成功
                            ' 特别注意:只有signMsg.ToUpper() == merchantSignMsg.ToUpper(),且payResult=10,才表示支付成功!
                             * 因为快钱会重复通知这个页面,首先判断订单是否已经更新,没有更新做更新有则不做更新,
                             * 同时将返回的付款金额payamount与提交订单前的订单金额进行对比校验,如果一致则更新订单。
                            */

                            //报告给快钱处理结果,并提供将要重定向的地址。

                            //Common.DataContext ctx = new Common.DataContext();
                            //Model.odr_Order_S os = new Model.odr_Order_S();
                            //os.Action = 14;
                            //os.Number = orderId;
                            //BLL.Order od = new BLL.Order();
                            //DataTable dt = od.GetOrderInfo(ctx, os);
                            //double totalFee = 0;
                            var orderSevice = new OrderService(this.UserSession.UserID, false);
                            var order = orderSevice.QueryByOrderCode(orderId);

                            //todo:此处应验证订单支付金额是否正确,测试使用1分
                            //if (double.Parse(payAmount) == 1)
                            double payMoney = 0.00;
                            if (!double.TryParse(payAmount, out payMoney))
                            {
                                LogUtils.Log("快钱返回支付金额异常,异常金额为:"+payAmount);
                            }
                            else if (payMoney == Math.Round(order.DeliveryCost + order.TotalMoney, 2) * 100)
                            {
                                if (order.PaymentStatus != 1) //需要更新订单状态信息
                                {
                                    ViewBag.Message = "快钱--支付成功,"
                                                      + string.Format(
                                                          "订单号:{0},快钱交易号:{1},支付金额:{2}",
                                                          order.OrderCode,
                                                          dealId,
                                                          payAmount);

                                    if (order.PaymentStatus != 1)
                                    {
                                        order.PaymentStatus = 1;
                                        order.Status = order.Status == 255 ? 0 : order.Status;
                                        if (orderSevice.OrderOnLinePayment(
                                            order,
                                            Math.Round(payMoney/100,2), //转为元单位
                                            7,
                                            dealId))
                                        {
                                            LogUtils.Log(
                                                string.Format(
                                                    "订单支付成功,订单状态更新成功,订单号:{0},快钱交易号:{1},支付金额:{2}",
                                                    order.OrderCode,
                                                    dealId,
                                                    payAmount),
                                                "快钱支付--支付通知",
                                                Category.Info,
                                                this.Session.SessionID,
                                                this.UserSession.UserID,
                                                "Tenpay/Return");
                                        }
                                        else
                                        {
                                            ViewBag.Message = "快钱通--支付成功,"
                                                              + string.Format(
                                                                  "订单状态更新失败,为了保护您的权益,请立即与客服人员联系。订单号:{0},快钱交易号:{1},支付金额:{2}",
                                                                  order.OrderCode,
                                                                  dealId,
                                                                  payAmount);
                                            LogUtils.Log(
                                                string.Format(
                                                    "订单支付成功,订单状态更新失败,订单号:{0},快钱交易号:{1},支付金额:{2}",
                                                    order.OrderCode,
                                                    dealId,
                                                    payAmount),
                                                "快钱支付--支付通知",
                                                Category.Info,
                                                this.Session.SessionID,
                                                this.UserSession.UserID,
                                                "Tenpay/Return");
                                        }
                                    }

                                    //判断是否以插入流程
                                    //bool isExists = Controllers.Logic.CartCircuit.IsExists(oc.Id, 1);
                                    //if (isExists)
                                    //{
                                    //    Controllers.Logic.Cart.SetPayWay(oc.Number);
                                    //}
                                }
                                payState = true;

                                //报告给快钱处理结果,并提供将要重定向的地址。
                                rtnOk = "1";
                                rtnUrl = ConstantParams.SiteUrl + "_99Bill/Success";
                            }
                            else
                            {
                                strPayMsg = "支付金额不匹配(" + double.Parse(payAmount).ToString() + "!=" + Math.Round((order.TotalMoney + order.DeliveryCost) * 100, 0).
                                            ToString() + "),订单号:" + orderId + ",快钱交易号:" + dealId + ",银行交易号:" + bankDealId;
                                LogUtils.Log(strPayMsg,"快钱支付通知",Category.Error,this.Session.SessionID,this.UserSession.UserID,"_99Bill/ReceivePki");
                                ViewBag.Message = "订单支付金额异常,为了保护您的权益,请立即与客服人员联系。"
                                                  + string.Format(
                                                      "订单号:{0},快钱交易号:{1},已支付金额:{2},应支付金额{3}",
                                                      orderId,
                                                      dealId,
                                                      Math.Round(Convert.ToDouble(payAmount) / (double)100, 2),
                                                      order.DeliveryCost + order.TotalMoney);

                                rtnUrl = ConstantParams.SiteUrl + "_99Bill/Fail";
                            }
                            break;
                        default:
                            strPayMsg = "支付结果:支付失败";
                            rtnOk = "1";
                            rtnUrl = ConstantParams.SiteUrl + "_99Bill/Fail"; //Common.Constant.SiteUrl + "/purchase/Success-Number-" + orderId + "-Msg-1.htm";
                            break;
                    }
                }
                else//验签失败
                {
                    strPayMsg = "验签失败";
                    rtnOk = "0";
                    rtnUrl = ConstantParams.SiteUrl + "_99Bill/Fail";//Common.Constant.SiteUrl + "/purchase/Success-Number-" + orderId + "-Msg-1.htm";
                }
            }
            catch (Exception ex)
            {
                strPayMsg = strPayMsg + ex.Message;
                rtnOk = "0";
                rtnUrl = ConstantParams.SiteUrl + "_99Bill/Fail";
            }
            finally
            {
                if (!payState)
                {
                    if (!string.IsNullOrEmpty(strNumber))
                    {
                        strPayMsg = "(Number:" + strNumber + ")" + strPayMsg;
                    }
                    strPayMsg = "快钱支付异常," + strPayMsg;
                    if (strPayMsg.Length > 200)
                    {
                        strPayMsg = strPayMsg.Substring(0, 200);
                    }
                    //log.AddLog("odr_order", 0, strPayMsg);
                    LogUtils.Log(
                        strPayMsg,
                        "快钱支付--支付通知",
                        Category.Info,
                        this.Session.SessionID,
                        this.UserSession.UserID,
                        "Tenpay/Return");
                }
            }

            ViewBag.rtnOk = rtnOk;
            ViewBag.rtnUrl = rtnUrl;
            return this.View("ReceivePki");
        }
Ejemplo n.º 2
0
        public ActionResult Return()
        {
            try
            {
                ArrayList sArrary = GetRequestGet();
                ///////////////////////以下参数是需要设置的相关配置参数,设置后不会更改的//////////////////////
                string partner = "2088301856479212"; //合作身份者ID
                string key = "sji2sos0koz072vl07sg5xvtctvq6hfp"; //安全检验码
                string input_charset = "utf-8"; //字符编码格式 目前支持 gb2312 或 utf-8
                string sign_type = "MD5"; //加密方式 不需修改
                string transport = "http"; //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
                //////////////////////////////////////////////////////////////////////////////////////////////

                if (sArrary.Count > 0) //判断是否有带返回参数
                {
                    AlipayClass.AlipayNotify aliNotify = new AlipayClass.AlipayNotify(
                        sArrary,
                        Request.QueryString["notify_id"],
                        partner,
                        key,
                        input_charset,
                        sign_type,
                        transport);

                    string responseTxt = aliNotify.ResponseTxt; //获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求
                    string sign = Request.QueryString["sign"]; //获取支付宝反馈回来的sign结果
                    string mysign = aliNotify.Mysign; //获取通知返回后计算后(验证)的加密结果

                    //写日志记录(若要调试,请取消下面两行注释)
                    //string sWord = "responseTxt=" + responseTxt + "\n return_url_log:sign=" + Request.QueryString["sign"] + "&mysign=" + mysign + "\n return回来的参数:" + AlipayFunction.Create_linkstring(sArrary);
                    //AlipayFunction.log_result(Server.MapPath("log/" + DateTime.Now.ToString().Replace(":", "")) + ".txt", sWord);

                    //判断responsetTxt是否为ture,生成的签名结果mysign与获得的签名结果sign是否一致
                    //responsetTxt的结果不是true,与服务器设置问题、合作身份者ID、notify_id一分钟失效有关
                    //mysign与sign不等,与安全校验码、请求时的参数格式(如:带自定义参数等)、编码格式有关
                    string order_code = Request.QueryString["out_trade_no"]; //获取订单号
                    if (responseTxt == "true" && sign == mysign) //验证成功
                    {
                        //获取支付宝的通知返回参数
                        string trade_no = Request.QueryString["trade_no"]; //支付宝交易号
                        string total_fee = Request.QueryString["total_fee"]; //获取总金额
                        string subject = Request.QueryString["subject"]; //商品名称、订单名称
                        string body = Request.QueryString["body"]; //商品描述、订单备注、描述
                        string buyer_email = Request.QueryString["buyer_email"]; //买家支付宝账号
                        string trade_status = Request.QueryString["trade_status"]; //交易状态
                        int sOld_trade_status = 0; //获取商户数据库中查询得到该笔交易当前的交易状态

                        //验证此第三方交易是否已经处理,若是,则不进行重复处理,否则,更新订单信息

                        var paymentList = new OrderPaymentService().QueryByTradeNo(trade_no);
                        if (paymentList != null && paymentList.Count > 0)
                        {
                            this.ViewBag.Message = "支付成功,订单号:" + order_code;
                            this.ViewBag.Money = paymentList.Sum(p => p.PaymentMoney);
                            return this.View("Success");
                        }

                        double totalFee = 0;
                        var orderSevice = new OrderService(this.UserSession.UserID, false);
                        var order = orderSevice.QueryByOrderCode(order_code);
                        if (!double.TryParse(total_fee, out totalFee))
                        {
                            LogUtils.Log(
                                string.Format(
                                    "支付宝支付返回支付金额异常,支付宝交易号{0},购酒网订单号{1},支付金额{2},买家支付宝账号{3},交易状态{4}",
                                    trade_no,
                                    order_code,
                                    total_fee,
                                    buyer_email,
                                    trade_status),
                                "支付宝交易通知",
                                Category.Error,
                                this.UserSession.SessionId);
                            this.ViewBag.Message = "支付异常,请速与我司客服联系处理。订单号:" + order_code;
                            return this.View("Success");
                        }

                        if (order == null)
                        {
                            LogUtils.Log(
                                string.Format(
                                    "支付宝支付返回时获取订单信息异常,支付宝交易号{0},购酒网订单号{1},支付金额{2},买家支付宝账号{3},交易状态{4}",
                                    trade_no,
                                    order_code,
                                    total_fee,
                                    buyer_email,
                                    trade_status),
                                "支付宝交易通知",
                                Category.Error,
                                this.UserSession.SessionId);
                            this.ViewBag.Message = "支付异常,请速与我司客服联系处理。订单号:" + order_code;
                            return this.View("Success");
                        }

                        //if (totalFee != 0.01)
                        if (totalFee != (order.TotalMoney + order.DeliveryCost))
                        {
                            LogUtils.Log(
                                string.Format(
                                    "支付宝支付返回支付金额异常,支付宝交易号{0},购酒网订单号{1},已支付金额{2},订单应支付金额{3},买家支付宝账号{4},交易状态{5}",
                                    trade_no,
                                    order_code,
                                    total_fee,
                                    order.TotalMoney + order.DeliveryCost,
                                    buyer_email,
                                    trade_status),
                                "支付宝交易通知",
                                Category.Error,
                                this.UserSession.SessionId);

                            this.ViewBag.Message = "支付异常,请速与我司客服联系,确认订单状态。订单号:" + order_code;
                            return this.View("Success");
                        }

                        //支付成功,改写数据库订单信息
                        //添加支付记录信息
                        //添加订单状态跟踪信息
                        order.PaymentStatus = 1;
                        order.Status = order.Status == 255 ? 0 : order.Status;
                        orderSevice.OrderOnLinePayment(order, totalFee, 4, trade_no);
                        this.ViewBag.Money = totalFee;
                        this.ViewBag.Message = "订单支付成功,订单号:" + order_code;
                        return this.View("Success");

                    }
                    else //验证失败
                    {
                        //lbVerify.Text = "验证失败";
                        //logpay = new BLL.AdminLog();
                        string strMsg =
                            string.Format(
                                "(Page:Return支付宝支付异常)验证失败(responseTxt:{0},sign:{1},mysign:{2},number:{3})",
                                responseTxt,
                                sign,
                                mysign,
                                order_code);
                        //logpay.AddLog("odr_order", 0, strMsg);
                        //Response.Redirect(
                        //    Common.Constant.SiteUrl + "/purchase/Success-Number-" + Request.QueryString["out_trade_no"]
                        //    + "-Msg-1.htm");
                        this.ViewBag.Message = strMsg;
                        return this.View("Success");
                    }
                }
                else
                {
                    //在处理你的请求时出错
                    //lbVerify.Text = "无返回参数";
                    //logpay = new BLL.AdminLog();
                    //logpay.AddLog("odr_order", 0, "(Page:Return支付宝支付异常)" + lbVerify.Text);
                    //Response.Redirect(
                    //    Common.Constant.SiteUrl + "/purchase/Success-Number-" + Request.QueryString["out_trade_no"]
                    //    + "-Msg-1.htm");
                    this.ViewBag.Message = "支付失败,无返回参数";
                    return this.View("Success");
                }
            }
            catch (Exception exception)
            {
                LogUtils.Log(
                    "支付宝支付完成跳转回时产生异常,异常消息:" + exception.Message + ",InnerException:" + exception.InnerException + ",堆栈:"
                    + exception.StackTrace,
                    "[Order] 订单支付宝支付",
                    Category.Error);
                return this.View("Success");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 通知
        /// </summary>
        /// 01. 多次支付问题:在未取得支付宝确认支付消息之前,须控制再次支付情况。
        /// 02. 大额支付问题:支付宝存在支付限额,在此情况下,大单支付须拆分到多次支付单中。
        /// <returns></returns>
        public ActionResult Notify()
        {
            LogUtils.Log(
                    string.Format("支付宝支付--接收到支付宝异步通知。准备处理订单支付信息"),
                    "支付宝通知",
                    Category.Info,
                    this.Session.SessionID,
                    this.UserSession.UserID,
                    "Alipay");

            ArrayList sArrary = GetRequestPost();
            string partner = "2088301856479212";		//合作身份者ID
            string key = "sji2sos0koz072vl07sg5xvtctvq6hfp";			//安全检验码
            string input_charset = "utf-8";                     //字符编码格式 目前支持 gb2312 或 utf-8
            string sign_type = "MD5";                           //加密方式 不需修改
            string transport = "http";                         //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http

            if (sArrary.Count > 0)//判断是否有带返回参数
            {
                AlipayClass.AlipayNotify aliNotify = new AlipayClass.AlipayNotify(sArrary, Request.Form["notify_id"], partner, key, input_charset, sign_type, transport);
                string responseTxt = aliNotify.ResponseTxt; //获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求
                string sign = Request.Form["sign"];         //获取支付宝反馈回来的sign结果
                string mysign = aliNotify.Mysign;           //获取通知返回后计算后(验证)的加密结果

                //写日志记录(若要调试,请取消下面两行注释)
                //string sWord = "responseTxt=" + responseTxt + "\n notify_url_log:sign=" + Request.Form["sign"] + "&mysign=" + mysign + "\n notify回来的参数:" + AlipayFunction.Create_linkstring(sArrary);
                //AlipayFunction.log_result(Server.MapPath("log/" + DateTime.Now.ToString().Replace(":", "")) + ".txt", sWord);

                //判断responsetTxt是否为ture,生成的签名结果mysign与获得的签名结果sign是否一致
                //responsetTxt的结果不是true,与服务器设置问题、合作身份者ID、notify_id一分钟失效有关
                //mysign与sign不等,与安全校验码、请求时的参数格式(如:带自定义参数等)、编码格式有关
                string order_code = Request.Form["out_trade_no"];     //获取订单号
                if (responseTxt == "true" && sign == mysign)//验证成功
                {
                    //获取支付宝的通知返回参数
                    string trade_no = Request.Form["trade_no"];         //支付宝交易号
                    string total_fee = Request.Form["total_fee"];       //获取总金额
                    string subject = Request.Form["subject"];           //商品名称、订单名称
                    string body = Request.Form["body"];                 //商品描述、订单备注、描述
                    string buyer_email = Request.Form["buyer_email"];   //买家支付宝账号
                    string trade_status = Request.Form["trade_status"]; //交易状态
                    //int sOld_trade_status = 0;						//获取商户数据库中查询得到该笔交易当前的交易状态

                    //检查此交易在我系统是否已处理,若是,则不进行其他处理
                    var paymentList = new OrderPaymentService().QueryByTradeNo(trade_no);

                    if (paymentList != null && paymentList.Count > 0)
                    {
                        this.ViewBag.Message = "支付成功,订单号:" + trade_no;
                        return this.Content("Success");
                    }

                    if (Request.Form["trade_status"] == "TRADE_FINISHED" || Request.Form["trade_status"] == "TRADE_SUCCESS")
                    {
                        //放入订单交易完成后的数据库更新程序代码,请务必保证response.Write出来的信息只有success
                        //为了保证不被重复调用,或重复执行数据库更新程序,请判断该笔交易状态是否是订单未处理状态
                        double totalFee = 0;
                        var orderSevice = new OrderService(this.UserSession.UserID, false);
                        var order = orderSevice.QueryByOrderCode(order_code);
                        if (!double.TryParse(total_fee, out totalFee))
                        {
                            LogUtils.Log(
                                string.Format(
                                    "支付宝支付返回支付金额异常,支付宝交易号{0},购酒网订单号{1},支付金额{2},买家支付宝账号{3},交易状态{4}",
                                    trade_no,
                                    order_code,
                                    total_fee,
                                    buyer_email,
                                    trade_status),
                                "支付宝交易通知",
                                Category.Error,
                                this.UserSession.SessionId);
                            return this.Content("failed");
                        }

                        if (order == null)
                        {
                            LogUtils.Log(
                                string.Format(
                                    "支付宝支付返回时获取订单信息异常,支付宝交易号{0},购酒网订单号{1},支付金额{2},买家支付宝账号{3},交易状态{4}",
                                    trade_no,
                                    order_code,
                                    total_fee,
                                    buyer_email,
                                    trade_status),
                                "支付宝交易通知",
                                Category.Error,
                                this.UserSession.SessionId);
                            return this.Content("Success"); //作为成功信息进行返回,防止支付宝服务器反复请求
                        }

                        //测试使用0.01作为支付金额
                        if (totalFee != (order.TotalMoney + order.DeliveryCost))
                        //if (totalFee != 0.01)
                        {
                            LogUtils.Log(
                                string.Format(
                                    "支付宝支付返回支付金额异常,支付宝交易号{0},购酒网订单号{1},支付金额{2},买家支付宝账号{3},交易状态{4}",
                                    trade_no,
                                    order_code,
                                    total_fee,
                                    buyer_email,
                                    trade_status),
                                "支付宝交易通知",
                                Category.Error,
                                this.UserSession.SessionId);
                            return this.Content("failed");
                        }

                        //支付成功,改写数据库订单信息
                        //添加支付记录信息
                        //添加订单状态跟踪信息
                        order.PaymentStatus = 1;
                        order.Status = order.Status == 255 ? 0 : order.Status;
                        if (orderSevice.OrderOnLinePayment(order, totalFee, 4, trade_no))
                        {
                            return this.Content("Success");
                        }
                        else
                        {
                            return this.Content("Fail");
                        }

                        //Response.Redirect(Common.Constant.SiteUrl + "/purchase/Success-Number-" + order_no + "-Msg-1.htm");
                    }
                    else
                    {
                        return this.Content("Success");  //其他状态判断。普通即时到帐中,其他状态不用判断,直接打印success。
                        //logpay = new BLL.AdminLog();
                        //logpay.AddLog("odr_order", 0, "(Page:notify支付宝支付异常)交易状态:" + Request.Form["trade_status"].ToString());
                        //Response.Redirect(Common.Constant.SiteUrl + "/purchase/Success-Number-" + order_no + "-Msg-1.htm");
                    }
                }
                else//验证失败
                {
                    return this.Content("fail");
                    //logpay = new BLL.AdminLog();
                    //string strMsg = string.Format("(Page:notify支付宝支付异常)验证失败(responseTxt:{0},sign:{1},mysign:{2},number:{3})", responseTxt, sign, mysign, order_no);
                    //logpay.AddLog("odr_order", 0, strMsg);
                    //Response.Redirect(Common.Constant.SiteUrl + "/purchase/Success-Number-" + Request.Form["out_trade_no"] + "-Msg-1.htm");
                }
            }
            else
            {
                LogUtils.Log("支付宝支付异步通知无返回参数");
                return this.Content("无通知参数");
                //logpay = new BLL.AdminLog();
                //logpay.AddLog("odr_order", 0, "(Page:notify支付宝支付异常)无通知参数");
            }
        }
Ejemplo n.º 4
0
        public ActionResult Return()
        {
            LogUtils.Log("接收财付通支付通知消息,订单","财付通支付--支付通知", Category.Info, this.Session.SessionID, this.UserSession.UserID, "Tenpay/Return");

            //密钥
            string key = "f24967df0075851d22411c36b04899ac";

            //创建PayResponseHandler实例
            PayResponseHandler resHandler = new PayResponseHandler(System.Web.HttpContext.Current);

            resHandler.setKey(key);

            string order_code = resHandler.getParameter("sp_billno");

            //判断签名
            if (resHandler.isTenpaySign())
            {
                LogUtils.Log("判断签名合法", "财付通支付--支付通知", Category.Info, this.Session.SessionID, this.UserSession.UserID, "Tenpay/Return");

                //交易单号
                string transaction_id = resHandler.getParameter("transaction_id");

                //支付金额,以分为单位
                string total_fee = resHandler.getParameter("total_fee");

                //支付结果
                string pay_result = resHandler.getParameter("pay_result");

                if (pay_result == "0") //0-已支付,1-未支付,其他为支付错误
                {
                    LogUtils.Log("订单支付成功"+string.Format("订单号:{0},财付通交易号:{1},支付金额:{2}",order_code,transaction_id,total_fee), "财付通支付--支付通知", Category.Info, this.Session.SessionID, this.UserSession.UserID, "Tenpay/Return");
                    //------------------------------
                    //处理业务开始
                    //------------------------------

                    //注意交易单不要重复处理
                    //注意判断返回金额

                    //Common.DataContext ctx = new Common.DataContext();
                    //Model.odr_Order_S os = new Model.odr_Order_S();
                    //os.Action = 14;
                    //os.Number = strNumber;
                    //BLL.Order od = new BLL.Order();
                    //DataTable dt = od.GetOrderInfo(ctx, os);

                    //double totalFee = 0;
                    var orderSevice = new OrderService(this.UserSession.UserID, false);
                    var order = orderSevice.QueryByOrderCode(order_code);

                    //todo:此处应验证订单支付金额是否正确,测试使用1分
                    //if (double.Parse(total_fee) == 1)
                    if (double.Parse(total_fee) == (Math.Round(order.DeliveryCost + order.TotalMoney, 2) * 100))
                    {
                        ViewBag.Message = "财富通--支付成功,"
                                          + string.Format(
                                              "订单号:{0},财付通交易号:{1},支付金额:{2}",
                                              order.OrderCode,
                                              transaction_id,
                                              total_fee);

                        if (order.PaymentStatus != 1)
                        {
                            order.PaymentStatus = 1;
                            order.Status = order.Status == 255 ? 0 : order.Status;
                            //默认5为财富通
                            if (orderSevice.OrderOnLinePayment(order, Math.Round(double.Parse(total_fee)/100,2), 5, transaction_id))
                            {
                                LogUtils.Log(
                                    string.Format(
                                        "订单支付成功,订单状态更新成功,订单号:{0},财付通交易号:{1},支付金额:{2}",
                                        order.OrderCode,
                                        transaction_id,
                                        total_fee),
                                    "财付通支付--支付通知",
                                    Category.Info,
                                    this.Session.SessionID,
                                    this.UserSession.UserID,
                                    "Tenpay/Return");
                            }
                            else
                            {
                                ViewBag.Message = "财富通--支付成功,"
                                                  + string.Format(
                                                      "订单状态更新失败,为了保护您的权益,请立即与客服人员联系。订单号:{0},财付通交易号:{1},支付金额:{2}",
                                                      order.OrderCode,
                                                      transaction_id,
                                                      total_fee);
                                LogUtils.Log(
                                    string.Format(
                                        "订单支付成功,订单状态更新失败,订单号:{0},财付通交易号:{1},支付金额:{2}",
                                        order.OrderCode,
                                        transaction_id,
                                        total_fee),
                                    "财付通支付--支付通知",
                                    Category.Info,
                                    this.Session.SessionID,
                                    this.UserSession.UserID,
                                    "Tenpay/Return");
                            }
                        }
                        ViewBag.Money = total_fee;
                        ViewBag.Url = ConstantParams.SiteUrl + "Tenpay/Success";
                    }
                }
                else
                {
                    LogUtils.Log("订单支付失败(参数指示未支付),"+string.Format("订单号:{0},财付通交易号:{1},支付金额:{2}",order_code,transaction_id,total_fee), "财付通支付--支付通知", Category.Info, this.Session.SessionID, this.UserSession.UserID, "Tenpay/Return");

                    //当做不成功处理
                    ViewBag.Message = "订单支付失败,"
                                      + string.Format(
                                          "订单号:{0},财付通交易号:{1},支付金额:{2}",
                                          order_code,
                                          transaction_id,
                                          total_fee);

                    //Response.Redirect(CONST.Url + "/purchase/PayFinish-Number-" + strNumber + "-Msg-1.htm");
                }

                if (ViewBag.Url == null)
                {
                    ViewBag.Url = ConstantParams.SiteUrl + "Tenpay/Fail";
                }

                return this.View("Return");
            }
            else
            {
                LogUtils.Log("认证签名失败,订单编号:"+order_code, "财付通支付--支付通知", Category.Info, this.Session.SessionID, this.UserSession.UserID, "Tenpay/Return");

                //当做不成功处理
                ViewBag.Message = "订单支付失败," + string.Format("订单号:{0}", order_code);

                return null; //不处理,等待下一次通知
                //Response.Redirect(CONST.Url + "/purchase/PayFinish-Number-" + strNumber + "-Msg-1.htm");
                //string debugInfo = resHandler.getDebugInfo();
                //Response.Write("<br/>debugInfo:" + debugInfo);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 检查订单是否重复
 /// </summary>
 /// <param name="orderCode">订单编号</param>
 /// <returns></returns>
 private static bool CheckOrderCodeIsOnly(string orderCode)
 {
     orderService = new OrderService();
     var order = orderService.QueryByOrderCode(orderCode);
     if (order != null)
     {
         if (order.OrderCode == orderCode)
         {
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 6
0
        public ActionResult API()
        {
            string orderCode = Server.UrlDecode(Request.QueryString["orderid"]);//订单编号
            string strMsg = "";
            SqlTransaction transaction = null;

            try
            {
                string username = Server.UrlDecode(Request.QueryString["username"]);//ERP昵称
                string password = Server.UrlDecode(Request.QueryString["password"]);//ERP密码
                string key = Server.UrlDecode(Request.QueryString["key"]);//公钥
                string sign = Server.UrlDecode(Request.QueryString["sign"]);//检验码
                string method = Server.UrlDecode(Request.QueryString["method"]);//调用接口

                #region 访问日志

                try
                {
                    new OrderERPLogService().AddHwLog(new Hw_Log { Content = Request.Url.ToString(), Number = orderCode }, null);
                    LogUtils.Log(string.Format("成功写入ERP系统访问日志,订单编号:{0},日志信息:{1}", orderCode, Request.Url.ToString()), "ERP订单回写", Category.Error);
                }
                catch (Exception exception)
                {
                    LogUtils.Log(string.Format("写入ERP系统访问日志失败,订单编号:{0},日志信息:{1},错误信息:{2}", orderCode, Request.Url.ToString(), exception.Message + "/" + exception.InnerException), "ERP订单回写", Category.Error);
                }

                #endregion

                #region 基本参数验证
                if (string.IsNullOrEmpty(username))
                {
                    strMsg = SetMsg("0", "昵称不能为空", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (string.IsNullOrEmpty(orderCode))
                {
                    strMsg = SetMsg("0", "订单不正确", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (username != "hongware")
                {
                    strMsg = SetMsg("0", "昵称不正确", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (string.IsNullOrEmpty(password))
                {
                    strMsg = SetMsg("0", "密码不能为空", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (password != "bir19ming19ham")
                {
                    strMsg = SetMsg("0", "密码不正确", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (string.IsNullOrEmpty(key))
                {
                    strMsg = SetMsg("0", "公钥不能为空", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (key != "g1w9j1r9w")
                {
                    strMsg = SetMsg("0", "公钥不正确", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (string.IsNullOrEmpty(sign))
                {
                    strMsg = SetMsg("0", "检验码不能为空", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (string.IsNullOrEmpty(orderCode))
                {
                    strMsg = SetMsg("0", "订单编号不能为空", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                string sign_Md5 = Encrypt.HwErpMd5(username + password + orderCode + key);
                if (sign != sign_Md5)
                {
                    strMsg = SetMsg("0", "检验码不正确", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                if (string.IsNullOrEmpty(method))
                {
                    strMsg = SetMsg("0", "调用方法名不能为空", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                method = method.ToLower();
                #endregion

                #region 订单当前状态

                var orderService = new OrderService();
                var orderTracking = new OrderStatusTrackingService();

                var order = orderService.QueryByOrderCode(orderCode);

                if (order == null || order.ID < 1)
                {
                    strMsg = SetMsg("0", "未查到此订单号,请核实", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);
                }
                int OrderID = order.ID;
                int State = order.Status;
                #endregion

                if (method == "api.order.send")
                {
                    #region 订单发货
                    if (State == 0)
                    {
                        strMsg = SetMsg("0", "订单还未确认", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    else if (State == 2)
                    {
                        strMsg = SetMsg("0", "订单已发货", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    else if (State == 3)
                    {
                        strMsg = SetMsg("0", "订单已签收", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    else if (State == 4 || State == 5 || State == 8)
                    {
                        strMsg = SetMsg("0", "订单已取消", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }

                    string expressno = string.Empty;
                    expressno = Server.UrlDecode(Request.QueryString["expressno"]);//快递代码
                    string expressnum = Server.UrlDecode(Request.QueryString["expressnum"]);//快递单号
                    string deliverydate = Server.UrlDecode(Request.QueryString["deliverydate"]);//发货日期

                    #region 参数验证
                    if (string.IsNullOrEmpty(expressno))
                    {
                        strMsg = SetMsg("0", "快递代码不能为空", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    if (string.IsNullOrEmpty(expressnum))
                    {
                        strMsg = SetMsg("0", "快递单号不能为空", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    if (string.IsNullOrEmpty(deliverydate))
                    {
                        strMsg = SetMsg("0", "发货日期不能为空", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    #endregion

                    Config_Delivery_Corporation deliveryCorporation = null;

                    #region 更新订单发货状态
                    /*
                     订单发货:
                     * 1.更改订单状态,
                     * 2.更改订单跟踪状态
                     */
                    try
                    {
                        var deliveryCorporations = new ConfigDeliveryCorporationService().QueryAllConfigDeliveryCorporations();

                        if (deliveryCorporations != null && deliveryCorporations.Count > 1)
                        {
                            deliveryCorporation =
                                deliveryCorporations.Find(dc => expressno.Equals(dc.Number, StringComparison.OrdinalIgnoreCase));

                        }

                        if (deliveryCorporation == null)
                        {
                            deliveryCorporation = new Config_Delivery_Corporation();
                            LogUtils.Log("没有获取到代号为:" + expressno.ToUpper() + "的配送公司", "API", Category.Warn);
                        }

                        var tracking = new Order_Status_Tracking();

                        tracking.MailNo = expressnum;
                        tracking.ExpressNumber = expressno.ToUpper();
                        tracking.OrderID = OrderID;
                        tracking.Status = 2;
                        tracking.EmployeeID = 0;
                        tracking.UserID = 0;
                        tracking.Remark = string.Format(
                            "订单已发货,配送单位:{0} {1}; 快递单号:{2}",
                            deliveryCorporation.Name,
                            deliveryCorporation.URL,
                            expressnum);

                        tracking.CreateTime = Convert.ToDateTime(deliverydate);

                        orderService.SqlServer.BeginTransaction();
                        transaction = orderService.SqlServer.Transaction;

                        orderTracking.Add(tracking, transaction);

                        order.Status = 2;
                        order.DeliveryCorporationID = deliveryCorporation.ID;
                        orderService.Edit(order, transaction);

                        transaction.Commit();
                    }
                    catch (Exception exception)
                    {
                        if (transaction != null)
                        {
                            transaction.Rollback();
                        }

                        LogUtils.Log(
                            string.Format(
                                "[Order_ERP]ERP回写发货订单时发生错误,订单编号:{0},错误消息:{1}",
                                orderCode,
                                exception.Message + "/" + exception.InnerException),
                            "[Order_ERP]ERP订单发货回写官网",
                            Category.Error);

                        strMsg = SetMsg("0", "更新订单发货状态发生异常", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    finally
                    {
                        if (transaction != null && transaction.Connection != null
                            && transaction.Connection.State != ConnectionState.Closed)
                        {
                            transaction.Connection.Close();
                        }
                    }
                    #endregion

                    #region 短信发送

                    try
                    {
                        var orderReceiver = new UserReceiveAddressService().QueryByID(order.UserID);

                        if (orderReceiver != null && orderReceiver.Mobile != null)
                        {
                            var moList = new List<string>();
                            moList.Add(orderReceiver.Mobile);

                            var sm = new ShortMessage
                            {
                                ReceiveMobiles = moList,
                                Content =
                                    string.Format(
                                        "亲爱的购酒网会员,您的订单(订单号:{0})支付方式为:{1},已经发货,配送公司:{2}, 单号:{3}。请注意保持手机畅通。",
                                        orderCode,
                                        order.PaymentMethodName,
                                        deliveryCorporation.Name,
                                        deliveryCorporation.Number)
                            };
                            sm.Send();
                            LogUtils.Log(
                                "用户:" + orderReceiver.Consignee + "电话:" + orderReceiver.Mobile + "成功发送短信",
                                "SendSms",
                                Category.Info,
                                Session.SessionID);

                        }
                        else
                        {
                            LogUtils.Log("[Order_ERP]由于没有获取到订单收货人或收货人的手机号不存在,因此订单(" + order.OrderCode + ")发货,未发送通知短信", "[Order_ERP]订单发货回写发送短信SendSms", Category.Error);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtils.Log("[Order_ERP]ERP订单发货发送短信发生错误,错误消息:" + ex.Message + "/" + ex.InnerException, "[Order_ERP]订单发货回写发送短信SendSms", Category.Error);
                    }

                    #endregion

                    strMsg = SetMsg("1", "订单状态更新成功", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);

                    #endregion
                }
                else if (method == "api.order.cancel")
                {
                    #region 订单取
                    if (State == 0)
                    {
                        strMsg = SetMsg("0", "订单还未确认", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    else if (State == 2)
                    {
                        strMsg = SetMsg("0", "订单已发货", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    else if (State == 3)
                    {
                        strMsg = SetMsg("0", "订单已确认收货", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }
                    else if (State == 4 || State == 5 || State == 8)
                    {
                        strMsg = SetMsg("0", "订单已取消", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }

                    try
                    {
                        orderService.SetInvalidByERP(order.ID, "订单作废");
                    }
                    catch (Exception exception)
                    {

                        LogUtils.Log(
                            string.Format(
                                "[Order_ERP]ERP订单取消回写发生错误,订单号:{0},错误消息:{1}",
                                orderCode,
                                exception.Message + "/" + exception.InnerException),
                            "[Order_ERP]ERP订单取消回写",
                            Category.Error);

                        strMsg = SetMsg("0", "订单作废发生错误", orderCode);
                        //Response.Write(strMsg);
                        return this.Content(strMsg);
                    }

                    strMsg = SetMsg("1", "订单作废成功", orderCode);
                    //Response.Write(strMsg);
                    return this.Content(strMsg);

                    #endregion
                }

                strMsg = SetMsg("0", "调用方法名不正确", orderCode);
                //Response.Write(strMsg);
                return this.Content(strMsg);
            }
            catch (Exception)
            {
                strMsg = SetMsg("0", "订单回写发生异常", orderCode);
                //Response.Write(strMsg);
                return this.Content(strMsg);
            }
        }