/// <summary>
        /// Post cart to google
        /// </summary>
        /// <param name="req">Pre-generated request</param>
        /// <param name="cart">Shopping cart</param>
        /// <returns>Response</returns>
        public GCheckoutResponse PostCartToGoogle(CheckoutShoppingCartRequest req,
            IList<Core.Domain.Orders.ShoppingCartItem> cart)
        {
            //there's no need to round prices (Math.Round(,2)) because GCheckout library does it for us
            //items
            foreach (Core.Domain.Orders.ShoppingCartItem sci in cart)
            {
                var productVariant = sci.ProductVariant;
                if (productVariant != null)
                {
                    decimal taxRate = decimal.Zero;
                    string description = _productAttributeFormatter.FormatAttributes(productVariant,
                        sci.AttributesXml, _workContext.CurrentCustomer,
                        ", ", false, true, true, true, false);
                    string fullName = "";
                    if (!String.IsNullOrEmpty(sci.ProductVariant.GetLocalized(x => x.Name)))
                        fullName = string.Format("{0} ({1})", sci.ProductVariant.Product.GetLocalized(x => x.Name), sci.ProductVariant.GetLocalized(x => x.Name));
                    else
                        fullName = sci.ProductVariant.Product.GetLocalized(x => x.Name);
                    decimal unitPrice = _taxService.GetProductPrice(sci.ProductVariant, _priceCalculationService.GetUnitPrice(sci, true), out taxRate);
                    req.AddItem(fullName, description, sci.Id.ToString(), unitPrice, sci.Quantity);
                }
            }

            if (cart.RequiresShipping())
            {
                //AddMerchantCalculatedShippingMethod
                //AddCarrierCalculatedShippingOption
                var shippingOptions = _shippingService.GetShippingOptions(cart, null);
                foreach (ShippingOption shippingOption in shippingOptions.ShippingOptions)
                {
                    //adjust rate
                    Discount appliedDiscount = null;
                    var shippingTotal = _orderTotalCalculationService.AdjustShippingRate(
                        shippingOption.Rate, cart, out appliedDiscount);
                    decimal shippingRateBase = _taxService.GetShippingPrice(shippingTotal, _workContext.CurrentCustomer);
                    req.AddFlatRateShippingMethod(shippingOption.Name, shippingRateBase);
                }
            }

            //add only US, GB states
            //CountryCollection countries = IoC.Resolve<ICountryService>().GetAllCountries();
            //foreach (Country country in countries)
            //{
            //    foreach (StateProvince state in country.StateProvinces)
            //    {
            //        TaxByStateProvinceCollection taxByStateProvinceCollection = TaxByIoC.Resolve<IStateProvinceService>().GetAllByStateProvinceID(state.StateProvinceID);
            //        foreach (TaxByStateProvince taxByStateProvince in taxByStateProvinceCollection)
            //        {
            //            if (!String.IsNullOrEmpty(state.Abbreviation))
            //            {
            //                Req.AddStateTaxRule(state.Abbreviation, (double)taxByStateProvince.Percentage, false);
            //            }
            //        }
            //    }
            //}

            //if (subTotalDiscountBase > decimal.Zero)
            //{
            //    req.AddItem("Discount", string.Empty, string.Empty, (decimal)(-1.0) * subTotalDiscountBase, 1);
            //}

            //foreach (AppliedGiftCard agc in appliedGiftCards)
            //{
            //    req.AddItem(string.Format("Gift Card - {0}", agc.GiftCard.GiftCardCouponCode), string.Empty, string.Empty, (decimal)(-1.0) * agc.AmountCanBeUsed, 1);
            //}

            var customerInfoDoc = new XmlDocument();
            XmlElement customerInfo = customerInfoDoc.CreateElement("CustomerInfo");
            customerInfo.SetAttribute("CustomerID", _workContext.CurrentCustomer.Id.ToString());
            customerInfo.SetAttribute("CustomerLanguageID", _workContext.WorkingLanguage.Id.ToString());
            customerInfo.SetAttribute("CustomerCurrencyID", _workContext.WorkingCurrency.Id.ToString());
            req.AddMerchantPrivateDataNode(customerInfo);

            req.ContinueShoppingUrl = _webHelper.GetStoreLocation(false);
            req.EditCartUrl = _webHelper.GetStoreLocation(false) + "cart";

            GCheckoutResponse resp = req.Send();
            return resp;
        }
        /// <summary>
        /// Post cart to google
        /// </summary>
        /// <param name="Req">Pre-generated request</param>
        /// <param name="Cart">Shopping cart</param>
        /// <returns>Response</returns>
        public GCheckoutResponse PostCartToGoogle(CheckoutShoppingCartRequest Req, NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCart Cart)
        {
            foreach (NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartItem sci in Cart)
            {
                ProductVariant productVariant = sci.ProductVariant;
                if (productVariant != null)
                {
                    string pvAttributeDescription = ProductAttributeHelper.FormatAttributes(productVariant, sci.AttributesXML, NopContext.Current.User, ", ", false);
                    string fullName = productVariant.FullProductName;
                    string description = pvAttributeDescription;
                    decimal unitPrice = TaxManager.GetPrice(sci.ProductVariant, PriceHelper.GetUnitPrice(sci, NopContext.Current.User, true));
                    Req.AddItem(fullName, description, sci.ShoppingCartItemID.ToString(), unitPrice, sci.Quantity);
                }
            }

            decimal shoppingCartSubTotalDiscount;
            decimal shoppingCartSubTotal = ShoppingCartManager.GetShoppingCartSubTotal(Cart, NopContext.Current.User, out shoppingCartSubTotalDiscount);
            if (shoppingCartSubTotalDiscount > decimal.Zero)
                Req.AddItem("Discount", string.Empty, string.Empty, (decimal)(-1.0) * shoppingCartSubTotalDiscount, 1);

            bool shoppingCartRequiresShipping = ShippingManager.ShoppingCartRequiresShipping(Cart);
            if (shoppingCartRequiresShipping)
            {
                string shippingError = string.Empty;
                //TODO AddMerchantCalculatedShippingMethod
                //TODO AddCarrierCalculatedShippingOption
                ShippingOptionCollection shippingOptions = ShippingManager.GetShippingOptions(Cart, NopContext.Current.User, null, ref shippingError);
                foreach (ShippingOption shippingOption in shippingOptions)
                    Req.AddFlatRateShippingMethod(shippingOption.Name, TaxManager.GetShippingPrice(shippingOption.Rate, NopContext.Current.User));
            }

            //add only US, GB states
            //CountryCollection countries = CountryManager.GetAllCountries();                
            //foreach (Country country in countries)
            //{
            //    foreach (StateProvince state in country.StateProvinces)
            //    {
            //        TaxByStateProvinceCollection taxByStateProvinceCollection = TaxByStateProvinceManager.GetAllByStateProvinceID(state.StateProvinceID);
            //        foreach (TaxByStateProvince taxByStateProvince in taxByStateProvinceCollection)
            //        {
            //            if (!String.IsNullOrEmpty(state.Abbreviation))
            //            {
            //                Req.AddStateTaxRule(state.Abbreviation, (double)taxByStateProvince.Percentage, false);
            //            }
            //        }
            //    }
            //}

            XmlDocument customerInfoDoc = new XmlDocument();
            XmlElement customerInfo = customerInfoDoc.CreateElement("CustomerInfo");
            customerInfo.SetAttribute("CustomerID", NopContext.Current.User.CustomerID.ToString());
            customerInfo.SetAttribute("CustomerLanguageID", NopContext.Current.WorkingLanguage.LanguageID.ToString());
            customerInfo.SetAttribute("CustomerCurrencyID", NopContext.Current.WorkingCurrency.CurrencyID.ToString());
            Req.AddMerchantPrivateDataNode(customerInfo);

            Req.ContinueShoppingUrl = CommonHelper.GetStoreLocation(false);
            Req.EditCartUrl = CommonHelper.GetStoreLocation(false) + "ShoppingCart.aspx";

            GCheckoutResponse Resp = Req.Send();

            return Resp;
        }
        public void TestExamples()
        {
            CheckoutShoppingCartRequest request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);

              //Make sure we can add an item to the cart.
              request.AddItem("Item 1", "Cool Candy 1", 2.00M, 1);

              request.AddStateTaxRule("CT", .06, true);

              byte[] cart = request.GetXml();

              //Console.WriteLine(EncodeHelper.Utf8BytesToString(cart));

              //test to see if the item can desialize
              Assert.IsNotNull(GCheckout.Util.EncodeHelper.Deserialize(cart));

              //example 2

              request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);

              //Make sure we can add an item to the cart.
              request.AddItem("Item 1", "Cool Candy 1", 2.00M, 1);

              request.AddStateTaxRule("CT", .06, true);
              request.AddStateTaxRule("MD", .05, false);

              cart = request.GetXml();

              //Console.WriteLine(EncodeHelper.Utf8BytesToString(cart));

              //test to see if the item can desialize
              Assert.IsNotNull(GCheckout.Util.EncodeHelper.Deserialize(cart));

              //example 2a

              request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);

              //Make sure we can add an item to the cart.
              request.AddItem("Item 1", "Cool Candy 1", 2.00M, 1);

              cart = request.GetXml();

              //Console.WriteLine(EncodeHelper.Utf8BytesToString(cart));

              //test to see if the item can desialize
              Assert.IsNotNull(GCheckout.Util.EncodeHelper.Deserialize(cart));

              //example 3

              request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);

              //Make sure we can add an item to the cart.
              request.AddItem("Item 1", "Cool Candy 1", 2.00M, 1);

              request.AddZipTaxRule("100*", 0.08375, false);
              request.AddStateTaxRule("NY", 0.0400, true);

              //this should be an invalid format
              try {
            request.AddZipTaxRule("255333", .05, true);
            Assert.Fail("255333 should not be a correct zip code format");
              }
              catch {
              }

              cart = request.GetXml();

              //Console.WriteLine(EncodeHelper.Utf8BytesToString(cart));

              //test to see if the item can desialize
              Assert.IsNotNull(GCheckout.Util.EncodeHelper.Deserialize(cart));

              request.AddMerchantCalculatedShippingMethod("Test 1", 12.11m);
              request.AddMerchantCalculatedShippingMethod("Test 2", 4.95m, new ShippingRestrictions());
              request.AddMerchantCalculatedShippingMethod("Test 3", 5.95m, new ShippingRestrictions());
              request.AddMerchantCalculatedShippingMethod("MerchantCalc", 12.95m, new ShippingRestrictions(), new ShippingRestrictions());

              //create a pickup shipping method
              request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);
              request.AddPickupShippingMethod("Name", 4.95m);
              request.AddCountryTaxRule(GCheckout.AutoGen.USAreas.ALL, .05, true);
              request.AddWorldAreaTaxRule(.02, true);
              //Tax Canada at 5%
              request.AddPostalAreaTaxRule("CA", .05, true);

              //Tax all cities that start with L4L at 7%
              request.AddPostalAreaTaxRule("CA", "L4L*", .07, true);

              XmlDocument doc = new XmlDocument();
              doc.LoadXml("<data />");
              request.AddMerchantPrivateDataNode(doc.DocumentElement);

              //we must pass in a valid node
              try {
            request.AddMerchantPrivateDataNode(null);
            Assert.Fail("Null can't be sent to AddMerchantPrivateDataNode.");
              }
              catch {
              }
        }
        /// <summary>
        /// Post cart to google
        /// </summary>
        /// <param name="req">Pre-generated request</param>
        /// <param name="cart">Shopping cart</param>
        /// <returns>Response</returns>
        public GCheckoutResponse PostCartToGoogle(CheckoutShoppingCartRequest req,
            NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCart cart)
        {
            //items
            foreach (NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartItem sci in cart)
            {
                ProductVariant productVariant = sci.ProductVariant;
                if (productVariant != null)
                {
                    decimal taxRate = decimal.Zero;
                    string description = ProductAttributeHelper.FormatAttributes(productVariant, sci.AttributesXml, NopContext.Current.User, ", ", false);
                    string fullName = productVariant.LocalizedFullProductName;
                    decimal unitPrice = TaxManager.GetPrice(sci.ProductVariant, PriceHelper.GetUnitPrice(sci, NopContext.Current.User, true), out taxRate);
                    req.AddItem(fullName, description, sci.ShoppingCartItemId.ToString(), unitPrice, sci.Quantity);
                }
            }

            //decimal subtotalBase = decimal.Zero;
            //string subTotalError = ShoppingCartManager.GetShoppingCartSubTotal(cart,
            //    NopContext.Current.User, out subtotalBase);

            bool shoppingCartRequiresShipping = ShippingManager.ShoppingCartRequiresShipping(cart);
            if (shoppingCartRequiresShipping)
            {
                string shippingError = string.Empty;
                //AddMerchantCalculatedShippingMethod
                //AddCarrierCalculatedShippingOption
                List<ShippingOption> shippingOptions = ShippingManager.GetShippingOptions(cart, NopContext.Current.User, null, ref shippingError);
                foreach (ShippingOption shippingOption in shippingOptions)
                    req.AddFlatRateShippingMethod(shippingOption.Name, TaxManager.GetShippingPrice(shippingOption.Rate, NopContext.Current.User));
            }

            //add only US, GB states
            //CountryCollection countries = CountryManager.GetAllCountries();
            //foreach (Country country in countries)
            //{
            //    foreach (StateProvince state in country.StateProvinces)
            //    {
            //        TaxByStateProvinceCollection taxByStateProvinceCollection = TaxByStateProvinceManager.GetAllByStateProvinceID(state.StateProvinceID);
            //        foreach (TaxByStateProvince taxByStateProvince in taxByStateProvinceCollection)
            //        {
            //            if (!String.IsNullOrEmpty(state.Abbreviation))
            //            {
            //                Req.AddStateTaxRule(state.Abbreviation, (double)taxByStateProvince.Percentage, false);
            //            }
            //        }
            //    }
            //}

            //if (subTotalDiscountBase > decimal.Zero)
            //{
            //    req.AddItem("Discount", string.Empty, string.Empty, (decimal)(-1.0) * subTotalDiscountBase, 1);
            //}

            //foreach (AppliedGiftCard agc in appliedGiftCards)
            //{
            //    req.AddItem(string.Format("Gift Card - {0}", agc.GiftCard.GiftCardCouponCode), string.Empty, string.Empty, (decimal)(-1.0) * agc.AmountCanBeUsed, 1);
            //}

            XmlDocument customerInfoDoc = new XmlDocument();
            XmlElement customerInfo = customerInfoDoc.CreateElement("CustomerInfo");
            customerInfo.SetAttribute("CustomerID", NopContext.Current.User.CustomerId.ToString());
            customerInfo.SetAttribute("CustomerLanguageID", NopContext.Current.WorkingLanguage.LanguageId.ToString());
            customerInfo.SetAttribute("CustomerCurrencyID", NopContext.Current.WorkingCurrency.CurrencyId.ToString());
            req.AddMerchantPrivateDataNode(customerInfo);

            req.ContinueShoppingUrl = CommonHelper.GetStoreLocation(false);
            req.EditCartUrl = CommonHelper.GetStoreLocation(false) + "ShoppingCart.aspx";

            GCheckoutResponse resp = req.Send();

            return resp;
        }
        public void TestAddItem()
        {
            //due to the complexity of the add items. we are going to create a known set of data points and add them to the collection.
              ShoppingCartItem si = new ShoppingCartItem();
              si.Description = "Description";
              si.DigitalContent = new DigitalItem("Digital Item Key", "Digital Item Description");
              si.MerchantItemID = "Merchant Item ID";
              si.MerchantPrivateItemData = "Private Data";

              XmlDocument mpdDoc = new XmlDocument();
              mpdDoc.LoadXml("<data />");
              mpdDoc.DocumentElement.AppendChild(mpdDoc.CreateElement("node1"));
              mpdDoc.DocumentElement.AppendChild(mpdDoc.CreateElement("node2"));
              XmlNode[] mpdNodes = new XmlNode[] { mpdDoc.DocumentElement.ChildNodes[0], mpdDoc.DocumentElement.ChildNodes[1] };

              si.MerchantPrivateItemDataNodes = mpdNodes;
              si.Name = "Name";
              si.Price = 0.99m;
              si.Quantity = 1;

              AlternateTaxTable taxTable = new AlternateTaxTable("Example");
              taxTable.AddStateTaxRule("OH", .06);

              si.TaxTable = taxTable;
              si.Weight = 10.5;

              si.TaxTable.AddCountryTaxRule(GCheckout.AutoGen.USAreas.ALL, 5.0);

              CheckoutShoppingCartRequest request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);

              request.ContinueShoppingUrl = "http://localhost/";
              request.AnalyticsData = "Test data";
              request.PlatformID = 1234567890;
              request.EditCartUrl = "http://localhost/editcart.aspx";
              request.RequestBuyerPhoneNumber = true;
              request.MerchantCalculationsUrl = "http://localhost/calculate.aspx";
              request.AcceptMerchantCoupons = true;
              request.AcceptMerchantGiftCertificates = true;
              request.SetRoundingPolicy(RoundingMode.FLOOR, RoundingRule.TOTAL);
              request.AddShippingPackage("main", "Cleveland", "OH", "44114");

              request.MerchantPrivateData = "Test Cool Stuff";
              request.AddMerchantPrivateDataNode(mpdNodes[0]);

              XmlNode[] mpdn = request.MerchantPrivateDataNodes;

              Assert.AreSame(mpdn[0], mpdNodes[0]);

              try {
            request.AddItem(null);
            Assert.Fail("Null can't be passed to the AddItem methods");
              }
              catch {
              }

              try {
            MethodInfo mi = typeof(CheckoutShoppingCartRequest).GetMethod("AddItem", new Type[] { typeof(IShoppingCartItem) });
            mi.Invoke(request, new object[] { null });
            Assert.Fail("Null can't be passed to the AddItem methods");
              }
              catch {
              }

              request.AddItem(si);
              request.AddItem(si.Clone() as IShoppingCartItem);

              MethodInfo[] methods = typeof(CheckoutShoppingCartRequest).GetMethods();

              foreach (MethodInfo mi in methods) {
            bool cancel = false;
            //we are only working with AddItems
            if (mi.Name == "AddItem") {
              Type sct = typeof(ShoppingCartItem);
              ShoppingCartItem si2 = si.Clone() as ShoppingCartItem;
              ParameterInfo[] parameters = mi.GetParameters();
              object[] setter = new object[parameters.Length];
              for (int i = 0; i < parameters.Length; i++) {
            ParameterInfo pi = parameters[i];
            if (pi.ParameterType == typeof(ShoppingCartItem) || pi.ParameterType == typeof(IShoppingCartItem)) {
              cancel = true;
              continue;
            }
            //get the property from the object
            PropertyInfo source;
            if (pi.Name != "digitalItem") {
              source = sct.GetProperty(pi.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            }
            else {
              source = sct.GetProperty("DigitalContent");
            }
            setter[i] = source.GetValue(si2, null);

            //we want to split and take the first item
            if (!pi.ParameterType.IsArray && source.PropertyType.IsArray) {
              object[] vals = setter[i] as object[];
              setter[i] = vals[0] as object;
            }
              }
              if (!cancel) {
            //now call the method
            ShoppingCartItem called = mi.Invoke(request, setter) as ShoppingCartItem;

            //this is to fix a params array issue.
            if (parameters[parameters.Length - 1].Name == "MerchantPrivateItemDataNodes") {
              called.MerchantPrivateItemDataNodes = si2.MerchantPrivateItemDataNodes;
            }
              }
            }
              }

              byte[] toXml = request.GetXml();

              //Make sure we can add an item to the cart.
              ShoppingCartItem item = request.AddItem("Item 1", "Cool Candy 1", "Merchant Item ID", 2.00M, 1);
              item.Weight = 2.2;
              item.MerchantPrivateItemData = null; //perform a null check

              //verify we can't set the price to fractions.

              item.Price = 1.975m;
              Assert.AreEqual(1.98m, item.Price);

              item.Price = 1.994m;
              Assert.AreEqual(1.99m, item.Price);

              Assert.AreEqual(2.2, item.Weight);
              Assert.AreEqual("Merchant Item ID", item.MerchantItemID);

              //this is a very specific test to make sure that if only one node exists, return it. it may be for a reason.

              XmlDocument doc = new XmlDocument();
              doc.LoadXml("<data />");
              doc.DocumentElement.SetAttribute("test", "cool");

              string xml = doc.OuterXml;
              item.MerchantPrivateItemDataNodes = new XmlNode[] { doc.DocumentElement };
              string xmlReturn = item.MerchantPrivateItemData;
              Assert.AreEqual(xml, xmlReturn);

              //create a new node
              XmlNode secondNode = doc.DocumentElement.AppendChild(doc.CreateElement("test"));
              item.MerchantPrivateItemDataNodes = new XmlNode[] { doc.DocumentElement, secondNode };

              xmlReturn = item.MerchantPrivateItemData;
              Assert.AreEqual(null, xmlReturn);

              item.MerchantPrivateItemDataNodes = null;
              Assert.AreEqual(new XmlNode[] { }, item.MerchantPrivateItemDataNodes);

              //this should throw an exception
              try {
            item.Weight = -1;
            Assert.Fail("Weight should not be allowed to be negative.");
              }
              catch {
              }

              //create a new instance of the cart item
              ShoppingCartItem testItem = new ShoppingCartItem();
        }