Ejemplo n.º 1
0
        public CreateAutoOrderRequest(Common.Api.ExigoOData.AutoOrder autoOrder)
        {
            var model = new CreateAutoOrderRequest();

            if (autoOrder == null)
            {
                return;
            }

            CustomerID                 = autoOrder.CustomerID;
            ExistingAutoOrderID        = autoOrder.AutoOrderID;
            OverwriteExistingAutoOrder = true;

            Frequency    = Exigo.GetFrequencyType(autoOrder.FrequencyTypeID);
            StartDate    = autoOrder.StartDate;
            StopDate     = autoOrder.StopDate;
            CurrencyCode = autoOrder.CurrencyCode;
            WarehouseID  = autoOrder.WarehouseID;
            ShipMethodID = autoOrder.ShipMethodID;
            PriceType    = PriceTypes.Wholesale;
            PaymentType  = Exigo.GetAutoOrderPaymentType(autoOrder.AutoOrderPaymentTypeID);
            ProcessType  = Exigo.GetAutoOrderProcessType(autoOrder.AutoOrderProcessTypeID);
            Details      = autoOrder.Details.Select(c => new OrderDetailRequest()
            {
                ItemCode                         = c.ItemCode,
                Quantity                         = c.Quantity,
                ParentItemCode                   = c.ParentItemCode,
                BusinessVolumeEachOverride       = c.BusinessVolumeEachOverride,
                CommissionableVolumeEachOverride = c.CommissionableVolumeEachOverride,
                DescriptionOverride              = c.ItemDescription,
                PriceEachOverride                = c.PriceEachOverride,
                ShippingPriceEachOverride        = c.ShippingPriceEachOverride,
                TaxableEachOverride              = c.TaxableEachOverride
            }).ToArray();

            FirstName = autoOrder.FirstName;
            LastName  = autoOrder.LastName;
            Company   = autoOrder.Company;
            Address1  = autoOrder.Address1;
            Address2  = autoOrder.Address2;
            City      = autoOrder.City;
            State     = autoOrder.State;
            Zip       = autoOrder.Zip;
            Country   = autoOrder.Country;
            Email     = autoOrder.Email;
            Phone     = autoOrder.Phone;

            Notes       = autoOrder.Notes;
            Other11     = autoOrder.Other11;
            Other12     = autoOrder.Other12;
            Other13     = autoOrder.Other13;
            Other14     = autoOrder.Other14;
            Other15     = autoOrder.Other15;
            Other16     = autoOrder.Other16;
            Other17     = autoOrder.Other17;
            Other18     = autoOrder.Other18;
            Other19     = autoOrder.Other19;
            Other20     = autoOrder.Other20;
            Description = autoOrder.AutoOrderDescription;
        }
        public CreateAutoOrderRequest(Common.Api.ExigoOData.AutoOrder autoOrder)
        {
            var model = new CreateAutoOrderRequest();
            if (autoOrder == null) return;

            CustomerID                           = autoOrder.CustomerID;
            ExistingAutoOrderID                  = autoOrder.AutoOrderID;
            OverwriteExistingAutoOrder           = true;

            Frequency                            = Exigo.GetFrequencyType(autoOrder.FrequencyTypeID);
            StartDate                            = autoOrder.StartDate;
            StopDate                             = autoOrder.StopDate;
            CurrencyCode                         = autoOrder.CurrencyCode;
            WarehouseID                          = autoOrder.WarehouseID;
            ShipMethodID                         = autoOrder.ShipMethodID;
            PriceType                            = PriceTypes.Wholesale;
            PaymentType                          = Exigo.GetAutoOrderPaymentType(autoOrder.AutoOrderPaymentTypeID);
            ProcessType                          = Exigo.GetAutoOrderProcessType(autoOrder.AutoOrderProcessTypeID);
            Details                              = autoOrder.Details.Select(c => new OrderDetailRequest()
            {
                ItemCode                         = c.ItemCode,
                Quantity                         = c.Quantity,
                ParentItemCode                   = c.ParentItemCode,
                BusinessVolumeEachOverride       = c.BusinessVolumeEachOverride,
                CommissionableVolumeEachOverride = c.CommissionableVolumeEachOverride,
                DescriptionOverride              = c.ItemDescription,
                PriceEachOverride                = c.PriceEachOverride,
                ShippingPriceEachOverride        = c.ShippingPriceEachOverride,
                TaxableEachOverride              = c.TaxableEachOverride
            }).ToArray();

            FirstName                            = autoOrder.FirstName;
            LastName                             = autoOrder.LastName;
            Company                              = autoOrder.Company;
            Address1                             = autoOrder.Address1;
            Address2                             = autoOrder.Address2;
            City                                 = autoOrder.City;
            State                                = autoOrder.State;
            Zip                                  = autoOrder.Zip;
            Country                              = autoOrder.Country;
            Email                                = autoOrder.Email;
            Phone                                = autoOrder.Phone;

            Notes                                = autoOrder.Notes;
            Other11                              = autoOrder.Other11;
            Other12                              = autoOrder.Other12;
            Other13                              = autoOrder.Other13;
            Other14                              = autoOrder.Other14;
            Other15                              = autoOrder.Other15;
            Other16                              = autoOrder.Other16;
            Other17                              = autoOrder.Other17;
            Other18                              = autoOrder.Other18;
            Other19                              = autoOrder.Other19;
            Other20                              = autoOrder.Other20;
            Description                          = autoOrder.AutoOrderDescription;
        }
        public JsonNetResult UpdateAutoOrder(int frequency, List<ShoppingCartItem> items, int runDay, int autoorderID = 0)
        {
            var currentDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
            try
            {
                // First, we determine if we are dealing with an active auto order or if we are just updating the new auto order in the current shopping cart
                if (autoorderID == 0)
                {
                    var autoorderItems = ShoppingCart.Items.Where(c => c.Type == ShoppingCartItemType.AutoOrder).ToList();

                    foreach (var item in autoorderItems)
                    {
                        var currentItem = items.Where(i => i.ID == item.ID).FirstOrDefault();

                        if (item.Quantity != currentItem.Quantity)
                        {
                            ShoppingCart.Items.Update(item.ID, currentItem.Quantity);
                        }
                    }

                    if (ShoppingCart.Items.Where(c => c.Type == ShoppingCartItemType.AutoOrder).Count() == 0)
                    {
                        ShoppingCart.AutoOrderFrequencyTypeID = 0;
                    }

                    Exigo.PropertyBags.Update(ShoppingCart);

                    var runDate = PropertyBag.AutoOrderStartDate;

                    var newStartDate = new DateTime(runDate.Year, runDate.Month, runDay);
                    if (newStartDate < currentDate)
                    {
                        newStartDate = newStartDate.AddMonths(1);
                    }
                    PropertyBag.AutoOrderStartDate = newStartDate;
                    Exigo.PropertyBags.Update(PropertyBag);
                }
                else
                {
                    var existingAutoOrder = Exigo.OData().AutoOrders.Expand("Details")
                    .Where(c => c.CustomerID == Identity.Customer.CustomerID)
                    .Where(c => c.AutoOrderID == autoorderID)
                    .FirstOrDefault();

                    // Re-create the autoorder
                    var request = new CreateAutoOrderRequest(existingAutoOrder);

                    var details = new List<OrderDetailRequest>();

                    foreach (var item in items.Where(i => i.Quantity > 0))
                    {
                        details.Add(new OrderDetailRequest
                        {
                            ItemCode = item.ItemCode,
                            Quantity = item.Quantity,
                            Type = ShoppingCartItemType.AutoOrder
                        });
                    }

                    request.Details = details.ToArray();

                    var startDate = request.StartDate;
                    var newStartDate = new DateTime(startDate.Year, startDate.Month, runDay);
                    if (newStartDate < currentDate)
                    {
                        newStartDate = newStartDate.AddMonths(1);
                    }
                    request.StartDate = newStartDate;

                    var response = Exigo.WebService().CreateAutoOrder(request);

                    // Clear out the auto order cart items since we have updated our auto order at this point
                    if (response.Result.Status == ResultStatus.Success)
                    {
                        ShoppingCart.Items.Remove(ShoppingCartItemType.AutoOrder);
                        Exigo.PropertyBags.Update(ShoppingCart);
                    }

                    // May need to return different type of view for updating existing auto orders
                    // return GetAutoOrderDetails(frequency);
                }

                return GetAutoOrderDetails(frequency);
            }
            catch (Exception ex)
            {
                return new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }
        public ActionResult SubmitCheckout()
        {
            // Do one final check to ensure that our logic points are met before attempting to submit
            var logicResult = LogicProvider.CheckLogic();
            if (!logicResult.IsValid)
            {
                return logicResult.NextAction;
            }

            // Set up our guest customer & testing variables
            var isGuestCheckout = Identity.Customer == null;
            var isLocal = Request.IsLocal;

            var isRedirectPayment = PropertyBag.IsRedirectPayment;

            try
            {
                // Start creating the API requests
                var details = new List<ApiRequest>();

                // Update our Retail Customer to Smart Shopper, if applicable
                if (PropertyBag.GetSmartShopperPrice)
                {
                    var address = PropertyBag.ShippingAddress;

                    var updateCustomerRequest = new UpdateCustomerRequest
                    {
                        CustomerID = Identity.Customer.CustomerID,
                        MainAddress1 = address.Address1,
                        MainAddress2 = address.Address2,
                        MainCity = address.City,
                        MainState = address.State,
                        MainZip = address.Zip,
                        MainCountry = address.Country,
                        CustomerType = CustomerTypes.SmartShopper
                    };

                    details.Add(updateCustomerRequest);

                    // Create the Replicated Site for our Smart Shopper
                    var newSite = new SetCustomerSiteRequest
                    {
                        CustomerID = Identity.Customer.CustomerID,
                        WebAlias = Identity.Customer.CustomerID.ToString(),
                        FirstName = Identity.Customer.FirstName,
                        LastName = Identity.Customer.LastName,
                        Address1 = address.Address1,
                        Address2 = address.Address2,
                        City = address.City,
                        State = address.State,
                        Zip = address.Zip,
                        Country = address.Country,
                        Email = address.Email,
                        Company = Identity.Customer.PublicName,
                        Phone = address.Phone
                    };

                    details.Add(newSite);
                }

                var orderItems = ShoppingCart.Items.Where(c => c.Type == ShoppingCartItemType.Order);
                var hasOrder = orderItems.Count() > 0;

                var autoOrderItems = ShoppingCart.Items.Where(c => c.Type == ShoppingCartItemType.AutoOrder);
                var hasAutoOrder = autoOrderItems.Count() > 0;

                // Create the order request, if applicable
                if (hasOrder)
                {
                    var orderRequest = new CreateOrderRequest(OrderConfiguration, PropertyBag.ShipMethodID, orderItems, PropertyBag.ShippingAddress);

                    if (!isGuestCheckout)
                    {
                        orderRequest.CustomerID = Identity.Customer.CustomerID;
                    }

                    if (isRedirectPayment)
                    {
                        orderRequest.OrderStatus = OrderStatusType.Pending;
                    }

                    if (Identity.Customer.CustomerTypeID == CustomerTypes.SmartShopper)
                    {
                        orderRequest.PriceType = PriceTypes.Wholesale;
                    }

                    if (hasAutoOrder)
                    {
                        orderRequest.PriceType = PriceTypes.Autoship;
                    }

                    details.Add(orderRequest);
                }

                // Create the autoorder request, if applicable
                if (hasAutoOrder)
                {
                    var autoOrderRequest = new CreateAutoOrderRequest(AutoOrderConfiguration, Exigo.GetAutoOrderPaymentType(PropertyBag.PaymentMethod), PropertyBag.AutoOrderStartDate, AutoOrderConfiguration.DefaultShipMethodID, autoOrderItems, PropertyBag.ShippingAddress);

                    autoOrderRequest.Frequency = FrequencyType.Monthly;

                    if (!isGuestCheckout)
                    {
                        autoOrderRequest.CustomerID = Identity.Customer.CustomerID;
                    }

                    details.Add(autoOrderRequest);
                }

                // Create the payment request
                if (PropertyBag.PaymentMethod is CreditCard)
                {
                    var card = PropertyBag.PaymentMethod as CreditCard;
                    if (card.Type == CreditCardType.New)
                    {
                        card = Exigo.SaveNewCustomerCreditCard(Identity.Customer.CustomerID, card);

                        if (hasAutoOrder)
                        {
                            if (!isGuestCheckout)
                            {
                                card = Exigo.SaveNewCustomerCreditCard(Identity.Customer.CustomerID, card);
                                ((CreateAutoOrderRequest)details.Where(c => c is CreateAutoOrderRequest).FirstOrDefault()).PaymentType = Exigo.GetAutoOrderPaymentType(card);
                            }
                            else
                            {
                                // Add logic if guest checkout is allowed : constructor for SetAccountCreditCardTokenRequest

                            }
                        }
                        if (hasOrder)
                        {
                            if (card.IsTestCreditCard)
                            {
                                // no need to charge card
                                ((CreateOrderRequest)details.Where(c => c is CreateOrderRequest).FirstOrDefault()).OrderStatus = OrderStatusType.Shipped;
                            }
                            else
                            {
                                if (!isLocal)
                                {
                                    details.Add(new ChargeCreditCardTokenRequest(card));
                                }
                            }
                        }
                    }
                    else
                    {
                        if (hasOrder)
                        {
                            if (card.IsTestCreditCard)
                            {
                                // no need to charge card
                            }
                            else
                            {
                                if (!isLocal)
                                {
                                    var cctype = (card.Type == CreditCardType.Primary) ? AccountCreditCardType.Primary : AccountCreditCardType.Secondary;
                                    details.Add(new ChargeCreditCardOnFileRequest { CreditCardAccountType = cctype });
                                }
                            }
                        }
                    }
                }

                // Process the transaction
                var transactionRequest = new TransactionalRequest();
                transactionRequest.TransactionRequests = details.ToArray();
                var transactionResponse = Exigo.WebService().ProcessTransaction(transactionRequest);

                var newOrderID = 1;
                var customerID = 0;
                if (transactionResponse.Result.Status == ResultStatus.Success)
                {
                    foreach (var response in transactionResponse.TransactionResponses)
                    {
                        if (response is CreateOrderResponse) newOrderID = ((CreateOrderResponse)response).OrderID;
                        if (response is CreateCustomerResponse) customerID = ((CreateCustomerResponse)response).CustomerID;
                    }
                }

                if (PropertyBag.GetSmartShopperPrice)
                {

                    Identity.Customer.Refresh();
                }

                customerID = Identity.Customer.CustomerID;
                var token = Security.Encrypt(new { OrderID = newOrderID, CustomerID = Identity.Customer.CustomerID });

                // handle redirect payments
                var selectedCountry = PropertyBag.ShippingAddress.Country;

                if (isRedirectPayment && hasOrder)
                {
                    var paymentProvider = PaymentService.GetPaymentProvider(selectedCountry);
                    var order = Exigo.GetCustomerOrders(new GetCustomerOrdersRequest()
                    {
                        CustomerID = customerID,
                        OrderID = newOrderID,
                        IncludeOrderDetails = true
                    }).FirstOrDefault();

                    if (paymentProvider.HandlerType == PaymentHandlerType.Remote)
                    {
                        //Exigo.PropertyBags.Delete(PropertyBag);
                        Exigo.PropertyBags.Delete(ShoppingCart);

                        paymentProvider.OrderConfiguration = OrderConfiguration;
                        paymentProvider.Order = order;
                        paymentProvider.Order.ShipMethodID = PropertyBag.ShipMethodID;

                    }

                    // Get the request data
                    var paymentRequest = paymentProvider.GetPaymentRequest(new PaymentRequestArgs() { ReturnUrl = PaymentRedirectURL, BillingName = order.Recipient.FullName, BillingAddress = PropertyBag.BillingAddress, WebAlias = Identity.Owner.WebAlias });

                    // Handle the request
                    var postPaymentRequest = paymentRequest as POSTPaymentRequest;
                    if (postPaymentRequest != null)
                    {

                        Exigo.PropertyBags.Delete(PropertyBag);

                        return new JsonNetResult(new
                        {
                            success = true,
                            redirectForm = postPaymentRequest.RequestForm
                        });
                    }
                    else
                    {
                        //var urlHelper = new UrlHelper(Request.RequestContext);
                        //var completeUrl = urlHelper.Action("OrderComplete", new { token = token });

                        return new JsonNetResult(new
                        {
                            success = false,
                        });
                    }
                }

                return new JsonNetResult(new
                {
                    success = true,
                    token = token
                });

            }
            catch (Exception exception)
            {
                return new JsonNetResult(new
                {
                    success = false,
                    message = exception.Message
                });
            }
        }