public UpdatedSaleSummary UpdateShipRateQuote(string customerToken, string methodKey)
        {
            var tokenKey     = customerToken.DecryptWithMachineKey();
            var customerKey  = new Guid(tokenKey);
            var customerBase = _merchelloContext.Services.CustomerService.GetAnyByKey(customerKey);

            var checkoutManager = customerBase.Basket().GetCheckoutManager();
            var shippingManager = checkoutManager.Shipping;
            var customerManager = checkoutManager.Customer;
            var paymentManager  = checkoutManager.Payment;


            var shipment = customerBase.Basket().PackageBasket(customerManager.GetShipToAddress()).FirstOrDefault();
            var quote    = shipment.ShipmentRateQuoteByShipMethod(methodKey);

            if (quote != null)
            {
                shippingManager.ClearShipmentRateQuotes();
                shippingManager.SaveShipmentRateQuote(quote);
            }

            var invoice = paymentManager.PrepareInvoice();

            var summary = new UpdatedSaleSummary
            {
                TotalLabel     = "Total",
                InvoiceTotal   = ModelExtensions.FormatPrice(invoice.Total, _currency),
                TaxTotal       = ModelExtensions.FormatPrice(invoice.TaxLineItems().Sum(x => x.TotalPrice), _currency),
                DiscountsTotal = ModelExtensions.FormatPrice(invoice.DiscountLineItems().Sum(x => x.TotalPrice), _currency),
                ShippingTotal  = ModelExtensions.FormatPrice(invoice.ShippingLineItems().Sum(x => x.TotalPrice), _currency)
            };

            return(summary);
        }
Beispiel #2
0
        /// <summary>
        /// Builds a <see cref="SalePreparationSummary"/>
        /// </summary>
        /// <param name="manager">
        /// The preparation.
        /// </param>
        /// <returns>
        /// The <see cref="SalePreparationSummary"/>.
        /// </returns>
        public SalePreparationSummary Build(ICheckoutManagerBase manager)
        {
            if (manager.Payment.IsReadyToInvoice())
            {
                var invoice = manager.Payment.PrepareInvoice();
                return(new SalePreparationSummary()
                {
                    TotalLabel = "Total",
                    Currency = _currency,
                    Items = invoice.Items.Where(x => x.LineItemType == LineItemType.Product).Select(x => _basketLineItemFactory.Build(x)),
                    ItemTotal = ModelExtensions.FormatPrice(invoice.TotalItemPrice(), _currency),
                    ShippingTotal = ModelExtensions.FormatPrice(invoice.TotalShipping(), _currency),
                    TaxTotal = ModelExtensions.FormatPrice(invoice.TotalTax(), _currency),
                    DiscountsTotal = ModelExtensions.FormatPrice(invoice.TotalDiscounts(), _currency),
                    InvoiceTotal = ModelExtensions.FormatPrice(invoice.Total, _currency),
                });
            }

            return(new SalePreparationSummary()
            {
                TotalLabel = "Sub Total",
                Currency = _currency,
                Items = manager.Context.ItemCache.Items.Select(x => _basketLineItemFactory.Build(x)),
                ItemTotal = ModelExtensions.FormatPrice(manager.Context.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Product).Sum(x => x.TotalPrice), _currency),
                ShippingTotal = ModelExtensions.FormatPrice(manager.Context.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Shipping).Sum(x => x.TotalPrice), _currency),
                TaxTotal = ModelExtensions.FormatPrice(manager.Context.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Tax).Sum(x => x.TotalPrice), _currency),
                DiscountsTotal = ModelExtensions.FormatPrice(manager.Context.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Discount).Sum(x => x.TotalPrice), _currency),
                InvoiceTotal = ModelExtensions.FormatPrice(manager.Context.ItemCache.Items.Where(x => x.LineItemType != LineItemType.Discount).Sum(x => x.TotalPrice) - manager.Context.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Discount).Sum(x => x.TotalPrice), _currency),
            });
        }
        /// <summary>
        /// Builds a <see cref="SalePreparationSummary"/>
        /// </summary>
        /// <param name="preparation">
        /// The preparation.
        /// </param>
        /// <returns>
        /// The <see cref="SalePreparationSummary"/>.
        /// </returns>
        public SalePreparationSummary Build(SalePreparationBase preparation)
        {
            if (preparation.IsReadyToInvoice())
            {
                var invoice = preparation.PrepareInvoice();
                return(new SalePreparationSummary()
                {
                    TotalLabel = "Total",
                    CurrencySymbol = _currency.Symbol,
                    Items = invoice.Items.Where(x => x.LineItemType == LineItemType.Product).Select(x => _basketLineItemFactory.Build(x)),
                    ItemTotal = ModelExtensions.FormatPrice(invoice.TotalItemPrice(), _currency.Symbol),
                    ShippingTotal = ModelExtensions.FormatPrice(invoice.TotalShipping(), _currency.Symbol),
                    TaxTotal = ModelExtensions.FormatPrice(invoice.TotalTax(), _currency.Symbol),
                    DiscountsTotal = ModelExtensions.FormatPrice(invoice.TotalDiscounts(), _currency.Symbol),
                    InvoiceTotal = ModelExtensions.FormatPrice(invoice.Total, _currency.Symbol)
                });
            }

            return(new SalePreparationSummary()
            {
                TotalLabel = "Sub Total",
                CurrencySymbol = _currency.Symbol,
                Items = preparation.ItemCache.Items.Select(x => _basketLineItemFactory.Build(x)),
                ItemTotal = ModelExtensions.FormatPrice(preparation.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Product).Sum(x => x.TotalPrice), _currency.Symbol),
                ShippingTotal = ModelExtensions.FormatPrice(preparation.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Shipping).Sum(x => x.TotalPrice), _currency.Symbol),
                TaxTotal = ModelExtensions.FormatPrice(preparation.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Tax).Sum(x => x.TotalPrice), _currency.Symbol),
                DiscountsTotal = ModelExtensions.FormatPrice(preparation.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Discount).Sum(x => x.TotalPrice), _currency.Symbol),
                InvoiceTotal = ModelExtensions.FormatPrice(preparation.ItemCache.Items.Where(x => x.LineItemType != LineItemType.Discount).Sum(x => x.TotalPrice) - preparation.ItemCache.Items.Where(x => x.LineItemType == LineItemType.Discount).Sum(x => x.TotalPrice), _currency.Symbol)
            });
        }
