예제 #1
0
        /// <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;
        }
예제 #2
0
        /// <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);
        }