Ejemplo n.º 1
0
        public static void ProcessRequest(HttpContext context)
        {
            try
            {
                string RequestIP = IP.Current;

                //Log
                string LogMessage = context.Request.Params.ToRawString();
                ErrorLogger.Log(LogMessage, LogType.CPAGPTNetworks);

                AppSettings.Points.Reload();

                //Get proper Network
                string Hash = CPAFileManager.GetHashFromClientHandlerHit(context);

                AffiliateNetwork Network = TableHelper.SelectRows <AffiliateNetwork>(TableHelper.MakeDictionary("Hash", Hash))[0];

                //All OK
                string Username = GetFromRequest(context, Network.VariableNameOfUsername);
                //string _Balance = GetFromRequest(context, Network.VariableNameOfBalance);
                string OfferID    = GetFromRequest(context, Network.VariableNameOfOfferId);
                string _WebsiteId = GetFromRequest(context, Network.VariableNameOfWebsiteId); //Can be null

                string OfferTitle = GetFromRequest(context, Network.VariableNameOfOfferName); //Can be null

                string _CreditVal = GetFromRequest(context, Network.VariableNameOfType);      //Can be null
                string Signature  = GetFromRequest(context, Network.VariableNameOfSignature); //Can be null
                string UserIP     = GetFromRequest(context, Network.VariableNameOfMemberIP);  //Can be null

                string ShortOfferTitle = OfferTitle != null ? ": " + ShortenString(OfferTitle, 8) + "..." : "";
                string TrackingInfo    = OfferID;

                bool isLocked = false;  //Points Locking feature

                Money Balance = new Money(0);
                //try
                //{
                //    Balance = Money.Parse(_Balance);
                //}
                //catch (Exception ex) { }

                //Check Status and IP restrictions
                bool ActiveRestriction = Network.Status == NetworkStatus.Active;
                bool IPRestriction     = !Network.HasRestrictedIPs || (Network.HasRestrictedIPs && Network.RestrictedIPs.Contains(RequestIP));

                if (ActiveRestriction && IPRestriction)
                {
                    //Check condition
                    if (Signature == null || Signature.Trim() == CPAParser.ParseSignatureCondition(Network.SIgnatureCondition, context).Trim())
                    {
                        //All OK, Verified
                        CPAPostBackLogStatus Status = CPAPostBackLogStatus.Null;
                        Money Calculated            = new Money(0);

                        //Let's get our offer
                        CPAOffer OurOffer = null;
                        try
                        {
                            var where = TableHelper.MakeDictionary("NetworkOfferIdInt", OfferID);
                            where.Add("AdvertiserUsername", Network.Name);
                            where.Add("Status", (int)AdvertStatus.Active);

                            OurOffer = TableHelper.SelectRows <CPAOffer>(where)[0];
                            Balance  = OurOffer.BaseValue;
                        }
                        catch (Exception ex)
                        { ErrorLogger.Log(ex); }


                        if (OurOffer != null)
                        {
                            //Lets find submission
                            try
                            {
                                Member             User       = new Member(Username);
                                CreditAs           As         = GetCreditAs(OurOffer, Network);
                                PostbackActionType ActionType = CPAHandler.GetActionType(Network, Balance, _CreditVal);

                                var conditions = TableHelper.MakeDictionary("Username", Username);
                                conditions.Add("OfferId", OurOffer.Id);
                                conditions.Add("OfferStatus", (int)OfferStatus.Pending);

                                int IsSubmissionPresent = TableHelper.CountOf <OfferRegisterEntry>(conditions);

                                //Add submission automatically if credit and not found
                                if (IsSubmissionPresent == 0 && ActionType == PostbackActionType.Credit)
                                {
                                    OfferRegisterEntry.CheckDuplicateAndStatus(User.Name, OurOffer, true);
                                    OfferRegisterEntry.AddNew(OurOffer, User.Name, OfferStatus.Pending, String.Empty, String.Empty);

                                    IsSubmissionPresent = TableHelper.CountOf <OfferRegisterEntry>(conditions);
                                }

                                if (IsSubmissionPresent > 0)
                                {
                                    OfferRegisterEntry ThisEntry = TableHelper.SelectRows <OfferRegisterEntry>(conditions)[0];

                                    if (ActionType == PostbackActionType.Reverse)
                                    {
                                        CPAPostbackLog oldLog = PointsLockingHelper.FindSimilarCpaLog(User.Name, Network.Name, TrackingInfo);

                                        if (oldLog == null)
                                        {
                                            Calculated = CPAManager.DenyEntryFromPostback(ThisEntry, Balance, As, OurOffer.Id,
                                                                                          Network.Name, OfferTitle, Network.RequiresConversion);
                                            Status = CPAPostBackLogStatus.ReversedByNetwork;
                                        }
                                        else
                                        {
                                            Status        = CPAPostBackLogStatus.ReversedByNetwork;
                                            oldLog.Status = Status;
                                            CPAManager.DenyEntry(ThisEntry, User, OfferTitle);
                                        }
                                    }

                                    if (ActionType == PostbackActionType.Credit)
                                    {
                                        if (PointsLockingManager.CPAGPTShouldBeLocked(Balance, As, User, Network.RequiresConversion))
                                        {
                                            ThisEntry.HasBeenLocked = true;
                                            ThisEntry.Save();

                                            Calculated = CPAGPTCrediter.CalculatePostback(Balance, Network.RequiresConversion, User, As);
                                            Status     = CPAPostBackLogStatus.CreditedAndPointsLocked;
                                            History.AddOfferLocked(User.Name, Network.Name, OfferTitle, Calculated, As);
                                        }
                                        else
                                        {
                                            var where = TableHelper.MakeDictionary("Id", ThisEntry._OfferId);
                                            var CpaOffer_entryId = TableHelper.SelectRows <CPAOffer>(where);
                                            Calculated = CPAManager.AcceptEntryFromPostback(ThisEntry, Balance, As, OurOffer.Id,
                                                                                            Network.Name, CpaOffer_entryId[0].Title, Network.RequiresConversion, out isLocked);
                                            Status = CPAPostBackLogStatus.CreditedByNetwork;
                                        }
                                    }

                                    //Add CPAPostbackLog
                                    CPAPostbackLog.Create(Network, Username, Balance, Calculated, TrackingInfo, Status);

                                    //If All was handled OK, than succResponse
                                    context.Response.Write(Network.ValueOfSuccessfulResponse);
                                }
                                else
                                {
                                    CPAPostbackLog.Create(Network, Username, Balance, Calculated, TrackingInfo, CPAPostBackLogStatus.SubmissionNotFound);
                                }
                            }
                            catch (MsgException ex)
                            {
                                //Offer has been already submited and it's not daily
                                //Offer is daily, but was submited today
                                //Offer is daly and can be submited X times a day, but was submited >= X today
                                CPAPostbackLog.Create(Network, Username, Balance, Calculated, TrackingInfo, CPAPostBackLogStatus.ExceededSubmissionLimitForThisOffer);
                                context.Response.Write(Network.ValueOfSuccessfulResponse);
                            }
                            catch (Exception ex)
                            {
                                ErrorLogger.Log(ex);
                                CPAPostbackLog.Create(Network, Username, Balance, Calculated, TrackingInfo, CPAPostBackLogStatus.Null);
                            }
                        }
                        else
                        {
                            CPAPostbackLog.Create(Network, Username, Balance, null, TrackingInfo, CPAPostBackLogStatus.OfferNotFoundByOfferId);
                        }
                    }
                    else
                    {
                        CPAPostbackLog.Create(Network, Username, Balance, null, TrackingInfo, CPAPostBackLogStatus.WrongSignature);
                    }
                }
                else if (IPRestriction)
                {
                    CPAPostbackLog.Create(Network, Username, Balance, null, TrackingInfo, CPAPostBackLogStatus.NetworkInactive);
                }
                else if (ActiveRestriction)
                {
                    CPAPostbackLog.Create(Network, Username, Balance, null, TrackingInfo, CPAPostBackLogStatus.SentFromUnallowedIP);
                }
            }
            catch (Exception ex) { Prem.PTC.ErrorLogger.Log(ex); }
        }
