protected void CancelButton_Click(object sender, EventArgs e)
 {
     if (UrlValidator.IsUrlLocalToHost(Request, ReturnUrl))
     {
         Response.RedirectUser(ReturnUrl);
     }
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         BindAllDropDowns();
         SetCreditCardOnFileDetails(CurrentOrder);
         BindShippingDetailGrid();
         if (UrlValidator.IsUrlLocalToHost(Request, Request.UrlReferrer.PathAndQuery))
         {
             ReturnUrl = Request.UrlReferrer.PathAndQuery;
         }
     }
 }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (BillingAddressSameRadio.Checked)
            {
                ListItem itemAlreadySelectedState = StateDropDown.Items.FindByValue(StateIdHidden.Value);
                if (itemAlreadySelectedState != null)
                {
                    itemAlreadySelectedState.Selected = true;
                }
                CityText.Text     = CityHidden.Value;
                ZipText.Text      = ZipHidden.Value;
                Address1Text.Text = Address1Hidden.Value;
            }

            bool isStatusUpdated = false;

            TotalAmountPayable = 0.0m;
            var selectedRows = GetSelectedRows();

            try
            {
                using (var transaction = new TransactionScope())
                {
                    if (PaymentMode.SelectedValue != PaymentType.CreditCard.PersistenceLayerId.ToString() ||
                        AddressValidation())
                    {
                        foreach (var row in selectedRows)
                        {
                            if (row == null)
                            {
                                continue;
                            }

                            var orderDetailIdLiteral    = row.FindControl("OrderDetailIdLiteral") as Literal;
                            var shippingDetailIdLiteral = row.FindControl("ShippingDetailIdLiteral") as Literal;
                            var actualPrice             = row.FindControl("PriceLabel") as Label;

                            if (orderDetailIdLiteral != null && shippingDetailIdLiteral != null)
                            {
                                var orderDetailId    = Convert.ToInt64(orderDetailIdLiteral.Text);
                                var shippingDetailId = Convert.ToInt64(shippingDetailIdLiteral.Text);

                                isStatusUpdated = _shippingDetailOrderDetailRepository.UpdateStatus(shippingDetailId, orderDetailId, false);

                                var shippingDetail = _shippingDetailRepository.GetById(shippingDetailId);
                                shippingDetail.Status = ShipmentStatus.Cancelled;
                                _shippingDetailRepository.Save(shippingDetail);

                                var resultShippingDetails  = _shippingDetailRepository.GetShippingDetailsForCancellation(orderDetailId).ToList();
                                var productShippingDetails = _shippingDetailRepository.GetProductShippingDetailsForCancellation(orderDetailId).ToList();
                                if (productShippingDetails.Count > resultShippingDetails.Count)
                                {
                                    var productShippingDetail = productShippingDetails.FirstOrDefault();
                                    if (productShippingDetail != null)
                                    {
                                        _shippingDetailOrderDetailRepository.UpdateStatus(productShippingDetail.Id, orderDetailId, false);
                                        productShippingDetail.Status = ShipmentStatus.Cancelled;
                                        _shippingDetailRepository.Save(productShippingDetail);
                                        CheckEventCustomerResultStateAndDeleteCdGenTrackRecord();
                                    }
                                }

                                if (actualPrice != null && !string.IsNullOrEmpty(actualPrice.Text))
                                {
                                    TotalAmountPayable += Convert.ToDecimal(actualPrice.Text);
                                }
                            }
                        }

                        if (AmountPaid < (DiscountedTotal - TotalAmountPayable))
                        {
                            TotalAmountPayable = 0;
                        }
                        else
                        {
                            TotalAmountPayable = AmountPaid - (DiscountedTotal - TotalAmountPayable);
                        }

                        var isRefundQueueEnabled = IoC.Resolve <ISettings>().IsRefundQueueEnabled;

                        if (PaymentMode.SelectedValue == PaymentType.CreditCard.PersistenceLayerId.ToString() &&
                            BillingAddressDifferentRadio.Checked && !isRefundQueueEnabled)
                        {
                            var creatorOrganizationRoleUser = GetCreatorOrganizationRoleUser();
                            UpdateCustomerBillingAddress(creatorOrganizationRoleUser.Id);
                        }

                        if (isStatusUpdated)
                        {
                            if (TotalAmountPayable > 0)
                            {
                                if (isRefundQueueEnabled)
                                {
                                    var refundRequest          = PrepareRefundRequestObject();
                                    var refundRequestValidator = IoC.Resolve <RefundRequestEditModelValidator>();
                                    var result = refundRequestValidator.Validate(refundRequest);
                                    if (result.IsValid)
                                    {
                                        IoC.Resolve <IRefundRequestService>().SaveRequest(refundRequest);
                                    }
                                    else
                                    {
                                        var propertyNames = result.Errors.Select(er => er.PropertyName).Distinct();
                                        var htmlString    = propertyNames.Aggregate("", (current, property) => current + (result.Errors.Where(er => er.PropertyName == property).FirstOrDefault().ErrorMessage + "&nbsp;&nbsp;"));
                                        MessageBox.ShowErrorMessage(htmlString);
                                        return;
                                    }
                                }
                                else
                                {
                                    var creatorOrganizationRoleUser = GetCreatorOrganizationRoleUser();
                                    var paymentInstrument           = RefundPayment(creatorOrganizationRoleUser,
                                                                                    OrderId + "_" + CustomerId);
                                    if (paymentInstrument != null &&
                                        ApplyPaymentToOrder(creatorOrganizationRoleUser, CurrentOrder, paymentInstrument))
                                    {
                                        transaction.Complete();
                                        Response.RedirectUser(ReturnUrl);
                                    }
                                    else
                                    {
                                        SetAndDisplayErrorMessage("Oops some error occured in the system please try again later.");
                                    }
                                }
                            }
                            transaction.Complete();
                            if (UrlValidator.IsUrlLocalToHost(Request, ReturnUrl))
                            {
                                Response.RedirectUser(ReturnUrl);
                            }
                        }
                        else
                        {
                            SetAndDisplayErrorMessage("Oops some error occured in the system please try again later.");
                        }
                    }
                }
            }
            catch (InvalidOperationException)
            {
            }
            catch { SetAndDisplayErrorMessage("Oops some error occured in the system please try again later."); }
        }