public static bool ValidateBlogUrlName(this IGstoreDb db, GStoreData.ControllerBase.BaseController controller, string urlName, int storeFrontId, int clientId, int?currentBlogId) { string nameField = "UrlName"; if (string.IsNullOrWhiteSpace(urlName)) { string errorMessage = "URL Name is required \n Please enter a unique URL name for this Blog"; controller.ModelState.AddModelError(nameField, errorMessage); return(false); } if (urlName.Trim().ToLower() == "all") { string errorMessage = "URL Name cannot be 'All'\n Please enter a unique URL name for this Blog"; controller.ModelState.AddModelError(nameField, errorMessage); return(false); } Blog conflict = db.Blogs.Where(p => p.ClientId == clientId && p.StoreFrontId == storeFrontId && p.UrlName.ToLower() == urlName && (p.BlogId != currentBlogId)).FirstOrDefault(); if (conflict == null) { return(true); } string errorConflictMessage = "URL Name '" + urlName + "' is already in use for Blog '" + conflict.Name + "' [" + conflict.BlogId + "] in Store Front '" + conflict.StoreFront.CurrentConfig().Name.ToHtml() + "' [" + conflict.StoreFrontId + "]. \n You must enter a unique URL Name or change the conflicting Blog URL Name."; controller.ModelState.AddModelError(nameField, errorConflictMessage); return(false); }
public static bool MoveStoreFrontFolders(this StoreFrontConfiguration config, string applicationPath, HttpServerUtilityBase server, string oldFolder, BaseController controller) { string oldFolderToMap = config.ClientVirtualDirectoryToMapToStoreFronts(applicationPath) + "/" + oldFolder; string newFolderToMap = config.StoreFrontVirtualDirectoryToMap(applicationPath); string oldFolderPath = server.MapPath(oldFolderToMap); string newFolderPath = server.MapPath(newFolderToMap); //default behavior is to move the old folder to the new name if (System.IO.Directory.Exists(oldFolderPath)) { try { System.IO.Directory.Move(oldFolderPath, newFolderPath); if (controller != null) { controller.AddUserMessage("Store Front Folder Moved", "Store Front folder was moved from '" + oldFolderToMap.ToHtml() + "' to '" + newFolderToMap.ToHtml() + "'", UserMessageType.Success); } return true; } catch (Exception ex) { if (controller != null) { controller.AddUserMessage("Error Moving Client Folder!", "There was an error moving the client folder from '" + oldFolderToMap.ToHtml() + "' to '" + newFolderToMap.ToHtml() + "'. You will need to move the folder manually. Error: " + ex.Message, UserMessageType.Warning); } return false; } } else { try { bool result = config.CreateStoreFrontFolders(applicationPath, server); if (result) { if (controller != null) { controller.AddUserMessage("Folders Created", "Store Front folders were created in : " + newFolderToMap.ToHtml(), UserMessageType.Info); } return true; } return false; } catch (Exception ex) { if (controller != null) { controller.AddUserMessage("Error Creating Store Front Folders!", "There was an error creating the Store Front folders in '" + newFolderToMap.ToHtml() + "'. You will need to create the folders manually. Error: " + ex.Message, UserMessageType.Warning); } return false; } } }
public static bool SendOrderReceipt(this Order order, BaseController controller, out string userMessage, string orderEmailHtmlPartialViewName, string orderEmailTextPartialViewName, string emailAddressOverride = null, string emailNameOverride = null) { if (controller == null) { throw new ArgumentNullException("controller"); } if (order == null) { throw new ArgumentNullException("order"); } if (string.IsNullOrEmpty(orderEmailTextPartialViewName)) { throw new ArgumentNullException("orderEmailTextPartialViewName"); } if (string.IsNullOrEmpty(orderEmailHtmlPartialViewName)) { throw new ArgumentNullException("orderEmailHtmlPartialViewName"); } Client client = controller.CurrentClientOrThrow; string emailName = order.FullName; string emailAddress = order.Email; if (!string.IsNullOrEmpty(emailNameOverride)) { emailName = emailNameOverride; } if (!string.IsNullOrEmpty(emailAddressOverride)) { emailAddress = emailAddressOverride; } string subject = order.ReceiptSubject(controller.CurrentStoreFrontConfigOrThrow); string orderEmailHtml = null; try { orderEmailHtml = controller.RenderPartialView(orderEmailHtmlPartialViewName, order); } catch (Exception ex) { throw new ApplicationException("Error running order email HTML partial view: " + orderEmailHtmlPartialViewName, ex); } string orderEmailText = null; try { orderEmailText = controller.RenderPartialView(orderEmailTextPartialViewName, order); } catch (Exception ex) { throw new ApplicationException("Error running order email Text partial view: " + orderEmailTextPartialViewName, ex); } bool emailResult; try { emailResult = controller.SendEmail(emailAddress, emailName, subject, orderEmailText, orderEmailHtml); } catch (Exception ex) { if (controller.CurrentUserProfileOrNull != null && controller.CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin()) { controller.AddUserMessage("Email failed", "Order Email failed with exception: " + ex.Message + "\n" + ex.ToString(), UserMessageType.Danger); } emailResult = false; } IGstoreDb db = controller.GStoreDb; db.LogUserActionEvent(controller.HttpContext, controller.RouteData, controller, UserActionCategoryEnum.Orders, UserActionActionEnum.Orders_ReceiptSentToEmail, "Order Receipt", emailResult, orderNumber: order.OrderNumber); if (emailResult) { userMessage = "Receipt for Order #" + order.OrderNumber + " was emailed to " + emailAddress; } else { if (!Settings.AppEnableEmail) { userMessage = "Sorry, we cannot Email an order receipt because Email is not enabled for this server."; return false; } if (!client.UseSendGridEmail) { userMessage = "Sorry, we cannot Email an order receipt because Email is not enabled for this store front."; return false; } userMessage = "There was an error emailing receipt for Order #" + order.OrderNumber + " to " + emailAddress + ". Please Contact us if you do not receive it."; return false; } return emailResult; }
/// <summary> /// Uses controller for StoreFrontConfig, AddUserMessage, and UserProfile, and GStoreDb /// </summary> /// <param name="cart"></param> /// <param name="controller"></param> /// <returns></returns> public static ActionResult ProcessOrderAndPayment(this Cart cart, BaseController controller, string orderEmailHtmlPartialViewName = "_OrderEmailHtmlPartial", string orderEmailTextPartialViewName = "_OrderEmailTextPartial") { //process order if (controller == null) { throw new ArgumentNullException("controller", "controller needed to process an order and payment."); } if (cart == null) { throw new ArgumentNullException("cart"); } if (cart.OrderId.HasValue) { throw new ApplicationException("cart.OrderId.HasValue is set. This cart already has been processed and converted into an order. Order Id: " + cart.OrderId.Value); } if (cart.StatusPlacedOrder) { throw new ApplicationException("cart.StatusPlacedOrder = true. This cart already has been processed and converted into an order."); } StoreFrontConfiguration config = controller.CurrentStoreFrontConfigOrThrow; IGstoreDb db = controller.GStoreDb; UserProfile userProfile = controller.CurrentUserProfileOrNull; Payment payment = null; if (config.PaymentMethod_PayPal_Enabled) { try { payment = cart.ProcessPayPalPaymentForOrderAndSavePayment(config, db); } catch (PayPalExceptionOAuthFailed exOAuth) { string message = "Sorry, this store's configuration for PayPal OAuth is not operational. Please contact us for other payment options." + (exOAuth.IsSandbox ? "\nError in Sandbox Config." : "\nError in Live Config"); controller.AddUserMessage("PayPal Error", message, UserMessageType.Danger); if (userProfile != null && userProfile.AspNetIdentityUserIsInRoleSystemAdmin()) { string adminMessage = exOAuth.ToString() + "\n\nHTTP Response:\n" + exOAuth.ResponseString + "\n\nHTTP Headers:\n" + exOAuth.ResponseHeaders; controller.AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger); } cart.StatusPaymentInfoConfirmed = false; db.Carts.Update(cart); db.SaveChanges(); return controller.RedirectToActionResult("PaymentInfo", "Checkout"); } catch (PayPalExceptionCreatePaymentFailed exPaymentFailed) { string message = "Sorry, there was an error sending your order to PayPal for payment. Please contact us for other payment options." + (exPaymentFailed.IsSandbox ? "\nError in Sandbox." : "\nError in Live Site."); controller.AddUserMessage("PayPal Error", message, UserMessageType.Danger); if (userProfile != null && userProfile.AspNetIdentityUserIsInRoleSystemAdmin()) { string adminMessage = exPaymentFailed.ToString() + "\n\nHTTP Response:\n" + exPaymentFailed.ResponseString + "\n\nHTTP Headers:\n" + exPaymentFailed.ResponseHeaders; controller.AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger); } cart.StatusPaymentInfoConfirmed = false; db.Carts.Update(cart); db.SaveChanges(); return controller.RedirectToActionResult("PaymentInfo", "Checkout"); } catch (Exception ex) { string message = "Sorry, there was an error starting starting your order with PayPal. Please contact us for other payment options."; controller.AddUserMessage("PayPal Error", message, UserMessageType.Danger); if (userProfile != null && userProfile.AspNetIdentityUserIsInRoleSystemAdmin()) { string adminMessage = "Exception: " + ex.ToString(); controller.AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger); } cart.StatusPaymentInfoConfirmed = false; db.Carts.Update(cart); db.SaveChanges(); return controller.RedirectToActionResult("PaymentInfo", "Checkout"); } if (payment.PaymentFailed) { //fire the user back to "enter payment info" cart.StatusPaymentInfoConfirmed = false; db.Carts.Update(cart); db.SaveChanges(); controller.AddUserMessage("Payment Failed!", "Sorry, there was a problem processing your payment with PayPal. Please try again or contact us if you continue to get this error.", UserMessageType.Danger); if (userProfile != null && userProfile.AspNetIdentityUserIsInRoleSystemAdmin()) { string adminMessage = "PayPal Payment Failed see JSON\n" + payment.Json; controller.AddUserMessage("PayPal Error (admin info)", "Error " + adminMessage, UserMessageType.Danger); } return controller.RedirectToActionResult("PaymentInfo", "Checkout"); } } else { //no payment method in store, continue with pay after order flow } Order order = cart.CreateOrderFromCartAndSave(config, payment, db); cart.OrderId = order.OrderId; cart.StatusPlacedOrder = true; cart = db.Carts.Update(cart); if (payment != null) { payment.OrderId = order.OrderId; payment = db.Payments.Update(payment); } Discount discount = cart.Discount; if (discount != null) { discount.UseCount++; discount = db.Discounts.Update(discount); } db.SaveChanges(); db.LogUserActionEvent(controller.HttpContext, controller.RouteData, controller, UserActionCategoryEnum.Checkout, UserActionActionEnum.Checkout_PlacedOrder, "", true, cartId: cart.CartId, orderNumber: order.OrderNumber); db.CreateNewOrderNotificationToOrderAdminAndSave(config, userProfile, order, controller.Url); string userMessage; bool emailResult = order.SendOrderReceipt(controller, out userMessage, orderEmailHtmlPartialViewName, orderEmailTextPartialViewName); if (emailResult) { controller.AddUserMessage("Your Order is Placed!", userMessage, UserMessageType.Info); } else { controller.AddUserMessage("Your Order is Placed", userMessage, UserMessageType.Danger); } return new RedirectResult(controller.Url.Action(actionName: "View", controllerName: "OrderStatus", routeValues: new { id = order.OrderNumber, Email = order.Email })); }
/// <summary> /// Migrates anonymous orders to a new user profile; used when user signs up to bring their anonymous orders with them /// saves changes when done /// </summary> /// <param name="storeFront"></param> /// <param name="db"></param> /// <param name="userProfile"></param> /// <param name="controller"></param> public static void MigrateOrdersToNewProfile(this StoreFront storeFront, IGstoreDb db, UserProfile userProfile, BaseController controller) { if (storeFront == null) { throw new ArgumentNullException("storeFront"); } if (userProfile == null) { throw new ArgumentNullException("userProfile"); } if (controller == null) { throw new ArgumentNullException("controller"); } List<Order> orders = storeFront.Orders.Where(o => o.UserProfileId == null && o.Email.ToLower() == userProfile.Email.ToLower()).ToList(); foreach (Order order in orders) { order.UserProfileId = userProfile.UserProfileId; db.Orders.Update(order); } if (orders.Count != 0) { db.SaveChanges(); controller.AddUserMessage("Orders Available", "Your orders (" + orders.Count.ToString("N0") + ") are now available on your profile page.", UserMessageType.Info); } }
public static StoreBinding CreatAutoMapStoreBindingToCatchAll(this IGstoreDb storeDb, BaseController baseController) { UserProfile profile = storeDb.SeedAutoMapUserBestGuess(); StoreFrontConfiguration storeFrontConfig = storeDb.SeedAutoMapStoreFrontConfigBestGuess(); IGstoreDb systemDb = storeDb; systemDb.UserName = profile.UserName; systemDb.CachedStoreFront = storeFrontConfig.StoreFront; systemDb.CachedUserProfile = profile; string urlStoreName = baseController.RouteData.UrlStoreName(); StoreBinding binding = systemDb.CreateSeedStoreBindingToCatchAll(storeFrontConfig, urlStoreName); HttpRequestBase request = baseController.Request; StoreFrontConfiguration config = binding.StoreFront.CurrentConfigOrAny(); string storeFrontName = config == null ? "No config found for Store Front Id " + binding.StoreFrontId : config.Name; string message = "--Bindings Catch-All auto-mapped to StoreFront '" + storeFrontName + "' [" + binding.StoreFront.StoreFrontId + "]" + " For HostName: " + request.BindingHostName() + " Port: " + request.BindingPort() + " RootPath: " + request.BindingRootPath() + " UrlStoreName: " + request.BindingUrlStoreName() + " UseUrlStoreName: " + binding.UseUrlStoreName.ToString() + " UrlStoreName: " + binding.UrlStoreName + " From RawUrl: " + request.RawUrl + " QueryString: " + request.QueryString + " ContentLength: " + request.ContentLength + " HTTPMethod: " + request.HttpMethod + " Client IP: " + request.UserHostAddress; System.Diagnostics.Trace.WriteLine(message); EventLogExtensions.LogSystemEvent(systemDb, baseController.HttpContext, baseController.RouteData, baseController.RouteData.ToSourceString(), SystemEventLevel.Information, message, string.Empty, string.Empty, string.Empty, baseController); return binding; }