Ejemplo n.º 2
0
        public static void ProcessRequest(HttpContext context)
        {
            try
            {
                string RequestIP = IP.Current;

                string LogMessage = context.Request.Params.ToRawString();
                ErrorLogger.Log(LogMessage, LogType.OfferWalls);

                //Force refresh
                AppSettings.Offerwalls.Reload();
                AppSettings.Points.Reload();

                //Get proper Offerwall
                string Hash = OfferwallFileManager.GetHashFromClientHandlerHit(context);

                Offerwall Wall = TableHelper.SelectRows <Offerwall>(TableHelper.MakeDictionary("Hash", Hash))[0];

                //All OK
                string Username     = GetFromRequest(context, Wall.VariableNameOfUsername);
                string _Balance     = GetFromRequest(context, Wall.VariableNameOfBalance);
                string TrackingInfo = GetFromRequest(context, Wall.VariableNameOfTrackingInfo); //Can be null
                string _CreditVal   = GetFromRequest(context, Wall.VariableNameOfType);         //Can be null
                string Signature    = GetFromRequest(context, Wall.VariableNameOfSignature);    //Can be null
                Money  Balance      = Money.Parse(_Balance);

                Member member = null;

                //Check Status and IP restrictions
                bool ActiveRestriction = Wall.Status == OfferwallStatus.Active;
                bool IPRestriction     = !Wall.HasRestrictedIPs || (Wall.HasRestrictedIPs && IpRangeHelper.isOK(Wall.RestrictedIPs, RequestIP));

                if (ActiveRestriction && IPRestriction)
                {
                    //Check condition
                    if (Signature == null || Signature.Trim() == OfferwallParser.ParseSignatureCondition(Wall.SIgnatureCondition, context).Trim())
                    {
                        //All OK, Verified
                        OfferwallsLogStatus Status = OfferwallsLogStatus.Null;
                        Money Calculated           = new Money(0);

                        //Check if member exists
                        try
                        {
                            member = new Member(Username);

                            if ((_CreditVal != null && _CreditVal == Wall.VariableValueOfTypeReversed) ||
                                (_CreditVal == null && Balance < Money.Zero))
                            {
                                //Reverse
                                OfferwallsLog oldLog = PointsLockingHelper.FindSimilarLog(member.Name, Wall.DisplayName, TrackingInfo);

                                if (oldLog == null)
                                {
                                    OfferwallCrediter Crediter = new OfferwallCrediter(member, Wall);
                                    Calculated = Crediter.ReverseCredit(Balance, Wall.CreditAs, Wall.DisplayName, Wall.RequiresConversion);

                                    History.AddOfferwalRevereseCompleted(Username, Wall.DisplayName, Calculated, Wall.CreditAs);
                                    Status = OfferwallsLogStatus.ReversedByOfferwall;
                                }
                                //change old log status
                                else
                                {
                                    Status        = OfferwallsLogStatus.ReversedByOfferwall;
                                    oldLog.Status = Status;
                                }
                            }

                            if ((_CreditVal != null && _CreditVal == Wall.VariableValueOfTypeCredited) ||
                                (_CreditVal == null && Balance >= Money.Zero))
                            {
                                //Credit
                                if (PointsLockingManager.OfferwallShouldBeLocked(Balance, member, Wall))
                                {
                                    Calculated = OfferwallCrediter.CalculatedAndConversion(Balance, member, Wall);
                                    History.AddOfferLocked(Username, Wall.DisplayName, null, Calculated, Wall.CreditAs);

                                    Status = OfferwallsLogStatus.CreditedAndPointsLocked;
                                }
                                else
                                {
                                    OfferwallCrediter Crediter = new OfferwallCrediter(member, Wall);
                                    Calculated = Crediter.CreditMember(Balance, Wall);
                                    History.AddOfferwalCompleted(Username, Wall.DisplayName, Calculated, Wall.CreditAs);
                                    Status = OfferwallsLogStatus.CreditedByOfferwall;
                                }
                            }

                            if ((_CreditVal != null && (_CreditVal != Wall.VariableValueOfTypeCredited && _CreditVal != Wall.VariableValueOfTypeReversed)))
                            {
                                Status = OfferwallsLogStatus.WrongCreditVariable;
                            }

                            //Add OfferwallsLog
                            OfferwallsLog.Create(Wall, member.Name, Balance, Calculated, TrackingInfo, Status);

                            //If All was handled OK, than succResponse
                            context.Response.Write(Wall.ValueOfSuccessfulResponse);
                        }
                        catch (MsgException ex)
                        {
                            Status = OfferwallsLogStatus.MemberNotFoundByUsername;
                            OfferwallsLog.Create(Wall, Username, Balance, Calculated, TrackingInfo, Status);
                        }
                    }
                    else
                    {
                        OfferwallsLog.Create(Wall, Username, Balance, null, TrackingInfo, OfferwallsLogStatus.WrongSignature);
                    }
                }
                else if (IPRestriction)
                {
                    OfferwallsLog.Create(Wall, Username, Balance, null, TrackingInfo, OfferwallsLogStatus.OfferwallInactive);
                }
                else if (ActiveRestriction)
                {
                    OfferwallsLog.Create(Wall, Username, Balance, null, TrackingInfo, OfferwallsLogStatus.SentFromUnallowedIP);
                }
            }
            catch (Exception ex) { Prem.PTC.ErrorLogger.Log(ex); }
        }