Beispiel #4
0
        public UpdatedSaleSummary UpdateShipRateQuote(string customerToken, string methodKey)
        {
            var tokenKey     = customerToken.DecryptWithMachineKey();
            var customerKey  = new Guid(tokenKey);
            var customerBase = _merchelloContext.Services.CustomerService.GetAnyByKey(customerKey);

            var preparation = customerBase.Basket().SalePreparation();

            preparation.RaiseCustomerEvents = false;

            var shipment = customerBase.Basket().PackageBasket(preparation.GetShipToAddress()).FirstOrDefault();
            var quote    = shipment.ShipmentRateQuoteByShipMethod(methodKey);

            if (quote != null)
            {
                preparation.ClearShipmentRateQuotes();
                preparation.SaveShipmentRateQuote(quote);
            }

            var invoice = preparation.PrepareInvoice();

            var summary = new UpdatedSaleSummary()
            {
                TotalLabel     = "Total",
                InvoiceTotal   = ModelExtensions.FormatPrice(invoice.Total, _currency.Symbol),
                TaxTotal       = ModelExtensions.FormatPrice(invoice.TaxLineItems().Sum(x => x.TotalPrice), _currency.Symbol),
                DiscountsTotal = ModelExtensions.FormatPrice(invoice.DiscountLineItems().Sum(x => x.TotalPrice), _currency.Symbol),
                ShippingTotal  = ModelExtensions.FormatPrice(invoice.ShippingLineItems().Sum(x => x.TotalPrice), _currency.Symbol)
            };

            return(summary);
        }
