public ActionResult AddDiscountCode(string name)
        {
            Price price = crumbsRepository.FindDiscountCodeByName(name);
            if (price != null)
            {
                try
                {
                    UserPrice uPrice = crumbsRepository.FindUserPriceByUserID(CurrentUser.UserID, 99);
                    if (uPrice == null)
                    {
                        uPrice = new UserPrice()
                        {
                            User = CurrentUser,
                            Price = price,
                            Amount = 0.0M,
                            Subscribed = 0
                        };
                    }
                    else
                    {
                        uPrice.Price = price;
                    }
                    crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                    crumbsRepository.Save();
                }
                catch (Exception e)
                {
                    return Json(new { Message = e.Message, result = -1 }, JsonRequestBehavior.AllowGet);
                }

                return Json(new { Message = "Discount code has been applied to your account.", result = 0 }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { Message = "Discount code not found or expired", result = -1 }, JsonRequestBehavior.AllowGet);
        }
        private void ProcessIPN(string transactionType, string strRequest)
        {
            int UserID = 20;
            Decimal amountPaid = 0;
            int? invoiceID = null;
            try
            {
                Decimal.TryParse(Request["mc_gross"], out amountPaid);

                if (transactionType == "recurring_payment_profile_created" ||
                    transactionType == "recurring_payment_profile_cancel" ||
                    transactionType == "recurring_payment_skipped" ||
                    transactionType == "recurring_payment")
                {
                    Int32.TryParse(Request["rp_invoice_id"], out UserID);
                }
                else
                {
                    Int32.TryParse(Request["custom"], out UserID);

                    Int32 tempInvoiceID = 0;
                    if (true == Int32.TryParse(Request["invoice"], out tempInvoiceID))
                    {
                        invoiceID = tempInvoiceID;
                    }
                }
            }
            catch (Exception)
            {
            }

            int? midiID = null;
            short? packageID = null;
            short paymentType = -1;
            Invoice invoice = null;

            if (invoiceID.HasValue)
            {
                invoice = crumbsRepository.FindInvoice(invoiceID.Value);
                if (invoice != null)
                {
                    paymentType = invoice.Type;
                    midiID = invoice.MidiID;
                    packageID = invoice.PackageID;
                }
            }

            User user = (crumbsRepository as IUserRepository).FindUserByID(UserID);
            if (user == null)
            {
                user = (crumbsRepository as IUserRepository).FindUserByID(20);
            }

            DateTime creationDate = DateTime.Now;
            DateTime expirationDate = creationDate;

            if (paymentType == 10 || paymentType >= 20) // MIDI Payment, MIDI Package
            {
                string email = Request["payer_email"];
                string lastName = Request["last_name"];
                string firstName = Request["first_name"];

                if (transactionType == "cart")
                {
                    OrderHelper.ProcessOrder(crumbsRepository, invoice, Request.ServerVariables["APPL_PHYSICAL_PATH"], email, true);
                }
            }
            else if (transactionType == "express_checkout" || transactionType == "cart")
            {
                if (isProvideTrialPeriod)
                {
                    expirationDate = creationDate.AddDays(14);

                    try
                    {
                        FreePlay prevTrial = crumbsRepository.FindFreePlay(user.UserID, 8);
                        if (prevTrial != null)
                        {
                            prevTrial.Comment = "Warning: Multiple trials detected.";
                            crumbsRepository.InsertOrUpdateFreePlay(prevTrial);
                        }

                        FreePlay newTrial = new FreePlay();
                        newTrial.User = user;
                        newTrial.Promo = crumbsRepository.FindPromotionByID(8);
                        newTrial.Date = creationDate;
                        newTrial.Expiration = expirationDate;
                        crumbsRepository.InsertOrUpdateFreePlay(newTrial);
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    PrepaidOption option = crumbsRepository.FindPrepaidOption(paymentType);
                    if (option != null)
                    {
                        if (option.Months == 12)
                        {
                            expirationDate = creationDate.AddYears(1);
                        }
                        else
                        {
                            expirationDate = creationDate.AddMonths(option.Months);
                        }
                    }
                }
            }
            else if (transactionType == "subscr_payment" || transactionType == "recurring_payment")
            {
                string next_payment_date = Request["next_payment_date"];

                DateTime dtNextPayment = ConvertPayPalDateTime(next_payment_date);
                if(dtNextPayment > DateTime.MinValue) {

                    expirationDate = dtNextPayment.AddDays(1);
                }
                else if (true == DateTime.TryParse(next_payment_date, out dtNextPayment))
                {
                    expirationDate = dtNextPayment.AddDays(1);
                }
            }
            else if (transactionType == "subscr_signup" || transactionType == "recurring_payment_profile_created")
            {
                string PaymentID = Request["recurring_payment_id"];

                Price price = crumbsRepository.FindPriceForUser(user.UserID, 0);
                UserPrice uPrice = crumbsRepository.FindUserPriceByUserID(user.UserID, 0);
                if (uPrice != null)
                {
                    uPrice.Price = price;
                    uPrice.Subscribed = 1;
                    uPrice.Amount = price.Amount;
                    uPrice.PaymentID = PaymentID;
                    crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                }
                else
                {
                    // No special promotions found - create a new one
                    UserPrice newUserPrice = new UserPrice()
                    {
                        User = user,
                        Price = price,
                        Subscribed = 1,
                        Amount = price.Amount,
                        PaymentID = PaymentID
                    };
                    crumbsRepository.InsertOrUpdateUserPrice(newUserPrice);
                }
            }
            else if (transactionType == "subscr_cancel" || transactionType == "recurring_payment_profile_cancel")
            {
                string PaymentID = Request["recurring_payment_id"];

                UserPrice uPrice = crumbsRepository.FindUserPriceByPaymentID(PaymentID);
                if (uPrice != null)
                {
                    uPrice.Subscribed = 0;
                    crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                }
            }

            Payment paymentmodel = new Payment()
            {
                UserID = user.UserID,
                Method = transactionType,
                Date = creationDate,
                Expiration = expirationDate,
                Paid = amountPaid,
                InvoiceID = invoiceID,
                MidiID = midiID,
                PackageID = packageID,
                Comment = strRequest
            };
            crumbsRepository.InsertOrUpdatePayment(paymentmodel);
            crumbsRepository.Save();
        }
        private bool UpdatePromotionalPricing(User user, Price price)
        {
            try
            {
                UserPrice uPrice = crumbsRepository.FindUserPriceByUserID(user.UserID, price.Type);
                if (uPrice == null)
                {
                    // no current promotions for this user - create a new one
                    UserPrice newUserPrice = new UserPrice()
                    {
                        User = user,
                        Price = price,
                        Subscribed = 0
                    };
                    crumbsRepository.InsertOrUpdateUserPrice(newUserPrice);
                }
                else if (uPrice.Subscribed == 0)
                {
                    if (uPrice.Price.Expiration < DateTime.Now)
                    {
                        // expired promotion found - replace it
                        uPrice.Price = price;
                        uPrice.Amount = 0;
                        uPrice.PaymentID = "";
                        crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                    }
                    else
                    {
                        // un-expired promotion found - keep it
                        return false;
                    }
                }
                else
                {
                    // already subscribed - leave it alone
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception is:" + ex.ToString());
                return false;
            }

            return true;
        }