public async Task <IActionResult> NewLicense() { var cardResult = await cardDataStore.GetCardFromUser(LogUser.LoginUser.UserId, LogUser.Token.Token); if (cardResult.Count() > 0) { var subcriptionToken = await stripeServiceDS.CreateACustomerSubcription(LogUser.LoginUser.StripeUserId); if (!string.IsNullOrEmpty(subcriptionToken)) { var newStoreLicense = new StoreLicense() { LicenseId = Guid.NewGuid(), StartDate = DateTime.Today, LicenseHolderUserId = LogUser.LoginUser.UserId }; //Lo insertamos a nuestra base de datos var storelicenceresult = await storeLicenseDataStore.AddItemAsync(newStoreLicense); if (storelicenceresult) { var subcription = new Library.Models.Subcription() { StripeCustomerId = LogUser.LoginUser.StripeUserId, StripeSubCriptionID = subcriptionToken, StoreLicense = newStoreLicense.LicenseId, IsDisable = false }; var subcriptionResult = await subcriptionDataStore.AddItemAsync(subcription); if (subcriptionResult) { var licenseReuslt = await storeLicenseDataStore.PostStoreLicense(LogUser.LoginUser.Email, LogUser.LoginUser.Name); return(RedirectToAction("RegisterControl")); } } } } return(RedirectToAction("RegisterControl")); }
public async Task <IActionResult> UserRegistration(UserViewModel userVm) { if (UserInfoNotNullOrEmpty(userVm)) { if (userVm.Password == userVm.ConfirmPassword) { var credentialExist = await userDataStore.CheckIfUsernameAndPasswordExist(userVm.Username, userVm.Password); if (!credentialExist) { //Las credenciales de entrada del usuario var userLogin = new Login() { LoginId = Guid.NewGuid(), Password = userVm.Password, Username = userVm.Username }; //Creamos el usuario var newUser = new User() { UserId = Guid.NewGuid(), Email = userVm.Email, LoginId = userLogin.LoginId, Name = userVm.Name, UserLogin = userLogin, Address = userVm.Address }; //Obtenemos datos de empleados necesarios para el registro en stripe var optionsCustomers = new UserDTO { Name = userVm.Name, Email = userVm.Email, Phone = userVm.Phone, Address = userVm.Address, }; //Creamos un nuevo usuario en stripe var customerToken = await StripeServiceDS.CreateStripeCustomer(optionsCustomers); //Le insertamos la tarjeta al nuevo usuario de stripe var cardservicetokenId = await StripeServiceDS.InsertStripeCardToCustomer(PaymentCard, customerToken); //Creamos la subcripcion al usuario var subcriptiontoken = await StripeServiceDS.CreateACustomerSubcription(customerToken); //Verificamos que la informacion este correcta if (!string.IsNullOrEmpty(subcriptiontoken)) { //Le damos el id del nuevo customer de la cuenta de stripe. newUser.StripeUserId = customerToken; //Lo agregamos a la base de datos. var addedUser = await userDataStore.AddItemAsync(newUser); //Verificamos si el usuario se inserto a nuestra base de datos if (addedUser) { //Verificamos si el token de la tarjeta insertada es correcta. if (!string.IsNullOrEmpty(cardservicetokenId)) { //Agregamos la tarjeta a nuestra base de datos. var cardadded = cardDataStore.AddItemAsync(new PaymentCard() { UserId = newUser.UserId, StripeCardId = cardservicetokenId, CardNumber = PaymentCard.CardNumber, Cvc = PaymentCard.Cvc, Month = PaymentCard.Month, Year = PaymentCard.Year, HolderName = PaymentCard.HolderName, PaymentCardId = Guid.NewGuid() }); } //Creamos el lincense var newStoreLicense = new StoreLicense() { LicenseId = Guid.NewGuid(), StartDate = DateTime.Today, LicenseHolderUserId = newUser.UserId }; //Lo insertamos a nuestra base de datos var storelicenceresult = await storeLicenseDataStore.AddItemAsync(newStoreLicense); //Verificamos el resultado if (storelicenceresult) { var subcription = new Subcription() { IsDisable = false, StripeCustomerId = customerToken, StripeSubCriptionID = subcriptiontoken, StoreLicense = newStoreLicense.LicenseId }; var result = SubcriptionDataStore.AddItemAsync(subcription); //Enviamos el email con el codio de la nueva licensia. SendStoreLicenceEmailCode(newUser.Email, newStoreLicense.LicenseId.ToString()); } //Verificamos que los credenciales esten correctos. var resultCredentials = userDataStore.CheckUserCredential(userLogin.Username, userLogin.Password); //Validamos que el resultado no sea vacio if (resultCredentials != null) { //Le damos los credenciales al LogUser LogUser.LoginUser = resultCredentials; } //Luego de todo completado de manera correcta nos vamos a registrar una tienda. return(RedirectToAction("RegisterStore", "Store")); } } else { return(View()); } } else { ViewBag.ErrorMsg = "The creditals already exists."; return(View()); } } else { ViewBag.ErrorMsg = "The password and confirm pasword are different."; return(View()); } } return(View()); }