コード例 #1
0
 private void CallBackMethod(IAsyncResult ar)
 {
     try
     {
         UpGradeDel del = (UpGradeDel)ar.AsyncState;
         del.EndInvoke(ar);
     }
     catch (Exception)
     {
     }
 }
コード例 #2
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));
        }
コード例 #3
0
        public void RechargePayResult()
        {
            //接收从微信后台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());

                Member member = _memberBLL.GetMemberByOpenId(data.GetValue("openid").ToString());
                member.Assets += double.Parse(data.GetValue("total_fee").ToString());

                if (_memberBLL.Update(member))
                {
                    Recharge recharge = new Recharge();
                    recharge.Id          = Guid.NewGuid();
                    recharge.MemberId    = member.Id;
                    recharge.Amount      = double.Parse(data.GetValue("total_fee").ToString());
                    recharge.CreatedTime = DateTime.Now;
                    recharge.DeletedTime = DateTime.MinValue.AddHours(8);
                    recharge.IsDeleted   = false;
                    recharge.Payway      = 0;

                    if (_recharegeBLL.Add(recharge))
                    {
                        LogHelper.Log.Write("充值成功");
                        // 添加充值积分记录
                        if (AddRechargeCredit(member, double.Parse(data.GetValue("total_fee").ToString()) / 100))
                        {
                            // 异步判断是否够积分升级vip
                            UpGradeDel   del = new UpGradeDel(UpGradeVIP);
                            IAsyncResult ar  = del.BeginInvoke(member.Id, CallBackMethod, null);
                        }
                    }
                    else
                    {
                        LogHelper.Log.Write("充值失败");
                    }
                }
                else
                {
                    LogHelper.Log.Write("更新余额失败");
                }
            }
            catch (WxPayException ex)
            {
                LogHelper.Log.Write(ex.Message);
                LogHelper.Log.Write(ex.StackTrace);
                //若签名错误,则立即返回结果给微信支付后台
                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();
        }
コード例 #4
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());
        }