public static CustomerAddress AsWebModel(this Data.Address address)
        {
            var customerAddress = new CustomerAddress();

            customerAddress.Address1 = address.Line1;
            customerAddress.Address2 = address.Line2;
            customerAddress.City = address.City;
            customerAddress.Company = address.Organization;
            customerAddress.Country = address.CountryName;
            customerAddress.CountryCode = "US";
            customerAddress.FirstName = address.FirstName;
            customerAddress.LastName = address.LastName;
            customerAddress.Phone = address.Phone;
            customerAddress.Province = address.RegionName;
            customerAddress.ProvinceCode = address.RegionId;
            customerAddress.Zip = address.PostalCode ?? address.Zip;

            return customerAddress;
        }
        public static CustomerAddress ToViewModel(this DataContracts.Address address)
        {
            var addressModel = new CustomerAddress();

            addressModel.Address1 = address.Line1;
            addressModel.Address2 = address.Line2;
            addressModel.City = address.City;
            addressModel.Company = address.Organization;
            addressModel.Country = address.CountryName;
            addressModel.CountryCode = address.CountryCode;
            addressModel.FirstName = address.FirstName;
            addressModel.LastName = address.LastName;
            addressModel.Phone = address.Phone;
            addressModel.Province = address.RegionName;
            addressModel.ProvinceCode = address.RegionId;
            addressModel.Zip = address.PostalCode;

            return addressModel;
        }