Beispiel #5
0
        /// <summary>
        /// The create checkout confirmation.
        /// </summary>
        /// <param name="model">
        /// The model.
        /// </param>
        /// <param name="basket">
        /// The basket.
        /// </param>
        /// <param name="shippingRateQuotes">
        /// The shipping rate quotes.
        /// </param>
        /// <param name="paymentMethods">
        /// The payment methods.
        /// </param>
        /// <param name="paymentMethodUiInfos">
        /// The payment Method UI information.
        /// </param>
        /// <returns>
        /// The <see cref="CheckoutModel"/>.
        /// </returns>
        public CheckoutConfirmationModel CreateCheckoutConfirmation(RenderModel model, IBasket basket, IEnumerable <IShipmentRateQuote> shippingRateQuotes, IEnumerable <IPaymentMethod> paymentMethods, IEnumerable <PaymentMethodUiInfo> paymentMethodUiInfos)
        {
            var viewModel = this.Build <CheckoutConfirmationModel>(model);



            viewModel.CheckoutConfirmationForm = new CheckoutConfirmationForm()
            {
                ThemeName      = viewModel.Theme,
                CustomerToken  = basket.Customer.Key.ToString().EncryptWithMachineKey(),
                SaleSummary    = _salePreparationSummaryFactory.Value.Build(basket.SalePreparation()),
                ShippingQuotes = shippingRateQuotes.Select(x => new SelectListItem()
                {
                    Value = x.ShipMethod.Key.ToString(),
                    Text  = string.Format("{0} ({1})", x.ShipMethod.Name, ModelExtensions.FormatPrice(x.Rate, _currency.Symbol))
                }),
                PaymentMethods = paymentMethods.Select(x => new SelectListItem()
                {
                    Value = x.Key.ToString(),
                    Text  = x.Name
                }),
                PaymentMethodUiInfo = paymentMethodUiInfos,
                ReceiptPageId       = viewModel.ReceiptPage.Id
            };

            return(viewModel);
        }
        public object GetProductVariantPriceAndInventory(Guid productKey, string optionChoiceKeys)
        {
            var optionsArray = optionChoiceKeys.Split(',').Where(x => !string.IsNullOrEmpty(x)).Select(x => new Guid(x)).ToArray();

            // changed in 1.9.1 to use the MerchelloHelper for performance and to include the data modifiers
            var product = _merchello.Query.Product.GetByKey(productKey);
            var variant = product.GetProductVariantDisplayWithAttributes(optionsArray);

            var data = new
            {
                variant.OnSale,
                SalePrice       = ModelExtensions.FormatPrice(variant.SalePrice, _currency),
                Price           = ModelExtensions.FormatPrice(variant.Price, _currency),
                TracksInventory = variant.TrackInventory,
                variant.TotalInventoryCount
            };

            return(data);
        }
Beispiel #7
0
        public object GetProductVariantPriceAndInventory(Guid productKey, string optionChoiceKeys)
        {
            var optionsArray = optionChoiceKeys.Split(',').Where(x => !string.IsNullOrEmpty(x)).Select(x => new Guid(x)).ToArray();


            var product = _merchelloContext.Services.ProductService.GetByKey(productKey);
            var variant = _merchelloContext.Services.ProductVariantService.GetProductVariantWithAttributes(product, optionsArray);

            var data = new
            {
                variant.OnSale,
                SalePrice       = ModelExtensions.FormatPrice(variant.SalePrice.GetValueOrDefault(), _currency.Symbol),
                Price           = ModelExtensions.FormatPrice(variant.Price, _currency.Symbol),
                TracksInventory = variant.TrackInventory,
                variant.TotalInventoryCount
            };

            return(data);
        }
