private void LoadSettings()
        {
            PageId = WebUtils.ParseInt32FromQueryString("pageid", -1);
            ModuleId = WebUtils.ParseInt32FromQueryString("mid", -1);
            payPalGetExpressCheckoutLogGuid = WebUtils.ParseGuidFromQueryString("plog", payPalGetExpressCheckoutLogGuid);

            if (payPalGetExpressCheckoutLogGuid == Guid.Empty)
            {
                Response.Redirect(SiteUtils.GetCurrentPageUrl());
            }

            checkoutDetailsLog = new PayPalLog(payPalGetExpressCheckoutLogGuid);

            if (checkoutDetailsLog.RowGuid == Guid.Empty)
            {
                Response.Redirect(SiteUtils.GetCurrentPageUrl());
            }

            cart = (Cart)SerializationHelper.DeserializeFromString(typeof(Cart), checkoutDetailsLog.SerializedObject);

            if (cart == null)
            {
                Response.Redirect(SiteUtils.GetCurrentPageUrl());
            }
            cart.DeSerializeCartOffers();

            cart.RefreshTotals();

            if ((cart.LastModified < DateTime.UtcNow.AddDays(-1)) && (cart.DiscountCodesCsv.Length > 0))
            {
                StoreHelper.EnsureValidDiscounts(store, cart);
            }

            siteUser = SiteUtils.GetCurrentSiteUser();
            //if (siteUser == null)
            //{
            //    Response.Redirect(SiteUtils.GetCurrentPageUrl());
            //}

            if ((siteUser != null)&&(cart.UserGuid == Guid.Empty))
            {
                // user wasn't logged in when express checkout was called
                cart.UserGuid = siteUser.UserGuid;
                cart.Save();
                //if (checkoutDetailsLog.UserGuid == Guid.Empty)
                //{
                //    // we need to make sure we have the user in the log and serialized cart
                //    checkoutDetailsLog.UserGuid = siteUser.UserGuid;
                //    cart.SerializeCartOffers();
                //    checkoutDetailsLog.SerializedObject = SerializationHelper.SerializeToSoap(cart);
                //    checkoutDetailsLog.Save();

                //}
            }

            if ((siteUser != null)&&(cart.UserGuid != siteUser.UserGuid))
            {
                Response.Redirect(SiteUtils.GetCurrentPageUrl());
            }

            if (ModuleId == -1)
            {
                ModuleId = StoreHelper.FindStoreModuleId(CurrentPage);
            }

            store = StoreHelper.GetStore();

            commerceConfig = SiteUtils.GetCommerceConfig();
            currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code);

            if (siteUser != null)
            {
                pnlRequireLogin.Visible = false;
            }
            else
            {
                btnMakePayment.Visible = false;
            }

            AddClassToBody("webstore webstoreexpresscheckout");
        }
        private void ProcessOrder(
            Cart cart,
            Store store,
            WorldPayPaymentResponse wpResponse,
            PayPalLog worldPayLog,
            Page page)
        {
            // process the cart into an order then
            // return an html order result template for use at world pay

            cart.DeSerializeCartOffers();

            if (wpResponse.CompName.Length > 0)
            {
                cart.OrderInfo.CustomerCompany = wpResponse.CompName;
            }
            if (wpResponse.Address1.Length > 0)
            {
                cart.OrderInfo.CustomerAddressLine1 = wpResponse.Address1;
            }

            if (wpResponse.Address2.Length > 0)
            {
                cart.OrderInfo.CustomerAddressLine2 = wpResponse.Address2;
            }

            if (wpResponse.Address3.Length > 0)
            {
                cart.OrderInfo.CustomerAddressLine2 += " " + wpResponse.Address3;
            }

            if (wpResponse.Town.Length > 0)
            {
                cart.OrderInfo.CustomerCity = wpResponse.Town;
            }
            //cart.OrderInfo.DeliveryFirstName = wpResponse.Name;
            if(
                (wpResponse.Name.Length > 0)
                && ((cart.OrderInfo.CustomerLastName.Length == 0) || (!wpResponse.Name.Contains((cart.OrderInfo.CustomerLastName))))
                )
            {
                cart.OrderInfo.CustomerLastName = wpResponse.Name; // this is full name
            }
            if (wpResponse.Postcode.Length > 0)
            {
                cart.OrderInfo.CustomerPostalCode = wpResponse.Postcode;
            }
            if (wpResponse.Region.Length > 0)
            {
                cart.OrderInfo.CustomerState = wpResponse.Region;
            }
            if (wpResponse.Country.Length > 0)
            {
                cart.OrderInfo.CustomerCountry = wpResponse.Country;
            }

            if (wpResponse.Tel.Length > 0)
            {
                cart.OrderInfo.CustomerTelephoneDay = wpResponse.Tel;
            }

            if (wpResponse.Email.Length > 0)
            {
                cart.OrderInfo.CustomerEmail = wpResponse.Email;
            }

            cart.CopyCustomerToBilling();
            cart.CopyCustomerToShipping();
            //cart.TaxTotal = taxAmount;
            //cart.OrderTotal = grossAmount;
            //if (shippingAmount > 0)
            //{
            //    cart.ShippingTotal = shippingAmount;
            //}

            StoreHelper.EnsureUserForOrder(cart);

            cart.Save();

            Order order = Order.CreateOrder(
                store,
                cart,
                wpResponse.TransId,
                wpResponse.TransId,
                string.Empty,
                wpResponse.Currency,
                "WorldPay",
                OrderStatus.OrderStatusFulfillableGuid);

            // grab the return url before we delete the un-needed logs
            string orderDetailUrl = worldPayLog.ReturnUrl;
            string storePageUrl = worldPayLog.RawResponse;

            // remove any previous logs
            GoogleCheckoutLog.DeleteByCart(order.OrderGuid);
            PayPalLog.DeleteByCart(order.OrderGuid);

            // create a final log that has the serialized reposnse from worldpay rather than the serialized cart
            worldPayLog = new PayPalLog();
            worldPayLog.SiteGuid = store.SiteGuid;
            worldPayLog.StoreGuid = store.Guid;
            worldPayLog.CartGuid = order.OrderGuid;
            worldPayLog.UserGuid = order.UserGuid;
            worldPayLog.ProviderName = "WebStoreWorldPayResponseHandler";
            worldPayLog.RequestType = "WorldPay";
            worldPayLog.PaymentStatus = "Paid";
            worldPayLog.PaymentType = "WorldPay";
            worldPayLog.CartTotal = order.OrderTotal;
            worldPayLog.PayPalAmt = wpResponse.AuthAmount;
            worldPayLog.TransactionId = wpResponse.TransId;
            worldPayLog.CurrencyCode = wpResponse.Currency;
            worldPayLog.ReasonCode = wpResponse.AVS;
            worldPayLog.RawResponse = SerializationHelper.SerializeToString(wpResponse);
            worldPayLog.CreatedUtc = DateTime.UtcNow;
            worldPayLog.ReturnUrl = orderDetailUrl;
            worldPayLog.Save();

            try
            {
                StoreHelper.ConfirmOrder(store, order);

            }
            catch (Exception ex)
            {
                log.Error("error sending confirmation email", ex);
            }

            // retrun the html

            if (config.WorldPayProduceShopperResponse)
            {
                CultureInfo currencyCulture = ResourceHelper.GetCurrencyCulture(wpResponse.Currency);

                string htmlTemplate = ResourceHelper.GetMessageTemplate(CultureInfo.CurrentUICulture, config.WorldPayShopperResponseTemplate);
                StringBuilder finalOutput = new StringBuilder();
                finalOutput.Append(htmlTemplate);
                finalOutput.Replace("#WorldPayBannerToken", "<WPDISPLAY ITEM=banner>"); //required by worldpay
                finalOutput.Replace("#CustomerName", wpResponse.Name);
                finalOutput.Replace("#StoreName", store.Name);
                finalOutput.Replace("#OrderId", order.OrderGuid.ToString());
                finalOutput.Replace("#StorePageLink", "<a href='" + storePageUrl + "'>" + storePageUrl + "</a>");
                finalOutput.Replace("#OrderDetailLink", "<a href='" + orderDetailUrl + "'>" + orderDetailUrl + "</a>");

                StringBuilder orderDetails = new StringBuilder();
                DataSet dsOffers = Order.GetOrderOffersAndProducts(store.Guid, order.OrderGuid);

                foreach (DataRow row in dsOffers.Tables["Offers"].Rows)
                {
                    string og = row["OfferGuid"].ToString();
                    orderDetails.Append(row["Name"].ToString() + " ");
                    orderDetails.Append(row["Quantity"].ToString() + " @ ");
                    orderDetails.Append(string.Format(currencyCulture, "{0:c}", Convert.ToDecimal(row["OfferPrice"])));
                    orderDetails.Append("<br />");

                    string whereClause = string.Format("OfferGuid = '{0}'", og);
                    DataView dv = new DataView(dsOffers.Tables["Products"], whereClause, "", DataViewRowState.CurrentRows);

                    if (dv.Count > 1)
                    {
                        foreach (DataRow r in dsOffers.Tables["Products"].Rows)
                        {
                            string pog = r["OfferGuid"].ToString();
                            if (og == pog)
                            {
                                orderDetails.Append(r["Name"].ToString() + " ");
                                orderDetails.Append(r["Quantity"].ToString() + "  <br />");

                            }

                        }
                    }

                }

                finalOutput.Replace("#OrderDetails", orderDetails.ToString());
                page.Response.Write(finalOutput.ToString());
                page.Response.Flush();

            }
        }