public IActionResult ShippingInfoSave(ShippingInfoModel requestModel) { if (!CanCheckout(out Cart cart)) { return(R.Fail.Result); } var shippingRequired = CartHelper.IsShippingRequired(cart); if (!shippingRequired) { return(R.Success.Result); } if (cart.BillingAddressId == 0 || cart.ShippingAddressId == 0) { return(R.Fail.With("error", T("The address details were not provided")).Result); } if (cart.ShippingOptionsSerialized.IsNullEmptyOrWhiteSpace() || requestModel.ShippingOption == null || !requestModel.ShippingOption.Any()) { return(R.Fail.With("error", T("No shipping options selected")).Result); } var shippingOptionModels = _dataSerializer.DeserializeAs <IList <WarehouseShippingOptionModel> >(cart.ShippingOptionsSerialized).SelectMany(x => x.ShippingOptions).ToList(); var selectedOptions = new List <ShippingOptionModel>(); foreach (var shippingOptionModel in requestModel.ShippingOption) { var shippingOptionId = shippingOptionModel.Id; var option = shippingOptionModels.FirstOrDefault(x => x.Id == shippingOptionId); if (option == null) { return(R.Fail.With("error", T("Could not find one of selected shipping option")).Result); } selectedOptions.Add(option); } var additionalFee = cart.CartItems.Sum(x => x.Product.AdditionalShippingCharge); //shipping handler validation if (requestModel.ShippingMethod != null) { //validate shipping method var shippingHandler = PluginHelper.GetShipmentHandler(requestModel.ShippingMethod.SystemName); if (shippingHandler == null || !shippingHandler.IsMethodAvailable(cart)) { return(R.Fail.With("error", T("Shipping method unavailable")).Result); } cart.ShippingMethodName = requestModel.ShippingMethod.SystemName; cart.ShippingFee = additionalFee + selectedOptions.Sum(x => x.Rate); cart.ShippingMethodDisplayName = shippingHandler.PluginInfo.Name; } cart.SelectedShippingOption = _dataSerializer.Serialize(ToShippingOptions(selectedOptions)); _cartService.Update(cart); return(R.Success.Result); }
public OrderModel Create(Order entity) { var model = _modelMapper.Map <OrderModel>(entity); model.User = _modelMapper.Map <UserModel>(entity.User); if (entity.BillingAddressSerialized != null) { var billingAddress = _dataSerializer.DeserializeAs <Address>(entity.BillingAddressSerialized); model.BillingAddress = _modelMapper.Map <AddressModel>(billingAddress); model.BillingAddress.CountryName = billingAddress.Country.Name; model.BillingAddress.StateProvinceName = billingAddress.StateOrProvince?.Name ?? billingAddress.StateProvinceName; } if (entity.ShippingAddressSerialized != null) { var shippingAddress = _dataSerializer.DeserializeAs <Address>(entity.ShippingAddressSerialized); model.ShippingAddress = _modelMapper.Map <AddressModel>(shippingAddress); model.ShippingAddress.CountryName = shippingAddress.Country.Name; model.ShippingAddress.StateProvinceName = shippingAddress.StateOrProvince?.Name ?? shippingAddress.StateProvinceName; } model.OrderItems = entity.OrderItems?.Select(Create).ToList(); //is this subscription model? if (model.IsSubscription) { //get the latest payment info var paymentTransaction = _paymentTransactionService .Get(x => x.OrderGuid == entity.Guid && x.PaymentStatus == PaymentStatus.Complete) .OrderByDescending(x => x.Id).FirstOrDefault(); model.LastInvoiceDate = paymentTransaction?.CreatedOn; if (paymentTransaction != null && model.OrderItems != null) { var minCycleDays = model.OrderItems.Min(x => (int)x.SubscriptionCycle); model.NextInvoiceDate = model.LastInvoiceDate.Value.AddDays(minCycleDays); } } if (!entity.SelectedShippingOption.IsNullEmptyOrWhiteSpace()) { var selectedOptions = _dataSerializer.DeserializeAs <IList <ShippingOption> >(entity.SelectedShippingOption); model.SelectedShippingOptions = selectedOptions.Select(x => new ShippingOptionModel() { Id = x.Id, Name = x.Name, Description = x.Description, Rate = x.Rate, DeliveryTime = x.DeliveryTime }).ToList(); } return(model); }
public string FormatProductAttributes(string attributeJson) { if (string.IsNullOrEmpty(attributeJson)) { return(string.Empty); } var productAttributes = _dataSerializer.DeserializeAs <Dictionary <string, IList <string> > >(attributeJson); var attributeText = string.Join(Environment.NewLine, productAttributes.Select(y => y.Key + " : " + string.Join(", ", y.Value))); return(attributeText); }
public T LoadWidgetSettings <T>(string widgetId) { var widgetSettingName = string.Format(WidgetSettingKey, widgetId); var setting = _settingService.FirstOrDefault(x => x.Key == widgetSettingName); if (setting == null) { return(Activator.CreateInstance <T>()); } return(_dataSerializer.DeserializeAs <T>(setting.Value)); }
public override IViewComponentResult Invoke(object data = null) { var r = R; try { UpdatesResponseModel updates = null; //do we need to fetch? if (_systemSettings.LatestUpdatesFetched.IsNullEmptyOrWhiteSpace() || DateTime.UtcNow.Subtract(_systemSettings.LatestFetchedOn).TotalHours >= _systemSettings.UpdateFetchIntervalInHours) { //we need to updates = _requestProvider.Get <UpdatesResponseModel>(UpdatesFetchUrl, new NameValueCollection() { { "storeDomain", _generalSettings.StoreDomain }, { "storeName", _generalSettings.StoreName } }); if (updates != null) { if (!updates.Success && _systemSettings.LatestUpdatesFetched.IsNullEmptyOrWhiteSpace()) { return(R.Fail.ComponentResult); } _systemSettings.LatestUpdatesFetched = _dataSerializer.Serialize(updates); _systemSettings.LatestFetchedOn = DateTime.UtcNow; _settingService.Save(_systemSettings, CurrentStore.Id); } } updates = updates ?? (_systemSettings.LatestUpdatesFetched.IsNullEmptyOrWhiteSpace() ? null : _dataSerializer.DeserializeAs <UpdatesResponseModel>(_systemSettings.LatestUpdatesFetched)); if (updates != null) { r.With("evencartUpdates", updates.FeedItems); } return(r.Success.ComponentResult); } catch { return(r.Success.ComponentResult); } }
private ThemeInfo GetThemeInfo(DirectoryInfo directoryInfo) { var themeConfigPath = _localFileProvider.CombinePaths(_themeDirectory, directoryInfo.Name, "config.json"); if (_localFileProvider.FileExists(themeConfigPath)) { var configJson = _localFileProvider.ReadAllText(themeConfigPath); var themeInfo = _dataSerializer.DeserializeAs <ThemeInfo>(configJson); var imagePath = _localFileProvider.CombinePaths(_themeDirectory, directoryInfo.Name, "Assets", "theme.jpg"); //do we have an image file? if (_localFileProvider.FileExists(imagePath)) { themeInfo.ThumbnailUrl = $"/{directoryInfo.Name}/assets/theme.jpg"; } themeInfo.DirectoryName = directoryInfo.Name; return(themeInfo); } return(null); }
public async Task <T> GetAsync <T>(string url, NameValueCollection data = null) { var response = await GetStringAsync(url, data); return(_dataSerializer.DeserializeAs <T>(response)); }
public OrderModel Create(Order order) { var model = _modelMapper.Map <OrderModel>(order); if (!order.SelectedShippingOption.IsNullEmptyOrWhiteSpace()) { var selectedOptions = _dataSerializer.DeserializeAs <IList <ShippingOption> >(order.SelectedShippingOption); model.SelectedShippingOption = string.Join(", ", selectedOptions.Select(x => x.Name)); } if (order.BillingAddressSerialized != null) { var billingAddress = _dataSerializer.DeserializeAs <Address>(order.BillingAddressSerialized); model.BillingAddress = _modelMapper.Map <AddressInfoModel>(billingAddress); model.BillingAddress.StateProvinceName = billingAddress.StateOrProvince?.Name ?? billingAddress.StateProvinceName; model.BillingAddress.CountryName = billingAddress.Country.Name; } if (order.ShippingAddressSerialized != null) { var shippingAddress = _dataSerializer.DeserializeAs <Address>(order.ShippingAddressSerialized); model.ShippingAddress = _modelMapper.Map <AddressInfoModel>(shippingAddress); model.ShippingAddress.StateProvinceName = shippingAddress.StateOrProvince?.Name ?? shippingAddress.StateProvinceName; model.ShippingAddress.CountryName = shippingAddress.Country.Name; } model.OrderItems = order.OrderItems?.Select(Create).ToList(); //when at least one shipment has been added and shipped if (model.OrderItems != null && order.Shipments != null && order.Shipments.Any(x => (int)x.ShipmentStatus > (int)ShipmentStatus.Preparing)) { model.Shipments = order.Shipments.Select(_shipmentModelFactory.Create).ToList(); //remove order items which have already been shipped var shipmentItems = order.Shipments.SelectMany(x => x.ShipmentItems).ToList(); foreach (var orderItemModel in model.OrderItems) { var shippedItemCount = shipmentItems.Where(x => x.OrderItemId == orderItemModel.Id) .Sum(x => x.Quantity); if (orderItemModel.Quantity > shippedItemCount) { //fewer items were shipped orderItemModel.Quantity = orderItemModel.Quantity - shippedItemCount; } else { //we'll remove this item when loop is done orderItemModel.Id = 0; } } //remove all the order items with id 0 while (model.OrderItems.Any(x => x.Id == 0)) { model.OrderItems.Remove(model.OrderItems.First(x => x.Id == 0)); } } if (model.OrderItems != null) { model.Taxes = model.OrderItems.GroupBy(x => new { x.TaxName, x.TaxPercent }).Select(x => new OrderTaxModel() { TaxName = x.Key.TaxName, TaxPercent = x.Key.TaxPercent, Amount = x.Sum(y => y.Tax) }).ToList(); } return(model); }