Beispiel #8
0
        /// <summary>
        /// The create checkout confirmation.
        /// </summary>
        /// <param name="model">
        /// The model.
        /// </param>
        /// <param name="basket">
        /// The basket.
        /// </param>
        /// <param name="shippingRateQuotes">
        /// The shipping rate quotes.
        /// </param>
        /// <param name="paymentMethods">
        /// The payment methods.
        /// </param>
        /// <param name="paymentMethodUiInfos">
        /// The payment Method UI information.
        /// </param>
        /// <returns>
        /// The <see cref="CheckoutModel"/>.
        /// </returns>
        public CheckoutConfirmationModel CreateCheckoutConfirmation(RenderModel model, IBasket basket, IEnumerable <IShipmentRateQuote> shippingRateQuotes, IEnumerable <IPaymentGatewayMethod> paymentMethods, IEnumerable <PaymentMethodUiInfo> paymentMethodUiInfos)
        {
            var viewModel = this.Build <CheckoutConfirmationModel>(model);

            // Introduced resolvable forms in Bazaar 1.8.3
            try
            {
                viewModel.ResolvePaymentForms = bool.Parse(WebConfigurationManager.AppSettings["Bazaar:ResolvePaymentForms"]);
            }
            catch (Exception ex)
            {
                LogHelper.Error <ViewModelFactory>("Failed to find AppSetting 'Bazaar:ResolvePaymentForms", ex);
                viewModel.ResolvePaymentForms = false;
            }

            var paymentInfoArray    = paymentMethodUiInfos as PaymentMethodUiInfo[] ?? paymentMethodUiInfos.ToArray();
            var paymentMethodsArray = paymentMethods as IPaymentGatewayMethod[] ?? paymentMethods.ToArray();

            var isAnonymous    = basket.Customer.IsAnonymous;
            var allowedMethods = new List <IPaymentGatewayMethod>();

            // Payment methods, such as vaulted/stored credit cards may not be available to anonymous customers
            if (isAnonymous)
            {
                foreach (var method in paymentMethodsArray.ToArray())
                {
                    var addMethod = true;

                    var att = method.GetType().GetCustomAttribute <PaymentGatewayMethodAttribute>(false);
                    if (att != null && att.RequiresCustomer)
                    {
                        addMethod = false;
                    }
                    if (addMethod)
                    {
                        allowedMethods.Add(method);
                    }
                }
            }
            else
            {
                allowedMethods.AddRange(paymentMethodsArray);
            }

            var salesPreparation = basket.SalePreparation();

            // prepare the invoice
            var invoice = salesPreparation.PrepareInvoice();

            // get the existing shipMethodKey if any
            var shipmentLineItem = invoice.ShippingLineItems().FirstOrDefault();
            var shipMethodKey    = shipmentLineItem != null?shipmentLineItem.ExtendedData.GetShipMethodKey() : Guid.Empty;

            viewModel.CheckoutConfirmationForm = new CheckoutConfirmationForm()
            {
                ThemeName      = viewModel.Theme,
                CustomerToken  = basket.Customer.Key.ToString().EncryptWithMachineKey(),
                InvoiceSummary = new InvoiceSummary()
                {
                    Invoice       = invoice,
                    Currency      = viewModel.Currency,
                    CurrentPageId = viewModel.Id
                },
                ShipMethodKey  = shipMethodKey,
                ShippingQuotes = shippingRateQuotes.Select(x => new SelectListItem()
                {
                    Value = x.ShipMethod.Key.ToString(),
                    Text  = string.Format("{0} ({1})", x.ShipMethod.Name, ModelExtensions.FormatPrice(x.Rate, _currency))
                }),
                PaymentMethods = (viewModel.ResolvePaymentForms
                     ? allowedMethods.Where(x => paymentInfoArray.Any(y => y.PaymentMethodKey == x.PaymentMethod.Key && y.UrlActionParams != null))
                     : paymentMethodsArray)
                                 .Select(x => new SelectListItem()
                {
                    Value = x.PaymentMethod.Key.ToString(), Text = x.PaymentMethod.Name
                }),

                PaymentMethodUiInfo = viewModel.ResolvePaymentForms ?
                                      paymentInfoArray.Where(x => x.UrlActionParams != null) :
                                      paymentInfoArray,

                ReceiptPageId = viewModel.ReceiptPage.Id,

                ResolvePaymentForms = viewModel.ResolvePaymentForms
            };


            viewModel.RedeemCouponOfferForm = new RedeemCouponOfferForm()
            {
                ThemeName      = viewModel.Theme,
                CurrencySymbol = viewModel.Currency.Symbol
            };

            return(viewModel);
        }