public async Task <ActionResult> Register(string username, string email, string password, string phone, string firstName, string lastName) { UserManager <IdentityUser> manager = HttpContext.GetOwinContext().Get <UserManager <IdentityUser> >(); if (ModelState.IsValid) { var user = new IdentityUser() { UserName = username, Email = email, EmailConfirmed = false, }; var result = await manager.CreateAsync(user, password); if (result.Succeeded) { BraintreeService service = new BraintreeService(_braintreeGateway); await service.GetCustomerId(email, phone, firstName, lastName); string confirmationToken = await manager.GenerateEmailConfirmationTokenAsync(user.Id); string confirmationLink = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Confirm/" + user.Id + "?token=" + confirmationToken; string htmlContent = string.Format("<a href=\"{0}\">Confirm Your Account</a>", confirmationLink); await manager.SendEmailAsync(user.Id, "Confirm your Outdoor Gear Rental Account", htmlContent); TempData["EmailAddress"] = email; return(RedirectToAction("ConfirmationSent")); } else { ViewBag.Error = result.Errors; } } return(View()); }
public async Task <ActionResult> Index(CheckoutModel model) { //Check if the model-state is valid -- this will catch anytime someone hacks your client-side validation if (ModelState.IsValid) { if (model.CustomerId == null) { BraintreeService service = new BraintreeService(this._braintreeGateway); model.CustomerId = await service.GetCustomerId(model.Email, model.Phone, model.FirstName, model.LastName); } if ((model.CardToken == "NewCard") || (model.CardToken == null)) { Braintree.CreditCardRequest card = new Braintree.CreditCardRequest(); card.Number = model.CreditCardNumber; card.CVV = model.CreditCardVerificationValue; card.ExpirationMonth = model.CreditCardExpirationMonth.ToString().PadLeft(2, '0'); card.ExpirationYear = model.CreditCardExpirationYear.ToString(); card.CardholderName = model.BillingFirstName + " " + model.BillingLastName; card.CustomerId = model.CustomerId; var cardResult = await _braintreeGateway.CreditCard.CreateAsync(card); model.CardToken = cardResult.Target.Token; } if ((model.BillingAddressId == "NewAddress") || (model.BillingAddressId == null)) { Braintree.AddressRequest address = new Braintree.AddressRequest(); address.StreetAddress = model.BillingAddressLine1 + " " + model.BillingAddressLine2; address.CountryName = model.BillingCountry; address.Locality = model.BillingCity; address.PostalCode = model.BillingPostalCode; address.Region = model.BillingState; var addressResult = await _braintreeGateway.Address.CreateAsync(model.CustomerId, address); model.BillingAddressId = addressResult.Target.Id; } if ((model.ShippingAddressId == "NewAddress") || (model.ShippingAddressId == null)) { if ((model.ShippingAddressLine1 == model.BillingAddressLine1) && (model.ShippingAddressLine2 == model.BillingAddressLine2) && (model.ShippingCity == model.BillingCity) && (model.ShippingPostalCode == model.BillingPostalCode) && (model.ShippingState == model.BillingState) && (model.ShippingCountry == model.BillingCountry)) { model.ShippingAddressId = model.BillingAddressId; } else { Braintree.AddressRequest address = new Braintree.AddressRequest(); address.StreetAddress = model.ShippingAddressLine1 + " " + model.ShippingAddressLine2; address.CountryName = model.ShippingCountry; address.Locality = model.ShippingCity; address.PostalCode = model.ShippingPostalCode; address.Region = model.ShippingState; var addressResult = await _braintreeGateway.Address.CreateAsync(model.CustomerId, address); model.ShippingAddressId = addressResult.Target.Id; } } Shipment s = new Shipment { AddressLine1 = model.ShippingAddressLine1, AddressLine2 = model.ShippingAddressLine2, City = model.ShippingCity, State = model.ShippingState, PostalCode = model.ShippingPostalCode, Country = model.ShippingCountry, Modified = DateTime.UtcNow, Created = DateTime.UtcNow }; Purchase p = new Purchase { SubmittedDate = DateTime.UtcNow, AspNetUserID = User.Identity.IsAuthenticated ? db.AspNetUsers.First(x => x.UserName == User.Identity.Name).Id : null, Created = DateTime.UtcNow, Modified = DateTime.UtcNow, OrderIdentifier = Guid.NewGuid().ToString().Substring(0, 8), PurchaseProducts = this.GetBasket(db).BasketProducts.Select(x => new Models.PurchaseProduct { ProductID = x.ProductID, Quantity = x.Quantity, Created = DateTime.UtcNow, Modified = DateTime.UtcNow, ProductName = x.Product.Name, ProductPrice = x.Product.Price, Shipment = s, }).ToArray() }; db.Purchases.Add(p); db.Baskets.Remove(this.GetBasket(db)); db.SaveChanges(); Braintree.TransactionRequest transaction = new Braintree.TransactionRequest(); transaction.Amount = this.GetBasket(db).BasketProducts.Sum(x => x.Quantity * (x.Product.Price ?? 0)); transaction.CustomerId = model.CustomerId; transaction.PaymentMethodToken = model.CardToken; transaction.OrderId = p.ID.ToString(); transaction.PurchaseOrderNumber = p.OrderIdentifier; var transactionResult = await _braintreeGateway.Transaction.SaleAsync(transaction); await _emailService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage { Subject = string.Format("Your Coding Cookware Order {0}", p.OrderIdentifier), Destination = model.Email, Body = CreateReceiptEmail(p) }); if (!string.IsNullOrEmpty(model.Phone)) { await _smsService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage { Subject = "", Destination = model.Phone, Body = "You placed order " + p.OrderIdentifier }); } return(RedirectToAction("Index", "Receipt", new { id = p.OrderIdentifier })); } ; return(View()); }