Example #1
0
        public ActionResult ConfirmOrderTransaction(int orderId, decimal amount, bool IsEmiOption)
        {
            //validation
            var customer = _workContext.CurrentCustomer;

            if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
                return new HttpUnauthorizedResult();
            var model = new CheckoutConfirmModel();
            try
            {
                var processPaymentRequest = _httpContext.Session["OrderPaymentInfo"] as ProcessPaymentRequest;
                if (processPaymentRequest == null)
                {

                    processPaymentRequest = new ProcessPaymentRequest();
                }

                //place order
                processPaymentRequest.StoreId = _storeContext.CurrentStore.Id;
                processPaymentRequest.CustomerId = _workContext.CurrentCustomer.Id;
                processPaymentRequest.PaymentMethodSystemName = _workContext.CurrentCustomer.GetAttribute<string>(
                    SystemCustomerAttributeNames.SelectedPaymentMethod,
                    _genericAttributeService, _storeContext.CurrentStore.Id);

                OrderTransactionDetailServiceModel transactionModel = new OrderTransactionDetailServiceModel()
                {
                     Amount = amount,
                     OrderId = orderId,
                     CustomerId = _workContext.CurrentCustomer.Id,
                     IsEmiOption = IsEmiOption,
                     PaymentMethodSystemName = processPaymentRequest.PaymentMethodSystemName
                };

                var placeOrderResult = _orderProcessingService.ProcessOrderTransaction(transactionModel);

                if (placeOrderResult.Success)
                {
                    _httpContext.Session["OrderPaymentInfo"] = null;
                    var postProcessPaymentRequest = new PostProcessTransactionPaymentRequest
                    {
                         CurrentOrderTransaction = placeOrderResult.PlacedOrder
                    };
                    var order = _orderService.GetOrderById(placeOrderResult.PlacedOrder.OrderId);
                    postProcessPaymentRequest.Order = order;
                    postProcessPaymentRequest.Order.PaymentMethodSystemName = processPaymentRequest.PaymentMethodSystemName;
                    _paymentService.PostProcessTransactionPayment(postProcessPaymentRequest);

                    if (_webHelper.IsRequestBeingRedirected || _webHelper.IsPostBeingDone)
                    {
                        //redirection or POST has been done in PostProcessPayment
                        return Content("Redirected");
                    }

                    return RedirectToRoute("CheckoutCompleted", new { orderId = placeOrderResult.PlacedOrder.Id });
                }

                foreach (var error in placeOrderResult.Errors)
                {
                    //Check the code
                }

            }
            catch (Exception exc)
            {
                _logger.Warning(exc.Message, exc);
                // model.Warnings.Add(exc.Message);
            }

            //If we got this far, something failed, redisplay form
            return View(model);
        }
        public PlaceOrderTransactionResult ProcessOrderTransaction(OrderTransactionDetailServiceModel transactiondetail)
        {
            var result = new PlaceOrderTransactionResult();

            //customer
            var customer = _customerService.GetCustomerById(transactiondetail.CustomerId);
            if (customer == null)
                throw new ArgumentException("Customer is not set");
            //billing address
            Address billingAddress;
           
                if (customer.BillingAddress == null)
                    throw new NopException("Billing address is not provided");

                if (!CommonHelper.IsValidEmail(customer.BillingAddress.Email))
                    throw new NopException("Email is not valid");

                //clone billing address
                billingAddress = (Address)customer.BillingAddress.Clone();
                if (billingAddress.Country != null && !billingAddress.Country.AllowsBilling)
                    throw new NopException(string.Format("Country '{0}' is not allowed for billing", billingAddress.Country.Name));



            if (transactiondetail.OrderId != null)
            
            {
                Order order = _orderService.GetOrderById(transactiondetail.OrderId);
                if(order!= null)
                {
                    try
                    {
                        decimal? amountTobePaid = null;
                        if (order.OrderStatus == OrderStatus.OrderPaymentComplete)
                        {
                            throw new NopException(string.Format("Order Payment has already been Completed"));
                        }

                        if (order.OrderStatus == OrderStatus.Booked)
                        {
                            if (transactiondetail.IsEmiOption)
                                amountTobePaid = order.OrderTotal - order.TotalTransactionAmount;
                            else
                                amountTobePaid = (_catalogSettings.PercentConfirmAmount * order.OrderTotal) / 100;
                        }

                        else if (order.OrderStatus == OrderStatus.Confirmed)
                        {
                            amountTobePaid = order.OrderTotal - order.TotalTransactionAmount;
                        }

                        if (transactiondetail.Amount != amountTobePaid)
                        {
                            throw new NopException(string.Format("Security Issue. Transaction Cannot be processesed"));
                        }

                        OrderTransactionDetails ordertransactiondetail = new OrderTransactionDetails()
                        {
                            TransactionAmount = transactiondetail.Amount,
                            Order = order,
                            PaymentMethod = transactiondetail.PaymentMethodSystemName,
                            TransactionId = Guid.NewGuid().ToString(),
                            TransactionDate = DateTime.Now


                        };
                        order.OrderTransactionDetailItems.Add(ordertransactiondetail);
                        _orderService.UpdateOrder(order);
                        result.PlacedOrder = ordertransactiondetail;
                    }
                    catch (Exception exc)
                    {
                        _logger.Error(exc.Message, exc);
                        result.AddError(exc.Message);
                    }
                        


                    }



                return result;
               

                }

            return result;
        }