private void processNewOrderNotification(string xmlData)
        {
            try
            {
                NewOrderNotification newOrderNotification = (NewOrderNotification)EncodeHelper.Deserialize(xmlData, typeof(NewOrderNotification));
                string googleOrderNumber = newOrderNotification.googleordernumber;

                XmlNode  CustomerInfo       = newOrderNotification.shoppingcart.merchantprivatedata.Any[0];
                int      CustomerID         = Convert.ToInt32(CustomerInfo.Attributes["CustomerID"].Value);
                int      CustomerLanguageID = Convert.ToInt32(CustomerInfo.Attributes["CustomerLanguageID"].Value);
                int      CustomerCurrencyID = Convert.ToInt32(CustomerInfo.Attributes["CustomerCurrencyID"].Value);
                Customer customer           = IoC.Resolve <ICustomerService>().GetCustomerById(CustomerID);

                NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCart Cart = IoC.Resolve <IShoppingCartService>().GetCustomerShoppingCart(customer.CustomerId, ShoppingCartTypeEnum.ShoppingCart);

                if (customer == null)
                {
                    logMessage("Could not load a customer");
                    return;
                }

                NopContext.Current.User = customer;

                if (Cart.Count == 0)
                {
                    logMessage("Cart is empty");
                    return;
                }

                //validate cart
                foreach (NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartItem sci in Cart)
                {
                    bool ok = false;
                    foreach (Item item in newOrderNotification.shoppingcart.items)
                    {
                        if (!String.IsNullOrEmpty(item.merchantitemid))
                        {
                            if ((Convert.ToInt32(item.merchantitemid) == sci.ShoppingCartItemId) && (item.quantity == sci.Quantity))
                            {
                                ok = true;
                                break;
                            }
                        }
                    }

                    if (!ok)
                    {
                        logMessage(string.Format("Shopping Cart item has been changed. {0}. {1}", sci.ShoppingCartItemId, sci.Quantity));
                        return;
                    }
                }


                string[] billingFullname  = newOrderNotification.buyerbillingaddress.contactname.Trim().Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                string   billingFirstName = billingFullname[0];
                string   billingLastName  = string.Empty;
                if (billingFullname.Length > 1)
                {
                    billingLastName = billingFullname[1];
                }
                string        billingEmail           = newOrderNotification.buyerbillingaddress.email.Trim();
                string        billingAddress1        = newOrderNotification.buyerbillingaddress.address1.Trim();
                string        billingAddress2        = newOrderNotification.buyerbillingaddress.address2.Trim();
                string        billingPhoneNumber     = newOrderNotification.buyerbillingaddress.phone.Trim();
                string        billingCity            = newOrderNotification.buyerbillingaddress.city.Trim();
                int           billingStateProvinceID = 0;
                StateProvince billingStateProvince   = IoC.Resolve <IStateProvinceService>().GetStateProvinceByAbbreviation(newOrderNotification.buyerbillingaddress.region.Trim());
                if (billingStateProvince != null)
                {
                    billingStateProvinceID = billingStateProvince.StateProvinceId;
                }
                string  billingZipPostalCode = newOrderNotification.buyerbillingaddress.postalcode.Trim();
                int     billingCountryID     = 0;
                Country billingCountry       = IoC.Resolve <ICountryService>().GetCountryByTwoLetterIsoCode(newOrderNotification.buyerbillingaddress.countrycode.Trim());
                if (billingCountry != null)
                {
                    billingCountryID = billingCountry.CountryId;
                }

                NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.Address billingAddress = customer.BillingAddresses.FindAddress(
                    billingFirstName, billingLastName, billingPhoneNumber,
                    billingEmail, string.Empty, string.Empty, billingAddress1, billingAddress2, billingCity,
                    billingStateProvinceID, billingZipPostalCode, billingCountryID);

                if (billingAddress == null)
                {
                    billingAddress = new BusinessLogic.CustomerManagement.Address()
                    {
                        CustomerId       = CustomerID,
                        IsBillingAddress = true,
                        FirstName        = billingFirstName,
                        LastName         = billingLastName,
                        PhoneNumber      = billingPhoneNumber,
                        Email            = billingEmail,
                        Address1         = billingAddress1,
                        Address2         = billingAddress2,
                        City             = billingCity,
                        StateProvinceId  = billingStateProvinceID,
                        ZipPostalCode    = billingZipPostalCode,
                        CountryId        = billingCountryID,
                        CreatedOn        = DateTime.UtcNow,
                        UpdatedOn        = DateTime.UtcNow
                    };
                    IoC.Resolve <ICustomerService>().InsertAddress(billingAddress);
                }
                //set default billing address
                customer.BillingAddressId = billingAddress.AddressId;
                IoC.Resolve <ICustomerService>().UpdateCustomer(customer);

                NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.Address shippingAddress = null;
                customer.LastShippingOption = null;
                bool shoppingCartRequiresShipping = IoC.Resolve <IShippingService>().ShoppingCartRequiresShipping(Cart);
                if (shoppingCartRequiresShipping)
                {
                    string[] shippingFullname  = newOrderNotification.buyershippingaddress.contactname.Trim().Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                    string   shippingFirstName = shippingFullname[0];
                    string   shippingLastName  = string.Empty;
                    if (shippingFullname.Length > 1)
                    {
                        shippingLastName = shippingFullname[1];
                    }
                    string        shippingEmail           = newOrderNotification.buyershippingaddress.email.Trim();
                    string        shippingAddress1        = newOrderNotification.buyershippingaddress.address1.Trim();
                    string        shippingAddress2        = newOrderNotification.buyershippingaddress.address2.Trim();
                    string        shippingPhoneNumber     = newOrderNotification.buyershippingaddress.phone.Trim();
                    string        shippingCity            = newOrderNotification.buyershippingaddress.city.Trim();
                    int           shippingStateProvinceID = 0;
                    StateProvince shippingStateProvince   = IoC.Resolve <IStateProvinceService>().GetStateProvinceByAbbreviation(newOrderNotification.buyershippingaddress.region.Trim());
                    if (shippingStateProvince != null)
                    {
                        shippingStateProvinceID = shippingStateProvince.StateProvinceId;
                    }
                    int     shippingCountryID     = 0;
                    string  shippingZipPostalCode = newOrderNotification.buyershippingaddress.postalcode.Trim();
                    Country shippingCountry       = IoC.Resolve <ICountryService>().GetCountryByTwoLetterIsoCode(newOrderNotification.buyershippingaddress.countrycode.Trim());
                    if (shippingCountry != null)
                    {
                        shippingCountryID = shippingCountry.CountryId;
                    }

                    shippingAddress = customer.ShippingAddresses.FindAddress(
                        shippingFirstName, shippingLastName, shippingPhoneNumber,
                        shippingEmail, string.Empty, string.Empty,
                        shippingAddress1, shippingAddress2, shippingCity,
                        shippingStateProvinceID, shippingZipPostalCode, shippingCountryID);
                    if (shippingAddress == null)
                    {
                        shippingAddress = new BusinessLogic.CustomerManagement.Address()
                        {
                            CustomerId       = CustomerID,
                            IsBillingAddress = false,
                            FirstName        = shippingFirstName,
                            LastName         = shippingLastName,
                            PhoneNumber      = shippingPhoneNumber,
                            Email            = shippingEmail,
                            Address1         = shippingAddress1,
                            Address2         = shippingAddress2,
                            City             = shippingCity,
                            StateProvinceId  = shippingStateProvinceID,
                            ZipPostalCode    = shippingZipPostalCode,
                            CountryId        = shippingCountryID,
                            CreatedOn        = DateTime.UtcNow,
                            UpdatedOn        = DateTime.UtcNow
                        };
                        IoC.Resolve <ICustomerService>().InsertAddress(shippingAddress);
                    }
                    //set default shipping address
                    customer.ShippingAddressId = shippingAddress.AddressId;
                    IoC.Resolve <ICustomerService>().UpdateCustomer(customer);

                    string  shippingMethod = string.Empty;
                    decimal shippingCost   = decimal.Zero;
                    if (newOrderNotification.orderadjustment != null &&
                        newOrderNotification.orderadjustment.shipping != null &&
                        newOrderNotification.orderadjustment.shipping.Item != null)
                    {
                        FlatRateShippingAdjustment ShippingMethod = (FlatRateShippingAdjustment)newOrderNotification.orderadjustment.shipping.Item;
                        shippingMethod = ShippingMethod.shippingname;
                        shippingCost   = ShippingMethod.shippingcost.Value;


                        ShippingOption shippingOption = new ShippingOption();
                        shippingOption.Name         = shippingMethod;
                        shippingOption.Rate         = shippingCost;
                        customer.LastShippingOption = shippingOption;
                    }
                }

                //customer.LastCalculatedTax = decimal.Zero;

                PaymentMethod googleCheckoutPaymentMethod = IoC.Resolve <IPaymentService>().GetPaymentMethodBySystemKeyword("GoogleCheckout");

                PaymentInfo paymentInfo = new PaymentInfo();
                paymentInfo.PaymentMethodId   = googleCheckoutPaymentMethod.PaymentMethodId;
                paymentInfo.BillingAddress    = billingAddress;
                paymentInfo.ShippingAddress   = shippingAddress;
                paymentInfo.CustomerLanguage  = IoC.Resolve <ILanguageService>().GetLanguageById(CustomerLanguageID);
                paymentInfo.CustomerCurrency  = IoC.Resolve <ICurrencyService>().GetCurrencyById(CustomerCurrencyID);
                paymentInfo.GoogleOrderNumber = googleOrderNumber;
                int    orderID = 0;
                string result  = IoC.Resolve <IOrderService>().PlaceOrder(paymentInfo, customer, out orderID);
                if (!String.IsNullOrEmpty(result))
                {
                    logMessage("new-order-notification received. CreateOrder() error: Order Number " + orderID + ". " + result);
                    return;
                }

                Order order = IoC.Resolve <IOrderService>().GetOrderById(orderID);
                if (order != null)
                {
                    logMessage("new-order-notification received and saved: Order Number " + orderID);
                }
            }
            catch (Exception exc)
            {
                logMessage("processNewOrderNotification Exception: " + exc.Message + ": " + exc.StackTrace);
            }
        }
        private void processNewOrderNotification(string xmlData)
        {
            try
            {
                NewOrderNotification newOrderNotification = (NewOrderNotification)EncodeHelper.Deserialize(xmlData, typeof(NewOrderNotification));
                string googleOrderNumber = newOrderNotification.googleordernumber;

                XmlNode CustomerInfo = newOrderNotification.shoppingcart.merchantprivatedata.Any[0];
                int CustomerID = Convert.ToInt32(CustomerInfo.Attributes["CustomerID"].Value);
                int CustomerLanguageID = Convert.ToInt32(CustomerInfo.Attributes["CustomerLanguageID"].Value);
                int CustomerCurrencyID = Convert.ToInt32(CustomerInfo.Attributes["CustomerCurrencyID"].Value);
                Customer customer = IoC.Resolve<ICustomerService>().GetCustomerById(CustomerID);

                NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCart Cart = IoC.Resolve<IShoppingCartService>().GetCustomerShoppingCart(customer.CustomerId, ShoppingCartTypeEnum.ShoppingCart);

                if (customer == null)
                {
                    logMessage("Could not load a customer");
                    return;
                }

                NopContext.Current.User = customer;

                if (Cart.Count == 0)
                {
                    logMessage("Cart is empty");
                    return;
                }

                //validate cart
                foreach (NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartItem sci in Cart)
                {
                    bool ok = false;
                    foreach (Item item in newOrderNotification.shoppingcart.items)
                    {
                        if (!String.IsNullOrEmpty(item.merchantitemid))
                        {
                            if ((Convert.ToInt32(item.merchantitemid) == sci.ShoppingCartItemId) && (item.quantity == sci.Quantity))
                            {
                                ok = true;
                                break;
                            }
                        }
                    }

                    if (!ok)
                    {
                        logMessage(string.Format("Shopping Cart item has been changed. {0}. {1}", sci.ShoppingCartItemId, sci.Quantity));
                        return;
                    }
                }

                string[] billingFullname = newOrderNotification.buyerbillingaddress.contactname.Trim().Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                string billingFirstName = billingFullname[0];
                string billingLastName = string.Empty;
                if (billingFullname.Length > 1)
                    billingLastName = billingFullname[1];
                string billingEmail = newOrderNotification.buyerbillingaddress.email.Trim();
                string billingAddress1 = newOrderNotification.buyerbillingaddress.address1.Trim();
                string billingAddress2 = newOrderNotification.buyerbillingaddress.address2.Trim();
                string billingPhoneNumber = newOrderNotification.buyerbillingaddress.phone.Trim();
                string billingCity = newOrderNotification.buyerbillingaddress.city.Trim();
                int billingStateProvinceID = 0;
                StateProvince billingStateProvince = IoC.Resolve<IStateProvinceService>().GetStateProvinceByAbbreviation(newOrderNotification.buyerbillingaddress.region.Trim());
                if (billingStateProvince != null)
                    billingStateProvinceID = billingStateProvince.StateProvinceId;
                string billingZipPostalCode = newOrderNotification.buyerbillingaddress.postalcode.Trim();
                int billingCountryID = 0;
                Country billingCountry = IoC.Resolve<ICountryService>().GetCountryByTwoLetterIsoCode(newOrderNotification.buyerbillingaddress.countrycode.Trim());
                if (billingCountry != null)
                    billingCountryID = billingCountry.CountryId;

                NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.Address billingAddress = customer.BillingAddresses.FindAddress(
                    billingFirstName, billingLastName, billingPhoneNumber,
                    billingEmail, string.Empty, string.Empty, billingAddress1, billingAddress2, billingCity,
                    billingStateProvinceID, billingZipPostalCode, billingCountryID);

                if (billingAddress == null)
                {
                    billingAddress = new BusinessLogic.CustomerManagement.Address()
                    {
                        CustomerId = CustomerID,
                        IsBillingAddress = true,
                        FirstName = billingFirstName,
                        LastName = billingLastName,
                        PhoneNumber = billingPhoneNumber,
                        Email = billingEmail,
                        Address1 = billingAddress1,
                        Address2 = billingAddress2,
                        City = billingCity,
                        StateProvinceId = billingStateProvinceID,
                        ZipPostalCode = billingZipPostalCode,
                        CountryId = billingCountryID,
                        CreatedOn = DateTime.UtcNow,
                        UpdatedOn = DateTime.UtcNow
                    };
                    IoC.Resolve<ICustomerService>().InsertAddress(billingAddress);
                }
                //set default billing address
                customer.BillingAddressId = billingAddress.AddressId;
                IoC.Resolve<ICustomerService>().UpdateCustomer(customer);

                NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.Address shippingAddress = null;
                customer.LastShippingOption = null;
                bool shoppingCartRequiresShipping = IoC.Resolve<IShippingService>().ShoppingCartRequiresShipping(Cart);
                if (shoppingCartRequiresShipping)
                {
                    string[] shippingFullname = newOrderNotification.buyershippingaddress.contactname.Trim().Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                    string shippingFirstName = shippingFullname[0];
                    string shippingLastName = string.Empty;
                    if (shippingFullname.Length > 1)
                        shippingLastName = shippingFullname[1];
                    string shippingEmail = newOrderNotification.buyershippingaddress.email.Trim();
                    string shippingAddress1 = newOrderNotification.buyershippingaddress.address1.Trim();
                    string shippingAddress2 = newOrderNotification.buyershippingaddress.address2.Trim();
                    string shippingPhoneNumber = newOrderNotification.buyershippingaddress.phone.Trim();
                    string shippingCity = newOrderNotification.buyershippingaddress.city.Trim();
                    int shippingStateProvinceID = 0;
                    StateProvince shippingStateProvince = IoC.Resolve<IStateProvinceService>().GetStateProvinceByAbbreviation(newOrderNotification.buyershippingaddress.region.Trim());
                    if (shippingStateProvince != null)
                        shippingStateProvinceID = shippingStateProvince.StateProvinceId;
                    int shippingCountryID = 0;
                    string shippingZipPostalCode = newOrderNotification.buyershippingaddress.postalcode.Trim();
                    Country shippingCountry = IoC.Resolve<ICountryService>().GetCountryByTwoLetterIsoCode(newOrderNotification.buyershippingaddress.countrycode.Trim());
                    if (shippingCountry != null)
                        shippingCountryID = shippingCountry.CountryId;

                    shippingAddress = customer.ShippingAddresses.FindAddress(
                        shippingFirstName, shippingLastName, shippingPhoneNumber,
                        shippingEmail, string.Empty, string.Empty,
                        shippingAddress1, shippingAddress2, shippingCity,
                        shippingStateProvinceID, shippingZipPostalCode, shippingCountryID);
                    if (shippingAddress == null)
                    {
                        shippingAddress = new BusinessLogic.CustomerManagement.Address()
                        {
                            CustomerId = CustomerID,
                            IsBillingAddress = false,
                            FirstName = shippingFirstName,
                            LastName = shippingLastName,
                            PhoneNumber = shippingPhoneNumber,
                            Email = shippingEmail,
                            Address1 = shippingAddress1,
                            Address2 = shippingAddress2,
                            City = shippingCity,
                            StateProvinceId = shippingStateProvinceID,
                            ZipPostalCode = shippingZipPostalCode,
                            CountryId = shippingCountryID,
                            CreatedOn = DateTime.UtcNow,
                            UpdatedOn = DateTime.UtcNow
                        };
                        IoC.Resolve<ICustomerService>().InsertAddress(shippingAddress);
                    }
                    //set default shipping address
                    customer.ShippingAddressId =  shippingAddress.AddressId;
                    IoC.Resolve<ICustomerService>().UpdateCustomer(customer);

                    string shippingMethod = string.Empty;
                    decimal shippingCost = decimal.Zero;
                    if (newOrderNotification.orderadjustment != null &&
                        newOrderNotification.orderadjustment.shipping != null &&
                        newOrderNotification.orderadjustment.shipping.Item != null)
                    {
                        FlatRateShippingAdjustment ShippingMethod = (FlatRateShippingAdjustment)newOrderNotification.orderadjustment.shipping.Item;
                        shippingMethod = ShippingMethod.shippingname;
                        shippingCost = ShippingMethod.shippingcost.Value;

                        ShippingOption shippingOption = new ShippingOption();
                        shippingOption.Name = shippingMethod;
                        shippingOption.Rate = shippingCost;
                        customer.LastShippingOption = shippingOption;
                    }
                }

                //customer.LastCalculatedTax = decimal.Zero;

                PaymentMethod googleCheckoutPaymentMethod = IoC.Resolve<IPaymentService>().GetPaymentMethodBySystemKeyword("GoogleCheckout");

                PaymentInfo paymentInfo = new PaymentInfo();
                paymentInfo.PaymentMethodId = googleCheckoutPaymentMethod.PaymentMethodId;
                paymentInfo.BillingAddress = billingAddress;
                paymentInfo.ShippingAddress = shippingAddress;
                paymentInfo.CustomerLanguage = IoC.Resolve<ILanguageService>().GetLanguageById(CustomerLanguageID);
                paymentInfo.CustomerCurrency = IoC.Resolve<ICurrencyService>().GetCurrencyById(CustomerCurrencyID);
                paymentInfo.GoogleOrderNumber = googleOrderNumber;
                int orderID = 0;
                string result = IoC.Resolve<IOrderService>().PlaceOrder(paymentInfo, customer, out orderID);
                if (!String.IsNullOrEmpty(result))
                {
                    logMessage("new-order-notification received. CreateOrder() error: Order Number " + orderID + ". " + result);
                    return;
                }

                Order order = IoC.Resolve<IOrderService>().GetOrderById(orderID);
                if (order != null)
                {
                    logMessage("new-order-notification received and saved: Order Number " + orderID);
                }
            }
            catch (Exception exc)
            {
                logMessage("processNewOrderNotification Exception: " + exc.Message + ": " + exc.StackTrace);
            }
        }