Exemplo n.º 1
0
        public ChargeResult MarketingCharge(BMarketOrderCharge orderCharge)
        {
            logger.Info("MarketingCharge");
            ChargeResult result = new ChargeResult();

            if (orderCharge == null)
            {
                result.Status  = ChargeStatus.FAILED;
                result.Message = "参数不正确";
                return(result);
            }
            if (orderCharge.AgentId == 0 || orderCharge.CustomerId == 0 || orderCharge.ActivityId == 0)
            {
                result.Status  = ChargeStatus.FAILED;
                result.Message = "参数不正确";
                return(result);
            }
            if (string.IsNullOrEmpty(orderCharge.SPName))
            {
                result.Status  = ChargeStatus.FAILED;
                result.Message = "参数不正确";
                return(result);
            }
            if (string.IsNullOrEmpty(orderCharge.Phone))
            {
                result.Status  = ChargeStatus.FAILED;
                result.Message = "参数不正确";
                return(result);
            }
            int sp = 0;

            if (orderCharge.SPName.Contains("联通"))
            {
                sp = 3;
            }
            else if (orderCharge.SPName.Contains("移动"))
            {
                sp = 1;
            }
            else if (orderCharge.SPName.Contains("电信"))
            {
                sp = 2;
            }
            chargebitEntities         db      = new chargebitEntities();
            Marketing_Activity_Taocan mtaocan = null;
            Marketing_Orders          mOrder  = null;

            try
            {
                Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == orderCharge.ActivityId select a).FirstOrDefault <Marketing_Activities>();
                if (activity == null)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = "参数不正确";
                    return(result);
                }
                if (activity.CustomerId != orderCharge.CustomerId)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = "参数不正确";
                    return(result);
                }
                if (activity.AgentId != orderCharge.AgentId)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = "参数不正确";
                    return(result);
                }
                //非直接扫码活动,必须传入特定的marketing order id
                if (activity.ScanType != 1 && orderCharge.ActivityOrderId == 0)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = "参数不正确";
                    return(result);
                }
                ChargeOrder order = new ChargeOrder()
                {
                    AgencyId     = orderCharge.AgentId,
                    ChargeType   = 1,
                    City         = orderCharge.City,
                    CreatedTime  = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),
                    IsMarket     = true,
                    MacAddress   = orderCharge.OpenId,
                    MobileNumber = orderCharge.Phone,
                    MobileSP     = orderCharge.SPName,
                    Payed        = true,
                    Province     = orderCharge.Province,
                };

                //direct scan
                if (activity.ScanType == 1 && orderCharge.ActivityOrderId <= 0)
                {
                    //判断是否还有可用marketing order

                    List <Marketing_Activity_Taocan> rTaocans = (from mt in db.Marketing_Activity_Taocan
                                                                 join t in db.Resource_taocan on mt.ResourceTaocanId equals t.Id
                                                                 where mt.ActivityId == orderCharge.ActivityId && t.Sp_id == sp
                                                                 select mt).ToList <Marketing_Activity_Taocan>();

                    if (rTaocans.Count == 0)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "本次活动" + orderCharge.SPName + "不能扫码充值";
                        return(result);
                    }

                    mtaocan = rTaocans[0];
                    mOrder  = (from o in db.Marketing_Orders where o.ActivityId == orderCharge.ActivityId && o.Sent == false && o.Used == false && o.ActivityTaocanId == mtaocan.Id select o).FirstOrDefault <Marketing_Orders>();
                    if (mOrder == null)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动";
                        return(result);
                    }
                    if (mOrder.Used)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动";
                        return(result);
                    }
                    mOrder.Used = true;
                    mOrder.Sent = true;
                    //db.SaveChanges();
                    order.MarketOrderId    = mOrder.Id;
                    order.ResourceTaocanId = mtaocan.ResourceTaocanId;
                }//weichat push
                else if (activity.ScanType == 2 && orderCharge.ActivityOrderId > 0)
                {
                    mOrder = (from o in db.Marketing_Orders where o.Id == orderCharge.ActivityOrderId select o).FirstOrDefault <Marketing_Orders>();
                    if (mOrder == null)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "参数有误";
                        return(result);
                    }
                    if (mOrder.Used)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动";
                        return(result);
                    }
                    mtaocan = (from mt in db.Marketing_Activity_Taocan where mt.Id == mOrder.ActivityTaocanId select mt).FirstOrDefault <Marketing_Activity_Taocan>();
                    if (mtaocan == null)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "参数有误";
                        return(result);
                    }
                    Resource_taocan rT = (from r in db.Resource_taocan where r.Id == mtaocan.ResourceTaocanId select r).FirstOrDefault <Resource_taocan>();
                    if (rT == null)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = "参数有误";
                        return(result);
                    }
                    if (rT.Sp_id != sp)
                    {
                        string tmpSPName = "";
                        if (rT.Sp_id == 1)
                        {
                            tmpSPName = "中国移动";
                        }
                        else if (rT.Sp_id == 2)
                        {
                            tmpSPName = "中国电信";
                        }
                        else if (rT.Sp_id == 3)
                        {
                            tmpSPName = "中国联通";
                        }
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = string.Format("此二维码链接不能充值{0}的手机号码的流量,只能充值{1}的号码的流量", orderCharge.SPName, tmpSPName);
                        return(result);
                    }
                    order.ResourceTaocanId = mtaocan.ResourceTaocanId;
                    order.MarketOrderId    = orderCharge.ActivityOrderId;
                    mOrder.Used            = true;
                    mOrder.Sent            = true;
                }
                else if (activity.ScanType == 1 && orderCharge.ActivityOrderId > 0)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = "参数有误";
                    return(result);
                }
                else if (activity.ScanType == 2 && orderCharge.ActivityOrderId <= 0)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = "参数有误";
                    return(result);
                }

                OrderManagement orderMgr = new OrderManagement(CurrentLoginUser);
                order = orderMgr.GenerateOrder(order);
                ChargeBridge chargeBridge = new ChargeBridge();
                if (order.Id > 0)
                {
                    db.SaveChanges();
                    result = chargeBridge.Charge(order);
                    if (result.Status == ChargeStatus.FAILED)
                    {
                        //Rollback, the order cannot be used next time
                        mOrder.UsedTime = 0;
                        mOrder.Used     = false;
                        if (activity.ScanType == 1)
                        {
                            mOrder.Sent = false;
                        }

                        db.SaveChanges();
                    }
                    else
                    {
                        mOrder.UsedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    }
                    db.SaveChanges();
                }
            }
            catch (KMBitException kex)
            {
                logger.Warn(kex);
                result.Status  = ChargeStatus.FAILED;
                result.Message = kex.Message;
                if (mOrder != null)
                {
                    mOrder.Used = false;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
                result.Status  = ChargeStatus.FAILED;
                result.Message = "系统错误,稍后再试";
                if (mOrder != null)
                {
                    mOrder.Used = false;
                    db.SaveChanges();
                }
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
            logger.Info("Finished MarketingCharge");
            return(result);
        }
Exemplo n.º 2
0
        public ActionResult DoSaoMa()
        {
            string p        = Request["p"];
            string number   = Request["mobile_number"];
            string spName   = Request["SPName"];
            string province = Request["Province"];
            string city     = Request["City"];

            if (string.IsNullOrEmpty(p))
            {
                ViewBag.Message = "参数错误,请正确扫码,输入手机号码点充值";
            }
            else
            {
                int    agentId         = 0;
                int    customerId      = 0;
                int    activityId      = 0;
                int    activityOrderId = 0;
                string parameters      = KMEncoder.Decode(p);
                if (!string.IsNullOrEmpty(parameters))
                {
                    string signature = string.Empty;
                    SortedDictionary <string, string> pvs = parseParameters(parameters, out signature);
                    if (string.IsNullOrEmpty(signature))
                    {
                        ViewBag.Message = "URL参数不正确,请重新扫码";
                        return(View("SaoMa"));
                    }
                    System.Text.StringBuilder pBuilder = new System.Text.StringBuilder();
                    if (pvs.Count > 0)
                    {
                        int count = 1;
                        foreach (KeyValuePair <string, string> pair in pvs)
                        {
                            pBuilder.Append(pair.Key);
                            pBuilder.Append("=");
                            pBuilder.Append(pair.Value);
                            if (count < pvs.Count)
                            {
                                pBuilder.Append("&");
                            }
                            count++;
                            switch (pair.Key)
                            {
                            case "agentId":
                                int.TryParse(pair.Value, out agentId);
                                break;

                            case "customerId":
                                int.TryParse(pair.Value, out customerId);
                                break;

                            case "activityId":
                                int.TryParse(pair.Value, out activityId);
                                break;

                            case "activityOrderId":
                                int.TryParse(pair.Value, out activityOrderId);
                                break;
                            }
                        }
                        CustomerManagement customerMgr = new CustomerManagement(0);
                        int total;
                        List <BCustomer> customers = customerMgr.FindCustomers(0, customerId, out total);
                        if (total <= 0 || total > 1)
                        {
                            ViewBag.Message = "URL参数不正确,请重新扫码";
                            return(View("SaoMa"));
                        }
                        pBuilder.Append("&key=");
                        pBuilder.Append(customers[0].Token);
                        string sign = UrlSignUtil.GetMD5(pBuilder.ToString());
                        if (sign != signature)
                        {
                            ViewBag.Message = "URL参数不正确,请重新扫码";
                            return(View("SaoMa"));
                        }
                        ActivityManagement activityMgr = new ActivityManagement(0);
                        BMarketOrderCharge order       = new BMarketOrderCharge()
                        {
                            ActivityId      = activityId,
                            ActivityOrderId = activityOrderId,
                            AgentId         = agentId,
                            CustomerId      = customerId,
                            City            = city,
                            Province        = province,
                            OpenId          = "",
                            Phone           = number,
                            SPName          = spName
                        };
                        KMBit.BL.Charge.ChargeResult result = activityMgr.MarketingCharge(order);
                        ViewBag.Message = result.Message;
                        //if(result.Status == ChargeStatus.FAILED)
                        //{
                        //    ViewBag.Paras = pvs;
                        //    //paras.Add("p", p);
                        //}
                    }
                }
                else
                {
                    ViewBag.Message = "不能重复扫码,或者修改扫码后的URL地址";
                }
            }

            return(View("SaoMa"));
        }