/// <summary> /// Report User location. Pass Location object in post data. /// </summary> /// <param name="userId"></param> /// <param name="serializedLocation"></param> /// <returns>JSON</returns> public HttpResponseMessage Post(string userId, [FromBody] string serializedLocation) { try { dynamic deserializedLocation = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedLocation); ObjectId refId = ObjectId.Parse(userId.Trim()); ObjectId tncId = ObjectId.Parse(deserializedLocation.TNCId.Value); String uiScreen = deserializedLocation.UIScreen.Value; uiScreen = uiScreen.Replace("com.driveswitch.", "").Replace("Activity", ""); uiScreen = uiScreen.Replace("class ", ""); Double latitude = Convert.ToDouble(deserializedLocation.Latitude.Value); Double longitude = Convert.ToDouble(deserializedLocation.Longitude.Value); Location _location = new Location(refId, tncId, uiScreen, latitude, longitude); _repo.UpdateUserLocation(_location); // Need to return the user here for integrated subscription validation User _user = _repo.GetUser(userId.Trim()); // Check if subscription valid and set event accordingly DateTime currentDate = DateTime.Now; DateTime expiresDate = Convert.ToDateTime(_user.ExpireDate); DateTime currentUtcDate = TimeZoneInfo.ConvertTimeToUtc(currentDate); DateTime expireUtcDate = TimeZoneInfo.ConvertTimeToUtc(expiresDate); TimeSpan difference = currentUtcDate - expireUtcDate; if (difference.TotalSeconds > 0) { // Update expired user in db _user.Expired = true; _repo.UpdateUser(_user, 0.00, 0.00); } var jsonResponse = ""; if (_user != null) { jsonResponse = _repo.SanitizeJsonString(_user.ToJson()); } else { jsonResponse = "{'Status':'User location saved'}"; } var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonResponse, System.Text.Encoding.UTF8, "application/json"); return(response); } catch (Exception ex) { ServiceResponse _response = new ServiceResponse(ex.ToString()); var _tmpResponse = _response.ToJson(); var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(_tmpResponse, System.Text.Encoding.UTF8, "application/json"); return(response); } }
// POST: api/User /// <summary> /// Updates User account. Pass User object in post data. Returns updated User data. /// </summary> /// <param name="userId"></param> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <param name="serializedUser"></param> /// <returns>JSON</returns> public HttpResponseMessage Post(string userId, string latitude, string longitude, [FromBody] string serializedUser) { var jsonResponse = ""; try { User _user = _repo.GetUser(userId.Trim()); dynamic deserializedUser = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedUser); _user.FirstName = deserializedUser.FirstName; _user.LastName = deserializedUser.LastName; _user.DeviceType = deserializedUser.DeviceType; _user.Pwd = deserializedUser.Pwd; _user.Gender = deserializedUser.Gender; // Update Address // Process Zipcode to get ObjectId values where needed Int32 _zipCode = Convert.ToInt32(deserializedUser.Contact.Address.ZipCode); _user.Contact.Address.ZipCode = _zipCode; if (_zipCode != 0) { // Lookup ZipCode data ZipCode myZipcode = _repo.GetZipByValue(_zipCode); _user.Contact.Address.CountryId = myZipcode.CountryId; _user.Contact.Address.StateId = myZipcode.StateId; _user.Contact.Address.CountyId = myZipcode.CountyId; _user.Contact.Address.CityId = myZipcode.CityId; _user.Contact.Address.TimeZoneId = myZipcode.TimeZoneId; } _user.Contact.Address.Address1 = deserializedUser.Contact.Address.Address1; _user.Contact.Address.Address2 = deserializedUser.Contact.Address.Address2; // Update Phone _user.Contact.Phone.PhoneType = deserializedUser.Contact.Phone.PhoneType; _user.Contact.Phone.AreaCode = deserializedUser.Contact.Phone.AreaCode; _user.Contact.Phone.Exchange = deserializedUser.Contact.Phone.Exchange; _user.Contact.Phone.Number = deserializedUser.Contact.Phone.Number; // Update Email _user.Contact.Email.UserName = deserializedUser.Contact.Email.UserName; _user.Contact.Email.Domain = deserializedUser.Contact.Email.Domain; #region Update testing _user.TNCs.Clear(); foreach (var _tnc in deserializedUser.TNCs) { // Lookup TNC in db and use one selected by name TNC currentTNC = _repo.GetTNCByName(_tnc.Name.Value); TNCList _tncItem = new TNCList(); _tncItem._id = currentTNC._id; _tncItem.Name = currentTNC.Name; _user.TNCs.Add(_tncItem); } #endregion // Ensure phone and email is unique bool emailIsUnique = _repo.emailIsUnique(_user); bool phoneIsUnique = _repo.phoneIsUnique(_user); if (!emailIsUnique) { jsonResponse = "{'Result':'Email (" + _user.Contact.Email.UserName + "@" + _user.Contact.Email.Domain + ") already registered'}"; } else if (!phoneIsUnique) { jsonResponse = "{'Result':'Phone (" + _user.Contact.Phone.AreaCode + ") " + _user.Contact.Phone.Exchange + "-" + _user.Contact.Phone.Number + ") already registered'}"; } else { _user = _repo.UpdateUser(_user, Convert.ToDouble(latitude), Convert.ToDouble(longitude)); // Get rid of object identifiers and dates as they break json formatting if (_user != null) { jsonResponse = _repo.SanitizeJsonString(_user.ToJson()); } else { jsonResponse = "{'Result':'UserId (" + userId + ") not registered'}"; } } var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonResponse, System.Text.Encoding.UTF8, "application/json"); return(response); } catch (Exception ex) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(ex.ToJson(), System.Text.Encoding.UTF8, "application/json"); return(response); } }
/// <summary> /// If a User email address is not found, we will generate a new User and return the new User object for registration purposes. Otherwise, we return the registered User data. /// </summary> /// <param name="email"></param> /// <returns>JSON</returns> public HttpResponseMessage Get(string email) { var jsonResponse = ""; //Boolean isExpired = false; try { User _user = _repo.GetUserByEmailAddress(email.Trim()); if (_user == null) // New user { _user = new User(); _user._id = ObjectId.GenerateNewId(); _user.Enabled = false; _user.ExpireDate = DateTime.UtcNow.AddDays(userExpires); var tmpEmail = email.Split('@'); var userName = tmpEmail[0].Trim(); var userDomain = tmpEmail[1].Trim(); Email newEmail = new Email(); newEmail.UserName = userName; newEmail.Domain = userDomain; _user.Contact.Email = newEmail; _repo.CreateUser(_user, 0.00, 0.00); // Get rid of object identifiers and dates as they break json formatting jsonResponse = _repo.SanitizeJsonString(_user.ToJson()); } else { //Boolean isExpired = false; jsonResponse = "{"; jsonResponse += "'Status': '" + email.Trim() + " already registered.', "; jsonResponse += "'Enabled': '" + _user.Enabled + "', "; // Create Subscription check event Event subscriptionEvent = new Event(0.00, 0.00); // Check if subscription valid and set event accordingly DateTime currentDate = DateTime.Now; DateTime expiresDate = Convert.ToDateTime(_user.ExpireDate); DateTime currentUtcDate = TimeZoneInfo.ConvertTimeToUtc(currentDate); DateTime expireUtcDate = TimeZoneInfo.ConvertTimeToUtc(expiresDate); TimeSpan difference = currentUtcDate - expireUtcDate; if (difference.TotalSeconds > 0) { // Account expired //isExpired = true; subscriptionEvent.TypeId = Constants.Event.Account.SubscriptionExpired.Item1; subscriptionEvent.Name = Constants.Event.Account.SubscriptionExpired.Item2; // Update expired user in db _user.Expired = true; _repo.UpdateUser(_user, 0.00, 0.00); } else { //isExpired = false; subscriptionEvent.TypeId = Constants.Event.Account.SubscriptionCurrent.Item1; subscriptionEvent.Name = Constants.Event.Account.SubscriptionCurrent.Item2; } jsonResponse += "'Expired': '" + _user.Expired + "', "; jsonResponse += "'ExpireDate': '" + _user.ExpireDate + "'"; jsonResponse += "}"; jsonResponse = _repo.SanitizeJsonString(jsonResponse); Reference userRef = new Reference(_user._id, Constants.Reference.TypeUser); subscriptionEvent.Reference = userRef; //_repo.SaveEvent(subscriptionEvent); } var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonResponse, System.Text.Encoding.UTF8, "application/json"); return(response); } catch (Exception ex) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(ex.ToJson(), System.Text.Encoding.UTF8, "application/json"); return(response); } }
// POST: api/Transaction /// <summary> /// User subscription renewal transaction. Pass Transaction object in post data. /// </summary> /// <param name="userId"></param> /// <param name="transaction"></param> /// <returns>JSON</returns> public HttpResponseMessage Post(string userId, [FromBody] string transaction) { try { User _user = _repo.GetUser(userId); dynamic deserializedTransaction = Newtonsoft.Json.JsonConvert.DeserializeObject(transaction); #region populate credit card CreditCard myCard = new CreditCard(); myCard.FullName = deserializedTransaction.PaymentCard.FullName; myCard.Number = deserializedTransaction.PaymentCard.Number; myCard.Expires = deserializedTransaction.PaymentCard.Expires; myCard.Zipcode = deserializedTransaction.PaymentCard.Zipcode; myCard.CVVCode = deserializedTransaction.PaymentCard.CVVCode; #endregion #region Determine car type from number var cardType = _repo.GetCardType(myCard.Number); switch (cardType.ToString()) { case "Unknown": myCard.CardTypeId = Constants.Transaction.CreditCard.Unknown.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.Unknown.Item2; break; case "MasterCard": myCard.CardTypeId = Constants.Transaction.CreditCard.MasterCard.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.MasterCard.Item2; break; case "VISA": myCard.CardTypeId = Constants.Transaction.CreditCard.VISA.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.VISA.Item2; break; case "Amex": myCard.CardTypeId = Constants.Transaction.CreditCard.Amex.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.Amex.Item2; break; case "Discover": myCard.CardTypeId = Constants.Transaction.CreditCard.Discover.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.Discover.Item2; break; case "DinersClub": myCard.CardTypeId = Constants.Transaction.CreditCard.DinersClub.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.DinersClub.Item2; break; case "JCB": myCard.CardTypeId = Constants.Transaction.CreditCard.JCB.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.JCB.Item2; break; case "enRoute": myCard.CardTypeId = Constants.Transaction.CreditCard.enRoute.Item1; myCard.CardTypeName = Constants.Transaction.CreditCard.enRoute.Item2; break; } #endregion #region save credit card if requested if (Convert.ToBoolean(deserializedTransaction.SavePaymentMethod)) { Boolean cardExists = false; if (_user.CreditCards.Count > 0) { foreach (CreditCard _card in _user.CreditCards) { if (_repo.NormalizeCardNumber(_card.Number) == _repo.NormalizeCardNumber(myCard.Number)) { cardExists = true; } } } if (!cardExists) { _user.CreditCards.Add(myCard); } } #endregion RenewalPeriod _renewalPeriod = _repo.GetRenewalPeriod(Convert.ToInt16(deserializedTransaction.RenewalPeriod)); Transaction myTransaction = new Transaction(); myTransaction.Date = DateTime.UtcNow; myTransaction.Type = _renewalPeriod.Name; myTransaction.Amount = _renewalPeriod.Amount; myTransaction.ProcessorId = Constants.Transaction.PaymentProcessor.PayPal.Item1; // PayPal myTransaction.PaymentMethodId = myCard._id; myTransaction.PaymentCard = myCard; // Submit to PayPal for processing here... myTransaction.ResultCode = 0; myTransaction.ResultName = "Success"; myTransaction.ResultDetails = "Payment was successful"; // If payment successful, renew user and expire date if (myTransaction.ResultName == "Success") { _user.Expired = false; switch (_renewalPeriod.Period) { case 1: myTransaction.Type = "Month"; _user.ExpireDate = DateTime.UtcNow.AddDays(30); // Month break; case 2: myTransaction.Type = "Quarter"; _user.ExpireDate = DateTime.UtcNow.AddDays(90); // Quarter break; case 3: myTransaction.Type = "Annual"; _user.ExpireDate = DateTime.UtcNow.AddDays(365); // Annual break; } } _user.Transactions.Add(myTransaction); _repo.UpdateUser(_user, 0.00, 0.00); var jsonResponse = _repo.SanitizeJsonString(_user.ToJson()); var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonResponse, System.Text.Encoding.UTF8, "application/json"); return(response); } catch (Exception ex) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(ex.ToJson(), System.Text.Encoding.UTF8, "application/json"); return(response); } }