// Ajax.
        public ActionResult ConfirmOrder(string formData)
        {
            string redirectUrl = null;
            var    messages    = new List <string>();
            var    success     = false;

            try
            {
                var store    = Services.StoreContext.CurrentStore;
                var customer = Services.WorkContext.CurrentCustomer;
                var processPaymentRequest = (_httpContext.Session["OrderPaymentInfo"] as ProcessPaymentRequest) ?? new ProcessPaymentRequest();

                processPaymentRequest.StoreId    = store.Id;
                processPaymentRequest.CustomerId = customer.Id;
                processPaymentRequest.PaymentMethodSystemName = AmazonPayPlugin.SystemName;

                // We must check here if an order can be placed to avoid creating unauthorized Amazon payment objects.
                var warnings = _orderProcessingService.GetOrderPlacementWarnings(processPaymentRequest);

                if (!warnings.Any())
                {
                    if (_orderProcessingService.IsMinimumOrderPlacementIntervalValid(customer, store))
                    {
                        if (_apiService.ConfirmOrderReference())
                        {
                            success = true;

                            var state = _httpContext.GetAmazonPayState(Services.Localization);
                            state.FormData = formData.EmptyNull();
                        }
                        else
                        {
                            _httpContext.Session["AmazonPayFailedPaymentReason"] = "PaymentMethodNotAllowed";

                            redirectUrl = Url.Action("PaymentMethod", "Checkout", new { area = "" });
                        }
                    }
                    else
                    {
                        messages.Add(T("Checkout.MinOrderPlacementInterval"));
                    }
                }
                else
                {
                    messages.AddRange(warnings.Select(x => HtmlUtils.ConvertPlainTextToHtml(x)));
                }
            }
            catch (Exception ex)
            {
                messages.Add(ex.Message);
                Logger.Error(ex);
            }

            return(Json(new { success, redirectUrl, messages }));
        }
Example #2
0
        public ActionResult ConfirmOrder(FormCollection form)
        {
            var store    = _storeContext.CurrentStore;
            var customer = _workContext.CurrentCustomer;
            var cart     = customer.GetCartItems(ShoppingCartType.ShoppingCart, store.Id);

            if (cart.Count == 0)
            {
                return(RedirectToRoute("ShoppingCart"));
            }

            if (customer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)
            {
                return(new HttpUnauthorizedResult());
            }

            var model = new CheckoutConfirmModel();
            PlaceOrderResult          placeOrderResult          = null;
            PostProcessPaymentRequest postProcessPaymentRequest = null;

            try
            {
                var processPaymentRequest = _httpContext.Session["OrderPaymentInfo"] as ProcessPaymentRequest;
                if (processPaymentRequest == null)
                {
                    // Check whether payment workflow is required.
                    if (IsPaymentWorkflowRequired(cart))
                    {
                        return(RedirectToAction("PaymentMethod"));
                    }

                    processPaymentRequest = new ProcessPaymentRequest();
                }

                // Prevent 2 orders being placed within an X seconds time frame.
                if (!_orderProcessingService.IsMinimumOrderPlacementIntervalValid(customer, store))
                {
                    throw new Exception(T("Checkout.MinOrderPlacementInterval"));
                }

                // Place order.
                processPaymentRequest.StoreId    = store.Id;
                processPaymentRequest.CustomerId = customer.Id;
                processPaymentRequest.PaymentMethodSystemName = customer.GetAttribute <string>(SystemCustomerAttributeNames.SelectedPaymentMethod, _genericAttributeService, store.Id);

                var placeOrderExtraData = new Dictionary <string, string>();
                placeOrderExtraData["CustomerComment"]               = form["customercommenthidden"];
                placeOrderExtraData["SubscribeToNewsLetter"]         = form["SubscribeToNewsLetter"];
                placeOrderExtraData["AcceptThirdPartyEmailHandOver"] = form["AcceptThirdPartyEmailHandOver"];

                placeOrderResult = _orderProcessingService.PlaceOrder(processPaymentRequest, placeOrderExtraData);

                if (!placeOrderResult.Success)
                {
                    model.Warnings.AddRange(placeOrderResult.Errors.Select(x => HtmlUtils.ConvertPlainTextToHtml(x)));
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, ex.Message);

                if (!model.Warnings.Any(x => x == ex.Message))
                {
                    model.Warnings.Add(ex.Message);
                }
            }

            if (placeOrderResult == null || !placeOrderResult.Success || model.Warnings.Any())
            {
                return(View(model));
            }

            try
            {
                postProcessPaymentRequest = new PostProcessPaymentRequest
                {
                    Order = placeOrderResult.PlacedOrder
                };

                _paymentService.PostProcessPayment(postProcessPaymentRequest);
            }
            catch (Exception ex)
            {
                NotifyError(ex);
            }
            finally
            {
                _httpContext.Session["OrderPaymentInfo"] = null;
                _httpContext.RemoveCheckoutState();
            }

            if (postProcessPaymentRequest != null && postProcessPaymentRequest.RedirectUrl.HasValue())
            {
                return(Redirect(postProcessPaymentRequest.RedirectUrl));
            }

            return(RedirectToAction("Completed"));
        }