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);
        }
Пример #2
0
        public OrderPart CreateOrder(
            AddressesVM model,
            string paymentGuid,
            string countryName = null,
            string postalCode  = null)
        {
            var checkoutItems = _shoppingCart.GetProducts()
                                .Select(scp => new CheckoutItem {
                Attributes          = scp.AttributeIdsToValues,
                LinePriceAdjustment = scp.LinePriceAdjustment,
                OriginalPrice       = scp.OriginalPrice,
                Price = scp.Product.DiscountPrice >= 0 && scp.Product.DiscountPrice < scp.Product.Price
                        ? _productPriceService.GetDiscountPrice(scp.Product, countryName, postalCode)
                        : _productPriceService.GetPrice(scp.Product, countryName, postalCode),
                ProductId   = scp.Product.Id,
                PromotionId = scp.Promotion == null ? null : (int?)(scp.Promotion.Id),
                Quantity    = scp.Quantity,
                Title       = _contentManager.GetItemMetadata(scp.Product).DisplayText
            });

            var charge = new PaymentGatewayCharge("Checkout Controller", paymentGuid);
            // 2. Create the Order ContentItem
            var user         = _workContextAccessor.GetContext().CurrentUser;
            var orderContext = new OrderContext {
                WorkContextAccessor = _workContextAccessor,
                ShoppingCart        = _shoppingCart,
                Charge          = charge,
                ShippingAddress = model.ShippingAddress,
                BillingAddress  = model.BillingAddress
            };
            var order = _orderService.CreateOrder(
                charge,
                checkoutItems,
                _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,
                user != null ? user.Id : -1,
                0,
                "",
                _currencyProvider.CurrencyCode,
                _orderAdditionalInformationProviders
                .SelectMany(oaip => oaip.PrepareAdditionalInformation(orderContext)));

            // 2.1. Verify address information in the AddressOrderPart
            //   (we have to do this explicitly because the management of Order
            //   ContentItems does not go through drivers and such)
            var addressPart = order.As <AddressOrderPart>();

            if (addressPart != null)
            {
                // shipping info
                if (model.ShippingAddressVM != null)
                {
                    // may not have a shipping address is shipping isn't required
                    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;
                    // added information to manage saving in bo
                    addressPart.ShippingAddressIsOptional = false;
                }
                else
                {
                    addressPart.ShippingAddressIsOptional = true;
                }
                // 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", _workContextAccessor.GetContext()?.CurrentUser?.UserName ?? (!string.IsNullOrWhiteSpace(model.Email) ? model.Email : "Anonymous"));
            // 2.2. Unpublish the order
            // 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);

            return(order);
        }
Пример #3
0
        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(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);
        }