public ActionResult _EditProfile(FormCollection fc) { Dictionary<string, object> collection = new Dictionary<string, object>(); ErrorCodes errorCode = ErrorCodes.UnknownError; string errorMessage = MyUtility.getErrorMessage(ErrorCodes.UnknownError); collection = MyUtility.setError(errorCode, errorMessage); DateTime registDt = DateTime.Now; if (Request.IsAjaxRequest()) { if (MyUtility.isUserLoggedIn()) { try { string CountryCode = fc["CountryCode"]; string State = String.IsNullOrEmpty(fc["State"]) ? fc["StateDD"] : fc["State"]; if (String.IsNullOrEmpty(fc["FirstName"]) || String.IsNullOrEmpty(fc["LastName"]) || String.IsNullOrEmpty(fc["CountryCode"])) { collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields); return Content(MyUtility.buildJson(collection), "application/json"); } string FirstName = fc["FirstName"]; string LastName = fc["LastName"]; string City = fc["City"]; if (FirstName.Length > 32) { collection = MyUtility.setError(ErrorCodes.LimitReached, "First Name cannot exceed 32 characters."); return Content(MyUtility.buildJson(collection), "application/json"); } if (LastName.Length > 32) { collection = MyUtility.setError(ErrorCodes.LimitReached, "Last Name cannot exceed 32 characters."); return Content(MyUtility.buildJson(collection), "application/json"); } if (!String.IsNullOrEmpty(State)) if (State.Length > 30) { collection = MyUtility.setError(ErrorCodes.LimitReached, "State cannot exceed 30 characters."); return Content(MyUtility.buildJson(collection), "application/json"); } if (!String.IsNullOrEmpty(City)) if (City.Length > 50) { collection = MyUtility.setError(ErrorCodes.LimitReached, "City cannot exceed 50 characters."); return Content(MyUtility.buildJson(collection), "application/json"); } var context = new IPTV2Entities(); /***** CHECK FOR COUNTRY CODE ****/ if (context.Countries.Count(c => String.Compare(c.Code, CountryCode, true) == 0) <= 0) { collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields, "Country Code is invalid."); return Content(MyUtility.buildJson(collection), "application/json"); } else if (GlobalConfig.ExcludedCountriesFromRegistrationDropDown.Split(',').Contains(CountryCode)) { collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields, "Country Code is invalid."); return Content(MyUtility.buildJson(collection), "application/json"); } if (String.IsNullOrEmpty(State)) { collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields, "State is required."); return Content(MyUtility.buildJson(collection), "application/json"); } else { if (context.States.Count(c => c.CountryCode == CountryCode.ToUpper()) > 0) { if (context.States.Count(s => s.CountryCode == CountryCode.ToUpper() && (s.StateCode == State || s.Name == State)) == 0) { collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields, "State is invalid."); return Content(MyUtility.buildJson(collection), "application/json"); } } } //if (String.IsNullOrEmpty(State)) //{ // collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields, "State is required."); // return Content(MyUtility.buildJson(collection), "application/json"); //} //else //{ // if (context.Countries.Count(c => c.Code == CountryCode.ToUpper()) > 0) // { // if (context.States.Count(s => s.CountryCode == CountryCode.ToUpper() && (s.StateCode == State || s.Name == State)) == 0) // { // collection = MyUtility.setError(ErrorCodes.IsMissingRequiredFields, "State is invalid."); // return Content(MyUtility.buildJson(collection), "application/json"); // } // } //} User user = context.Users.FirstOrDefault(u => u.UserId == new System.Guid(User.Identity.Name)); if (user != null) { string CurrencyCode = GlobalConfig.DefaultCurrency; string OldCountryCode = GlobalConfig.DefaultCountry; Country country = context.Countries.FirstOrDefault(c => c.Code == user.CountryCode); if (country != null) { CurrencyCode = country.Currency.Code; country = null; OldCountryCode = user.Country.Code; } UserWallet currentWallet = user.UserWallets.FirstOrDefault(w => w.Currency == CurrencyCode); if (currentWallet == null) //If no wallet, get default USD wallet. currentWallet = user.UserWallets.FirstOrDefault(w => w.Currency == GlobalConfig.DefaultCurrency); string newCountryCode = fc["CountryCode"]; user.FirstName = fc["FirstName"]; user.LastName = fc["LastName"]; user.City = fc["City"]; user.State = String.IsNullOrEmpty(fc["State"]) || fc["State"] == "" ? (String.IsNullOrEmpty(fc["StateDD"]) ? user.State : fc["StateDD"]) : fc["State"]; country = context.Countries.FirstOrDefault(c => c.Code == newCountryCode); if (country != null) { Currency currency = country.Currency; CurrencyCode = (currency == null) ? GlobalConfig.DefaultCurrency : currency.Code; } UserWallet wallet = user.UserWallets.FirstOrDefault(w => w.Currency == CurrencyCode); decimal balance = 0; decimal oldWalletBalance = currentWallet.Balance; string oldCurrency = currentWallet.Currency; var oldGomsWalletId = currentWallet.GomsWalletId; if (wallet == null) // Wallet does not exist. Create new wallet for User. { if (currentWallet != null) { balance = currentWallet.Currency != CurrencyCode ? Forex.Convert(context, currentWallet.Currency, CurrencyCode, currentWallet.Balance) : currentWallet.Balance; //balance = currentWallet.Balance; currentWallet.Balance = 0; currentWallet.IsActive = false; //currentWallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.LastUpdated = registDt; } wallet = ContextHelper.CreateWallet(balance, CurrencyCode, registDt); user.UserWallets.Add(wallet); } else // Wallet already exists. Update the balance only. { if (currentWallet.Currency != wallet.Currency) { balance = currentWallet.Currency != wallet.Currency ? Forex.Convert(context, currentWallet.Currency, wallet.Currency, currentWallet.Balance) : currentWallet.Balance; wallet.Balance = balance; //wallet.Balance += (currentWallet.Balance * 1); wallet.IsActive = true; wallet.LastUpdated = registDt; wallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.Balance = 0; // Deactivate old wallet currentWallet.IsActive = false; //Deactivate //currentWallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.LastUpdated = registDt; } } user.CountryCode = newCountryCode; // Update user country user.LastUpdated = registDt; // lastUpdate // update the user's recurring billing if (!String.IsNullOrEmpty(fc["rb_list"])) UpdateRecurringBillingViaEditProfile(context, user, fc["rb_list"]); if (OldCountryCode != newCountryCode) { var offering = context.Offerings.Find(GlobalConfig.offeringId); //Get User Transactions if (user.HasOtherPendingGomsTransaction(offering)) { errorMessage = "We are still processing your transactions. Please try again after a few minutes."; collection = MyUtility.setError(ErrorCodes.HasPendingChangeCountryTransaction, errorMessage); return Content(MyUtility.buildJson(collection), "application/json"); } if (user.HasPendingGomsChangeCountryTransaction(offering)) { errorMessage = "We are processing your recent change in location. Please try again after a few minutes."; collection = MyUtility.setError(ErrorCodes.HasPendingChangeCountryTransaction, errorMessage); return Content(MyUtility.buildJson(collection), "application/json"); } if (user.HasTVEverywhereEntitlement(MyUtility.StringToIntList(GlobalConfig.TVEverywherePackageIds), offering)) { errorMessage = "You are not allowed to change country being a TV Everywhere user."; collection = MyUtility.setError(ErrorCodes.HasPendingChangeCountryTransaction, errorMessage); return Content(MyUtility.buildJson(collection), "application/json"); } ChangeCountryTransaction transaction = new ChangeCountryTransaction() { OldCountryCode = OldCountryCode, NewCountryCode = newCountryCode, Date = registDt, OfferingId = GlobalConfig.offeringId, Reference = "Change Country", UserId = user.UserId, Amount = 0, NewWalletBalance = balance, OldWalletBalance = oldWalletBalance, OldGomsCustomerId = user.GomsCustomerId, OldGomsWalletId = oldGomsWalletId, Currency = oldCurrency, StatusId = GlobalConfig.Visible }; user.Transactions.Add(transaction); } if (context.SaveChanges() > 0) { //string ProfilePageShare = fc["ProfilePageShare"]; //string EveryoneActivityFeedShare = fc["EveryoneActivityFeedShare"]; //string SocialNetworkShare = fc["SocialNetworkShare"]; string IsInternalSharingEnabled = fc["IsInternalSharingEnabled"]; string IsExternalSharingEnabled = fc["IsExternalSharingEnabled"]; string IsProfilePrivate = fc["IsProfilePrivate"]; UserData userData = new UserData() { IsInternalSharingEnabled = IsInternalSharingEnabled, IsExternalSharingEnabled = IsExternalSharingEnabled, IsProfilePrivate = IsProfilePrivate //ProfilePageShare = ProfilePageShare, //EveryoneActivityFeedShare = EveryoneActivityFeedShare, //SocialNetworkShare = SocialNetworkShare }; var res = GigyaMethods.SetUserData(user.UserId, userData); //setUserData setUserData(user.UserId.ToString(), user); errorMessage = "Your information has been updated successfully."; collection = MyUtility.setError(ErrorCodes.Success, errorMessage); } else { errorMessage = "Error in updating your profile."; collection = MyUtility.setError(ErrorCodes.EntityUpdateError, errorMessage); } } } catch (Exception e) { collection = MyUtility.setError(ErrorCodes.UnknownError, e.Message); } } } return Content(MyUtility.buildJson(collection), "application/json"); }
public ActionResult EditProfile(FormCollection fc) { Dictionary<string, object> collection = new Dictionary<string, object>(); ErrorCodes errorCode = ErrorCodes.UnknownError; string errorMessage = MyUtility.getErrorMessage(ErrorCodes.UnknownError); collection = MyUtility.setError(errorCode, errorMessage); DateTime registDt = DateTime.Now; if (MyUtility.isUserLoggedIn()) { try { var context = new IPTV2Entities(); User user = context.Users.FirstOrDefault(u => u.UserId == new System.Guid(User.Identity.Name)); if (user != null) { string CurrencyCode = GlobalConfig.DefaultCurrency; string OldCountryCode = GlobalConfig.DefaultCountry; Country country = context.Countries.FirstOrDefault(c => String.Compare(c.Code, user.CountryCode, true) == 0); if (country != null) { CurrencyCode = country.Currency.Code; country = null; OldCountryCode = user.Country.Code; } UserWallet currentWallet = user.UserWallets.FirstOrDefault(w => String.Compare(w.Currency, CurrencyCode, true) == 0); if (currentWallet == null) //If no wallet, get default USD wallet. currentWallet = user.UserWallets.FirstOrDefault(w => String.Compare(w.Currency, GlobalConfig.DefaultCurrency, true) == 0); string newCountryCode = fc["CountryCode"]; user.FirstName = fc["FirstName"]; user.LastName = fc["LastName"]; user.City = fc["City"]; user.State = String.IsNullOrEmpty(fc["State"]) || fc["State"] == "" ? (String.IsNullOrEmpty(fc["StateDD"]) ? user.State : fc["StateDD"]) : fc["State"]; country = context.Countries.FirstOrDefault(c => String.Compare(c.Code, newCountryCode, true) == 0); if (country != null) { Currency currency = country.Currency; CurrencyCode = (currency == null) ? GlobalConfig.DefaultCurrency : currency.Code; } UserWallet wallet = user.UserWallets.FirstOrDefault(w => String.Compare(w.Currency, CurrencyCode, true) == 0); decimal balance = 0; decimal oldWalletBalance = currentWallet.Balance; string oldCurrency = currentWallet.Currency; if (wallet == null) // Wallet does not exist. Create new wallet for User. { if (currentWallet != null) { balance = currentWallet.Currency != CurrencyCode ? Forex.Convert(context, currentWallet.Currency, CurrencyCode, currentWallet.Balance) : currentWallet.Balance; //balance = currentWallet.Balance; currentWallet.Balance = 0; currentWallet.IsActive = false; currentWallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.LastUpdated = registDt; } wallet = ContextHelper.CreateWallet(balance, CurrencyCode, registDt); user.UserWallets.Add(wallet); } else // Wallet already exists. Update the balance only. { if (currentWallet.Currency != wallet.Currency) { balance = currentWallet.Currency != wallet.Currency ? Forex.Convert(context, currentWallet.Currency, wallet.Currency, currentWallet.Balance) : currentWallet.Balance; wallet.Balance = balance; //wallet.Balance += (currentWallet.Balance * 1); wallet.IsActive = true; wallet.LastUpdated = registDt; wallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.Balance = 0; // Deactivate old wallet currentWallet.IsActive = false; //Deactivate currentWallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.LastUpdated = registDt; } } user.CountryCode = newCountryCode; // Update user country user.LastUpdated = registDt; // lastUpdate if (OldCountryCode != newCountryCode) { ChangeCountryTransaction transaction = new ChangeCountryTransaction() { OldCountryCode = OldCountryCode, NewCountryCode = newCountryCode, Date = registDt, OfferingId = GlobalConfig.offeringId, Reference = "Change Country", UserId = user.UserId, Amount = 0, NewWalletBalance = balance, OldWalletBalance = oldWalletBalance, Currency = oldCurrency, StatusId = GlobalConfig.Visible }; user.Transactions.Add(transaction); } if (context.SaveChanges() > 0) { //setUserData setUserData(user.UserId.ToString(), user); errorMessage = "Your information has been updated successfully."; collection = MyUtility.setError(ErrorCodes.Success, errorMessage); } else { errorMessage = "Error in updating your profile."; collection = MyUtility.setError(ErrorCodes.EntityUpdateError, errorMessage); } } } catch (Exception e) { collection = MyUtility.setError(ErrorCodes.UnknownError, e.Message); } } return Content(MyUtility.buildJson(collection), "application/json"); }
public ActionResult _EditUserProfile(FormCollection fc) { var ReturnCode = new TransactionReturnType() { StatusCode = (int)ErrorCodes.UnknownError, StatusMessage = String.Empty }; string url = Url.Action("EditYourProfile", "User").ToString(); var field_names = new string[] { "recurring_status", "disabled_list", "enabled_list", "internal_share", "external_share", "private_profile" }; try { if (!User.Identity.IsAuthenticated) return RedirectToAction("EditYourProfile", "User"); DateTime registDt = DateTime.Now; Dictionary<string, string> tmpCollection = fc.AllKeys.ToDictionary(k => k, v => fc[v]); bool isMissingRequiredFields = false; foreach (var x in tmpCollection) { if (!field_names.Contains(x.Key)) if (String.IsNullOrEmpty(x.Value)) { isMissingRequiredFields = true; break; } } if (!isMissingRequiredFields) // process form { string FirstName = tmpCollection["first_name"]; string LastName = tmpCollection["last_name"]; string CountryCode = tmpCollection["country"]; string City = tmpCollection["city"]; string State = tmpCollection["state"]; string browser = Request.UserAgent; if (FirstName.Length > 32) ReturnCode.StatusMessage = "First Name cannot exceed 32 characters."; if (LastName.Length > 32) ReturnCode.StatusMessage = "Last Name cannot exceed 32 characters."; if (State.Length > 30) ReturnCode.StatusMessage = "State cannot exceed 30 characters."; if (City.Length > 50) ReturnCode.StatusMessage = "City cannot exceed 50 characters."; var context = new IPTV2Entities(); var userId = new Guid(User.Identity.Name); User user = context.Users.FirstOrDefault(u => u.UserId == userId); if (user == null) return RedirectToAction("Index", "Home"); if (GlobalConfig.ExcludedCountriesFromRegistrationDropDown.Split(',').Contains(CountryCode)) // check if country is part of the exclusion list first ReturnCode.StatusMessage = "Country does not exist."; else if (context.Countries.Count(c => String.Compare(c.Code, CountryCode, true) == 0) <= 0) // then check if country is part of the list ReturnCode.StatusMessage = "Country does not exist."; if (context.States.Count(s => String.Compare(s.CountryCode, CountryCode, true) == 0) > 0) if (context.States.Count(s => String.Compare(s.CountryCode, CountryCode, true) == 0 && (String.Compare(s.StateCode, State, true) == 0 || String.Compare(s.Name, State, true) == 0)) <= 0) ReturnCode.StatusMessage = "State is invalid for this country."; if (!String.IsNullOrEmpty(ReturnCode.StatusMessage)) { TempData["ErrorMessage"] = ReturnCode; return RedirectToAction("EditYourProfile", "User"); } string currentCountryCode = user.CountryCode; string newCountryCode = CountryCode; string currentCurrencyCode = user.Country.CurrencyCode; string newCurrencyCode = GlobalConfig.DefaultCurrency; //Update information that is not affected by country change user.FirstName = FirstName; user.LastName = LastName; user.LastUpdated = registDt; //Update privacy policy string IsInternalSharingEnabled = fc["internal_share"]; string IsExternalSharingEnabled = fc["external_share"]; string IsProfilePrivate = fc["private_profile"]; try { UserData userData = new UserData() { IsInternalSharingEnabled = IsInternalSharingEnabled, IsExternalSharingEnabled = IsExternalSharingEnabled, IsProfilePrivate = IsProfilePrivate }; GigyaMethods.SetUserData(user.UserId, userData); } catch (Exception) { } // Update recurring billing if (!String.IsNullOrEmpty(fc["disabled_list"])) UpdateRecurringBillingViaEditProfile2(context, user, fc["disabled_list"], false); if (!String.IsNullOrEmpty(fc["enabled_list"])) UpdateRecurringBillingViaEditProfile2(context, user, fc["enabled_list"], true); //Check if country of user changed if (String.Compare(user.CountryCode, CountryCode, true) != 0) { var offering = context.Offerings.Find(GlobalConfig.offeringId); //Get User Transactions if (user.HasOtherPendingGomsTransaction(offering)) ReturnCode.StatusMessage = "We are still processing your transactions. Please try again after a few minutes."; else if (user.HasPendingGomsChangeCountryTransaction(offering)) ReturnCode.StatusMessage = "We are processing your recent change in location. Please try again after a few minutes."; else if (user.HasTVEverywhereEntitlement(MyUtility.StringToIntList(GlobalConfig.TVEverywherePackageIds), offering)) ReturnCode.StatusMessage = "You are not allowed to change country being a TV Everywhere user."; else { var newCountry = context.Countries.FirstOrDefault(c => String.Compare(c.Code, newCountryCode, true) == 0); if (newCountry != null) newCurrencyCode = newCountry.CurrencyCode; var currentWallet = user.UserWallets.FirstOrDefault(w => String.Compare(w.Currency, currentCurrencyCode, true) == 0); if (currentWallet == null) //If no wallet, get default USD wallet. currentWallet = user.UserWallets.FirstOrDefault(w => String.Compare(w.Currency, GlobalConfig.DefaultCurrency, true) == 0); var newWallet = user.UserWallets.FirstOrDefault(w => String.Compare(w.Currency, newCurrencyCode, true) == 0); decimal balance = 0; decimal currentWalletBalance = currentWallet.Balance; var currentGomsWalletId = currentWallet.GomsWalletId; if (newWallet == null) // new wallet currency does not exist. create new wallet for user { if (currentWallet != null) { balance = Forex.Convert(context, currentWallet.Currency, newCurrencyCode, currentWallet.Balance); currentWallet.Balance = 0; currentWallet.IsActive = false; currentWallet.LastUpdated = registDt; var wallet = ContextHelper.CreateWallet(balance, newCurrencyCode, registDt); user.UserWallets.Add(wallet); } } else // new wallet currency exists, update the balance onl { if (String.Compare(newCurrencyCode, currentCurrencyCode, true) != 0) { balance = Forex.Convert(context, currentWallet.Currency, newWallet.Currency, currentWallet.Balance); newWallet.Balance = balance; newWallet.IsActive = true; newWallet.LastUpdated = registDt; newWallet.GomsWalletId = null; // Reset Goms WalletId currentWallet.Balance = 0; // Deactivate old wallet currentWallet.IsActive = false; //Deactivate currentWallet.LastUpdated = registDt; } else { newWallet.GomsWalletId = null; newWallet.LastUpdated = registDt; } } ChangeCountryTransaction transaction = new ChangeCountryTransaction() { OldCountryCode = currentCountryCode, NewCountryCode = newCountryCode, Date = registDt, OfferingId = GlobalConfig.offeringId, Reference = "Change Country", UserId = user.UserId, Amount = 0, NewWalletBalance = balance, OldWalletBalance = currentWalletBalance, OldGomsCustomerId = user.GomsCustomerId, OldGomsWalletId = currentGomsWalletId, Currency = currentCurrencyCode, StatusId = GlobalConfig.Visible }; user.Transactions.Add(transaction); user.CountryCode = CountryCode; } if (!String.IsNullOrEmpty(ReturnCode.StatusMessage)) { TempData["ErrorMessage"] = ReturnCode; return RedirectToAction("EditYourProfile", "User"); } user.State = State; user.City = City; if (context.SaveChanges() > 0) { setUserData(user.UserId.ToString(), user); ReturnCode.StatusCode = (int)ErrorCodes.Success; ReturnCode.StatusHeader = "Profile update complete!"; ReturnCode.StatusMessage = "You should see the changes on your profile automatically."; ReturnCode.StatusMessage2 = String.Empty; TempData["ErrorMessage"] = ReturnCode; return RedirectToAction("Index", "Profile"); } else ReturnCode.StatusMessage = "Ooops! We encountered a problem updating your profile. Please try again later."; } else { user.City = City; user.State = State; if (context.SaveChanges() > 0) { setUserData(user.UserId.ToString(), user); ReturnCode.StatusCode = (int)ErrorCodes.Success; ReturnCode.StatusHeader = "Profile update complete!"; ReturnCode.StatusMessage = "You should see the changes on your profile automatically."; ReturnCode.StatusMessage2 = String.Empty; TempData["ErrorMessage"] = ReturnCode; return RedirectToAction("Index", "Profile"); } else ReturnCode.StatusMessage = "The system encountered an unspecified error while updating your profile. Please contact support."; } } else ReturnCode.StatusMessage = "Please fill in all required fields."; TempData["ErrorMessage"] = ReturnCode; url = Request.UrlReferrer.AbsolutePath; } catch (System.Data.Entity.Validation.DbEntityValidationException en) { MyUtility.LogException((Exception)en, string.Join(",", en.EntityValidationErrors).ToString()); } catch (Exception e) { MyUtility.LogException(e); } return Redirect(url); }
public void ProcessChangeCountryTransactionInGoms(IPTV2Entities context, ChangeCountryTransaction t) { try // added try catch to save GomsRemarks { if (t.GomsTransactionId != null) { throw new Exception("Transaction already processed in GOMS."); } if (!IsUserVerifiedInTv(context, t.UserId)) { throw new Exception("User is not verified in TFC.tv"); } // validate user GomsException validationResult = UserValidation(context, t.UserId); if (!(validationResult is GomsSuccess)) { throw validationResult; } if (t.OldGomsCustomerId != null) { var result = UpdateSubscriber(context, t.UserId); if (result.IsSuccess) { t.GomsTransactionId = result.WalletId; t.GomsTransactionDate = DateTime.Now; t.GomsRemarks = null; Console.WriteLine(String.Format("Successfully processed transaction id: {0}", t.TransactionId)); context.SaveChanges(); } else throw new Exception(result.StatusMessage); } else { t.GomsTransactionId = -1; //Not applicable t.GomsTransactionDate = DateTime.Now; context.SaveChanges(); } } catch (Exception e) { if (e is GomsUserNotRegisteredException) { //var result = RegisterUser(context, t.UserId); var result = RegisterUser2(context, t.UserId); t.GomsTransactionId = result.WalletId; t.GomsTransactionDate = DateTime.Now; context.SaveChanges(); } else { //try //{ // t.GomsRemarks = e.Message; // context.SaveChanges(); //} //catch (Exception ex) //{ // Console.WriteLine("Error in saving GomsRemarks:" + ex.Message); //} throw e; } } }