Beispiel #1
0
        private string SaveOrder(bool saveToDB)
        {
            try
            {
                //validation
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                var          cart         = CartHelper.GetShoppingCart(siteSettings.SiteId, ShoppingCartTypeEnum.ShoppingCart);

                if (cart.Count == 0)
                {
                    return(StringHelper.ToJsonString(new
                    {
                        success = false,
                        message = ProductResources.CartIsEmptyLabel
                    }));
                }

                //if (!ProductConfiguration.OnePageCheckoutEnabled)
                //{
                //    return StringHelper.ToJsonString(new
                //    {
                //        success = false,
                //        message = "One page checkout is disabled"
                //    });
                //}

                if ((!Request.IsAuthenticated && !ProductConfiguration.AnonymousCheckoutAllowed))
                {
                    return(StringHelper.ToJsonString(new
                    {
                        success = false,
                        message = ProductResources.CheckoutAnonymousNotAllowed
                    }));
                }

                //string validateResult = string.Empty;
                //bool validate = CheckValidate(out validateResult);
                //if (!validate)
                //    return validateResult;

                Order order = CartHelper.GetOrderSession(siteSettings.SiteId);
                if (order == null)
                {
                    order        = new Order();
                    order.SiteId = siteSettings.SiteId;
                }

                order.BillingFirstName = GetPostValue("Address_FirstName", order.BillingFirstName);
                order.BillingLastName  = GetPostValue("Address_LastName", order.BillingLastName);
                order.BillingEmail     = GetPostValue("Address_Email", order.BillingEmail);
                order.BillingAddress   = GetPostValue("Address_Address", order.BillingAddress);
                order.BillingPhone     = GetPostValue("Address_Phone", order.BillingPhone);
                order.BillingMobile    = GetPostValue("Address_Mobile", order.BillingMobile);
                order.BillingFax       = GetPostValue("Address_Fax", order.BillingFax);
                order.BillingStreet    = GetPostValue("Address_Street", order.BillingStreet);
                order.BillingWard      = GetPostValue("Address_Ward", order.BillingWard);

                string district = GetPostValue("Address_District", order.BillingDistrictGuid.ToString());
                if (district.Length == 36)
                {
                    order.BillingDistrictGuid = new Guid(district);
                }
                else
                {
                    order.BillingDistrictGuid = Guid.Empty;
                }

                string province = GetPostValue("Address_Province", order.BillingProvinceGuid.ToString());
                if (province.Length == 36)
                {
                    order.BillingProvinceGuid = new Guid(province);
                }
                else
                {
                    order.BillingProvinceGuid = Guid.Empty;
                }

                string country = GetPostValue("Address_Country", order.BillingCountryGuid.ToString());
                if (country.Length == 36)
                {
                    order.BillingCountryGuid = new Guid(country);
                }
                else
                {
                    order.BillingCountryGuid = Guid.Empty;
                }

                // Shipping method
                bool hasShipping = false;
                foreach (var key in postParams.AllKeys)
                {
                    if (key == "ShippingMethod")
                    {
                        hasShipping = true;
                        break;
                    }
                }
                if (hasShipping)
                {
                    order.ShippingMethod = -1;
                    string shippingMethod     = GetPostValue("ShippingMethod");
                    var    lstShippingMethods = ShippingMethod.GetByActive(siteSettings.SiteId, 1);
                    foreach (ShippingMethod shipping in lstShippingMethods)
                    {
                        if (shippingMethod == shipping.ShippingMethodId.ToString())
                        {
                            order.ShippingMethod = shipping.ShippingMethodId;
                            break;
                        }
                    }

                    if (order.ShippingMethod == -1)
                    {
                        return(StringHelper.ToJsonString(new
                        {
                            success = false,
                            message = ProductResources.CheckoutShippingMethodRequired
                        }));
                    }
                }

                // Payment method
                bool hasPayment = false;
                foreach (var key in postParams.AllKeys)
                {
                    if (key == "PaymentMethod")
                    {
                        hasPayment = true;
                        break;
                    }
                }
                if (hasPayment)
                {
                    order.PaymentMethod = -1;
                    string paymentMethod     = GetPostValue("PaymentMethod");
                    var    lstPaymentMethods = PaymentMethod.GetByActive(siteSettings.SiteId, 1);
                    foreach (PaymentMethod payment in lstPaymentMethods)
                    {
                        if (paymentMethod == payment.PaymentMethodId.ToString())
                        {
                            order.PaymentMethod = payment.PaymentMethodId;
                            break;
                        }
                    }

                    if (order.PaymentMethod == -1)
                    {
                        return(StringHelper.ToJsonString(new
                        {
                            success = false,
                            message = ProductResources.CheckoutPaymentMethodRequired
                        }));
                    }
                }

                // Company Info
                order.InvoiceCompanyName    = GetPostValue("Invoice.CompanyName", order.InvoiceCompanyName);
                order.InvoiceCompanyAddress = GetPostValue("Invoice.CompanyAddress", order.InvoiceCompanyAddress);
                order.InvoiceCompanyTaxCode = GetPostValue("Invoice.CompanyTaxCode", order.InvoiceCompanyTaxCode);
                order.OrderNote             = GetPostValue("OrderNote", order.OrderNote);

                string result = string.Empty;
                if (!IsBillingAddressValid(order, out result))
                {
                    return(result);
                }
                if (!IsShippingAddressValid(order, out result))
                {
                    return(result);
                }

                if (saveToDB)
                {
                    order.OrderCode     = ProductHelper.GenerateOrderCode(order.SiteId);
                    order.CreatedFromIP = SiteUtils.GetIP4Address();
                    if (Request.IsAuthenticated)
                    {
                        SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
                        if (siteUser != null)
                        {
                            order.UserGuid = siteUser.UserGuid;
                            siteUser.ICQ   = order.BillingProvinceGuid.ToString();
                            siteUser.AIM   = order.BillingDistrictGuid.ToString();
                            siteUser.Save();
                        }
                    }

                    order.Save();
                    if (SaveOrderSummary(order, cart))
                    {
                        //CartHelper.CouponCodeInput = null;
                        CartHelper.ClearCartCookie(order.SiteId);
                        CartHelper.SetOrderSession(siteSettings.SiteId, null);
                        HttpContext.Current.Session[GetOrderIDSessionKey(order.SiteId)] = order.OrderId;

                        var onePayUrl = OnePayHelper.GetPaymentUrlIfNeeded(order);
                        if (!string.IsNullOrEmpty(onePayUrl))
                        {
                            //System.Timers.Timer timer1 = new System.Timers.Timer();
                            //timer1.Interval = 5 * 60 * 1000; //ms, 5 minutes
                            //timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
                            //timer1.Enabled = true;
                            //GC.KeepAlive(timer1);

                            return(StringHelper.ToJsonString(new
                            {
                                success = true,
                                redirect = onePayUrl
                            }));
                        }
                    }
                }
                else
                {
                    CartHelper.SetOrderSession(siteSettings.SiteId, order);
                }

                return(StringHelper.ToJsonString(new
                {
                    success = true,
                    redirect = GetPostValue("redirect", string.Empty)
                }));
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return(StringHelper.ToJsonString(new { success = false, message = ex.Message }));
            }
        }