/// <summary>
 /// Loads the favorite products.
 /// </summary>
 private void LoadFavoriteProducts()
 {
     if (base.MasterPage.SiteSettings.CollectBrowsingProduct) {
     DataSet ds = new ProductController().FetchFavoriteProducts(WebUtility.GetUserName());
     if (ds.Tables[0].Rows.Count > 0) {
       rptrFavoriteProducts.DataSource = ds;
       rptrFavoriteProducts.DataBind();
     }
       }
 }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try {
            Page.Title = string.Format(WebUtility.MainTitleTemplate, Master.SiteSettings.SiteName, LocalizationUtility.GetText("titleCart"));
            order = new OrderController().FetchOrder(WebUtility.GetUserName());
            PaymentServiceSettings providers = PaymentService.FetchConfiguredPaymentProviders();
            if (providers != null) {
              if (providers.DefaultProvider == "PayPalProPaymentProvider" || !(string.IsNullOrEmpty(providers.ProviderSettingsCollection["PayPalProPaymentProvider"].Parameters[PayPalProPaymentProvider.BUSINESS_EMAIL]))) {
            pnlExpressCheckout.Visible = true;
              }
            }
            if (!Page.IsPostBack) {
              bool changesMade = OrderController.NormalizeCartQuantities(order);
              if (changesMade) {
            messageCenter.Visible = true;
            messageCenter.DisplayInformationMessage(LocalizationUtility.GetText("lblCartChanged"));
              }
              if (order.OrderId > 0 && order.OrderItemCollection.Count > 0) {
            pnlNoCart.Visible = false;
            orderSummary.Order = order;

            lblSubTotalAmountTop.Text = StoreUtility.GetFormattedAmount(order.SubTotal, true);
            lbUpdate.Text = LocalizationUtility.GetText("lblUpdate");
              }
              else {
            pnlCart.Visible = false;

            ProductCollection productCollection;
            if (this.Master.SiteSettings.CollectBrowsingProduct) {
              productCollection = Store.Caching.ProductCache.GetMostPopularProducts();
            }
            else {
              productCollection = new ProductController().FetchRandomProducts();//Should we cache this?
            }
            catalogList.ProductCollection = productCollection;
              }
            }
              }
              catch (Exception ex) {
            Logger.Error(typeof(cart).Name + ".Page_Load", ex);
            throw;
              }
        }
 /// <summary>
 /// Loads the downloads.
 /// </summary>
 private void LoadDownloads()
 {
     DownloadCollection downloadCollection = new ProductController().FetchAssociatedDownloadsByProductIdAndNotForPurchase(productId);
       if (downloadCollection.Count > 0) {
     dlDownloads.DataSource = downloadCollection;
     dlDownloads.ItemDataBound += new DataListItemEventHandler(dlFiles_ItemDataBound);
     dlDownloads.DataBind();
       }
       else {
     //turn stuff off
     pnlDownloadsTitle.Visible = false;
     pnlDownloads.Visible = false;
       }
 }
 /// <summary>
 /// Loads the categories.
 /// </summary>
 private void LoadCategories()
 {
     CategoryCollection categoryCollection = new ProductController().FetchAssociatedCategoriesByProductId(productId);
       dgProductCategories.DataSource = categoryCollection;
       dgProductCategories.Columns[0].HeaderText = LocalizationUtility.GetText("lblCategory");
       dgProductCategories.Columns[1].HeaderText = LocalizationUtility.GetText("lblDelete");
       dgProductCategories.ItemDataBound += new DataGridItemEventHandler(dgProductCategories_ItemDataBound);
       dgProductCategories.DataBind();
 }
 private void LoadAvailableDownloads()
 {
     DownloadCollection artifactCollection = new ProductController().FetchAvailableDownloadsByProductId(productId);
       dgDownloads.DataSource = artifactCollection;
       dgDownloads.ItemDataBound += new DataGridItemEventHandler(dgDownloads_ItemDataBound);
       dgDownloads.Columns[0].HeaderText = LocalizationUtility.GetText("hdrTitle");
       dgDownloads.Columns[1].HeaderText = LocalizationUtility.GetText("hdrPath");
       dgDownloads.Columns[2].HeaderText = LocalizationUtility.GetText("hdrAdd");
       dgDownloads.DataBind();
 }
 /// <summary>
 /// Loads the categories.
 /// </summary>
 private void LoadAssociatedDownloads()
 {
     DownloadCollection artifactCollection = new ProductController().FetchAssociatedDownloadsByProductId(productId);
       dgProductDownloads.DataSource = artifactCollection;
       dgProductDownloads.ItemDataBound += new DataGridItemEventHandler(dgProductDownloads_ItemDataBound);
       dgProductDownloads.Columns[0].HeaderText = LocalizationUtility.GetText("hdrTitle");
       dgProductDownloads.Columns[1].HeaderText = LocalizationUtility.GetText("lblDelete");
       dgProductDownloads.DataBind();
 }
        /// <summary>
        /// Charges the specified order.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public static Transaction Charge(Order order, string userName)
        {
            //update the order with IP
              order.IPAddress = HttpContext.Current.Request.UserHostAddress;
              PaymentService paymentService = new PaymentService();
              Transaction transaction = paymentService.Charge(order);
              order.OrderStatusDescriptorId = (int)OrderStatus.ReceivedPaymentProcessingOrder;
              order.OrderTypeId = (int)OrderType.Purchase;
              order.Save(userName);
              Guid userGuid = new Guid(Membership.GetUser(userName).ProviderUserKey.ToString());
              try {
            //Add an OrderNote
            OrderNote orderNote = new OrderNote();
            orderNote.OrderId = order.OrderId;
            orderNote.Note = Strings.ResourceManager.GetString(ORDER_CHARGED);
            orderNote.Save(userName);
            Sku sku;
            DownloadCollection downloadCollection;
            DownloadAccessControlCollection downloadAccessControlCollection;
            DownloadAccessControl downloadAccessControl;
            foreach (OrderItem orderItem in order.OrderItemCollection) {
              //Adjust the Inventory
              sku = new Sku(SKU, orderItem.Sku);
              sku.Inventory = sku.Inventory - orderItem.Quantity;
              sku.Save(SYSTEM);
              ProductCache.RemoveSKUFromCache(orderItem.Sku);
              //Add access control for orderitems
              downloadCollection = new ProductController().FetchAssociatedDownloadsByProductIdAndForPurchase(orderItem.ProductId);
              if (downloadCollection.Count > 0) {
            foreach (Download download in downloadCollection) {
              Query query = new Query(DownloadAccessControl.Schema).
                AddWhere(DownloadAccessControl.Columns.UserId, Comparison.Equals, userGuid).
                AddWhere(DownloadAccessControl.Columns.DownloadId, Comparison.Equals, download.DownloadId);
              downloadAccessControlCollection = new DownloadAccessControlController().FetchByQuery(query);
              if (downloadAccessControlCollection.Count == 0) {
                downloadAccessControl = new DownloadAccessControl();
                downloadAccessControl.DownloadId = download.DownloadId;
                downloadAccessControl.UserId = userGuid;
                downloadAccessControl.Save(SYSTEM);
              }
            }
              }
            }

            //Send out the messages
            //Send these last in case something happens with the email
            MessageService messageService = new MessageService();
            messageService.SendOrderReceivedNotificationToCustomer(order);
            messageService.SendOrderReceivedNotificationToMerchant(order);
              }
              catch (Exception ex) {
            //swallow the exception here because the transaction is saved
            //and, while this is an inconvenience, it's not critical
            Logger.Error(typeof(OrderController).Name + ".Charge", ex);
              }
              return transaction;
        }
 /// <summary>
 /// Loads the products.
 /// </summary>
 private void LoadProducts()
 {
     ProductCollection productCollection;
       if(ddlParentCategory.SelectedValue == "0") {
     productCollection = new ProductController().FetchAll();
       }
       else {
     int categoryId = 0;
     int.TryParse(ddlParentCategory.SelectedValue, out categoryId);
     productCollection = new ProductController().FetchAllProductsByCategoryId(categoryId);
       }
       lblNumberOfTotalProducts.Text = productCollection.Count.ToString();
       dgProducts.CurrentPageIndex = 0;
       dgProducts.DataSource = productCollection;
       dgProducts.ItemDataBound += new DataGridItemEventHandler(dgProducts_ItemDataBound);
       dgProducts.Columns[0].HeaderText = LocalizationUtility.GetText("hdrEdit");
       dgProducts.Columns[1].HeaderText = LocalizationUtility.GetText("hdrSku");
       dgProducts.Columns[2].HeaderText = LocalizationUtility.GetText("hdrName");
       dgProducts.Columns[3].HeaderText = LocalizationUtility.GetText("hdrIsEnabled");
       dgProducts.Columns[4].HeaderText = LocalizationUtility.GetText("hdrIsDeleted");
       dgProducts.DataBind();
 }
        /// <summary>
        /// Refunds the specified transaction.
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        /// <param name="refundedOrder">The refunded order.</param>
        /// <param name="userName">Name of the user.</param>
        public static void RefundStandard(Transaction transaction, Order refundedOrder, string userName)
        {
            Order order = new Order(transaction.OrderId);
              Transaction refundTransaction = new Transaction();
              //refundTransaction.OrderId = transaction.OrderId;
              refundTransaction.TransactionTypeDescriptorId = (int)TransactionType.Refund;
              refundTransaction.PaymentMethod = PAYPAL;
              refundTransaction.GatewayName = PAYPAL_STANDARD;
              refundTransaction.GatewayResponse = SUCCESS;
              refundTransaction.GatewayTransactionId = CoreUtility.GenerateRandomString(16);
              refundTransaction.GrossAmount = refundedOrder.Total;
              refundTransaction.NetAmount = 0.00M;
              refundTransaction.FeeAmount = 0.00M;
              refundTransaction.TransactionDate = DateTime.Now;
              //refundTransaction.Save(userName);
              refundedOrder.Save(userName);

              //set the orderid for the refund
              foreach(OrderItem orderItem in refundedOrder.OrderItemCollection) {
            orderItem.OrderId = refundedOrder.OrderId;
              }
              refundedOrder.OrderItemCollection.SaveAll(userName);
              //set the orderId to the refunded orderId
              refundTransaction.OrderId = refundedOrder.OrderId;
              refundTransaction.Save(userName);
              Guid userGuid = new Guid(Membership.GetUser(order.UserName).ProviderUserKey.ToString());
              DownloadCollection downloadCollection;
              foreach(OrderItem orderItem in refundedOrder.OrderItemCollection) {
            //put the stock back
            Sku sku = new Sku(Sku.Columns.SkuX, orderItem.Sku);
            sku.Inventory = sku.Inventory + orderItem.Quantity;
            sku.Save(userName);
            ProductCache.RemoveSKUFromCache(orderItem.Sku);
            //remove the access control
            downloadCollection = new ProductController().FetchAssociatedDownloadsByProductIdAndForPurchase(orderItem.ProductId);
            if (downloadCollection.Count > 0) {
              foreach (Download download in downloadCollection) {
            new DownloadAccessControlController().Delete(userGuid, download.DownloadId);
              }
            }

              }
              if(refundedOrder.Total == order.Total) {
            order.OrderStatusDescriptorId = (int)OrderStatus.OrderFullyRefunded;
              }
              else {
            order.OrderStatusDescriptorId = (int)OrderStatus.OrderPartiallyRefunded;
              }
              order.Save(userName);
              //Add an OrderNote
              OrderNote orderNote = new OrderNote();
              orderNote.OrderId = order.OrderId;
              orderNote.Note = Strings.ResourceManager.GetString(ORDER_REFUNDED);
              orderNote.Save(userName);
              //send off the notifications
              MessageService messageService = new MessageService();
              messageService.SendOrderRefundToCustomer(refundedOrder);
        }
 /// <summary>
 /// Refunds the specified transaction.
 /// </summary>
 /// <param name="transaction">The transaction.</param>
 /// <param name="refundedOrder">The order the refund should be applied to.</param>
 /// <param name="userName">Name of the user.</param>
 public static void Refund(Transaction transaction, Order refundedOrder, string userName)
 {
     Order order = new Order(transaction.OrderId);
       PaymentService paymentService = new PaymentService();
       Transaction refundTransaction = paymentService.Refund(transaction, refundedOrder);
       refundedOrder.Save(userName);
       //set the orderid for the refund
       foreach(OrderItem orderItem in refundedOrder.OrderItemCollection) {
     orderItem.OrderId = refundedOrder.OrderId;
       }
       refundedOrder.OrderItemCollection.SaveAll(userName);
       //set the orderId to the refunded orderId
       refundTransaction.OrderId = refundedOrder.OrderId;
       refundTransaction.Save(userName);
       Guid userGuid = new Guid(Membership.GetUser(order.UserName).ProviderUserKey.ToString());
       foreach(OrderItem orderItem in refundedOrder.OrderItemCollection) {
     new Product(orderItem.ProductId);
     //put the stock back
     Sku sku = new Sku(Sku.Columns.SkuX, orderItem.Sku);
     sku.Inventory = sku.Inventory + orderItem.Quantity;
     sku.Save(userName);
     ProductCache.RemoveSKUFromCache(orderItem.Sku);
     //remove the access control
     DownloadCollection downloadCollection = new ProductController().FetchAssociatedDownloadsByProductIdAndForPurchase(orderItem.ProductId);
     if (downloadCollection.Count > 0) {
       foreach (Download download in downloadCollection) {
     new DownloadAccessControlController().Delete(userGuid, download.DownloadId);
       }
     }
       }
       if(refundedOrder.Total == order.Total) {
     order.OrderStatusDescriptorId = (int)OrderStatus.OrderFullyRefunded;
       }
       else {
     order.OrderStatusDescriptorId = (int)OrderStatus.OrderPartiallyRefunded;
       }
       order.Save(userName);
       //Add an OrderNote
       OrderNote orderNote = new OrderNote();
       orderNote.OrderId = order.OrderId;
       orderNote.Note = Strings.ResourceManager.GetString(ORDER_REFUNDED);
       orderNote.Save(userName);
       //send off the notifications
       MessageService messageService = new MessageService();
       messageService.SendOrderRefundToCustomer(refundedOrder);
 }
 /// <summary>
 /// Loads the products.
 /// </summary>
 private void LoadProducts()
 {
     crossSellCollection = new CrossSellController().FetchByID(productId);
       Query query = new Query(Product.Schema).WHERE(Product.Columns.ProductId, Comparison.NotEquals, productId);
       ProductCollection productCollection = new ProductController().FetchByQuery(query);
       if(productCollection.Count > 0) {
     dgCrossSells.DataSource = productCollection;
     dgCrossSells.ItemDataBound += new DataGridItemEventHandler(dgCrossSells_ItemDataBound);
     dgCrossSells.Columns[0].HeaderText = LocalizationUtility.GetText("hdrAddRemove");
     dgCrossSells.Columns[1].HeaderText = LocalizationUtility.GetText("hdrName");
     dgCrossSells.DataBind();
       }
 }
        /// <summary>
        /// Charges the specified order.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public static Transaction Charge(Order order, string userName)
        {
            //update the order with IP
            order.IPAddress = HttpContext.Current.Request.UserHostAddress == "::1" || HttpContext.Current.Request.UserHostAddress == "127.0.0.1" ? "127.0.0.1" : HttpContext.Current.Request.UserHostAddress;
            PaymentService paymentService = new PaymentService();
            Transaction    transaction    = paymentService.Charge(order);

            order.OrderStatusDescriptorId = (int)OrderStatus.ReceivedPaymentProcessingOrder;
            order.OrderTypeId             = (int)OrderType.Purchase;
            order.Save(userName);
            Guid userGuid = new Guid(Membership.GetUser(userName).ProviderUserKey.ToString());

            try {
                //Add an OrderNote
                OrderNote orderNote = new OrderNote();
                orderNote.OrderId = order.OrderId;
                orderNote.Note    = Strings.ResourceManager.GetString(ORDER_CHARGED);
                orderNote.Save(userName);
                Sku sku;
                DownloadCollection downloadCollection;
                DownloadAccessControlCollection downloadAccessControlCollection;
                DownloadAccessControl           downloadAccessControl;
                foreach (OrderItem orderItem in order.OrderItemCollection)
                {
                    //Adjust the Inventory
                    sku           = new Sku(SKU, orderItem.Sku);
                    sku.Inventory = sku.Inventory - orderItem.Quantity;
                    sku.Save(SYSTEM);
                    ProductCache.RemoveSKUFromCache(orderItem.Sku);
                    //Add access control for orderitems
                    downloadCollection = new ProductController().FetchAssociatedDownloadsByProductIdAndForPurchase(orderItem.ProductId);
                    if (downloadCollection.Count > 0)
                    {
                        foreach (Download download in downloadCollection)
                        {
                            Query query = new Query(DownloadAccessControl.Schema).
                                          AddWhere(DownloadAccessControl.Columns.UserId, Comparison.Equals, userGuid).
                                          AddWhere(DownloadAccessControl.Columns.DownloadId, Comparison.Equals, download.DownloadId);
                            downloadAccessControlCollection = new DownloadAccessControlController().FetchByQuery(query);
                            if (downloadAccessControlCollection.Count == 0)
                            {
                                downloadAccessControl            = new DownloadAccessControl();
                                downloadAccessControl.DownloadId = download.DownloadId;
                                downloadAccessControl.UserId     = userGuid;
                                downloadAccessControl.Save(SYSTEM);
                            }
                        }
                    }
                }

                //Send out the messages
                //Send these last in case something happens with the email
                MessageService messageService = new MessageService();
                messageService.SendOrderReceivedNotificationToCustomer(order);
                messageService.SendOrderReceivedNotificationToMerchant(order);
            }
            catch (Exception ex) {
                //swallow the exception here because the transaction is saved
                //and, while this is an inconvenience, it's not critical
                Logger.Error(typeof(OrderController).Name + ".Charge", ex);
            }
            return(transaction);
        }
        /// <summary>
        /// Refunds the specified transaction.
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        /// <param name="refundedOrder">The refunded order.</param>
        /// <param name="userName">Name of the user.</param>
        public static void RefundStandard(Transaction transaction, Order refundedOrder, string userName)
        {
            Order       order             = new Order(transaction.OrderId);
            Transaction refundTransaction = new Transaction();

            //refundTransaction.OrderId = transaction.OrderId;
            refundTransaction.TransactionTypeDescriptorId = (int)TransactionType.Refund;
            refundTransaction.PaymentMethod        = PAYPAL;
            refundTransaction.GatewayName          = PAYPAL_STANDARD;
            refundTransaction.GatewayResponse      = SUCCESS;
            refundTransaction.GatewayTransactionId = CoreUtility.GenerateRandomString(16);
            refundTransaction.GrossAmount          = refundedOrder.Total;
            refundTransaction.NetAmount            = 0.00M;
            refundTransaction.FeeAmount            = 0.00M;
            refundTransaction.TransactionDate      = DateTime.Now;
            //refundTransaction.Save(userName);
            refundedOrder.Save(userName);

            //set the orderid for the refund
            foreach (OrderItem orderItem in refundedOrder.OrderItemCollection)
            {
                orderItem.OrderId = refundedOrder.OrderId;
            }
            refundedOrder.OrderItemCollection.SaveAll(userName);
            //set the orderId to the refunded orderId
            refundTransaction.OrderId = refundedOrder.OrderId;
            refundTransaction.Save(userName);
            Guid userGuid = new Guid(Membership.GetUser(order.UserName).ProviderUserKey.ToString());
            DownloadCollection downloadCollection;

            foreach (OrderItem orderItem in refundedOrder.OrderItemCollection)
            {
                //put the stock back
                Sku sku = new Sku(Sku.Columns.SkuX, orderItem.Sku);
                sku.Inventory = sku.Inventory + orderItem.Quantity;
                sku.Save(userName);
                ProductCache.RemoveSKUFromCache(orderItem.Sku);
                //remove the access control
                downloadCollection = new ProductController().FetchAssociatedDownloadsByProductIdAndForPurchase(orderItem.ProductId);
                if (downloadCollection.Count > 0)
                {
                    foreach (Download download in downloadCollection)
                    {
                        new DownloadAccessControlController().Delete(userGuid, download.DownloadId);
                    }
                }
            }
            if (refundedOrder.Total == order.Total)
            {
                order.OrderStatusDescriptorId = (int)OrderStatus.OrderFullyRefunded;
            }
            else
            {
                order.OrderStatusDescriptorId = (int)OrderStatus.OrderPartiallyRefunded;
            }
            order.Save(userName);
            //Add an OrderNote
            OrderNote orderNote = new OrderNote();

            orderNote.OrderId = order.OrderId;
            orderNote.Note    = Strings.ResourceManager.GetString(ORDER_REFUNDED);
            orderNote.Save(userName);
            //send off the notifications
            MessageService messageService = new MessageService();

            messageService.SendOrderRefundToCustomer(refundedOrder);
        }