Beispiel #3
0
        private List <string> GetAddressErrors(CustomerAddress address)
        {
            var errors = new List <string>();

            if (string.IsNullOrEmpty(address.Address1))
            {
                errors.Add("Field \"Address line 1\" is required");
            }
            if (string.IsNullOrEmpty(address.City))
            {
                errors.Add("Field \"City\" is required");
            }
            if (string.IsNullOrEmpty(address.Company))
            {
                errors.Add("Field \"Company\" is required");
            }
            if (string.IsNullOrEmpty(address.Country))
            {
                errors.Add("Field \"Country\" is required");
            }
            if (string.IsNullOrEmpty(address.FirstName))
            {
                errors.Add("Field \"First name\" is required");
            }
            if (string.IsNullOrEmpty(address.LastName))
            {
                errors.Add("Field \"Last name\" is required");
            }
            if (string.IsNullOrEmpty(address.Phone))
            {
                errors.Add("Field \"Phone\" is required");
            }
            if (string.IsNullOrEmpty(address.Zip))
            {
                errors.Add("Field \"Postal code\" is required");
            }

            return(errors);
        }
        public static CustomerAddress AsWebModel(this ApiClient.DataContracts.Orders.Address address)
        {
            var ret = new CustomerAddress
                      {
                          Address1 = address.Line1,
                          Address2 = address.Line2,
                          City = address.City,
                          Company = address.Organization,
                          Country = address.CountryName,
                          CountryCode = address.CountryCode,
                          FirstName = address.FirstName,
                          LastName = address.LastName,
                          Phone = address.Phone,
                          Province = address.RegionName,
                          ProvinceCode = address.RegionId,
                          Zip = address.PostalCode
                      };

            return ret;
        }
        public static CustomerAddress AsWebModel(this NewAddressFormModel formModel)
        {
            var customerAddress = new CustomerAddress();

            customerAddress.Address1 = formModel.Address1;
            customerAddress.Address2 = formModel.Address2;
            customerAddress.City = formModel.City;
            customerAddress.Company = formModel.Company;
            customerAddress.Country = formModel.Country;
            customerAddress.CountryCode = "US";
            customerAddress.FirstName = formModel.FirstName;
            customerAddress.LastName = formModel.LastName;
            customerAddress.Phone = formModel.Phone;
            customerAddress.Province = formModel.Province;
            customerAddress.ProvinceCode = formModel.ProvinceCode;
            customerAddress.Zip = formModel.Zip;

            return customerAddress;
        }
        private List<string> GetAddressErrors(CustomerAddress address)
        {
            var errors = new List<string>();

            if (string.IsNullOrEmpty(address.Address1))
            {
                errors.Add("Field \"Address line 1\" is required");
            }
            if (string.IsNullOrEmpty(address.City))
            {
                errors.Add("Field \"City\" is required");
            }
            if (string.IsNullOrEmpty(address.Company))
            {
                errors.Add("Field \"Company\" is required");
            }
            if (string.IsNullOrEmpty(address.Country))
            {
                errors.Add("Field \"Country\" is required");
            }
            if (string.IsNullOrEmpty(address.FirstName))
            {
                errors.Add("Field \"First name\" is required");
            }
            if (string.IsNullOrEmpty(address.LastName))
            {
                errors.Add("Field \"Last name\" is required");
            }
            if (string.IsNullOrEmpty(address.Phone))
            {
                errors.Add("Field \"Phone\" is required");
            }
            if (string.IsNullOrEmpty(address.Zip))
            {
                errors.Add("Field \"Postal code\" is required");
            }

            return errors;
        }
        public async Task<ActionResult> Step1(CheckoutFirstStepFormModel formModel)
        {
            var form = GetForm(formModel.form_type);

            var checkout = await Service.GetCheckoutAsync();

            if (form != null)
            {
                var formErrors = GetFormErrors(ModelState);

                if (formErrors == null)
                {
                    form.PostedSuccessfully = true;

                    var shippingAddress = new CustomerAddress
                    {
                        Address1 = formModel.Address1,
                        Address2 = !string.IsNullOrEmpty(formModel.Address2) ? formModel.Address2 : null,
                        City = formModel.City,
                        Company = !string.IsNullOrEmpty(formModel.Company) ? formModel.Company : null,
                        Country = formModel.Country,
                        CountryCode = "US", //TODO Set country code by selected country name
                        FirstName = formModel.FirstName,
                        LastName = formModel.LastName,
                        Phone = !string.IsNullOrEmpty(formModel.Phone) ? formModel.Phone : null,
                        Province = formModel.Province,
                        ProvinceCode = "CA", //TODO Set province code by selected province name
                        Zip = formModel.Zip
                    };

                    checkout.Currency = Context.Shop.Currency;
                    checkout.Email = formModel.Email;
                    checkout.ShippingAddress = shippingAddress;

                    if (User.Identity.IsAuthenticated)
                    {
                        var customer = await this.CustomerService.GetCustomerAsync(formModel.Email, Context.Shop.StoreId);
                        if (customer != null)
                        {
                            customer.Addresses.Add(shippingAddress);
                            await CustomerService.UpdateCustomerAsync(customer);
                        }
                    }

                    await Service.UpdateCheckoutAsync(checkout);

                    return RedirectToAction("Step2", "Checkout");
                }
                else
                {
                    form.Errors = formErrors;
                    form.PostedSuccessfully = false;

                    return RedirectToAction("Step1", "Checkout");
                }
            }

            Context.ErrorMessage = "Liquid error: Form context was not found.";

            return View("error");
        }
        public async Task<ActionResult> Step2(CheckoutSecondStepFormModel formModel)
        {
            var form = GetForm(formModel.form_type);

            if (form != null)
            {
                var formErrors = GetFormErrors(ModelState);

                if (formErrors == null)
                {
                    var checkout = await Service.GetCheckoutAsync();

                    if (!checkout.RequiresShipping)
                    {
                        checkout.Email = formModel.Email;
                    }

                    var billingAddress = new CustomerAddress
                    {
                        Address1 = formModel.Address1,
                        Address2 = !string.IsNullOrEmpty(formModel.Address2) ? formModel.Address2 : null,
                        City = formModel.City,
                        Company = !string.IsNullOrEmpty(formModel.Company) ? formModel.Company : null,
                        Country = formModel.Country,
                        CountryCode = "US", //TODO Set country code by selected country name
                        FirstName = formModel.FirstName,
                        LastName = formModel.LastName,
                        Phone = !string.IsNullOrEmpty(formModel.Phone) ? formModel.Phone : null,
                        Province = formModel.Province,
                        ProvinceCode = "CA", //TODO Set province code by selected province name
                        Zip = formModel.Zip
                    };

                    checkout.BillingAddress = billingAddress;

                    checkout.ShippingMethods = await Service.GetShippingMethodsAsync(checkout.Id);
                    checkout.PaymentMethods = await Service.GetCartPaymentMethodsAsync(checkout.Id);

                    if (checkout.RequiresShipping)
                    {
                        checkout.ShippingMethod = checkout.ShippingMethods.FirstOrDefault(sm => sm.Handle == formModel.ShippingMethodId);
                    }

                    checkout.PaymentMethod = checkout.PaymentMethods.FirstOrDefault(pm => pm.Code == formModel.PaymentMethodId);
                    if (checkout.PaymentMethod.Type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
                    {
                        checkout.PaymentMethod.CardNumber = formModel.CardNumber;
                        checkout.PaymentMethod.CardExpirationMonth = formModel.CardExpirationMonth;
                        checkout.PaymentMethod.CardExpirationYear = formModel.CardExpirationYear;
                        checkout.PaymentMethod.CardCvv = formModel.CardCvv;
                    }

                    if (User.Identity.IsAuthenticated)
                    {
                        var customer = await CustomerService.GetCustomerAsync(checkout.Email, Context.Shop.StoreId);
                        if (customer != null)
                        {
                            customer.Addresses.Add(billingAddress);
                            await this.CustomerService.UpdateCustomerAsync(customer);
                        }
                    }

                    await Service.UpdateCheckoutAsync(checkout);

                    var dtoOrder = await Service.CreateOrderAsync();

                    checkout.Order = dtoOrder.AsWebModel();
                    Context.Checkout = checkout;

                    var inPayment = dtoOrder.InPayments.FirstOrDefault(); // For test

                    if (inPayment != null)
                    {
                        VirtoCommerce.ApiClient.DataContracts.ProcessPaymentResult paymentResult = null;

                        if (checkout.PaymentMethod.Type.Equals("Standard"))
                        {
                            string cardType = GetCreditCardType(formModel.CardNumber);

                            if (!String.IsNullOrEmpty(cardType))
                            {
                                var cardInfo = new ApiClient.DataContracts.BankCardInfo
                                {
                                    BankCardCVV2 = formModel.CardCvv,
                                    BankCardMonth = int.Parse(formModel.CardExpirationMonth),
                                    BankCardNumber = formModel.CardNumber,
                                    BankCardType = cardType,
                                    BankCardYear = int.Parse(formModel.CardExpirationYear)
                                };

                                paymentResult = await Service.ProcessPaymentAsync(dtoOrder.Id, inPayment.Id, cardInfo);
                            }
                        }
                        else
                        {
                            paymentResult = await Service.ProcessPaymentAsync(dtoOrder.Id, inPayment.Id, null);
                        }

                        if (paymentResult != null)
                        {
                            if (paymentResult != null)
                            {
                                if (paymentResult.IsSuccess)
                                {
                                    if (paymentResult.PaymentMethodType == ApiClient.DataContracts.PaymentMethodType.Redirection)
                                    {
                                        if (!string.IsNullOrEmpty(paymentResult.RedirectUrl))
                                        {
                                            return Redirect(paymentResult.RedirectUrl);
                                        }
                                    }
                                    if (paymentResult.PaymentMethodType == ApiClient.DataContracts.PaymentMethodType.PreparedForm)
                                    {
                                        if (!string.IsNullOrEmpty(paymentResult.HtmlForm))
                                        {
                                            SiteContext.Current.Set("payment_html_form", paymentResult.HtmlForm);
                                            return View("payment");
                                        }
                                    }
                                    if (paymentResult.PaymentMethodType == ApiClient.DataContracts.PaymentMethodType.Standard)
                                    {
                                        var productsIds = dtoOrder.Items.Select(i => i.ProductId);
                                        var catalogItems = await Service.GetCatalogItemsByIdsAsync(productsIds, "ItemAssets");

                                        var nonShippingProducts = catalogItems.Where(ci => ci.ProductType == "Digital");
                                        if (nonShippingProducts.Count() > 0)
                                        {
                                            var downloadLinks = new List<ProductDownloadLinks>();

                                            foreach (var nonShippingProduct in nonShippingProducts)
                                            {
                                                var productLinks = new ProductDownloadLinks
                                                {
                                                    ProductName = nonShippingProduct.Name
                                                };

                                                int linkCount = 1;
                                                foreach (var asset in nonShippingProduct.Assets)
                                                {
                                                    var url = Url.Action("Index", "Download", new { @file = asset.Name, @oid = dtoOrder.Id, @pid = nonShippingProduct.Id }, Request.Url.Scheme);
                                                    productLinks.Links.Add(new DownloadLink
                                                    {
                                                        Filename = asset.Name,
                                                        Text = nonShippingProduct.Assets.Count() > 1 ? String.Format("Download link {0}", linkCount) : "Download link",
                                                        Url = url
                                                    });

                                                    linkCount++;
                                                }

                                                downloadLinks.Add(productLinks);
                                            }

                                            Context.Set("download_links", downloadLinks);
                                        }

                                        Context.Order = dtoOrder.AsWebModel();

                                        return View("thanks_page");
                                    }
                                }
                                else
                                {
                                    Context.ErrorMessage = paymentResult.Error;

                                    return View("error");
                                }
                            }
                        }
                    }
                }
                else
                {
                    form.Errors = formErrors;
                    form.PostedSuccessfully = false;

                    return RedirectToAction("Step2");
                }
            }

            return View("error");
        }