Ejemplo n.º 1
0
        public ActionResult Index(string errorMessage, string returnUrl, bool?showErrors)
        {
            PushErrorMessages(errorMessage);

            var defaultReturnUrl = "";

            if (string.IsNullOrEmpty(defaultReturnUrl))
            {
                defaultReturnUrl = Url.Action(ActionNames.Index, ControllerNames.Home);
            }

            var safeReturnUrl = Url.MakeSafeReturnUrl(returnUrl, defaultReturnUrl);

            // Get the current checkout state
            var customer = HttpContext.GetCustomer();

            // We need to validate that a (potentially previously) selected PM is still available and valid (as any number of site settings/configs/customerLevel values could have changed)
            if (!string.IsNullOrEmpty(customer.RequestedPaymentMethod) &&
                !PaymentOptionProvider.PaymentMethodSelectionIsValid(customer.RequestedPaymentMethod, customer))
            {
                customer.UpdateCustomer(requestedPaymentMethod: string.Empty);
            }

            var storeId = AppLogic.StoreID();
            var cart    = CachedShoppingCartProvider.Get(customer, CartTypeEnum.ShoppingCart, AppLogic.StoreID());

            // Make sure there's a GiftCard record for every gift card product in the cart
            CreateGiftCards(customer, cart);

            var checkoutConfiguration = CheckoutConfigurationProvider.GetCheckoutConfiguration();

            var selectedPaymentMethod = PaymentMethodInfoProvider
                                        .GetPaymentMethodInfo(
                paymentMethod: customer.RequestedPaymentMethod,
                gateway: AppLogic.ActivePaymentGatewayCleaned());

            var persistedCheckoutContext = PersistedCheckoutContextProvider
                                           .LoadCheckoutContext(customer);

            var cartContext = CartContextProvider
                              .LoadCartContext(
                customer: customer,
                configuration: checkoutConfiguration,
                persistedCheckoutContext: persistedCheckoutContext,
                selectedPaymentMethod: selectedPaymentMethod);

            var checkoutSelectionContext = CheckoutSelectionProvider
                                           .GetCheckoutSelection(
                customer: customer,
                persistedCheckoutContext: persistedCheckoutContext,
                selectedPaymentMethod: selectedPaymentMethod);

            var result = CheckoutEngine
                         .EvaluateCheckout(
                customer: customer,
                configuration: checkoutConfiguration,
                persistedCheckoutContext: persistedCheckoutContext,
                checkoutSelectionContext: checkoutSelectionContext,
                storeId: storeId,
                cartContext: cartContext);

            var updated = CheckoutSelectionProvider.ApplyCheckoutSelections(customer, result.Selections);

            var action = GetActionForState(result.State);

            // Perform the resulting action
            switch (action)
            {
            case CheckoutAction.Error:
                if (result.State == CheckoutState.CustomerIsNotOver13)
                {
                    break;
                }
                else if (result.State == CheckoutState.TermsAndConditionsRequired)
                {
                    break;
                }
                else if (result.State == CheckoutState.ShippingAddressDoesNotMatchBillingAddress)
                {
                    NoticeProvider.PushNotice("This store can only ship to the billing address", NoticeType.Failure);
                }
                else if (result.State == CheckoutState.MicroPayBalanceIsInsufficient)
                {
                    NoticeProvider.PushNotice(
                        string.Format("Your MicroPay balance is insufficient for purchasing this order. <br />(<b>Your MicroPay Balance is {0}</b>)",
                                      Localization.CurrencyStringForDisplayWithExchangeRate(updated.Customer.MicroPayBalance, updated.Customer.CurrencySetting)),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.SubtotalDoesNotMeetMinimumAmount)
                {
                    NoticeProvider.PushNotice(
                        string.Format("The minimum allowed order amount is {0} Please add additional items to your cart, or increase item quantities.",
                                      updated.Customer.CurrencyString(AppLogic.AppConfigNativeDecimal("CartMinOrderAmount"))),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.CartItemsLessThanMinimumItemCount)
                {
                    NoticeProvider.PushNotice(
                        string.Format("Please make sure you have ordered at least {0} items - any {1} items from our site - before checking out.",
                                      AppLogic.AppConfigNativeInt("MinCartItemsBeforeCheckout"),
                                      AppLogic.AppConfigNativeInt("MinCartItemsBeforeCheckout")),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.CartItemsGreaterThanMaximumItemCount)
                {
                    NoticeProvider.PushNotice(
                        string.Format("We allow a maximum of {0} items per order.  Please remove some products from your cart before beginning checkout.",
                                      AppLogic.AppConfigNativeInt("MaxCartItemsBeforeCheckout")),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.RecurringScheduleMismatchOnItems)
                {
                    NoticeProvider.PushNotice("Your cart has Auto-Ship items with conflicting shipment schedules. Only one Auto-Ship schedule is allowed per order.", NoticeType.Failure);
                }
                else
                {
                    NoticeProvider.PushNotice(result.State.ToString(), NoticeType.Failure);
                }
                break;

            case CheckoutAction.Empty:
                return(View("EmptyCart"));
            }

            return(RenderIndexView(
                       checkoutStageContext: result.CheckoutStageContext,
                       persistedCheckoutContext: updated.PersistedCheckoutContext,
                       selectedPaymentMethod: updated.SelectedPaymentMethod,
                       customer: updated.Customer,
                       termsAndConditionsAccepted: result.Selections.TermsAndConditionsAccepted,
                       returnUrl: safeReturnUrl,
                       showCheckoutStageErrors: showErrors ?? false));
        }
        public ActionResult Index(string errorMessage, string returnUrl, bool?showErrors)
        {
            PushErrorMessages(errorMessage);

            var safeReturnUrl = Url.MakeSafeReturnUrl(returnUrl, Url.GetDefaultContinueShoppingUrl());

            // Get the current checkout state
            var customer = HttpContext.GetCustomer();

            // We need to validate that a (potentially previously) selected PM is still available and valid (as any number of site settings/configs/customerLevel values could have changed)
            if (!string.IsNullOrEmpty(customer.RequestedPaymentMethod) &&
                !PaymentOptionProvider.PaymentMethodSelectionIsValid(customer.RequestedPaymentMethod, customer))
            {
                customer.UpdateCustomer(requestedPaymentMethod: string.Empty);
            }

            var storeId = AppLogic.StoreID();
            var cart    = CachedShoppingCartProvider.Get(customer, CartTypeEnum.ShoppingCart, AppLogic.StoreID());

            // Make sure there's a GiftCard record for every gift card product in the cart
            CreateGiftCards(customer, cart);

            var checkoutConfiguration = CheckoutConfigurationProvider.GetCheckoutConfiguration();

            var selectedPaymentMethod = PaymentMethodInfoProvider
                                        .GetPaymentMethodInfo(
                paymentMethod: customer.RequestedPaymentMethod,
                gateway: AppLogic.ActivePaymentGatewayCleaned());

            var persistedCheckoutContext = PersistedCheckoutContextProvider
                                           .LoadCheckoutContext(customer);

            var cartContext = CartContextProvider
                              .LoadCartContext(
                customer: customer,
                configuration: checkoutConfiguration,
                persistedCheckoutContext: persistedCheckoutContext,
                selectedPaymentMethod: selectedPaymentMethod);

            var checkoutSelectionContext = CheckoutSelectionProvider
                                           .GetCheckoutSelection(
                customer: customer,
                persistedCheckoutContext: persistedCheckoutContext,
                selectedPaymentMethod: selectedPaymentMethod);

            var result = CheckoutEngine
                         .EvaluateCheckout(
                customer: customer,
                configuration: checkoutConfiguration,
                persistedCheckoutContext: persistedCheckoutContext,
                checkoutSelectionContext: checkoutSelectionContext,
                storeId: storeId,
                cartContext: cartContext);

            var updated = CheckoutSelectionProvider.ApplyCheckoutSelections(customer, result.Selections);

            var action = GetActionForState(result.State);

            // Perform the resulting action
            switch (action)
            {
            case CheckoutAction.Error:
                if (result.State == CheckoutState.CustomerIsNotOver13)
                {
                    break;
                }
                else if (result.State == CheckoutState.TermsAndConditionsRequired)
                {
                    break;
                }
                else if (result.State == CheckoutState.ShippingAddressDoesNotMatchBillingAddress)
                {
                    NoticeProvider.PushNotice(AppLogic.GetString("checkout.AllowShipToDifferentThanBillTo"), NoticeType.Failure);
                }
                else if (result.State == CheckoutState.MicroPayBalanceIsInsufficient)
                {
                    NoticeProvider.PushNotice(
                        string.Format(AppLogic.GetString("checkoutpayment.aspx.26"),
                                      Localization.CurrencyStringForDisplayWithExchangeRate(updated.Customer.MicroPayBalance, updated.Customer.CurrencySetting)),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.SubtotalDoesNotMeetMinimumAmount)
                {
                    NoticeProvider.PushNotice(
                        string.Format(AppLogic.GetString("shoppingcart.aspx.4"),
                                      updated.Customer.CurrencyString(AppLogic.AppConfigNativeDecimal("CartMinOrderAmount"))),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.CartItemsLessThanMinimumItemCount)
                {
                    NoticeProvider.PushNotice(
                        string.Format(AppLogic.GetString("shoppingcart.cs.20"),
                                      AppLogic.AppConfigNativeInt("MinCartItemsBeforeCheckout"),
                                      AppLogic.AppConfigNativeInt("MinCartItemsBeforeCheckout")),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.CartItemsGreaterThanMaximumItemCount)
                {
                    NoticeProvider.PushNotice(
                        string.Format(AppLogic.GetString("shoppingcart.cs.119"),
                                      AppLogic.AppConfigNativeInt("MaxCartItemsBeforeCheckout")),
                        NoticeType.Failure);
                }
                else if (result.State == CheckoutState.RecurringScheduleMismatchOnItems)
                {
                    NoticeProvider.PushNotice(AppLogic.GetString("shoppingcart.aspx.19"), NoticeType.Failure);
                }
                else
                {
                    NoticeProvider.PushNotice(result.State.ToString(), NoticeType.Failure);
                }
                break;

            case CheckoutAction.Empty:
                return(View("EmptyCart"));
            }

            return(RenderIndexView(
                       checkoutStageContext: result.CheckoutStageContext,
                       persistedCheckoutContext: updated.PersistedCheckoutContext,
                       selectedPaymentMethod: updated.SelectedPaymentMethod,
                       customer: updated.Customer,
                       termsAndConditionsAccepted: result.Selections.TermsAndConditionsAccepted,
                       returnUrl: safeReturnUrl,
                       showCheckoutStageErrors: showErrors ?? false));
        }