public static IShopOrder CreateNewOrder(OrderCreationSettings settings)
        {
            if (OrderProcessor == null)
            {
                throw new InvalidOperationException("No order processor present, can't generate a new orderid");
            }

            var orderId = OrderProcessor.GenerateNextOrderId(settings);

            using (var data = new DataConnection())
            {
                var order = data.CreateNew <IShopOrder>();

                order.Id         = orderId;
                order.CreatedOn  = DateTime.UtcNow;
                order.OrderTotal = settings.TotalAmount;
                order.Currency   = settings.Currency.ToString();
                order.CustomData = settings.CustomData;

                order = data.Add(order);

                order.WriteLog("created");

                return(order);
            }
        }
Example #2
0
        public virtual string GenerateNextOrderId(OrderCreationSettings settings)
        {
            int orderId;

            lock (Lock)
            {
                var content = C1File.ReadAllText(LastUsedOrderIdFile);
                int.TryParse(content, out orderId);

                orderId = orderId + 1;

                C1File.WriteAllText(LastUsedOrderIdFile, orderId.ToString(CultureInfo.InvariantCulture));
            }

            string sOrderId;

            if (settings.MinimumOrderIdLength > 0)
            {
                var format = "D" + settings.MinimumOrderIdLength;

                sOrderId = orderId.ToString(format, CultureInfo.InvariantCulture);
            }
            else
            {
                sOrderId = orderId.ToString(CultureInfo.InvariantCulture);
            }

            if (!String.IsNullOrEmpty(settings.OrderIdPrefix))
            {
                sOrderId = settings.OrderIdPrefix + sOrderId;
            }

            if (ECommerce.IsTestMode)
            {
                var hashMachineName = Environment.MachineName.GetHashCode().ToString(CultureInfo.InvariantCulture);
                if (hashMachineName.StartsWith("-"))
                {
                    hashMachineName = hashMachineName.Remove(0, 1);
                }

                var maxMachineNameLength = 20 - 6 - sOrderId.Length;
                if (hashMachineName.Length > maxMachineNameLength)
                {
                    hashMachineName = hashMachineName.Substring(0, maxMachineNameLength);
                }

                sOrderId = String.Format("TEST-{0}-{1}", hashMachineName, sOrderId);
            }

            sOrderId = sOrderId.Trim();

            return(sOrderId);
        }