public ActionResult CheckOut(RDN.Store.Models.CheckOut model)
        {
            try
            {
                var shoppingCartId = StoreGateway.GetShoppingCartId(HttpContext);
                if (!shoppingCartId.HasValue)
                {

                    return Redirect(Url.Action("StoreCart"));
                }

                var sg = new StoreGateway();
                var checkout = sg.GetCheckoutData(shoppingCartId.Value, model.MerchantId);
                if (checkout == null || checkout.ShoppingCart.ItemsCount == 0)
                {
                    return Redirect(Url.Action("StoreCart"));

                }

                PaymentProvider paymentProvider;
                if (!Enum.TryParse(model.PaymentProviderId, out paymentProvider))
                {
                    return Redirect(Url.Action("StoreCart"));

                }

                var pg = new PaymentGateway();

                ShippingType shipType = ShippingType.Postal;
                if (checkout.WillPickUpAtStore)
                    shipType = ShippingType.PickUp;

                var invoice = pg.StartInvoiceWizard()
                    .Initalize(checkout.MerchantId, checkout.Currency, paymentProvider, PaymentMode.Live, ChargeTypeEnum.InStorePurchase)
                    .SetShipping(checkout.TotalShipping, shipType)
                    .SetInvoiceId(Guid.NewGuid())
                    .SetNotes(model.Notes)
                    .SetUserId(RDN.Library.Classes.Account.User.GetUserId())
                    .SetShoppingCartId(shoppingCartId.Value);


                if (paymentProvider == PaymentProvider.Stripe)
                {
                    string token = Request.Form["stripeToken"].ToString();
                    invoice.SetStripeTokenId(token);
                }
                var sellersAddress = new InvoiceContactInfo();
                if (shipType == ShippingType.PickUp)
                {
                    if (checkout.SellersAddress != null)
                    {
                        sellersAddress.City = checkout.SellersAddress.City;
                        sellersAddress.Country = checkout.SellersAddress.Country;
                        sellersAddress.CompanyName = checkout.SellersAddress.CompanyName;
                        sellersAddress.State = checkout.SellersAddress.State;
                        sellersAddress.Street = checkout.SellersAddress.Street;
                        sellersAddress.Street2 = checkout.SellersAddress.Street2;
                        sellersAddress.Zip = checkout.SellersAddress.Zip;
                        invoice.SetSellersAddress(sellersAddress);
                    }
                }

                var billingInfo = new InvoiceContactInfo();
                billingInfo.City = model.BillingAddress_City;
                billingInfo.Country = model.BillingAddress_Country;
                billingInfo.Email = model.BillingAddress_Email;
                billingInfo.FirstName = model.BillingAddress_FirstName;
                billingInfo.LastName = model.BillingAddress_LastName;
                billingInfo.Phone = model.BillingAddress_Phone;
                billingInfo.State = model.BillingAddress_State;
                billingInfo.Street = model.BillingAddress_Street;
                billingInfo.Street2 = model.BillingAddress_Street2;
                billingInfo.Zip = model.BillingAddress_Zip;
                if (User.Identity.IsAuthenticated)
                    RDN.Library.Classes.Account.User.AddContactToMember(RDN.Library.Classes.Account.User.GetMemberId(), billingInfo, AddressTypeEnum.Billing);
                if (model.IsBillingDifferentFromShipping)
                {
                    var shippingInfo = new InvoiceContactInfo();
                    shippingInfo.City = model.ShippingAddress_City;
                    shippingInfo.Country = model.ShippingAddress_Country;
                    shippingInfo.Email = model.ShippingAddress_Email;
                    shippingInfo.FirstName = model.ShippingAddress_FirstName;
                    shippingInfo.LastName = model.ShippingAddress_LastName;
                    shippingInfo.Phone = model.ShippingAddress_Phone;
                    shippingInfo.State = model.ShippingAddress_State;
                    shippingInfo.Street = model.ShippingAddress_Street;
                    shippingInfo.Street2 = model.ShippingAddress_Street2;
                    shippingInfo.Zip = model.ShippingAddress_Zip;
                    invoice.SetInvoiceContactData(billingInfo, shippingInfo);

                    if (User.Identity.IsAuthenticated)
                        RDN.Library.Classes.Account.User.AddContactToMember(RDN.Library.Classes.Account.User.GetMemberId(), shippingInfo, AddressTypeEnum.Shipping);

                }
                else
                    invoice.SetInvoiceContactData(billingInfo, billingInfo);


                foreach (var cartitem in checkout.ShoppingCart.Stores.FirstOrDefault().StoreItems)
                {
                    var item = new InvoiceItem();
                    item.ArticleNumber = cartitem.ArticleNumber;
                    item.Article2Number = cartitem.Article2Number;
                    item.Description = "Tax Included in Price: " + cartitem.Description;
                    item.Name = cartitem.Name;
                    item.BasePrice = cartitem.BasePrice + cartitem.BaseTaxOnItem;
                    item.Price = cartitem.Price;
                    item.TotalShipping = cartitem.Shipping;
                    item.Quantity = cartitem.QuantityOrdered;
                    item.Weight = cartitem.Weight;
                    item.SizeOfItem = cartitem.ItemSizeEnum;
                    item.ColorOfItem = cartitem.ColorAGB;
                    item.StoreItemId = cartitem.StoreItemId;
                    invoice.AddItemTaxIncluded(item);
                }

                var requestResponse = invoice.FinalizeInvoice();
                if (requestResponse.Error != null)
                    throw new Exception(requestResponse.Error);

                this.ClearCart(HttpContext.Session);

                if (requestResponse.Status == InvoiceStatus.Payment_Successful)
                    return Redirect(Url.Content("~/receipt/" + requestResponse.InvoiceId.ToString().Replace("-", "")));
                else if (requestResponse.Status == InvoiceStatus.Pending_Payment_From_Paypal)
                    return Redirect(requestResponse.RedirectLink);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return Redirect(Url.Content("~/?u=" + SiteMessagesEnum.sww));
        }
        private static DisplayInvoice DisplayInvoice(DataModels.PaymentGateway.Invoices.Invoice voice)
        {
            DisplayInvoice v = new DisplayInvoice();
            try
            {
                v.UserId = voice.UserId;
                v.AdminNote = voice.AdminNote;
                if (voice.CurrencyRate != null)
                {
                    v.Currency = voice.CurrencyRate.CurrencyAbbrName;
                    v.CurrencyCost = voice.CurrencyRate.CurrencyExchangePerUSD;
                }
                else
                {
                    v.Currency = "USD";
                    v.CurrencyCost = 1;
                }
                v.InvoiceId = voice.InvoiceId;
                v.TotalIncludingTax = voice.BasePriceForItems;
                v.ShoppingCartId = voice.ShoppingCartId;
                v.ShippingCost = voice.Shipping;
                v.RDNDeductedFee = voice.RDNDeductedFee;
                v.CreditCardCompanyProcessorDeductedFee = voice.CreditCardCompanyProcessorDeductedFee;
                v.PaymentProvider = (PaymentProvider)voice.PaymentProvider;
                v.Note = voice.Note;
                v.InvoiceStatus = (InvoiceStatus)voice.InvoiceStatus;
                v.ShippingType = (ShippingType)voice.ShippingType;
                v.Created = voice.Created;
                v.TotalItemsBeingSold = 0;
                if (voice.InvoiceShipping != null)
                {
                    v.InvoiceShipping.City = voice.InvoiceShipping.City;
                    if (!String.IsNullOrEmpty(voice.InvoiceShipping.Country))
                    {
                        var count = SiteCache.GetCountries().Where(x => x.CountryId == Convert.ToInt32(voice.InvoiceShipping.Country)).FirstOrDefault();
                        v.InvoiceShipping.Country = count.Name;
                    }
                    v.InvoiceShipping.Email = voice.InvoiceShipping.Email;
                    v.InvoiceShipping.FirstName = voice.InvoiceShipping.FirstName;
                    v.InvoiceShipping.LastName = voice.InvoiceShipping.LastName;
                    v.InvoiceShipping.State = voice.InvoiceShipping.State;
                    v.InvoiceShipping.Street = voice.InvoiceShipping.Street;
                    v.InvoiceShipping.Street2 = voice.InvoiceShipping.Street2;
                    v.InvoiceShipping.Zip = voice.InvoiceShipping.Zip;
                }
                foreach (var item in voice.Items)
                {
                    InvoiceItem i = new InvoiceItem();
                    i.Article2Number = item.Article2Number;
                    i.ArticleNumber = item.ArticleNumber;
                    i.Description = item.Description;
                    i.Name = item.Name;
                    i.Price = item.Price;
                    i.InvoiceItemId = item.InvoiceItemId;
                    i.Quantity = item.Quantity;
                    i.SizeOfItem = item.SizeOfItem;
                    if (i.SizeOfItem > 0)
                        i.SizeOfItemName = Enum.Parse(typeof(StoreItemShirtSizesEnum), item.SizeOfItem.ToString()).ToString();
                    i.StoreItemId = item.StoreItemId;
                    i.TotalShipping = item.Shipping;
                    if (item.ColorOfItem != null)
                    {
                        i.ColorName = item.ColorOfItem.ColorName;
                        Color c = Color.FromArgb(item.ColorOfItem.ColorIdCSharp);
                        i.ColorHex = ColorTranslator.ToHtml(c);
                    }
                    v.TotalItemsBeingSold += item.Quantity;
                    v.InvoiceItems.Add(i);
                }
                v.Merchant.MerchantId = voice.Merchant.MerchantId;
                v.Merchant.PrivateManagerId = voice.Merchant.PrivateManagerId;
                v.Merchant.ShopName = voice.Merchant.ShopName;
                var loc = Location.LocationFactory.DisplayLocation(voice.Merchant.Locations.FirstOrDefault());
                var address = loc.Contact.Addresses.FirstOrDefault();
                if (address != null)
                {
                    v.SellersAddress = new InvoiceContactInfo() { City = address.CityRaw, CompanyName = loc.LocationName, Country = address.Country, State = address.StateRaw, Street = address.Address1, Street2 = address.Address2, Zip = address.Zip };
                }

            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return v;
        }