Example #1
0
        public ActionResult Pay(string orderNo)
        {
            // 上来先判断 orderNo 是否为空
            // 如果为空  则直接返回 false
            if (string.IsNullOrEmpty(orderNo))
            {
                var res = new
                {
                    isSuccess = false,
                    msg       = "提交的参数有误"
                };
                return(Json(res, JsonRequestBehavior.AllowGet));
            }

            #region 更新订单状态

            Member member = _memberBLL.GetMemberByOpenId(System.Web.HttpContext.Current.Session["member"].ToString());

            // 取得 order对象
            Order order = _orderBLL.GetOrderByOrderNo(orderNo);

            #region 判断余额是否充足
            // 判断余额 是否 充值
            if (member.Assets < order.TotalPrice)
            {
                var res = new
                {
                    isSuccess = false,
                    msg       = "余额不足"
                };
                return(Json(res, JsonRequestBehavior.AllowGet));
            }
            #endregion

            #region 判断订单状态
            if (order.Status == 1)
            {
                var res = new
                {
                    isSuccess = false,
                    msg       = "此订单已支付,请不要重复提交,如有需要,请重新下单购买"
                };
                return(Json(res, JsonRequestBehavior.AllowGet));
            }
            #endregion

            // 修改order对象 payway = ye ye表示余额支付
            order.PayWay  = 1;
            order.PayTime = DateTime.Now;
            order.Status  = 1;
            #endregion

            #region 添加交易记录

            //更新订单状态
            if (_orderBLL.Update(order))
            {
                // 添加交易记录
                Transaction trans = new Transaction();
                trans.Amount      = order.TotalPrice;
                trans.CreatedTime = DateTime.Now;
                trans.DeletedTime = DateTime.MinValue.AddHours(8);
                trans.Id          = Guid.NewGuid();
                trans.IsDeleted   = false;
                trans.MemberId    = member.Id;
                trans.OrderId     = order.Id;
                trans.PayWay      = 1;

                // 如果添加交易记录失败

                // 则回滚订单状态 并返回 false
                if (!_transactionBLL.Add(trans))
                {
                    order.Status = 0;
                    _orderBLL.Update(order);

                    var res = new
                    {
                        isSuccess = false,
                        msg       = "更新数据失败"
                    };
                    return(Json(res, JsonRequestBehavior.AllowGet));
                }

                // 添加消费积分记录 并修改会员积分
                if (AddConsumeCredit(member, order.TotalPrice))
                {
                    // 当修改消费积分成功时 异步判断是否够积分升级vip
                    UpGradeDel   del = new UpGradeDel(UpGradeVIP);
                    IAsyncResult ra  = del.BeginInvoke(member.Id, CallBackMethod, null);

                    // 异步找到服务商品 并添加
                    AddMyServiceDel msDel    = new AddMyServiceDel(AddMyService);
                    IAsyncResult    msResult = msDel.BeginInvoke(member.Id, order.OrderNo, MyServiceCallBackMethod, null);
                }

                // 修改账户余额
                member.Assets = member.Assets - order.TotalPrice;
                if (!_memberBLL.Update(member))
                {
                    // 修改余额失败
                    // 则回滚 订单状态 和 交易记录
                    order.Status = 0;
                    _orderBLL.Update(order);
                    _transactionBLL.Remove(trans);

                    var res = new
                    {
                        isSuccess = false,
                        msg       = "数据库扣款失败"
                    };
                    return(Json(res, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                // 更新订单状态失败
                var res = new
                {
                    isSuccess = false,
                    msg       = "更新数据失败"
                };
                return(Json(res, JsonRequestBehavior.AllowGet));
            }
            #endregion

            var json = new
            {
                isSuccess = true,
                msg       = "支付成功"
            };

            return(Json(json, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public void WxPayResult()
        {
            //接收从微信后台POST过来的数据
            System.IO.Stream s = Request.InputStream;
            int count          = 0;

            byte[]        buffer  = new byte[1024];
            StringBuilder builder = new StringBuilder();

            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();


            //转换数据格式并验证签名
            WxPayData data = new WxPayData();

            try
            {
                data.FromXml(builder.ToString());
                Order order = _orderBLL.GetOrderByOrderNo(data.GetValue("out_trade_no").ToString());
                order.Status  = 1;
                order.PayTime = DateTime.Now;

                Member member = _memberBLL.GetMemberByOpenId(data.GetValue("openid").ToString());

                if (_orderBLL.Update(order))
                {
                    // 添加交易记录
                    Transaction trans = new Transaction();
                    trans.Amount      = order.TotalPrice;
                    trans.CreatedTime = DateTime.Now;
                    trans.DeletedTime = DateTime.MinValue.AddHours(8);
                    trans.Id          = Guid.NewGuid();
                    trans.IsDeleted   = false;
                    trans.MemberId    = member.Id;
                    trans.OrderId     = order.Id;
                    trans.PayWay      = 0;

                    _transactionBLL.Add(trans);

                    if (AddConsumeCredit(member, order.TotalPrice))
                    {
                        // 异步判断是否有足够的积分进行升级
                        UpGradeDel   del    = new UpGradeDel(UpGradeVIP);
                        IAsyncResult result = del.BeginInvoke(member.Id, CallBackMethod, null);

                        // 异步找到此订单所有的服务  并添加
                        AddMyServiceDel msDel = new AddMyServiceDel(AddMyService);
                        IAsyncResult    ra    = msDel.BeginInvoke(member.Id, order.OrderNo, MyServiceCallBackMethod, null);
                    }
                }
                else
                {
                    LogHelper.Log.Write("支付失败");
                }
            }
            catch (WxPayException ex)
            {
                //若签名错误,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", ex.Message);
                Log.Error(this.GetType().ToString(), "Sign check error : " + res.ToXml());

                Response.Write(res.ToXml());
                Response.End();
            }

            Log.Info(this.GetType().ToString(), "Check sign success");
            WxPayData successData = new WxPayData();

            successData.SetValue("return_code", "SUCCESS");
            successData.SetValue("return_msg", "OK");

            Response.Write(successData.ToXml());
            Response.End();
            //return Content(successData.ToXml());
        }