Example #1
0
        public ActionResult Pay(SelectModel model)
        {
            AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext();
            AnswerApp.Models.User thisUser = db.Users.Single(d => d.UserName.Equals(User.Identity.Name));
            if (thisUser == null) { RedirectToAction("LogIn", "Account"); }

            String filename = "" + model.Textbook +
                              "_" + model.Unit +
                              "_" + model.Chapter +
                              "_" + model.Section +
                              "_" + model.Page +
                              "_" + model.Question + ".pdf";

            model.CorrectAnswer = "Error 3";
            IQueryable<Question> retrieved2 = from theAnswers in db.Questions
                                              where theAnswers.Textbook_Title.Equals(model.Textbook)
                                              && theAnswers.Unit_Title.Equals(model.Unit)
                                              && theAnswers.Chapter_Title.Equals(model.Chapter)
                                              && theAnswers.Section_Title.Equals(model.Section)
                                              && theAnswers.Page_Number.Equals(model.Page)
                                              && theAnswers.Question_Number.Equals(model.Question)
                                              select theAnswers;
            Question[] results2 = retrieved2.ToArray<Question>();
            if (results2.Length != 0)
            {
                model.CorrectAnswer = results2.First().Practice_Problem_Answer;
            }

            db.SubmitChanges();

            PriceBreakdown thePriceBreakdown = new PriceBreakdown(model, thisUser, db);
            ViewData["UpgradeLevel"] = thePriceBreakdown.UpgradeLevel;
            ViewData["CurrentLevel"] = thePriceBreakdown.CurrentLevel;
            ViewData["Credit"] = thePriceBreakdown.Credit;
            ViewData["NumberOfSolutionsToPurchase"] = thePriceBreakdown.NumberOfSolutionsToPurchase;
            ViewData["TotalValue"] = (thePriceBreakdown.NumberOfSolutionsToPurchase * PriceOfSingleSolution).ToString("C");
            ViewData["NumberOfSolutionsForThisUser"] = thePriceBreakdown.NumberOfSolutionsForThisUser;
            ViewData["NumberSelectedUserAlreadyHas"] = thePriceBreakdown.NumberSelectedUserAlreadyHas;
            ViewData["SolutionsRemainingToBePurchased"] = thePriceBreakdown.SolutionsRemainingToBePurchased;
            ViewData["RemainingCost"] = thePriceBreakdown.RemainingCost.ToString("C");
            ViewData["UserCredit"] = thisUser.Credit;
            ViewData["TotalRemainingSolutions"] = thePriceBreakdown.TotalRemainingSolutions;
            ViewData["TotalRemainingCost"] = thePriceBreakdown.TotalRemainingPrice.ToString("C");
            ViewData["UserLevel"] = thePriceBreakdown.UserLevel;
            ViewData["UserLevelAfterPurchase"] = thePriceBreakdown.UserLevelAfterPurchase;
            ViewData["UpgradePrice"] = thePriceBreakdown.UpgradePrice.ToString("C");
            ViewData["AdditionalCredits"] = thePriceBreakdown.AdditionalCredits;
            ViewData["ShowUpgradeSavings"] = thePriceBreakdown.ShowUpgradeSavings;
            ViewData["UpgradeSavings"] = thePriceBreakdown.UpgradeSavings.ToString("C");
            ViewData["DisplayIndividualPurchasePrice"] = thePriceBreakdown.DisplayIndividualPurchasePrice;//*/

            return View(model);
        }
        public string Test()
        {
            string argument = "n/a";

            AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext();

            AnswerApp.Models.User administrator = db.Users.Single<AnswerApp.Models.User>(a => a.UserName.Equals("administrator"));

            administrator.MetaData += "(in) ";

            //Post back to either sandbox or live
            string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
            string strLive = "https://www.paypal.com/cgi-bin/webscr";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);//Sandbox);

            //Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = HttpContext.Current.Request.BinaryRead(HttpContext.Current.Request.ContentLength);//byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);

            string strRequest = Encoding.ASCII.GetString(param);
            strRequest += "&cmd=_notify-validate";
            req.ContentLength = strRequest.Length;

            //Send the request to PayPal and get the response
            StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();

            if (strResponse == "VERIFIED")
            {
                String[] parameters = strRequest.Split(new char[1] { '&' });

                String theParameterProxy = "error 03148";
                String[] KeyValue = new String[2];
                String Key = "", Value = "";

                String Properties = "";
                String UserName = "";
                String FileName = "";
                String[] ParsedFileProperties = null;
                double Price = 0;
                String ItemName = "";

                foreach (String theParameter in parameters)
                {
                    KeyValue = theParameter.Split(new char[1] { '=' });
                    Key = KeyValue[0];
                    Value = KeyValue[1];
                    if (Key.Equals("transaction_subject"))
                    {
                        administrator.MetaData += " transaction_subject: " + Value;//debug
                        db.SubmitChanges();//debug

                        Properties = Value.Replace('+', ' ');
                        UserName = Properties.Split(new char[1] { '_' })[0];
                        FileName = Properties.Replace(UserName + "_", "");
                        ParsedFileProperties = FileName.Split(new char[1] { '_' });
                    }
                    else if (Key.Equals("mc_gross"))
                    {
                        administrator.MetaData += " mc_gross: " + Value;//debug
                        db.SubmitChanges();//debug
                        Price = Convert.ToDouble(Value);
                    }
                    else if (Key.Equals("item_name"))
                    {
                        administrator.MetaData += " item_name: " + Value;//debug
                        db.SubmitChanges();//debug
                        ItemName = Value.Replace('+', ' ');
                    }
                }

                AnswerApp.Models.User theUser = db.Users.Single<User>(u => u.UserName.Equals(UserName));

                SelectModel model = new AnswerApp.Models.SelectModel();

                if (ParsedFileProperties.Length > 0) { model.Textbook = ParsedFileProperties[0]; }
                if (ParsedFileProperties.Length > 1) { model.Unit = ParsedFileProperties[1]; }
                else
                {
                    if (ItemName.Equals("1000-Pack of Solvation Credits"))
                    {
                        if (Math.Abs(Price - AnswerApp.Controllers.AnswersController.PriceOf1000Pack) < 0.01)
                        {
                            theUser.Credit += 1000;
                        }
                    }
                    else if (ItemName.Equals("250-Pack of Solvation Credits"))
                    {
                        if (Math.Abs(Price - AnswerApp.Controllers.AnswersController.PriceOf250Pack) < 0.01)
                        {
                            theUser.Credit += 250;
                        }
                    }
                    else if (ItemName.Equals("100-Pack of Solvation Credits"))
                    {
                        if (Math.Abs(Price - AnswerApp.Controllers.AnswersController.PriceOf100Pack) < 0.01)
                        {
                            theUser.Credit += 100;
                        }
                    }
                    else if (ItemName.Equals("50-Pack of Solvation Credits"))
                    {
                        if (Math.Abs(Price - AnswerApp.Controllers.AnswersController.PriceOf50Pack) < 0.01)
                        {
                            theUser.Credit += 50;
                        }
                    }
                    else if (ItemName.Equals("10-Pack of Solvation Credits"))
                    {
                        if (Math.Abs(Price - AnswerApp.Controllers.AnswersController.PriceOf10Pack) < 0.01)
                        {
                            theUser.Credit += 10;
                        }
                    }
                    else if (ItemName.Equals("Single Solvation Credit"))
                    {
                        if (Math.Abs(Price - AnswerApp.Controllers.AnswersController.PriceOfSingleSolution) < 0.01)
                        {
                            theUser.Credit += 1;
                        }
                    }
                    administrator.MetaData += "ItemName: " + ItemName;
                    db.SubmitChanges();
                    return strResponse;
                }
                if (ParsedFileProperties.Length > 2) { model.Chapter = ParsedFileProperties[2]; }
                if (ParsedFileProperties.Length > 3) { model.Section = ParsedFileProperties[3]; }
                if (ParsedFileProperties.Length > 4) { model.Page = ParsedFileProperties[4]; }
                if (ParsedFileProperties.Length > 5) { model.Question = ParsedFileProperties[5]; }
                if (ParsedFileProperties.Length > 6)
                {
                    FileName = model.Textbook + "_" + model.Unit + "_" + model.Chapter + "_" + model.Section + "_" + model.Page + "_" + model.Question;
                }

                PriceBreakdown thePriceBreakdown = new PriceBreakdown(model, theUser, db);

                if (ParsedFileProperties.Length > 6)//the upgrade was selected
                {
                    administrator.MetaData += " UpgradePrice: " + thePriceBreakdown.UpgradePrice.ToString("C");//debug

                    /*if (Math.Abs(Price - thePriceBreakdown.UpgradePrice) >= 0.01)
                    {
                        return strResponse;
                    }//*/

                    theUser.Credit = Convert.ToInt32(ParsedFileProperties[6]);

                    db.SubmitChanges();//debug
                }
                else//the individual solutions were purchased
                {
                    administrator.MetaData += " TotalRemainingPrice: " + thePriceBreakdown.TotalRemainingPrice.ToString("C");//debug

                    /*if (Math.Abs(Price - thePriceBreakdown.TotalRemainingPrice) >= 0.01)
                    {
                        return strResponse;
                    }//*/

                    theUser.Credit = Convert.ToInt32("0");

                    db.SubmitChanges();//debug
                }

                //make sure the selection contains solutions
                AnswerApp.Controllers.AnswersController theAnswersController = new AnswerApp.Controllers.AnswersController();
                if(!(theAnswersController.NumberOfQuestions(model, db) > 0)){return strResponse;}

                //Enter this answer
                String[] UserAnswers;
                UserAnswers= null;
                if (theUser.Answers != null)
                {
                    UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                    foreach (String Answer in UserAnswers)
                    {
                        String thisAnswer = Answer.Replace(".pdf", "");
                        if (model.Contains(thisAnswer))
                        {
                            theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                        }
                    }
                }
                theUser.Answers += FileName + ".pdf;";

                bool done = false;
                if (!model.Question.Equals("All") && !done)
                {
                    //Find all questions from the selected section
                    IQueryable<AnswerApp.Models.Question> retrieved = from theAnswers in db.Questions
                                                                        where theAnswers.Page_Number.Equals(model.Page)
                                                                        select theAnswers;
                    Question[] results = retrieved.ToArray<Question>();
                    bool UserHasAll = true;
                    foreach (Question theQuestion in results)
                    {
                        if (!UserHasAccess(theUser, theQuestion.Textbook_Title + "_" + theQuestion.Unit_Title + "_" + theQuestion.Chapter_Title + "_" + theQuestion.Section_Title + "_" + theQuestion.Page_Number + "_" + theQuestion.Question_Number + ".pdf", db))
                        {
                            UserHasAll = false;
                            break;
                        }
                    }
                    if (UserHasAll)
                    {
                        model.Question = "All";
                        FileName = model.Textbook + "_" + model.Unit + "_" + model.Chapter + "_" + model.Section + "_" + model.Page + "_All";
                        UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                        foreach (String Answer in UserAnswers)
                        {
                            String thisAnswer = Answer.Replace(".pdf", "");
                            if (model.Contains(thisAnswer))
                            {
                                theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                            }
                        }
                        theUser.Answers += FileName + ".pdf;";
                    }
                    else
                    {
                        done = true;
                    }
                }
                if (!model.Page.Equals("All") && !done)
                {
                    //Find all questions from the selected section
                    IQueryable<AnswerApp.Models.Page> retrieved = from thePages in db.Pages
                                                                    where thePages.Section_Title.Equals(model.Section)
                                                                    select thePages;
                    AnswerApp.Models.Page[] results = retrieved.ToArray<AnswerApp.Models.Page>();
                    bool UserHasAll = true;
                    foreach (AnswerApp.Models.Page thePage in results)
                    {
                        if (!UserHasAccess(theUser, thePage.Textbook_Title + "_" + thePage.Unit_Title + "_" + thePage.Chapter_Title + "_" + thePage.Section_Title + "_" + thePage.Page_Number + "_All", db))
                        {
                            UserHasAll = false;
                            break;
                        }
                    }
                    if (UserHasAll)
                    {
                        model.Page = "All";
                        FileName = model.Textbook + "_" + model.Unit + "_" + model.Chapter + "_" + model.Section + "_All_All";
                        UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                        foreach (String Answer in UserAnswers)
                        {
                            String thisAnswer = Answer.Replace(".pdf", "");
                            if (model.Contains(thisAnswer))
                            {
                                theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                            }
                        }
                        theUser.Answers += FileName + ".pdf;";
                    }
                    else
                    {
                        done = true;
                    }
                }
                if (!model.Section.Equals("All") && !done)
                {
                    //Find all Secrtions from the selected Chapter
                    IQueryable<AnswerApp.Models.Section> retrieved = from theSections in db.Sections
                                                                        where theSections.Chapter_Title.Equals(model.Chapter)
                                                                        select theSections;
                    AnswerApp.Models.Section[] results = retrieved.ToArray<AnswerApp.Models.Section>();
                    bool UserHasAll = true;
                    foreach (AnswerApp.Models.Section theSection in results)
                    {
                        if (!UserHasAccess(theUser, theSection.Textbook_Title + "_" + theSection.Unit_Title + "_" + theSection.Chapter_Title + "_" + theSection.Section_Title + "_All_All", db))
                        {
                            UserHasAll = false;
                            break;
                        }
                    }
                    if (UserHasAll)
                    {
                        model.Section = "All";
                        FileName = model.Textbook + "_" + model.Unit + "_" + model.Chapter + "_All_All_All";
                        UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                        foreach (String Answer in UserAnswers)
                        {
                            String thisAnswer = Answer.Replace(".pdf", "");
                            if (model.Contains(thisAnswer))
                            {
                                theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                            }
                        }
                        theUser.Answers += FileName + ".pdf;";
                    }
                    else
                    {
                        done = true;
                    }
                }
                if (!model.Chapter.Equals("All") && !done)
                {
                    //Find all Chapters from the selected Unit
                    IQueryable<AnswerApp.Models.Chapter> retrieved = from theChapters in db.Chapters
                                                                        where theChapters.Unit_Title.Equals(model.Unit)
                                                                        select theChapters;
                    AnswerApp.Models.Chapter[] results = retrieved.ToArray<AnswerApp.Models.Chapter>();
                    bool UserHasAll = true;
                    foreach (AnswerApp.Models.Chapter theChapter in results)
                    {
                        if (!UserHasAccess(theUser, theChapter.Textbook_Title + "_" + theChapter.Unit_Title + "_" + theChapter.Chapter_Title + "_All_All_All", db))
                        {
                            UserHasAll = false;
                            break;
                        }
                    }
                    if (UserHasAll)
                    {
                        model.Chapter = "All";
                        FileName = model.Textbook + "_" + model.Unit + "_All_All_All_All";
                        UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                        foreach (String Answer in UserAnswers)
                        {
                            String thisAnswer = Answer.Replace(".pdf", "");
                            if (model.Contains(thisAnswer))
                            {
                                theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                            }
                        }
                        theUser.Answers += FileName + ".pdf;";
                    }
                    else
                    {
                        done = true;
                    }
                }
                if (!model.Unit.Equals("All") && !done)
                {
                    //Find all Units from the selected Textbook
                    IQueryable<AnswerApp.Models.Unit> retrieved = from theUnits in db.Units
                                                                    where theUnits.Textbook_Title.Equals(model.Textbook)
                                                                    select theUnits;
                    AnswerApp.Models.Unit[] results = retrieved.ToArray<AnswerApp.Models.Unit>();
                    bool UserHasAll = true;
                    foreach (AnswerApp.Models.Unit theUnit in results)
                    {
                        if (!UserHasAccess(theUser, theUnit.Textbook_Title + "_" + theUnit.Unit_Title + "_All_All_All_All", db))
                        {
                            UserHasAll = false;
                            break;
                        }
                    }
                    if (UserHasAll)
                    {
                        model.Unit = "All";
                        FileName = model.Textbook + "_All_All_All_All_All";
                        UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                        foreach (String Answer in UserAnswers)
                        {
                            String thisAnswer = Answer.Replace(".pdf", "");
                            if (model.Contains(thisAnswer))
                            {
                                theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                            }
                        }
                        theUser.Answers += FileName + ".pdf;";
                    }
                    else
                    {
                        done = true;
                    }
                }

                administrator.MetaData += " request: " + strRequest + " response: " + strResponse + " if: " + theParameterProxy;

                db.SubmitChanges();
                //check the payment_status is Completed
                //check that txn_id has not been previously processed
                //check that receiver_email is your Primary PayPal email
                //check that payment_amount/payment_currency are correct
                //process payment
            }
            else if (strResponse == "INVALID")
            {
                //log for manual investigation

                administrator.MetaData += " request: " + strRequest + " response: " + strResponse + " else if: " + argument;

                db.SubmitChanges();
            }
            else
            {
                //log response/ipn data for manual investigation

                administrator.MetaData += " request: " + strRequest + " response: " + strResponse + " else: " + argument;

                db.SubmitChanges();
            }

            return strResponse;
        }