public ActionResult MyAddresses() { var user = _workContextAccessor.GetContext().CurrentUser; if (user == null) { // we should never be here, because the AuthorizeAttribute should // take care of anonymous users. return(new HttpUnauthorizedResult(T("Sign In to visualize your saved addresses.").Text)); } var billingAddresses = _nwazetCommunicationService.GetBillingByUser(user); var shippingAddresses = _nwazetCommunicationService.GetShippingByUser(user); return(View((object)_shapeFactory.ViewModel() .BillingAddresses(billingAddresses) .ShippingAddresses(shippingAddresses))); }
public ActionResult Index(AddressesVM model) { ActionResult result = null; switch (model.Submit) { case "cart": result = RedirectToAction("Index", "ShoppingCart", new { area = "Nwazet.Commerce" }); break; case "save": // costruisce la lista di CheckoutItems in base al contenuto del carrello List <CheckoutItem> items = new List <CheckoutItem>(); foreach (var prod in _shoppingCart.GetProducts()) { items.Add(new CheckoutItem { Attributes = prod.AttributeIdsToValues, LinePriceAdjustment = prod.LinePriceAdjustment, OriginalPrice = prod.OriginalPrice, Price = prod.Price, ProductId = prod.Product.Id, PromotionId = prod.Promotion == null ? null : (int?)(prod.Promotion.Id), Quantity = prod.Quantity, Title = prod.Product.ContentItem.As <TitlePart>().Title }); } var paymentGuid = Guid.NewGuid().ToString(); var charge = new PaymentGatewayCharge("Payment Gateway", paymentGuid); // get Orchard user id var userId = -1; var currentUser = _orchardServices.WorkContext.CurrentUser; if (currentUser != null) { userId = currentUser.Id; } var currency = _currencyProvider.CurrencyCode; var order = _orderService.CreateOrder( charge, items, _shoppingCart.Subtotal(), _shoppingCart.Total(), _shoppingCart.Taxes(), _shoppingCart.ShippingOption, model.ShippingAddress, model.BillingAddress, model.Email, model.PhonePrefix + " " + model.Phone, model.SpecialInstructions, OrderPart.Cancelled, null, false, userId, 0, "", currency); order.LogActivity(OrderPart.Event, "Order created"); var reason = string.Format("Purchase Order {0}", _posServiceIntegration.GetOrderNumber(order.Id)); var payment = new PaymentRecord { Reason = reason, Amount = order.Total, Currency = order.CurrencyCode, ContentItemId = order.Id }; var nonce = _paymentService.CreatePaymentNonce(payment); result = RedirectToAction("Pay", "Payment", new { area = "Laser.Orchard.PaymentGateway", nonce = nonce, newPaymentGuid = paymentGuid }); break; default: model.ShippingAddress = new Address(); model.BillingAddress = new Address(); var thecurrentUser = _orchardServices.WorkContext.CurrentUser; if (thecurrentUser != null) { model.ListAvailableBillingAddress = _nwazetCommunicationService.GetBillingByUser(thecurrentUser); model.ListAvailableShippingAddress = _nwazetCommunicationService.GetShippingByUser(thecurrentUser); model.Email = thecurrentUser.Email; var cel = _nwazetCommunicationService.GetPhone(thecurrentUser); if (cel.Length == 2) { model.PhonePrefix = cel[0]; model.Phone = cel[1]; } } result = View("Index", model); break; } return(result); }
public ActionResult Index(AddressesVM model) { ActionResult result = null; var thecurrentUser = _orchardServices.WorkContext.CurrentUser; switch (model.Submit) { case "cart": result = RedirectToAction("Index", "ShoppingCart", new { area = "Nwazet.Commerce" }); break; case "save": // validate addresses if (!TryUpdateModel(model.ShippingAddressVM) || !TryUpdateModel(model.BillingAddressVM) || !ValidateVM(model.ShippingAddressVM) || !ValidateVM(model.BillingAddressVM) || !ValidateVM(model)) { // in case of error, repopulate the default lists model.ShippingAddressVM = CreateVM(AddressRecordType.ShippingAddress); model.BillingAddressVM = CreateVM(AddressRecordType.BillingAddress); if (thecurrentUser != null) { model.ListAvailableBillingAddress = _nwazetCommunicationService.GetBillingByUser(thecurrentUser); model.ListAvailableShippingAddress = _nwazetCommunicationService.GetShippingByUser(thecurrentUser); } result = View("Index", model); break; } // Hack: based on the address coming in model.ShippingAddressVM, we can compute the actual // destinations to be used for tax computations at this stage var countryName = _addressConfigurationService ?.GetCountry(model.ShippingAddressVM.CountryId) ?.Record?.TerritoryInternalRecord.Name; // costruisce la lista di CheckoutItems in base al contenuto del carrello List <CheckoutItem> items = new List <CheckoutItem>(); foreach (var prod in _shoppingCart.GetProducts()) { items.Add(new CheckoutItem { Attributes = prod.AttributeIdsToValues, LinePriceAdjustment = prod.LinePriceAdjustment, OriginalPrice = prod.OriginalPrice, Price = prod.Product.DiscountPrice >= 0 && prod.Product.DiscountPrice < prod.Product.Price ? _productPriceService.GetDiscountPrice(prod.Product, countryName, null) : _productPriceService.GetPrice(prod.Product, countryName, null), ProductId = prod.Product.Id, PromotionId = prod.Promotion == null ? null : (int?)(prod.Promotion.Id), Quantity = prod.Quantity, Title = prod.Product.ContentItem.As <TitlePart>().Title }); } // check if there are products in the cart if (items.Count > 0) { var paymentGuid = Guid.NewGuid().ToString(); var charge = new PaymentGatewayCharge("Payment Gateway", paymentGuid); // get Orchard user id var userId = -1; var currentUser = _orchardServices.WorkContext.CurrentUser; if (currentUser != null) { userId = currentUser.Id; } // update addresses based on those populated in the form model.ShippingAddress = AddressFromVM(model.ShippingAddressVM); model.BillingAddress = AddressFromVM(model.BillingAddressVM); var currency = _currencyProvider.CurrencyCode; var order = _orderService.CreateOrder( charge, items, _shoppingCart.Subtotal(), _shoppingCart.Total(), _shoppingCart.Taxes(), _shoppingCart.ShippingOption, model.ShippingAddress, model.BillingAddress, model.Email, model.PhonePrefix + " " + model.Phone, model.SpecialInstructions, OrderPart.Pending, //.Cancelled, null, false, userId, 0, "", currency); // update advanced address information var addressPart = order.As <AddressOrderPart>(); if (addressPart != null) { // shipping info addressPart.ShippingCountryName = model.ShippingAddressVM.Country; addressPart.ShippingCountryId = model.ShippingAddressVM.CountryId; addressPart.ShippingCityName = model.ShippingAddressVM.City; addressPart.ShippingCityId = model.ShippingAddressVM.CityId; addressPart.ShippingProvinceName = model.ShippingAddressVM.Province; addressPart.ShippingProvinceId = model.ShippingAddressVM.ProvinceId; // billing addressPart.BillingCountryName = model.BillingAddressVM.Country; addressPart.BillingCountryId = model.BillingAddressVM.CountryId; addressPart.BillingCityName = model.BillingAddressVM.City; addressPart.BillingCityId = model.BillingAddressVM.CityId; addressPart.BillingProvinceName = model.BillingAddressVM.Province; addressPart.BillingProvinceId = model.BillingAddressVM.ProvinceId; } // To properly handle the order's advanced address configuration we need // to call again the providers to store the additional data, because when they // are invoked in Nwazet's IOrderService implementation we can't have access // to the new information yet. If we ever overhaul that module, we should // account for this extensibility requirement. foreach (var oaip in _orderAdditionalInformationProviders) { oaip.StoreAdditionalInformation(order); } order.LogActivity(OrderPart.Event, "Order created"); // we unpublish the order here. The service from Nwazet creates it // and publishes it. This would cause issues whenever a user leaves // mid checkout rather than completing the entire process, because we // would end up having unprocessed orders that are created and published. // By unpublishing, we practically turn the order in a draft. Later, // after processing payments, we publish the order again so it shows // in the "normal" queries and lists. // Note that this is a workaround for order management that only really // works as long as payments are processed and the order published there. // In cases where we may not wish to have payments happen when a new order // is created, this system should be reworked properly. _contentManager.Unpublish(order.ContentItem); // save the addresses for the contact doing the order. _nwazetCommunicationService.OrderToContact(order); var reason = string.Format("Purchase Order {0}", order.OrderKey); var payment = new PaymentRecord { Reason = reason, Amount = order.Total, Currency = order.CurrencyCode, ContentItemId = order.Id }; var nonce = _paymentService.CreatePaymentNonce(payment); result = RedirectToAction("Pay", "Payment", new { area = "Laser.Orchard.PaymentGateway", nonce = nonce, newPaymentGuid = paymentGuid }); } else { _notifier.Information(T("There are no products in the cart. Go back to the catalog and add products.")); result = View("Index", model); } break; default: model.ShippingAddressVM = CreateVM(AddressRecordType.ShippingAddress); model.BillingAddressVM = CreateVM(AddressRecordType.BillingAddress); if (thecurrentUser != null) { model.ListAvailableBillingAddress = _nwazetCommunicationService.GetBillingByUser(thecurrentUser); model.ListAvailableShippingAddress = _nwazetCommunicationService.GetShippingByUser(thecurrentUser); model.Email = thecurrentUser.Email; var cel = _nwazetCommunicationService.GetPhone(thecurrentUser); if (cel.Length == 2) { model.PhonePrefix = cel[0]; model.Phone = cel[1]; } } result = View("Index", model); break; } return(result); }
public ActionResult Index(CheckoutViewModel model) { // This will be the entry point for the checkout process. // This method should probably have parameters to handle displaying its form // with some information already in it in case of validation errors when posting // it. var user = _workContextAccessor.GetContext().CurrentUser; if (!_checkoutHelperService.UserMayCheckout(user, out ActionResult redirect)) { if (redirect != null) { return(redirect); } return(Redirect(RedirectUrl)); } // Try to fetch the model from TempData to handle the case where we have been // redirected here. if (TempData.ContainsKey("CheckoutViewModel")) { model = (CheckoutViewModel)TempData["CheckoutViewModel"]; } model.ShippingRequired = IsShippingRequired(); if (user != null) { // If the user is authenticated, set the model's email and any other information // we can get from the user's contact, but avoid overwriting previous input if (string.IsNullOrWhiteSpace(model.Email)) { model.Email = user.Email; } if (string.IsNullOrWhiteSpace(model.Phone)) { var cel = _nwazetCommunicationService.GetPhone(user); if (cel.Length == 2) { model.PhonePrefix = cel[0]; model.Phone = cel[1]; } } // also load the list of existing addresses for them model.ListAvailableBillingAddress = _nwazetCommunicationService.GetBillingByUser(user); // we are only going to load the shipping addresses if shipping is required if (model.ShippingRequired) { model.ListAvailableShippingAddress = _nwazetCommunicationService.GetShippingByUser(user); } } // attempt to shortcircuit the actions if (model.UseDefaultAddress) { // if the user has saved addresses, preselect the ones used most recently if (model.ListAvailableBillingAddress.Any() && (!model.ShippingRequired || (model.ShippingRequired && model.ListAvailableShippingAddress.Any()))) { model.BillingAddressVM = new AddressEditViewModel(model .ListAvailableBillingAddress // pick the one used/updated most recently .OrderByDescending(a => a.TimeStampUTC) .First()); if (model.ShippingRequired) { model.ShippingAddressVM = new AddressEditViewModel(model .ListAvailableShippingAddress // pick the one used/updated most recently .OrderByDescending(a => a.TimeStampUTC) .First()); } // redirect to next step // Put the model we validated in TempData so it can be reused in the next action. TempData["CheckoutViewModel"] = model; if (model.ShippingRequired) { // Set values into the ShoppingCart storage var country = _addressConfigurationService ?.GetCountry(model.ShippingAddressVM.CountryId); _shoppingCart.Country = _contentManager.GetItemMetadata(country).DisplayText; _shoppingCart.ZipCode = model.ShippingAddressVM.PostalCode; } // we redirect to this post action, where we do validation return(IndexPOST(model)); } } model.BillingAddressVM = CreateVM(AddressRecordType.BillingAddress, model.BillingAddressVM); // test whether shipping will be required for the order, because that will change // what must be displayed for the addresses as well as what happens when we go ahead // with the checkout: if no shipping is required, we can go straight to order review // and then payment. if (model.ShippingRequired) { model.ShippingAddressVM = CreateVM(AddressRecordType.ShippingAddress, model.ShippingAddressVM); } InjectServices(model); FinalizeVM(model); return(View(model)); }
public ActionResult Index(AddressesVM model) { ActionResult result = null; switch (model.Submit) { case "cart": result = RedirectToAction("Index", "ShoppingCart", new { area = "Nwazet.Commerce" }); break; case "save": // costruisce la lista di CheckoutItems in base al contenuto del carrello List <CheckoutItem> items = new List <CheckoutItem>(); foreach (var prod in _shoppingCart.GetProducts()) { items.Add(new CheckoutItem { Attributes = prod.AttributeIdsToValues, LinePriceAdjustment = prod.LinePriceAdjustment, OriginalPrice = prod.OriginalPrice, Price = _productPriceService.GetPrice(prod.Product), ProductId = prod.Product.Id, PromotionId = prod.Promotion == null ? null : (int?)(prod.Promotion.Id), Quantity = prod.Quantity, Title = prod.Product.ContentItem.As <TitlePart>().Title }); } var paymentGuid = Guid.NewGuid().ToString(); var charge = new PaymentGatewayCharge("Payment Gateway", paymentGuid); // get Orchard user id var userId = -1; var currentUser = _orchardServices.WorkContext.CurrentUser; if (currentUser != null) { userId = currentUser.Id; } var currency = _currencyProvider.CurrencyCode; var order = _orderService.CreateOrder( charge, items, _shoppingCart.Subtotal(), _shoppingCart.Total(), _shoppingCart.Taxes(), _shoppingCart.ShippingOption, model.ShippingAddress, model.BillingAddress, model.Email, model.PhonePrefix + " " + model.Phone, model.SpecialInstructions, OrderPart.Pending, //.Cancelled, null, false, userId, 0, "", currency); order.LogActivity(OrderPart.Event, "Order created"); // we unpublish the order here. The service from Nwazet create it // and publish it it. This would cause issues whenever a user leaves // mid checkout rather than completing the entire process, because we // would end up having unprocessed orders that are created and published. // By unpublishing, we practically turn the order in a draft. Later, // after processing payments, we publish the order again so it shows // in the "normal" queries and lists. // Note that this is a workaround for order management that only really // works as long as payments are processed and the order published there. // In cases where we may not wish to have payments happen when a new order // is created, this system should be reworked properly. _contentManager.Unpublish(order.ContentItem); // save the addresses for the contact doing the order. _nwazetCommunicationService.OrderToContact(order); var reason = string.Format("Purchase Order {0}", _posServiceIntegration.GetOrderNumber(order.Id)); var payment = new PaymentRecord { Reason = reason, Amount = order.Total, Currency = order.CurrencyCode, ContentItemId = order.Id }; var nonce = _paymentService.CreatePaymentNonce(payment); result = RedirectToAction("Pay", "Payment", new { area = "Laser.Orchard.PaymentGateway", nonce = nonce, newPaymentGuid = paymentGuid }); break; default: model.ShippingAddress = new Address(); model.BillingAddress = new Address(); var thecurrentUser = _orchardServices.WorkContext.CurrentUser; if (thecurrentUser != null) { model.ListAvailableBillingAddress = _nwazetCommunicationService.GetBillingByUser(thecurrentUser); model.ListAvailableShippingAddress = _nwazetCommunicationService.GetShippingByUser(thecurrentUser); model.Email = thecurrentUser.Email; var cel = _nwazetCommunicationService.GetPhone(thecurrentUser); if (cel.Length == 2) { model.PhonePrefix = cel[0]; model.Phone = cel[1]; } } result = View("Index", model); break; } return(result); }