public ActionResult ProcessCheckout(CheckoutModel checkoutModel) { //Need to submit changes again to make sure cart is still valid RecalculateCart(checkoutModel); if (!ModelState.IsValid) { return View("Index", checkoutModel); } if (checkoutModel.PaymentMethod.Equals("PayPal", StringComparison.OrdinalIgnoreCase)) { return PaypalExpress(checkoutModel, SolutionTypeType.MARK); } if (UserHelper.CustomerSession.IsRegistered) { var orgs = _userClient.GetOrganizationsForCurrentUser(); if (orgs != null) { var org = orgs.SingleOrDefault(); if (org != null) Ch.Cart.OrganizationId = org.MemberId; } var user = _userClient.GetCurrentCustomer(false); // Save addresses to customer address book if (checkoutModel.AddressBook.SaveBillingAddress) { var billing = ConvertToCustomerAddress(checkoutModel.BillingAddress.Address); billing.AddressId = Guid.NewGuid().ToString(); user.Addresses.Add(billing); } if (checkoutModel.AddressBook.SaveShippingAddress) { var shipping = ConvertToCustomerAddress(checkoutModel.ShippingAddress.Address); shipping.AddressId = Guid.NewGuid().ToString(); user.Addresses.Add(shipping); } //Save last ordered date&time to customer profile var lastOrdered = user.ContactPropertyValues.FirstOrDefault(x => x.Name == ContactPropertyValueName.LastOrder); if (lastOrdered != null) { lastOrdered.DateTimeValue = DateTime.UtcNow; } else { user.ContactPropertyValues.Add(new ContactPropertyValue { DateTimeValue = DateTime.UtcNow, Name = ContactPropertyValueName.LastOrder, ValueType = PropertyValueType.DateTime.GetHashCode() }); } _userClient.SaveCustomerChanges(user.MemberId); } else if (checkoutModel.CreateAccount) { var regModel = new RegisterModel(); regModel.InjectFrom(checkoutModel, checkoutModel.BillingAddress.Address); //Save billing address to book var billing = ConvertToCustomerAddress(checkoutModel.BillingAddress.Address); billing.AddressId = Guid.NewGuid().ToString(); regModel.Addresses.Add(billing); //save shipping address to book if (!checkoutModel.UseForShipping) { var shipping = ConvertToCustomerAddress(checkoutModel.ShippingAddress.Address); shipping.AddressId = Guid.NewGuid().ToString(); regModel.Addresses.Add(shipping); } string message; if (!UserHelper.Register(regModel, out message)) { ModelState.AddModelError("", message); return View("Index", checkoutModel); } UserHelper.OnPostLogon(regModel.Email); } if (DoCheckout()) { return RedirectToAction("ProcessCheckout", "Checkout", new { id = Ch.Cart.OrderGroupId }); } return View("Index", checkoutModel); }
public ActionResult ProcessCheckout(CheckoutModel checkoutModel) { //Need to submit changes again to make sure cart is still valid RecalculateCart(checkoutModel); if (!ModelState.IsValid) { return View("Index", checkoutModel); } if (checkoutModel.PaymentMethod.Equals("PayPal", StringComparison.OrdinalIgnoreCase)) { return PaypalExpress(checkoutModel, SolutionTypeType.MARK); } if (UserHelper.CustomerSession.IsRegistered) { var orgs = _userClient.GetOrganizationsForCurrentUser(); if (orgs != null) { var org = orgs.SingleOrDefault(); if (org != null) Ch.Cart.OrganizationId = org.MemberId; } var user = _userClient.GetCurrentCustomer(false); // Save addresses to customer address book if (checkoutModel.AddressBook.SaveBillingAddress) { var billing = ConvertToCustomerAddress(checkoutModel.BillingAddress.Address); billing.AddressId = Guid.NewGuid().ToString(); user.Addresses.Add(billing); } if (checkoutModel.AddressBook.SaveShippingAddress) { var shipping = ConvertToCustomerAddress(checkoutModel.ShippingAddress.Address); shipping.AddressId = Guid.NewGuid().ToString(); user.Addresses.Add(shipping); } //Save last ordered date&time to customer profile var lastOrdered = user.ContactPropertyValues.FirstOrDefault(x => x.Name == ContactPropertyValueName.LastOrder); if (lastOrdered != null) { lastOrdered.DateTimeValue = DateTime.UtcNow; } else { user.ContactPropertyValues.Add(new ContactPropertyValue { DateTimeValue = DateTime.UtcNow, Name = ContactPropertyValueName.LastOrder, ValueType = PropertyValueType.DateTime.GetHashCode() }); } _userClient.SaveCustomerChanges(user.MemberId); } else if (checkoutModel.CreateAccount) { var regModel = new RegisterModel(); regModel.InjectFrom(checkoutModel, checkoutModel.BillingAddress.Address); //Save billing address to book var billing = ConvertToCustomerAddress(checkoutModel.BillingAddress.Address); billing.AddressId = Guid.NewGuid().ToString(); regModel.Addresses.Add(billing); //save shipping address to book if (!checkoutModel.UseForShipping) { var shipping = ConvertToCustomerAddress(checkoutModel.ShippingAddress.Address); shipping.AddressId = Guid.NewGuid().ToString(); regModel.Addresses.Add(shipping); } string message,token; var requireConfirmation = StoreHelper.GetSettingValue("RequireAccountConfirmation", false); if (!UserHelper.Register(regModel, requireConfirmation, out message, out token)) { ModelState.AddModelError("", message); return View("Index", checkoutModel); } if (requireConfirmation) { var user = string.Format("{0} {1}", regModel.FirstName, regModel.LastName); var linkUrl = Url.Action("ConfirmAccount", "Account", new { token, username = regModel.Email }, Request.Url.Scheme); if ( UserHelper.SendEmail(linkUrl, user, regModel.Email, "confirm-account", emailMessage => { //Use default template emailMessage.Html = string.Format( "<b>{0}</b> <br/><br/> To confirm your account, click on the following link:<br/> <br/> <a href='{1}'>{1}</a> <br/>", user, linkUrl); emailMessage.Subject = "Account confirmation"; })) { TempData[GetMessageTempKey(MessageType.Success)] = new[] { "Your account was succesfully created. To confirm your account follow the instruction received in email.".Localize() }; } else { TempData[GetMessageTempKey(MessageType.Error)] = new[] { string.Format("Failed to send confirmation email to {0}.".Localize(), regModel.Email) }; } } else { UserHelper.OnPostLogon(regModel.Email); } } if (DoCheckout()) { return RedirectToAction("ProcessCheckout", "Checkout", new { id = Ch.Cart.OrderGroupId }); } return View("Index", checkoutModel); }